2 # include <simgear_config.h>
5 #include <simgear/compiler.h>
11 // working around MSVC weirdness with props.hxx and SGMathFwd
12 #include <simgear/math/SGMath.hxx>
14 #include "propertyObject.hxx"
16 #include <simgear/structure/exception.hxx>
22 using namespace simgear;
25 SGPropertyNode* testRoot = NULL;
29 PropertyObject<int> aBar("a/bar");
33 PropertyObject<int> aWib("a/wib"); // doesn't exist
34 aWib = 999; // create + set
35 assert(aWib == 999); // read back
36 assert(testRoot->getIntValue("a/wib") == 999);
41 PropertyObject<double> aFoo("a/foo");
45 assert(ff == 12.0); // comparison with literal
47 const float fff(12.0f);
48 assert(fff == aFoo); // comparion with float value
55 SGPropertyNode* n = testRoot->getNode("a");
58 PropertyObject<int> a1(n, "bar");
61 PropertyObject<int> a5(n, "some/child/path");
63 assert(n->getIntValue("some/child/path") == 4321);
65 SGPropertyNode* m = testRoot->getNode("a/alice");
66 PropertyObject<std::string> a4(m);
72 PropertyObject<std::string> sp("a/alice");
73 assert(sp == "aaaa"); // read
75 sp = "xxxx"; // assignment from char* literal
76 assert(!strcmp(testRoot->getStringValue("a/alice"), "xxxx"));
78 std::string d = "yyyy";
79 sp = d; // assignment from std::string
80 assert(sp == d); // comaprisom with std::string
82 std::string e(sp), f; // check construction-conversion
84 f = sp; // assignment conversion
90 PropertyObject<int> a1("a/bar");
91 PropertyObject<int> a2("b/blah/foo[1]");
93 // ensure assignment between property objects copies values, *not*
94 // copies the property reference
95 a2 = a1 = 88; // a2 should *not* point to a/bar after this!
96 assert(testRoot->getIntValue("b/blah/foo[1]") == 88);
97 assert(a2.node() == testRoot->getNode("b/blah/foo[1]"));
102 PropertyObject<int> a3(a1);
103 assert(a1.node() == a3.node());
109 void testSTLContainer()
111 std::vector<PropertyObject<int> > vec;
112 // enlarging the vec causes the copy-constructor to be called,
113 // when the storage is re-sized
114 vec.push_back(PropertyObject<int>("a/thing[0]"));
115 vec.push_back(PropertyObject<int>("a/thing[1]"));
116 vec.push_back(PropertyObject<int>("a/thing[2]"));
117 vec.push_back(PropertyObject<int>("a/thing[3]"));
124 assert(testRoot->getIntValue("a/thing[2]") == 6789);
125 assert(testRoot->getIntValue("a/thing[3]") == -11);
126 assert(testRoot->getIntValue("a/thing[0]") == 1234);
128 for (int i=0; i<100; ++i) {
130 ::snprintf(path, 128, "d/foo[%d]", i);
131 vec.push_back(PropertyObject<int>(path));
132 testRoot->setIntValue(path, i * 4);
135 assert(vec[0] == 1234);
136 assert(vec[3] == -11);
139 void testReadMissing()
141 PropertyObject<bool> b("not/found/honest");
145 assert(false && "read of missing property didn't throw");
146 } catch (sg_exception& e) {
150 PropertyObject<std::string> s("also/missing");
153 } catch (sg_exception& e) {
160 PropertyObject<bool> a = PropertyObject<bool>::create("a/lemon", true);
162 assert(testRoot->getBoolValue("a/lemon") == true);
165 PropertyObject<int> b(PropertyObject<int>::create("a/pear", 3142));
168 PropertyObject<std::string> c(PropertyObject<std::string>::create("a/lime", "fofofo"));
169 assert(c == "fofofo");
171 // check overloads for string version
172 SGPropertyNode* n = testRoot->getNode("b", true);
173 PropertyObject<std::string> d(PropertyObject<std::string>::create(n, "grape", "xyz"));
174 assert(!strcmp(testRoot->getStringValue("b/grape"), "xyz"));
179 int main(int argc, char* argv[])
181 testRoot = new SGPropertyNode();
182 simgear::PropertyObjectBase::setDefaultRoot(testRoot);
184 // create some properties 'manually'
185 testRoot->setDoubleValue("a/foo", 12.0);
186 testRoot->setIntValue("a/bar", 1234);
187 testRoot->setBoolValue("a/flags[3]", true);
188 testRoot->setStringValue("a/alice", "aaaa");
190 // basic reading / setting