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>
17 // working around MSVC weirdness with props.hxx and SGMathFwd
18 #include <simgear/math/SGMath.hxx>
20 #include "propertyObject.hxx"
22 #include <simgear/structure/exception.hxx>
28 using namespace simgear;
31 SGPropertyNode* testRoot = NULL;
35 PropertyObject<int> aBar("a/bar");
39 PropertyObject<int> aWib("a/wib"); // doesn't exist
40 aWib = 999; // create + set
41 assert(aWib == 999); // read back
42 assert(testRoot->getIntValue("a/wib") == 999);
47 PropertyObject<double> aFoo("a/foo");
51 assert(ff == 12.0); // comparison with literal
52 if (ff != 12.0) cout << "Error: a/foo != 12!" << endl;
54 const float fff(12.0f);
55 assert(fff == aFoo); // comparion with float value
56 if (fff != aFoo) cout << "Error: 12 != a/foo" << endl;
63 SGPropertyNode* n = testRoot->getNode("a");
66 PropertyObject<int> a1(n, "bar");
69 PropertyObject<int> a5(n, "some/child/path");
71 assert(n->getIntValue("some/child/path") == 4321);
73 SGPropertyNode* m = testRoot->getNode("a/alice");
74 PropertyObject<std::string> a4(m);
80 PropertyObject<std::string> sp("a/alice");
81 assert(sp == "aaaa"); // read
83 sp = "xxxx"; // assignment from char* literal
84 assert(!strcmp(testRoot->getStringValue("a/alice"), "xxxx"));
86 std::string d = "yyyy";
87 sp = d; // assignment from std::string
88 assert(sp == d); // comaprisom with std::string
90 std::string e(sp), f; // check construction-conversion
92 f = sp; // assignment conversion
98 PropertyObject<int> a1("a/bar");
99 PropertyObject<int> a2("b/blah/foo[1]");
101 // ensure assignment between property objects copies values, *not*
102 // copies the property reference
103 a2 = a1 = 88; // a2 should *not* point to a/bar after this!
104 assert(testRoot->getIntValue("b/blah/foo[1]") == 88);
105 assert(a2.node() == testRoot->getNode("b/blah/foo[1]"));
110 PropertyObject<int> a3(a1);
111 assert(a1.node() == a3.node());
117 void testSTLContainer()
119 std::vector<PropertyObject<int> > vec;
120 // enlarging the vec causes the copy-constructor to be called,
121 // when the storage is re-sized
122 vec.push_back(PropertyObject<int>("a/thing[0]"));
123 vec.push_back(PropertyObject<int>("a/thing[1]"));
124 vec.push_back(PropertyObject<int>("a/thing[2]"));
125 vec.push_back(PropertyObject<int>("a/thing[3]"));
132 assert(testRoot->getIntValue("a/thing[2]") == 6789);
133 assert(testRoot->getIntValue("a/thing[3]") == -11);
134 assert(testRoot->getIntValue("a/thing[0]") == 1234);
136 for (int i=0; i<100; ++i) {
138 ::snprintf(path, 128, "d/foo[%d]", i);
139 vec.push_back(PropertyObject<int>(path));
140 testRoot->setIntValue(path, i * 4);
143 assert(vec[0] == 1234);
144 assert(vec[3] == -11);
147 void testReadMissing()
149 PropertyObject<bool> b("not/found/honest");
153 assert(false && "read of missing property didn't throw");
154 (void) v; // don't warn about unused variable
155 } catch (sg_exception& e) {
159 PropertyObject<std::string> s("also/missing");
162 } catch (sg_exception& e) {
169 PropertyObject<bool> a = PropertyObject<bool>::create("a/lemon", true);
171 assert(testRoot->getBoolValue("a/lemon") == true);
174 PropertyObject<int> b(PropertyObject<int>::create("a/pear", 3142));
177 PropertyObject<std::string> c(PropertyObject<std::string>::create("a/lime", "fofofo"));
178 assert(c == "fofofo");
180 // check overloads for string version
181 SGPropertyNode* n = testRoot->getNode("b", true);
182 PropertyObject<std::string> d(PropertyObject<std::string>::create(n, "grape", "xyz"));
183 assert(!strcmp(testRoot->getStringValue("b/grape"), "xyz"));
188 int main(int argc, char* argv[])
190 testRoot = new SGPropertyNode();
191 simgear::PropertyObjectBase::setDefaultRoot(testRoot);
193 // create some properties 'manually'
194 testRoot->setDoubleValue("a/foo", 12.0);
195 testRoot->setIntValue("a/bar", 1234);
196 testRoot->setBoolValue("a/flags[3]", true);
197 testRoot->setStringValue("a/alice", "aaaa");
199 // basic reading / setting