1 #include <msp/core/variant.h>
2 #include <msp/test/test.h>
7 class VariantTests: public Test::RegisteredTest<VariantTests>
12 static const char *get_name() { return "Variant"; }
31 Counter(int &c): count(c) { ++count; }
32 Counter(const Counter &c): count(c.count) { ++count; }
33 ~Counter() { --count; }
36 VariantTests::VariantTests()
38 add(&VariantTests::integer, "Integer");
39 add(&VariantTests::stdstring, "std::string");
40 add(&VariantTests::pointer, "Pointer");
41 add(&VariantTests::ref_into, "Reference into");
42 add(&VariantTests::copying, "Copying");
43 add(&VariantTests::destruction, "Destruction");
44 add(&VariantTests::types, "Type checks");
45 add(&VariantTests::mismatch, "Mismatch").expect_throw<type_mismatch>();
48 void VariantTests::integer()
51 EXPECT_EQUAL(var.value<int>(), 42);
54 void VariantTests::stdstring()
56 Variant var = string("foobar");
57 EXPECT_EQUAL(var.value<string>(), "foobar");
60 void VariantTests::pointer()
64 *var.value<int *>() = 42;
68 void VariantTests::ref_into()
71 int &r = var.value<int>();
73 EXPECT_EQUAL(var.value<int>(), 42);
76 void VariantTests::copying()
80 EXPECT_EQUAL(var2.value<int>(), 42);
83 void VariantTests::destruction()
87 Variant var = Counter(count);
89 EXPECT_EQUAL(count, 2);
92 EXPECT_EQUAL(count, 3);
94 EXPECT_EQUAL(count, 2);
96 EXPECT_EQUAL(count, 0);
99 void VariantTests::types()
102 EXPECT(!var.check_type<int>());
103 EXPECT(var.check_type<unsigned>());
104 EXPECT(!var.check_type<float>());
105 EXPECT(!var.check_type<bool>());
106 EXPECT(!var.check_type<string>());
109 void VariantTests::mismatch()