summaryrefslogtreecommitdiff
path: root/tiger-compiler/tests/unit/misc/variant
diff options
context:
space:
mode:
Diffstat (limited to 'tiger-compiler/tests/unit/misc/variant')
-rw-r--r--tiger-compiler/tests/unit/misc/variant/local.am3
-rw-r--r--tiger-compiler/tests/unit/misc/variant/test_variant_three_types.cc35
-rw-r--r--tiger-compiler/tests/unit/misc/variant/test_variant_two_types.cc28
3 files changed, 66 insertions, 0 deletions
diff --git a/tiger-compiler/tests/unit/misc/variant/local.am b/tiger-compiler/tests/unit/misc/variant/local.am
new file mode 100644
index 0000000..f09d993
--- /dev/null
+++ b/tiger-compiler/tests/unit/misc/variant/local.am
@@ -0,0 +1,3 @@
+check_unit_SOURCES += \
+ %D%/test_variant_two_types.cc \
+ %D%/test_variant_three_types.cc
diff --git a/tiger-compiler/tests/unit/misc/variant/test_variant_three_types.cc b/tiger-compiler/tests/unit/misc/variant/test_variant_three_types.cc
new file mode 100644
index 0000000..8728059
--- /dev/null
+++ b/tiger-compiler/tests/unit/misc/variant/test_variant_three_types.cc
@@ -0,0 +1,35 @@
+#include <criterion/criterion.h>
+#include <criterion/assert.h>
+#include <misc/variant.hh>
+
+using multiple_variant_type = misc::variant<int, std::string, double>;
+
+Test(variant_three_types, simple_success_type_a)
+{
+ multiple_variant_type v("Hello");
+ std::string s = v;
+ cr_expect_str_eq(s.c_str(), "Hello");
+}
+
+Test(variant_three_types, simple_success_type_b)
+{
+ multiple_variant_type v(56.7);
+ double d = v;
+ cr_expect_eq(d, 56.7);
+}
+
+Test(variant_three_types, simple_failure_type_c)
+{
+ multiple_variant_type v(34.5);
+
+ try
+ {
+ [[maybe_unused]] int i = v;
+ }
+ catch (const std::bad_variant_access& e)
+ {
+ return;
+ }
+
+ cr_expect_eq(1, 0, "copy assignment did not throw an exception");
+}
diff --git a/tiger-compiler/tests/unit/misc/variant/test_variant_two_types.cc b/tiger-compiler/tests/unit/misc/variant/test_variant_two_types.cc
new file mode 100644
index 0000000..9a63d6a
--- /dev/null
+++ b/tiger-compiler/tests/unit/misc/variant/test_variant_two_types.cc
@@ -0,0 +1,28 @@
+#include <criterion/criterion.h>
+#include <criterion/assert.h>
+#include <misc/variant.hh>
+
+using variant_type = misc::variant<int, std::string>;
+
+Test(variant_two_types, simple_success)
+{
+ misc::variant<int, std::string> v("Hello");
+ std::string s = v;
+ cr_expect_str_eq(s.c_str(), "Hello");
+}
+
+Test(variant_two_types, simple_failure)
+{
+ variant_type v(42);
+
+ try
+ {
+ std::string s = v;
+ }
+ catch (const std::bad_variant_access& e)
+ {
+ return;
+ }
+
+ cr_expect_eq(1, 0, "copy assignment did not throw an exception");
+}