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
46 if (ff != 12.0) cout << "Error: a/foo != 12!" << endl;
48 const float fff(12.0f);
49 assert(fff == aFoo); // comparion with float value
50 if (fff != aFoo) cout << "Error: 12 != a/foo" << endl;
57 SGPropertyNode* n = testRoot->getNode("a");
60 PropertyObject<int> a1(n, "bar");
63 PropertyObject<int> a5(n, "some/child/path");
65 assert(n->getIntValue("some/child/path") == 4321);
67 SGPropertyNode* m = testRoot->getNode("a/alice");
68 PropertyObject<std::string> a4(m);
74 PropertyObject<std::string> sp("a/alice");
75 assert(sp == "aaaa"); // read
77 sp = "xxxx"; // assignment from char* literal
78 assert(!strcmp(testRoot->getStringValue("a/alice"), "xxxx"));
80 std::string d = "yyyy";
81 sp = d; // assignment from std::string
82 assert(sp == d); // comaprisom with std::string
84 std::string e(sp), f; // check construction-conversion
86 f = sp; // assignment conversion
92 PropertyObject<int> a1("a/bar");
93 PropertyObject<int> a2("b/blah/foo[1]");
95 // ensure assignment between property objects copies values, *not*
96 // copies the property reference
97 a2 = a1 = 88; // a2 should *not* point to a/bar after this!
98 assert(testRoot->getIntValue("b/blah/foo[1]") == 88);
99 assert(a2.node() == testRoot->getNode("b/blah/foo[1]"));
104 PropertyObject<int> a3(a1);
105 assert(a1.node() == a3.node());
111 void testSTLContainer()
113 std::vector<PropertyObject<int> > vec;
114 // enlarging the vec causes the copy-constructor to be called,
115 // when the storage is re-sized
116 vec.push_back(PropertyObject<int>("a/thing[0]"));
117 vec.push_back(PropertyObject<int>("a/thing[1]"));
118 vec.push_back(PropertyObject<int>("a/thing[2]"));
119 vec.push_back(PropertyObject<int>("a/thing[3]"));
126 assert(testRoot->getIntValue("a/thing[2]") == 6789);
127 assert(testRoot->getIntValue("a/thing[3]") == -11);
128 assert(testRoot->getIntValue("a/thing[0]") == 1234);
130 for (int i=0; i<100; ++i) {
132 ::snprintf(path, 128, "d/foo[%d]", i);
133 vec.push_back(PropertyObject<int>(path));
134 testRoot->setIntValue(path, i * 4);
137 assert(vec[0] == 1234);
138 assert(vec[3] == -11);
141 void testReadMissing()
143 PropertyObject<bool> b("not/found/honest");
147 assert(false && "read of missing property didn't throw");
148 } catch (sg_exception& e) {
152 PropertyObject<std::string> s("also/missing");
155 } catch (sg_exception& e) {
162 PropertyObject<bool> a = PropertyObject<bool>::create("a/lemon", true);
164 assert(testRoot->getBoolValue("a/lemon") == true);
167 PropertyObject<int> b(PropertyObject<int>::create("a/pear", 3142));
170 PropertyObject<std::string> c(PropertyObject<std::string>::create("a/lime", "fofofo"));
171 assert(c == "fofofo");
173 // check overloads for string version
174 SGPropertyNode* n = testRoot->getNode("b", true);
175 PropertyObject<std::string> d(PropertyObject<std::string>::create(n, "grape", "xyz"));
176 assert(!strcmp(testRoot->getStringValue("b/grape"), "xyz"));
181 int main(int argc, char* argv[])
183 testRoot = new SGPropertyNode();
184 simgear::PropertyObjectBase::setDefaultRoot(testRoot);
186 // create some properties 'manually'
187 testRoot->setDoubleValue("a/foo", 12.0);
188 testRoot->setIntValue("a/bar", 1234);
189 testRoot->setBoolValue("a/flags[3]", true);
190 testRoot->setStringValue("a/alice", "aaaa");
192 // basic reading / setting