summaryrefslogtreecommitdiffstats
path: root/tests/language-feature/types
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2022-08-10 15:37:19 -0700
committerGitHub <noreply@github.com>2022-08-10 15:37:19 -0700
commita083a37ee58dc48d92cf2b844466a295eb3e643e (patch)
tree69028f3d9f92fa7e2085c76bc234e1949ef645c5 /tests/language-feature/types
parent88f04c29244af23c1cdd472d8d1ae3e5a650494e (diff)
Add `none` literal that is convertible to `Optional`. (#2356)
* Add `none` literal that is convertible to `Optional`. * Fix cpu code gen. * Include vk and cpu test for is-as operator test. * Inline comparison operators. Co-authored-by: Yong He <yhe@nvidia.com>
Diffstat (limited to 'tests/language-feature/types')
-rw-r--r--tests/language-feature/types/optional.slang53
-rw-r--r--tests/language-feature/types/optional.slang.expected.txt2
2 files changed, 55 insertions, 0 deletions
diff --git a/tests/language-feature/types/optional.slang b/tests/language-feature/types/optional.slang
new file mode 100644
index 000000000..ae1711cd7
--- /dev/null
+++ b/tests/language-feature/types/optional.slang
@@ -0,0 +1,53 @@
+// optional.slang
+
+// Test that `Optional` construction and conversion works.
+
+//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute
+//TEST(compute):COMPARE_COMPUTE_EX:-slang -vk -compute
+//TEST(compute):COMPARE_COMPUTE_EX:-slang -cpu -compute
+
+//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; }
+}
+
+Optional<Impl1> getVal(bool shouldHaveVal)
+{
+ if (shouldHaveVal)
+ {
+ Impl1 val;
+ val.data = 1;
+ return val;
+ }
+ return none;
+}
+
+[numthreads(1, 1, 1)]
+void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID)
+{
+ let v1 = getVal(true);
+ let v2 = getVal(false);
+ int result = 0;
+ if (v1.hasValue)
+ result += v1.value.data;
+ if (v2.hasValue)
+ result += v2.value.data;
+ outputBuffer[0] = result;
+ if (v1 != none)
+ result += v1.value.data;
+ if (!(v2 == none))
+ result += v2.value.data;
+ outputBuffer[1] = result;
+
+}
diff --git a/tests/language-feature/types/optional.slang.expected.txt b/tests/language-feature/types/optional.slang.expected.txt
new file mode 100644
index 000000000..1191247b6
--- /dev/null
+++ b/tests/language-feature/types/optional.slang.expected.txt
@@ -0,0 +1,2 @@
+1
+2