2 #include <simgear/compiler.h>
8 #include "propertyObject.hxx"
10 #include <simgear/structure/exception.hxx>
16 using namespace simgear;
19 SGPropertyNode* testRoot = NULL;
23 PropertyObject<int> aBar("a/bar");
27 PropertyObject<int> aWib("a/wib"); // doesn't exist
28 aWib = 999; // create + set
29 assert(aWib == 999); // read back
30 assert(testRoot->getIntValue("a/wib") == 999);
35 PropertyObject<double> aFoo("a/foo");
39 assert(ff == 12.0); // comparison with literal
41 const float fff(12.0f);
42 assert(fff == aFoo); // comparion with float value
49 SGPropertyNode* n = testRoot->getNode("a");
52 PropertyObject<int> a1(n, "bar");
55 PropertyObject<int> a5(n, "some/child/path");
57 assert(n->getIntValue("some/child/path") == 4321);
59 SGPropertyNode* m = testRoot->getNode("a/alice");
60 PropertyObject<std::string> a4(m);
66 PropertyObject<std::string> sp("a/alice");
67 assert(sp == "aaaa"); // read
69 sp = "xxxx"; // assignment from char* literal
70 assert(!strcmp(testRoot->getStringValue("a/alice"), "xxxx"));
72 std::string d = "yyyy";
73 sp = d; // assignment from std::string
74 assert(sp == d); // comaprisom with std::string
76 std::string e(sp), f; // check construction-conversion
78 f = sp; // assignment conversion
84 PropertyObject<int> a1("a/bar");
85 PropertyObject<int> a2("b/blah/foo[1]");
87 // ensure assignment between property objects copies values, *not*
88 // copies the property reference
89 a2 = a1 = 88; // a2 should *not* point to a/bar after this!
90 assert(testRoot->getIntValue("b/blah/foo[1]") == 88);
91 assert(a2.node() == testRoot->getNode("b/blah/foo[1]"));
96 PropertyObject<int> a3(a1);
97 assert(a1.node() == a3.node());
103 void testSTLContainer()
105 std::vector<PropertyObject<int> > vec;
106 // enlarging the vec causes the copy-constructor to be called,
107 // when the storage is re-sized
108 vec.push_back(PropertyObject<int>("a/thing[0]"));
109 vec.push_back(PropertyObject<int>("a/thing[1]"));
110 vec.push_back(PropertyObject<int>("a/thing[2]"));
111 vec.push_back(PropertyObject<int>("a/thing[3]"));
118 assert(testRoot->getIntValue("a/thing[2]") == 6789);
119 assert(testRoot->getIntValue("a/thing[3]") == -11);
120 assert(testRoot->getIntValue("a/thing[0]") == 1234);
122 for (int i=0; i<100; ++i) {
124 ::snprintf(path, 128, "d/foo[%d]", i);
125 vec.push_back(PropertyObject<int>(path));
126 testRoot->setIntValue(path, i * 4);
129 assert(vec[0] == 1234);
130 assert(vec[3] == -11);
133 void testReadMissing()
135 PropertyObject<bool> b("not/found/honest");
139 assert(false && "read of missing property didn't throw");
140 } catch (sg_exception& e) {
144 PropertyObject<std::string> s("also/missing");
147 } catch (sg_exception& e) {
154 PropertyObject<bool> a = PropertyObject<bool>::create("a/lemon", true);
156 assert(testRoot->getBoolValue("a/lemon") == true);
159 PropertyObject<int> b(PropertyObject<int>::create("a/pear", 3142));
162 PropertyObject<std::string> c(PropertyObject<std::string>::create("a/lime", "fofofo"));
163 assert(c == "fofofo");
165 // check overloads for string version
166 SGPropertyNode* n = testRoot->getNode("b", true);
167 PropertyObject<std::string> d(PropertyObject<std::string>::create(n, "grape", "xyz"));
168 assert(!strcmp(testRoot->getStringValue("b/grape"), "xyz"));
173 int main(int argc, char* argv[])
175 testRoot = new SGPropertyNode();
176 simgear::PropertyObjectBase::setDefaultRoot(testRoot);
178 // create some properties 'manually'
179 testRoot->setDoubleValue("a/foo", 12.0);
180 testRoot->setIntValue("a/bar", 1234);
181 testRoot->setBoolValue("a/flags[3]", true);
182 testRoot->setStringValue("a/alice", "aaaa");
184 // basic reading / setting