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