2 # include <simgear_config.h>
6 // Always enable DEBUG mode in test application, otherwise "assert" test
7 // statements have no effect and don't actually test anything (catch 17 ;-) ).
11 #include <simgear/compiler.h>
19 #include "propertyObject.hxx"
21 #include <simgear/structure/exception.hxx>
27 using namespace simgear;
30 SGPropertyNode* testRoot = NULL;
34 PropertyObject<int> aBar("a/bar");
38 PropertyObject<int> aWib("a/wib"); // doesn't exist
39 aWib = 999; // create + set
40 assert(aWib == 999); // read back
41 assert(testRoot->getIntValue("a/wib") == 999);
46 PropertyObject<double> aFoo("a/foo");
50 assert(ff == 12.0); // comparison with literal
51 if (ff != 12.0) cout << "Error: a/foo != 12!" << endl;
53 const float fff(12.0f);
54 assert(fff == aFoo); // comparion with float value
55 if (fff != aFoo) cout << "Error: 12 != a/foo" << endl;
62 SGPropertyNode* n = testRoot->getNode("a");
65 PropertyObject<int> a1(n, "bar");
68 PropertyObject<int> a5(n, "some/child/path");
70 assert(n->getIntValue("some/child/path") == 4321);
72 SGPropertyNode* m = testRoot->getNode("a/alice");
73 PropertyObject<std::string> a4(m);
79 PropertyObject<std::string> sp("a/alice");
80 assert(sp == "aaaa"); // read
82 sp = "xxxx"; // assignment from char* literal
83 assert(!strcmp(testRoot->getStringValue("a/alice"), "xxxx"));
85 std::string d = "yyyy";
86 sp = d; // assignment from std::string
87 assert(sp == d); // comaprisom with std::string
89 std::string e(sp), f; // check construction-conversion
91 f = sp; // assignment conversion
97 PropertyObject<int> a1("a/bar");
98 PropertyObject<int> a2("b/blah/foo[1]");
100 // ensure assignment between property objects copies values, *not*
101 // copies the property reference
102 a2 = a1 = 88; // a2 should *not* point to a/bar after this!
103 assert(testRoot->getIntValue("b/blah/foo[1]") == 88);
104 assert(a2.node() == testRoot->getNode("b/blah/foo[1]"));
109 PropertyObject<int> a3(a1);
110 assert(a1.node() == a3.node());
114 // Compound assignment ops
137 void testSTLContainer()
139 std::vector<PropertyObject<int> > vec;
140 // enlarging the vec causes the copy-constructor to be called,
141 // when the storage is re-sized
142 vec.push_back(PropertyObject<int>("a/thing[0]"));
143 vec.push_back(PropertyObject<int>("a/thing[1]"));
144 vec.push_back(PropertyObject<int>("a/thing[2]"));
145 vec.push_back(PropertyObject<int>("a/thing[3]"));
152 assert(testRoot->getIntValue("a/thing[2]") == 6789);
153 assert(testRoot->getIntValue("a/thing[3]") == -11);
154 assert(testRoot->getIntValue("a/thing[0]") == 1234);
156 for (int i=0; i<100; ++i) {
158 ::snprintf(path, 128, "d/foo[%d]", i);
159 vec.push_back(PropertyObject<int>(path));
160 testRoot->setIntValue(path, i * 4);
163 assert(vec[0] == 1234);
164 assert(vec[3] == -11);
167 void testReadMissing()
169 PropertyObject<bool> b("not/found/honest");
173 assert(false && "read of missing property didn't throw");
174 (void) v; // don't warn about unused variable
175 } catch (sg_exception& e) {
179 PropertyObject<std::string> s("also/missing");
182 } catch (sg_exception& e) {
189 PropertyObject<bool> a = PropertyObject<bool>::create("a/lemon", true);
191 assert(testRoot->getBoolValue("a/lemon") == true);
194 PropertyObject<int> b(PropertyObject<int>::create("a/pear", 3142));
197 PropertyObject<std::string> c(PropertyObject<std::string>::create("a/lime", "fofofo"));
198 assert(c == "fofofo");
200 // check overloads for string version
201 SGPropertyNode* n = testRoot->getNode("b", true);
202 PropertyObject<std::string> d(PropertyObject<std::string>::create(n, "grape", "xyz"));
203 assert(!strcmp(testRoot->getStringValue("b/grape"), "xyz"));
208 int main(int argc, char* argv[])
210 testRoot = new SGPropertyNode();
211 simgear::PropertyObjectBase::setDefaultRoot(testRoot);
213 // create some properties 'manually'
214 testRoot->setDoubleValue("a/foo", 12.0);
215 testRoot->setIntValue("a/bar", 1234);
216 testRoot->setBoolValue("a/flags[3]", true);
217 testRoot->setStringValue("a/alice", "aaaa");
219 // basic reading / setting