]> git.tdb.fi Git - libs/core.git/blob - tests/variant.cpp
Use string::size_type rather than unsigned to store string offsets
[libs/core.git] / tests / variant.cpp
1 #include <msp/core/variant.h>
2 #include <msp/test/test.h>
3
4 using namespace std;
5 using namespace Msp;
6
7 class VariantTests: public Test::RegisteredTest<VariantTests>
8 {
9 public:
10         VariantTests();
11
12         static const char *get_name() { return "Variant"; }
13
14 private:
15         void integer();
16         void stdstring();
17         void pointer();
18         void ref_into();
19         void copying();
20         void destruction();
21         void types();
22         void mismatch();
23 };
24
25 class Counter
26 {
27 private:
28         int &count;
29
30 public:
31         Counter(int &c): count(c) { ++count; }
32         Counter(const Counter &c): count(c.count) { ++count; }
33         ~Counter() { --count; }
34 };
35
36 VariantTests::VariantTests()
37 {
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>();
46 }
47
48 void VariantTests::integer()
49 {
50         Variant var = 42;
51         EXPECT_EQUAL(var.value<int>(), 42);
52 }
53
54 void VariantTests::stdstring()
55 {
56         Variant var = string("foobar");
57         EXPECT_EQUAL(var.value<string>(), "foobar");
58 }
59
60 void VariantTests::pointer()
61 {
62         int i = 7;
63         Variant var = &i;
64         *var.value<int *>() = 42;
65         EXPECT_EQUAL(i, 42);
66 }
67
68 void VariantTests::ref_into()
69 {
70         Variant var = 7;
71         int &r = var.value<int>();
72         r = 42;
73         EXPECT_EQUAL(var.value<int>(), 42);
74 }
75
76 void VariantTests::copying()
77 {
78         Variant var = 42;
79         Variant var2 = var;
80         EXPECT_EQUAL(var2.value<int>(), 42);
81 }
82
83 void VariantTests::destruction()
84 {
85         int count = 0;
86         {
87                 Variant var = Counter(count);
88                 Variant var2 = var;
89                 EXPECT_EQUAL(count, 2);
90                 Variant var3;
91                 var3 = var;
92                 EXPECT_EQUAL(count, 3);
93                 var2 = Variant();
94                 EXPECT_EQUAL(count, 2);
95         }
96         EXPECT_EQUAL(count, 0);
97 }
98
99 void VariantTests::types()
100 {
101         Variant var = 42U;
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>());
107 }
108
109 void VariantTests::mismatch()
110 {
111         Variant var = 1;
112         var.value<string>();
113 }