diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:07:58 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:07:58 +0200 |
| commit | 967be9e750221ab2ab783f95df79bb26d290a45e (patch) | |
| tree | 6802900a5e975f9f68b169f0f503f040056d6952 /tiger-compiler/tests/unit/misc/variant | |
Diffstat (limited to 'tiger-compiler/tests/unit/misc/variant')
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"); +} |
