Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/towerdefenseHS14/test/util/ConvertTest.cc @ 10240

Last change on this file since 10240 was 9479, checked in by landauf, 12 years ago

consider different float to string conversions

  • Property svn:eol-style set to native
File size: 5.1 KB
Line 
1#include <gtest/gtest.h>
2#include "util/Convert.h"
3#include "util/Math.h"
4#include "util/MathConvert.h"
5
6namespace 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        {
23            // expect 1.234e+012 or 1.234e+12
24            std::string value = multi_cast<std::string>(1234000000000.0f);
25            EXPECT_TRUE(value == "1.234e+012" || value == "1.234e+12");
26        }
27
28        // double
29        EXPECT_EQ("0", multi_cast<std::string>(0.0));
30        EXPECT_EQ("10", multi_cast<std::string>(10.0));
31        EXPECT_EQ("123.456", multi_cast<std::string>(123.456));
32        {
33            // expect 1.234e+012 or 1.234e+12
34            std::string value = multi_cast<std::string>(1234000000000.0);
35            EXPECT_TRUE(value == "1.234e+012" || value == "1.234e+12");
36        }
37
38        // char
39        EXPECT_EQ("a", multi_cast<std::string>('a'));
40        EXPECT_EQ("\n", multi_cast<std::string>('\n'));
41
42        // bool
43        EXPECT_EQ("true", multi_cast<std::string>(true));
44        EXPECT_EQ("false", multi_cast<std::string>(false));
45
46        // string
47        EXPECT_EQ("testTEST123", multi_cast<std::string>("testTEST123"));
48        EXPECT_EQ("", multi_cast<std::string>(""));
49
50        // TODO: Check consistency of conversion depending on MathConvert.h and reversibility
51/*
52        // Vector2
53        EXPECT_EQ("Vector2(10.1, 20)", multi_cast<std::string>(Vector2(10.1, 20)));
54
55        // Vector3
56        EXPECT_EQ("Vector3(10.1, 20, 300000)", multi_cast<std::string>(Vector3(10.1, 20, 300000)));
57
58        // Vector4
59        EXPECT_EQ("Vector4(10.1, 20, 300000, -321)", multi_cast<std::string>(Vector4(10.1, 20, 300000, -321)));
60
61        // Quaternion
62        EXPECT_EQ("Quaternion(10.1, 20, 300000, -321)", multi_cast<std::string>(Quaternion(10.1, 20, 300000, -321)));
63
64        // ColourValue
65        EXPECT_EQ("ColourValue(10.1, 20, 300000, -321)", multi_cast<std::string>(ColourValue(10.1, 20, 300000, -321)));
66
67        // Radian
68        EXPECT_EQ("Radian(1.234)", multi_cast<std::string>(Radian(1.234)));
69
70        // Degree
71        EXPECT_EQ("Degree(123)", multi_cast<std::string>(Degree(123)));
72*/
73    }
74
75    TEST(multi_cast, FromString)
76    {
77        // int
78        EXPECT_EQ(5, multi_cast<int>(std::string("5")));
79        EXPECT_EQ(-13, multi_cast<int>(std::string("-13")));
80
81        // unsigned int
82        EXPECT_EQ(0U, multi_cast<unsigned int>(std::string("0")));
83        EXPECT_EQ(4000000000LL, multi_cast<unsigned int>(std::string("4000000000")));
84
85        // float
86        EXPECT_EQ(0.0f, multi_cast<float>(std::string("0")));
87        EXPECT_EQ(10.0f, multi_cast<float>(std::string("10")));
88        EXPECT_EQ(123.456f, multi_cast<float>(std::string("123.456")));
89        EXPECT_EQ(1234000000000.0f, multi_cast<float>(std::string("1.234e+012")));
90
91        // double
92        EXPECT_EQ(0.0, multi_cast<double>(std::string("0")));
93        EXPECT_EQ(10.0, multi_cast<double>(std::string("10")));
94        EXPECT_EQ(123.456, multi_cast<double>(std::string("123.456")));
95        EXPECT_EQ(1234000000000.0, multi_cast<double>(std::string("1.234e+012")));
96
97        // char
98        EXPECT_EQ('a', multi_cast<char>(std::string("a")));
99        EXPECT_EQ('\n', multi_cast<char>(std::string("\n")));
100        EXPECT_EQ('\0', multi_cast<char>(std::string("")));
101        EXPECT_EQ('1', multi_cast<char>(std::string("123")));
102
103        // bool
104        EXPECT_TRUE(multi_cast<bool>(std::string("true")));
105        EXPECT_FALSE(multi_cast<bool>(std::string("false")));
106        EXPECT_FALSE(multi_cast<bool>(std::string("0")));
107        EXPECT_FALSE(multi_cast<bool>(std::string("0.123")));
108        EXPECT_TRUE(multi_cast<bool>(std::string("1")));
109        EXPECT_TRUE(multi_cast<bool>(std::string("123.456")));
110
111        // TODO: Check consistency of conversion depending on MathConvert.h and reversibility
112/*
113        // Vector2
114        EXPECT_EQ(Vector2(10.1, 20), multi_cast<Vector2>(std::string("Vector2(10.1, 20)")));
115
116        // Vector3
117        EXPECT_EQ(Vector3(10.1, 20, 300000), multi_cast<Vector3>(std::string("Vector3(10.1, 20, 300000)")));
118
119        // Vector4
120        EXPECT_EQ(Vector4(10.1, 20, 300000, -321), multi_cast<Vector4>(std::string("Vector4(10.1, 20, 300000, -321)")));
121
122        // Quaternion
123        EXPECT_EQ(Quaternion(10.1, 20, 300000, -321), multi_cast<Quaternion>(std::string("Quaternion(10.1, 20, 300000, -321)")));
124
125        // ColourValue
126        EXPECT_EQ(ColourValue(10.1, 20, 300000, -321), multi_cast<ColourValue>(std::string("ColourValue(10.1, 20, 300000, -321)")));
127
128        // Radian
129        EXPECT_EQ(Radian(1.234), multi_cast<Radian>(std::string("Radian(1.234)")));
130
131        // Degree
132        EXPECT_EQ(Degree(123), multi_cast<Degree>(std::string("Degree(123)")));
133*/
134    }
135}
Note: See TracBrowser for help on using the repository browser.