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