1 | #include <gtest/gtest.h> |
---|
2 | #include "util/Convert.h" |
---|
3 | #include "util/Math.h" |
---|
4 | #include "util/MathConvert.h" |
---|
5 | |
---|
6 | namespace orxonox |
---|
7 | { |
---|
8 | TEST(multi_cast, ToString) |
---|
9 | { |
---|
10 | // int |
---|
11 | EXPECT_EQ("5", multi_cast<std::string>(static_cast<int>(5))); |
---|
12 | EXPECT_EQ("-13", multi_cast<std::string>(static_cast<int>(-13))); |
---|
13 | |
---|
14 | // unsigned int |
---|
15 | EXPECT_EQ("0", multi_cast<std::string>(static_cast<unsigned int>(0))); |
---|
16 | EXPECT_EQ("4000000000", multi_cast<std::string>(static_cast<unsigned int>(4000000000LL))); |
---|
17 | |
---|
18 | // float |
---|
19 | EXPECT_EQ("0", multi_cast<std::string>(0.0f)); |
---|
20 | EXPECT_EQ("10", multi_cast<std::string>(10.0f)); |
---|
21 | EXPECT_EQ("123.456", multi_cast<std::string>(123.456f)); |
---|
22 | EXPECT_EQ("1.234e+012", multi_cast<std::string>(1234000000000.0f)); |
---|
23 | |
---|
24 | // double |
---|
25 | EXPECT_EQ("0", multi_cast<std::string>(0.0)); |
---|
26 | EXPECT_EQ("10", multi_cast<std::string>(10.0)); |
---|
27 | EXPECT_EQ("123.456", multi_cast<std::string>(123.456)); |
---|
28 | EXPECT_EQ("1.234e+012", multi_cast<std::string>(1234000000000.0)); |
---|
29 | |
---|
30 | // char |
---|
31 | EXPECT_EQ("a", multi_cast<std::string>('a')); |
---|
32 | EXPECT_EQ("\n", multi_cast<std::string>('\n')); |
---|
33 | |
---|
34 | // bool |
---|
35 | EXPECT_EQ("true", multi_cast<std::string>(true)); |
---|
36 | EXPECT_EQ("false", multi_cast<std::string>(false)); |
---|
37 | |
---|
38 | // string |
---|
39 | EXPECT_EQ("testTEST123", multi_cast<std::string>("testTEST123")); |
---|
40 | EXPECT_EQ("", multi_cast<std::string>("")); |
---|
41 | |
---|
42 | // TODO: Check consistency of conversion depending on MathConvert.h and reversibility |
---|
43 | /* |
---|
44 | // Vector2 |
---|
45 | EXPECT_EQ("Vector2(10.1, 20)", multi_cast<std::string>(Vector2(10.1, 20))); |
---|
46 | |
---|
47 | // Vector3 |
---|
48 | EXPECT_EQ("Vector3(10.1, 20, 300000)", multi_cast<std::string>(Vector3(10.1, 20, 300000))); |
---|
49 | |
---|
50 | // Vector4 |
---|
51 | EXPECT_EQ("Vector4(10.1, 20, 300000, -321)", multi_cast<std::string>(Vector4(10.1, 20, 300000, -321))); |
---|
52 | |
---|
53 | // Quaternion |
---|
54 | EXPECT_EQ("Quaternion(10.1, 20, 300000, -321)", multi_cast<std::string>(Quaternion(10.1, 20, 300000, -321))); |
---|
55 | |
---|
56 | // ColourValue |
---|
57 | EXPECT_EQ("ColourValue(10.1, 20, 300000, -321)", multi_cast<std::string>(ColourValue(10.1, 20, 300000, -321))); |
---|
58 | |
---|
59 | // Radian |
---|
60 | EXPECT_EQ("Radian(1.234)", multi_cast<std::string>(Radian(1.234))); |
---|
61 | |
---|
62 | // Degree |
---|
63 | EXPECT_EQ("Degree(123)", multi_cast<std::string>(Degree(123))); |
---|
64 | */ |
---|
65 | } |
---|
66 | |
---|
67 | TEST(multi_cast, FromString) |
---|
68 | { |
---|
69 | // int |
---|
70 | EXPECT_EQ(5, multi_cast<int>(std::string("5"))); |
---|
71 | EXPECT_EQ(-13, multi_cast<int>(std::string("-13"))); |
---|
72 | |
---|
73 | // unsigned int |
---|
74 | EXPECT_EQ(0U, multi_cast<unsigned int>(std::string("0"))); |
---|
75 | EXPECT_EQ(4000000000LL, multi_cast<unsigned int>(std::string("4000000000"))); |
---|
76 | |
---|
77 | // float |
---|
78 | EXPECT_EQ(0.0f, multi_cast<float>(std::string("0"))); |
---|
79 | EXPECT_EQ(10.0f, multi_cast<float>(std::string("10"))); |
---|
80 | EXPECT_EQ(123.456f, multi_cast<float>(std::string("123.456"))); |
---|
81 | EXPECT_EQ(1234000000000.0f, multi_cast<float>(std::string("1.234e+012"))); |
---|
82 | |
---|
83 | // double |
---|
84 | EXPECT_EQ(0.0, multi_cast<double>(std::string("0"))); |
---|
85 | EXPECT_EQ(10.0, multi_cast<double>(std::string("10"))); |
---|
86 | EXPECT_EQ(123.456, multi_cast<double>(std::string("123.456"))); |
---|
87 | EXPECT_EQ(1234000000000.0, multi_cast<double>(std::string("1.234e+012"))); |
---|
88 | |
---|
89 | // char |
---|
90 | EXPECT_EQ('a', multi_cast<char>(std::string("a"))); |
---|
91 | EXPECT_EQ('\n', multi_cast<char>(std::string("\n"))); |
---|
92 | EXPECT_EQ('\0', multi_cast<char>(std::string(""))); |
---|
93 | |
---|
94 | // bool |
---|
95 | EXPECT_TRUE(multi_cast<bool>(std::string("true"))); |
---|
96 | EXPECT_FALSE(multi_cast<bool>(std::string("false"))); |
---|
97 | |
---|
98 | // TODO: Check consistency of conversion depending on MathConvert.h and reversibility |
---|
99 | /* |
---|
100 | // Vector2 |
---|
101 | EXPECT_EQ(Vector2(10.1, 20), multi_cast<Vector2>(std::string("Vector2(10.1, 20)"))); |
---|
102 | |
---|
103 | // Vector3 |
---|
104 | EXPECT_EQ(Vector3(10.1, 20, 300000), multi_cast<Vector3>(std::string("Vector3(10.1, 20, 300000)"))); |
---|
105 | |
---|
106 | // Vector4 |
---|
107 | EXPECT_EQ(Vector4(10.1, 20, 300000, -321), multi_cast<Vector4>(std::string("Vector4(10.1, 20, 300000, -321)"))); |
---|
108 | |
---|
109 | // Quaternion |
---|
110 | EXPECT_EQ(Quaternion(10.1, 20, 300000, -321), multi_cast<Quaternion>(std::string("Quaternion(10.1, 20, 300000, -321)"))); |
---|
111 | |
---|
112 | // ColourValue |
---|
113 | EXPECT_EQ(ColourValue(10.1, 20, 300000, -321), multi_cast<ColourValue>(std::string("ColourValue(10.1, 20, 300000, -321)"))); |
---|
114 | |
---|
115 | // Radian |
---|
116 | EXPECT_EQ(Radian(1.234), multi_cast<Radian>(std::string("Radian(1.234)"))); |
---|
117 | |
---|
118 | // Degree |
---|
119 | EXPECT_EQ(Degree(123), multi_cast<Degree>(std::string("Degree(123)"))); |
---|
120 | */ |
---|
121 | } |
---|
122 | } |
---|