2 #include <simgear/compiler.h>
8 // working around MSVC weirdness with props.hxx and SGMathFwd
9 #include <simgear/math/SGMath.hxx>
11 #include "propertyObject.hxx"
13 #include <simgear/structure/exception.hxx>
19 using namespace simgear;
22 SGPropertyNode* testRoot = NULL;
26 PropertyObject<int> aBar("a/bar");
30 PropertyObject<int> aWib("a/wib"); // doesn't exist
31 aWib = 999; // create + set
32 assert(aWib == 999); // read back
33 assert(testRoot->getIntValue("a/wib") == 999);
38 PropertyObject<double> aFoo("a/foo");
42 assert(ff == 12.0); // comparison with literal
44 const float fff(12.0f);
45 assert(fff == aFoo); // comparion with float value
52 SGPropertyNode* n = testRoot->getNode("a");
55 PropertyObject<int> a1(n, "bar");
58 PropertyObject<int> a5(n, "some/child/path");
60 assert(n->getIntValue("some/child/path") == 4321);
62 SGPropertyNode* m = testRoot->getNode("a/alice");
63 PropertyObject<std::string> a4(m);
69 PropertyObject<std::string> sp("a/alice");
70 assert(sp == "aaaa"); // read
72 sp = "xxxx"; // assignment from char* literal
73 assert(!strcmp(testRoot->getStringValue("a/alice"), "xxxx"));
75 std::string d = "yyyy";
76 sp = d; // assignment from std::string
77 assert(sp == d); // comaprisom with std::string
79 std::string e(sp), f; // check construction-conversion
81 f = sp; // assignment conversion
87 PropertyObject<int> a1("a/bar");
88 PropertyObject<int> a2("b/blah/foo[1]");
90 // ensure assignment between property objects copies values, *not*
91 // copies the property reference
92 a2 = a1 = 88; // a2 should *not* point to a/bar after this!
93 assert(testRoot->getIntValue("b/blah/foo[1]") == 88);
94 assert(a2.node() == testRoot->getNode("b/blah/foo[1]"));
99 PropertyObject<int> a3(a1);
100 assert(a1.node() == a3.node());
106 void testSTLContainer()
108 std::vector<PropertyObject<int> > vec;
109 // enlarging the vec causes the copy-constructor to be called,
110 // when the storage is re-sized
111 vec.push_back(PropertyObject<int>("a/thing[0]"));
112 vec.push_back(PropertyObject<int>("a/thing[1]"));
113 vec.push_back(PropertyObject<int>("a/thing[2]"));
114 vec.push_back(PropertyObject<int>("a/thing[3]"));
121 assert(testRoot->getIntValue("a/thing[2]") == 6789);
122 assert(testRoot->getIntValue("a/thing[3]") == -11);
123 assert(testRoot->getIntValue("a/thing[0]") == 1234);
125 for (int i=0; i<100; ++i) {
127 ::snprintf(path, 128, "d/foo[%d]", i);
128 vec.push_back(PropertyObject<int>(path));
129 testRoot->setIntValue(path, i * 4);
132 assert(vec[0] == 1234);
133 assert(vec[3] == -11);
136 void testReadMissing()
138 PropertyObject<bool> b("not/found/honest");
142 assert(false && "read of missing property didn't throw");
143 } catch (sg_exception& e) {
147 PropertyObject<std::string> s("also/missing");
150 } catch (sg_exception& e) {
157 PropertyObject<bool> a = PropertyObject<bool>::create("a/lemon", true);
159 assert(testRoot->getBoolValue("a/lemon") == true);
162 PropertyObject<int> b(PropertyObject<int>::create("a/pear", 3142));
165 PropertyObject<std::string> c(PropertyObject<std::string>::create("a/lime", "fofofo"));
166 assert(c == "fofofo");
168 // check overloads for string version
169 SGPropertyNode* n = testRoot->getNode("b", true);
170 PropertyObject<std::string> d(PropertyObject<std::string>::create(n, "grape", "xyz"));
171 assert(!strcmp(testRoot->getStringValue("b/grape"), "xyz"));
176 int main(int argc, char* argv[])
178 testRoot = new SGPropertyNode();
179 simgear::PropertyObjectBase::setDefaultRoot(testRoot);
181 // create some properties 'manually'
182 testRoot->setDoubleValue("a/foo", 12.0);
183 testRoot->setIntValue("a/bar", 1234);
184 testRoot->setBoolValue("a/flags[3]", true);
185 testRoot->setStringValue("a/alice", "aaaa");
187 // basic reading / setting