]> git.mxchange.org Git - simgear.git/blob - simgear/props/propertyObject_test.cxx
Remove using std:: from the metar header, remove HTTP support, add very basic unit...
[simgear.git] / simgear / props / propertyObject_test.cxx
1
2 #include <simgear/compiler.h>
3
4 #include <iostream>
5 #include <cassert>
6 #include <cstring>
7
8 // working around MSVC weirdness with props.hxx and SGMathFwd
9 #include <simgear/math/SGMath.hxx>
10
11 #include "propertyObject.hxx"
12
13 #include <simgear/structure/exception.hxx>
14
15 using std::cout;
16 using std::cerr;
17 using std::endl;
18
19 using namespace simgear;
20
21
22 SGPropertyNode* testRoot = NULL;
23
24 bool testBasic()
25 {
26   PropertyObject<int> aBar("a/bar");
27   assert(aBar == 1234);
28
29
30   PropertyObject<int> aWib("a/wib"); // doesn't exist
31   aWib = 999; // create + set
32   assert(aWib == 999); // read back
33   assert(testRoot->getIntValue("a/wib") == 999);
34
35   assert(aWib < 1000);
36   assert(998 < aWib);
37
38   PropertyObject<double> aFoo("a/foo");
39   assert(aFoo == 12.0);
40
41   double ff(aFoo);
42   assert(ff == 12.0); // comparison with literal
43   
44   const float fff(12.0f);
45   assert(fff == aFoo); // comparion with float value
46
47   return true;
48 }
49
50 void testRelative()
51 {
52   SGPropertyNode* n = testRoot->getNode("a");
53   assert(n);
54
55   PropertyObject<int> a1(n, "bar");
56   assert(a1 == 1234);
57
58   PropertyObject<int> a5(n, "some/child/path");
59   a5 = 4321;
60   assert(n->getIntValue("some/child/path") == 4321);
61
62   SGPropertyNode* m = testRoot->getNode("a/alice");
63   PropertyObject<std::string> a4(m);
64   assert(a4 == "aaaa");
65 }
66
67 void testString()
68 {
69   PropertyObject<std::string> sp("a/alice");
70   assert(sp == "aaaa"); // read
71
72   sp = "xxxx"; // assignment from char* literal
73   assert(!strcmp(testRoot->getStringValue("a/alice"), "xxxx"));
74
75   std::string d = "yyyy";
76   sp = d; // assignment from std::string
77   assert(sp == d);  // comaprisom with std::string
78
79   std::string e(sp), f; // check construction-conversion
80   assert(e == "yyyy");
81   f = sp; // assignment conversion
82   assert(f == "yyyy");
83 }
84
85 void testAssignment()
86 {
87   PropertyObject<int> a1("a/bar");
88   PropertyObject<int> a2("b/blah/foo[1]");
89
90  // ensure assignment between property objects copies values, *not*
91  // copies the property reference
92   a2 = a1 = 88; // a2 should *not* point to a/bar after this!
93   assert(testRoot->getIntValue("b/blah/foo[1]") == 88);
94   assert(a2.node() == testRoot->getNode("b/blah/foo[1]"));
95   a2 = 99;
96   assert(a2 == 99);
97   assert(a1 == 88);
98
99   PropertyObject<int> a3(a1);
100   assert(a1.node() == a3.node());
101   a3 = 44;
102   assert(a1 == 44);
103
104 }
105
106 void testSTLContainer()
107 {
108   std::vector<PropertyObject<int> > vec;
109 // enlarging the vec causes the copy-constructor to be called,
110 // when the storage is re-sized
111   vec.push_back(PropertyObject<int>("a/thing[0]")); 
112   vec.push_back(PropertyObject<int>("a/thing[1]")); 
113   vec.push_back(PropertyObject<int>("a/thing[2]")); 
114   vec.push_back(PropertyObject<int>("a/thing[3]")); 
115
116   vec[0] = 1234;
117   vec[1] = 2345;
118   vec[2] = 6789;
119   vec[3] = -11;
120
121   assert(testRoot->getIntValue("a/thing[2]") == 6789);
122   assert(testRoot->getIntValue("a/thing[3]") == -11);
123   assert(testRoot->getIntValue("a/thing[0]") == 1234);
124
125   for (int i=0; i<100; ++i) {
126     char path[128];
127     ::snprintf(path, 128, "d/foo[%d]", i);
128     vec.push_back(PropertyObject<int>(path));
129     testRoot->setIntValue(path, i * 4);
130   }
131
132   assert(vec[0] == 1234);
133   assert(vec[3] == -11);
134 }
135
136 void testReadMissing()
137 {
138   PropertyObject<bool> b("not/found/honest");
139
140   try {
141     bool v = b;    
142     assert(false && "read of missing property didn't throw");
143   } catch (sg_exception& e) {
144     // expected
145   }
146
147   PropertyObject<std::string> s("also/missing");
148   try {
149     std::string s2 = s;
150   } catch (sg_exception& e) {
151     // expected
152   }
153 }
154
155 void testCreate()
156 {
157   PropertyObject<bool> a = PropertyObject<bool>::create("a/lemon", true);
158   assert(a == true);
159   assert(testRoot->getBoolValue("a/lemon") == true);
160   
161
162   PropertyObject<int> b(PropertyObject<int>::create("a/pear", 3142));
163   assert(b == 3142);
164   
165   PropertyObject<std::string> c(PropertyObject<std::string>::create("a/lime", "fofofo"));
166   assert(c == "fofofo");
167
168 // check overloads for string version
169   SGPropertyNode* n = testRoot->getNode("b", true);
170   PropertyObject<std::string> d(PropertyObject<std::string>::create(n, "grape", "xyz"));
171   assert(!strcmp(testRoot->getStringValue("b/grape"), "xyz"));
172   
173   
174 }
175
176 int main(int argc, char* argv[])
177 {
178         testRoot = new SGPropertyNode();
179         simgear::PropertyObjectBase::setDefaultRoot(testRoot);
180
181 // create some properties 'manually'
182         testRoot->setDoubleValue("a/foo", 12.0);
183         testRoot->setIntValue("a/bar", 1234);
184         testRoot->setBoolValue("a/flags[3]", true);
185   testRoot->setStringValue("a/alice", "aaaa");
186
187 // basic reading / setting
188         if (!testBasic()) {
189                 return EXIT_FAILURE;            
190         }
191
192   testRelative();
193   testReadMissing();
194   testString();
195   testAssignment();
196   testSTLContainer();
197   testCreate();
198
199   return EXIT_SUCCESS;
200 }
201