diff options
| author | Yong He <yonghe@outlook.com> | 2022-08-10 14:11:27 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-10 14:11:27 -0700 |
| commit | 88f04c29244af23c1cdd472d8d1ae3e5a650494e (patch) | |
| tree | 398e55440e8f7ad157d15b2b75d9887236eaa126 /tests/language-feature/interfaces | |
| parent | fcdb4629c4c3dd2931eaa88b96b668d914c4519c (diff) | |
`is` and `as` operator and `Optional<T>`. (#2355)
* `is` and `as` operator and `Optional<T>`.
* Fix.
Co-authored-by: Yong He <yhe@nvidia.com>
Diffstat (limited to 'tests/language-feature/interfaces')
4 files changed, 96 insertions, 0 deletions
diff --git a/tests/language-feature/interfaces/is-as-dynamic.slang b/tests/language-feature/interfaces/is-as-dynamic.slang new file mode 100644 index 000000000..4499db53a --- /dev/null +++ b/tests/language-feature/interfaces/is-as-dynamic.slang @@ -0,0 +1,48 @@ +// is-as-dynamic.slang + +// Test that `is` and `as` operators works as intended in dynamic dispatch. + +//TEST(compute):COMPARE_COMPUTE: -shaderobj + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer<int> outputBuffer; + +[anyValueSize(8)] +interface IFoo +{ + int method(); +} + +//TEST_INPUT: type_conformance Impl1:IFoo = 0 +struct Impl1 : IFoo +{ + int data; + int method() { return data; } +} + +//TEST_INPUT: type_conformance Impl2:IFoo = 1 +struct Impl2 : IFoo +{ + int data1; + int data2; + int method() { return data1 + data2; } +} + +int getData(IFoo foo) +{ + let castResult = foo as Impl2; + if (castResult.hasValue && foo is Impl2 && !(foo is Impl1)) + { + return castResult.value.method(); + } + return 0; +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID) +{ + int2 data = int2(1, 2); + IFoo dynamicObject = createDynamicObject<IFoo, int2>(1, data); + int outVal = getData(dynamicObject); + outputBuffer[0] = outVal; +} diff --git a/tests/language-feature/interfaces/is-as-dynamic.slang.expected.txt b/tests/language-feature/interfaces/is-as-dynamic.slang.expected.txt new file mode 100644 index 000000000..00750edc0 --- /dev/null +++ b/tests/language-feature/interfaces/is-as-dynamic.slang.expected.txt @@ -0,0 +1 @@ +3 diff --git a/tests/language-feature/interfaces/is-as.slang b/tests/language-feature/interfaces/is-as.slang new file mode 100644 index 000000000..2712d9810 --- /dev/null +++ b/tests/language-feature/interfaces/is-as.slang @@ -0,0 +1,46 @@ +// is-as.slang + +// Test that `is` and `as` operators works as intended. + +//TEST(compute):COMPARE_COMPUTE: -shaderobj + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer<int> outputBuffer; + +interface IFoo +{ + int method(); +} + +struct Impl1 : IFoo +{ + int data; + int method() { return data; } +} + +struct Impl2 : IFoo +{ + int data1; + int data2; + int method() { return data1 + data2; } +} + +int getData(IFoo foo) +{ + let castResult = foo as Impl2; + if (castResult.hasValue) + { + return castResult.value.method(); + } + return 0; +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID) +{ + Impl2 obj; + obj.data1 = 1; + obj.data2 = 2; + int outVal = getData(obj); + outputBuffer[0] = outVal; +} diff --git a/tests/language-feature/interfaces/is-as.slang.expected.txt b/tests/language-feature/interfaces/is-as.slang.expected.txt new file mode 100644 index 000000000..00750edc0 --- /dev/null +++ b/tests/language-feature/interfaces/is-as.slang.expected.txt @@ -0,0 +1 @@ +3 |
