Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / inipp / repo / inipp / unittest / unittest.cpp
1 #include "test.h"
2
3 #ifdef _MSC_VER
4 #include "CppUnitTest.h"
5 using namespace Microsoft::VisualStudio::CppUnitTestFramework;
6 #else
7 #define TEST_CLASS(T) class T
8 #define TEST_METHOD(Func) void Func()
9
10 namespace Assert {
11
12         void IsTrue(bool result) {
13                 if (!result) throw std::runtime_error("expected true");
14         }
15
16         void IsFalse(bool result) {
17                 if (result) throw std::runtime_error("expected false");
18         }
19
20         template <typename T>
21         void AreEqual(const T & a, const T & b) {
22                 if (a != b) throw std::runtime_error("expected equality");
23         }
24
25 } // namespace Assert
26
27 namespace Logger {
28
29         void WriteMessage(const char *msg) {
30                 std::wcout << msg;
31         }
32
33         void WriteMessage(const wchar_t *msg) {
34                 std::wcout << msg;
35         }
36
37 } // namespace Logger
38 #endif
39
40 template <class CharT>
41 void WriteMessage(const Ini<CharT> & ini) {
42         std::basic_ostringstream<CharT> os;
43         ini.generate(os);
44         Logger::WriteMessage(os.str().c_str());
45 }
46
47 namespace unittest
48 {               
49         TEST_CLASS(UnitTest)
50         {
51         public:
52                 
53                 TEST_METHOD(TestParseGenerate1)
54                 {
55                         std::basic_ostringstream<char> os;
56                         Assert::IsTrue(runtest<char>("test1.ini", "test1.output", os));
57                         Logger::WriteMessage(os.str().c_str());
58                 }
59
60                 TEST_METHOD(TestParseGenerate1W)
61                 {
62                         std::basic_ostringstream<wchar_t> os;
63                         Assert::IsTrue(runtest<wchar_t>("test1.ini", "test1.output", os));
64                         Logger::WriteMessage(os.str().c_str());
65                 }
66
67                 TEST_METHOD(TestParseGenerate2)
68                 {
69                         std::basic_ostringstream<char> os;
70                         Assert::IsTrue(runtest<char>("test2.ini", "test2.output", os));
71                         Logger::WriteMessage(os.str().c_str());
72                 }
73
74                 TEST_METHOD(TestParseGenerate2W)
75                 {
76                         std::basic_ostringstream<wchar_t> os;
77                         Assert::IsTrue(runtest<wchar_t>("test2.ini", "test2.output", os));
78                         Logger::WriteMessage(os.str().c_str());
79                 }
80
81                 TEST_METHOD(TestParseGenerate3)
82                 {
83                         std::basic_ostringstream<char> os;
84                         Assert::IsTrue(runtest<char>("test3.ini", "test3.output", os));
85                         Logger::WriteMessage(os.str().c_str());
86                 }
87
88                 TEST_METHOD(TestParseGenerate4)
89                 {
90                         std::basic_ostringstream<char> os;
91                         Assert::IsTrue(runtest<char>("test4.ini", "test4.output", os));
92                         Logger::WriteMessage(os.str().c_str());
93                 }
94
95                 TEST_METHOD(TestInterpolate1)
96                 {
97                         Ini<char> ini;
98                         ini.sections["sec1"]["x"] = "${sec2:z}";
99                         ini.sections["sec1"]["y"] = "2";
100                         ini.sections["sec2"]["z"] = "${y}";
101                         ini.interpolate();
102                         // we do not want x to be 2
103                         Assert::AreEqual(ini.sections.at("sec1").at("x"), std::string("${y}"));
104                         ini.generate(std::cout);
105                 }
106
107                 TEST_METHOD(TestInfiniteRecursion1)
108                 {
109                         Ini<char> ini;
110                         ini.sections["test"]["x"] = "0 ${y}";
111                         ini.sections["test"]["y"] = "1 ${x}";
112                         ini.interpolate();
113                         WriteMessage(ini);
114                 }
115
116                 TEST_METHOD(TestInfiniteRecursion2)
117                 {
118                         Ini<char> ini;
119                         ini.sections["test1"]["x"] = "0 ${test2:y}";
120                         ini.sections["test2"]["y"] = "1 ${test1:x}";
121                         ini.interpolate();
122                         WriteMessage(ini);
123                 }
124
125                 TEST_METHOD(TestInfiniteRecursion3)
126                 {
127                         Ini<char> ini;
128                         ini.sections["test1"]["x"] = "${test2:x}";
129                         ini.sections["test2"]["x"] = "${test3:x}";
130                         ini.sections["test3"]["x"] = "${test4:x}";
131                         ini.sections["test4"]["x"] = "x${test1:x}";
132                         ini.interpolate();
133                         WriteMessage(ini);
134                 }
135
136                 TEST_METHOD(TestExtract)
137                 {
138                         std::string       str{ "oops" };
139                         int16_t           i16{ 0 };
140                         int32_t           i32{ 0 };
141                         bool              bool_{ true };
142                         Assert::IsTrue(extract(std::string(), str));
143                         Assert::AreEqual(std::string{ }, str);
144                         Assert::IsTrue(extract(std::string{ "hello" }, str));
145                         Assert::AreEqual(std::string{ "hello" }, str);
146                         Assert::IsTrue(extract(std::string{ "hello world" }, str));
147                         Assert::AreEqual(std::string{ "hello world" }, str);
148                         Assert::IsTrue(extract(std::string{ "-10" }, i16));
149                         Assert::AreEqual(int16_t{ -10 }, i16);
150                         Assert::IsTrue(extract(std::string{ "false" }, bool_));
151                         Assert::AreEqual(false, bool_);
152                         Assert::IsTrue(extract(std::string{ "-10" }, i16));
153                         Assert::AreEqual(int16_t{ -10 }, i16);
154                         Assert::IsFalse(extract(std::string{ "xxx" }, i16));
155                         Assert::AreEqual(int16_t{ -10 }, i16);
156                         Assert::IsFalse(extract(std::string{ "1000000" }, i16));
157                         Assert::AreEqual(int16_t{ -10 }, i16);
158                         Assert::IsFalse(extract(std::string{ "-20 xxx" }, i16));
159                         Assert::AreEqual(int16_t{ -10 }, i16);
160                         Assert::IsTrue(extract(std::string{ "1000000" }, i32));
161                         Assert::AreEqual(int32_t{ 1000000 }, i32);
162                 }
163
164                 TEST_METHOD(TestDefault)
165                 {
166                         Ini<char> ini;
167                         ini.sections["sec0"]["a"] = "0";
168                         ini.sections["sec0"]["b"] = "1";
169                         ini.sections["sec1"]["b"] = "2";
170                         ini.sections["sec1"]["c"] = "${a} ${b}";
171                         ini.sections["sec2"]["a"] = "3";
172                         ini.sections["sec2"]["c"] = "${a} ${b}";
173                         ini.default_section(ini.sections.at("sec0"));
174                         ini.interpolate();
175                         WriteMessage(ini);
176                         Assert::AreEqual(ini.sections.at("sec1").at("c"), std::string("0 2"));
177                         Assert::AreEqual(ini.sections.at("sec2").at("c"), std::string("3 1"));
178                 }
179         };
180 } // namespace unittest
181
182 #ifndef _MSC_VER
183 int main() {
184         unittest::UnitTest test;
185         test.TestParseGenerate1();
186         test.TestParseGenerate1W();
187         test.TestParseGenerate2();
188         test.TestParseGenerate2W();
189         test.TestParseGenerate3();
190         test.TestParseGenerate4();
191         test.TestInfiniteRecursion1();
192         test.TestInfiniteRecursion2();
193         test.TestInfiniteRecursion3();
194         test.TestInterpolate1();
195         test.TestExtract();
196         test.TestDefault();
197         return 0;
198 }
199 #endif