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>
18 #include "propertyObject.hxx"
20 #include <simgear/structure/exception.hxx>
26 using namespace simgear;
29 SGPropertyNode* testRoot = NULL;
33 PropertyObject<int> aBar("a/bar");
37 PropertyObject<int> aWib("a/wib"); // doesn't exist
38 aWib = 999; // create + set
39 assert(aWib == 999); // read back
40 assert(testRoot->getIntValue("a/wib") == 999);
45 PropertyObject<double> aFoo("a/foo");
49 assert(ff == 12.0); // comparison with literal
50 if (ff != 12.0) cout << "Error: a/foo != 12!" << endl;
52 const float fff(12.0f);
53 assert(fff == aFoo); // comparion with float value
54 if (fff != aFoo) cout << "Error: 12 != a/foo" << endl;
61 SGPropertyNode* n = testRoot->getNode("a");
64 PropertyObject<int> a1(n, "bar");
67 PropertyObject<int> a5(n, "some/child/path");
69 assert(n->getIntValue("some/child/path") == 4321);
71 SGPropertyNode* m = testRoot->getNode("a/alice");
72 PropertyObject<std::string> a4(m);
78 PropertyObject<std::string> sp("a/alice");
79 assert(sp == "aaaa"); // read
81 sp = "xxxx"; // assignment from char* literal
82 assert(!strcmp(testRoot->getStringValue("a/alice"), "xxxx"));
84 std::string d = "yyyy";
85 sp = d; // assignment from std::string
86 assert(sp == d); // comaprisom with std::string
88 std::string e(sp), f; // check construction-conversion
90 f = sp; // assignment conversion
96 PropertyObject<int> a1("a/bar");
97 PropertyObject<int> a2("b/blah/foo[1]");
99 // ensure assignment between property objects copies values, *not*
100 // copies the property reference
101 a2 = a1 = 88; // a2 should *not* point to a/bar after this!
102 assert(testRoot->getIntValue("b/blah/foo[1]") == 88);
103 assert(a2.node() == testRoot->getNode("b/blah/foo[1]"));
108 PropertyObject<int> a3(a1);
109 assert(a1.node() == a3.node());
115 void testSTLContainer()
117 std::vector<PropertyObject<int> > vec;
118 // enlarging the vec causes the copy-constructor to be called,
119 // when the storage is re-sized
120 vec.push_back(PropertyObject<int>("a/thing[0]"));
121 vec.push_back(PropertyObject<int>("a/thing[1]"));
122 vec.push_back(PropertyObject<int>("a/thing[2]"));
123 vec.push_back(PropertyObject<int>("a/thing[3]"));
130 assert(testRoot->getIntValue("a/thing[2]") == 6789);
131 assert(testRoot->getIntValue("a/thing[3]") == -11);
132 assert(testRoot->getIntValue("a/thing[0]") == 1234);
134 for (int i=0; i<100; ++i) {
136 ::snprintf(path, 128, "d/foo[%d]", i);
137 vec.push_back(PropertyObject<int>(path));
138 testRoot->setIntValue(path, i * 4);
141 assert(vec[0] == 1234);
142 assert(vec[3] == -11);
145 void testReadMissing()
147 PropertyObject<bool> b("not/found/honest");
151 assert(false && "read of missing property didn't throw");
152 (void) v; // don't warn about unused variable
153 } catch (sg_exception& e) {
157 PropertyObject<std::string> s("also/missing");
160 } catch (sg_exception& e) {
167 PropertyObject<bool> a = PropertyObject<bool>::create("a/lemon", true);
169 assert(testRoot->getBoolValue("a/lemon") == true);
172 PropertyObject<int> b(PropertyObject<int>::create("a/pear", 3142));
175 PropertyObject<std::string> c(PropertyObject<std::string>::create("a/lime", "fofofo"));
176 assert(c == "fofofo");
178 // check overloads for string version
179 SGPropertyNode* n = testRoot->getNode("b", true);
180 PropertyObject<std::string> d(PropertyObject<std::string>::create(n, "grape", "xyz"));
181 assert(!strcmp(testRoot->getStringValue("b/grape"), "xyz"));
186 int main(int argc, char* argv[])
188 testRoot = new SGPropertyNode();
189 simgear::PropertyObjectBase::setDefaultRoot(testRoot);
191 // create some properties 'manually'
192 testRoot->setDoubleValue("a/foo", 12.0);
193 testRoot->setIntValue("a/bar", 1234);
194 testRoot->setBoolValue("a/flags[3]", true);
195 testRoot->setStringValue("a/alice", "aaaa");
197 // basic reading / setting