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 // working around MSVC weirdness with props.hxx and SGMathFwd
19 #include <simgear/math/SGMath.hxx>
21 #include "propertyObject.hxx"
23 #include <simgear/structure/exception.hxx>
29 using namespace simgear;
32 SGPropertyNode* testRoot = NULL;
36 PropertyObject<int> aBar("a/bar");
40 PropertyObject<int> aWib("a/wib"); // doesn't exist
41 aWib = 999; // create + set
42 assert(aWib == 999); // read back
43 assert(testRoot->getIntValue("a/wib") == 999);
48 PropertyObject<double> aFoo("a/foo");
52 assert(ff == 12.0); // comparison with literal
53 if (ff != 12.0) cout << "Error: a/foo != 12!" << endl;
55 const float fff(12.0f);
56 assert(fff == aFoo); // comparion with float value
57 if (fff != aFoo) cout << "Error: 12 != a/foo" << endl;
64 SGPropertyNode* n = testRoot->getNode("a");
67 PropertyObject<int> a1(n, "bar");
70 PropertyObject<int> a5(n, "some/child/path");
72 assert(n->getIntValue("some/child/path") == 4321);
74 SGPropertyNode* m = testRoot->getNode("a/alice");
75 PropertyObject<std::string> a4(m);
81 PropertyObject<std::string> sp("a/alice");
82 assert(sp == "aaaa"); // read
84 sp = "xxxx"; // assignment from char* literal
85 assert(!strcmp(testRoot->getStringValue("a/alice"), "xxxx"));
87 std::string d = "yyyy";
88 sp = d; // assignment from std::string
89 assert(sp == d); // comaprisom with std::string
91 std::string e(sp), f; // check construction-conversion
93 f = sp; // assignment conversion
99 PropertyObject<int> a1("a/bar");
100 PropertyObject<int> a2("b/blah/foo[1]");
102 // ensure assignment between property objects copies values, *not*
103 // copies the property reference
104 a2 = a1 = 88; // a2 should *not* point to a/bar after this!
105 assert(testRoot->getIntValue("b/blah/foo[1]") == 88);
106 assert(a2.node() == testRoot->getNode("b/blah/foo[1]"));
111 PropertyObject<int> a3(a1);
112 assert(a1.node() == a3.node());
118 void testSTLContainer()
120 std::vector<PropertyObject<int> > vec;
121 // enlarging the vec causes the copy-constructor to be called,
122 // when the storage is re-sized
123 vec.push_back(PropertyObject<int>("a/thing[0]"));
124 vec.push_back(PropertyObject<int>("a/thing[1]"));
125 vec.push_back(PropertyObject<int>("a/thing[2]"));
126 vec.push_back(PropertyObject<int>("a/thing[3]"));
133 assert(testRoot->getIntValue("a/thing[2]") == 6789);
134 assert(testRoot->getIntValue("a/thing[3]") == -11);
135 assert(testRoot->getIntValue("a/thing[0]") == 1234);
137 for (int i=0; i<100; ++i) {
139 ::snprintf(path, 128, "d/foo[%d]", i);
140 vec.push_back(PropertyObject<int>(path));
141 testRoot->setIntValue(path, i * 4);
144 assert(vec[0] == 1234);
145 assert(vec[3] == -11);
148 void testReadMissing()
150 PropertyObject<bool> b("not/found/honest");
154 assert(false && "read of missing property didn't throw");
155 (void) v; // don't warn about unused variable
156 } catch (sg_exception& e) {
160 PropertyObject<std::string> s("also/missing");
163 } catch (sg_exception& e) {
170 PropertyObject<bool> a = PropertyObject<bool>::create("a/lemon", true);
172 assert(testRoot->getBoolValue("a/lemon") == true);
175 PropertyObject<int> b(PropertyObject<int>::create("a/pear", 3142));
178 PropertyObject<std::string> c(PropertyObject<std::string>::create("a/lime", "fofofo"));
179 assert(c == "fofofo");
181 // check overloads for string version
182 SGPropertyNode* n = testRoot->getNode("b", true);
183 PropertyObject<std::string> d(PropertyObject<std::string>::create(n, "grape", "xyz"));
184 assert(!strcmp(testRoot->getStringValue("b/grape"), "xyz"));
189 int main(int argc, char* argv[])
191 testRoot = new SGPropertyNode();
192 simgear::PropertyObjectBase::setDefaultRoot(testRoot);
194 // create some properties 'manually'
195 testRoot->setDoubleValue("a/foo", 12.0);
196 testRoot->setIntValue("a/bar", 1234);
197 testRoot->setBoolValue("a/flags[3]", true);
198 testRoot->setStringValue("a/alice", "aaaa");
200 // basic reading / setting