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 #include "propertyObject.hxx"
20 #include <simgear/structure/exception.hxx>
26 using namespace simgear;
29 SGPropertyNode* testRoot = NULL;
33 PropertyObject<int> aBar("a/bar");
37 PropertyObject<int> aWib("a/wib"); // doesn't exist
38 aWib = 999; // create + set
39 assert(aWib == 999); // read back
40 assert(testRoot->getIntValue("a/wib") == 999);
45 PropertyObject<double> aFoo("a/foo");
49 assert(ff == 12.0); // comparison with literal
50 if (ff != 12.0) cout << "Error: a/foo != 12!" << endl;
52 const float fff(12.0f);
53 assert(fff == aFoo); // comparion with float value
54 if (fff != aFoo) cout << "Error: 12 != a/foo" << endl;
61 SGPropertyNode* n = testRoot->getNode("a");
64 PropertyObject<int> a1(n, "bar");
67 PropertyObject<int> a5(n, "some/child/path");
69 assert(n->getIntValue("some/child/path") == 4321);
71 SGPropertyNode* m = testRoot->getNode("a/alice");
72 PropertyObject<std::string> a4(m);
78 PropertyObject<std::string> sp("a/alice");
79 assert(sp == "aaaa"); // read
81 sp = "xxxx"; // assignment from char* literal
82 assert(!strcmp(testRoot->getStringValue("a/alice"), "xxxx"));
84 std::string d = "yyyy";
85 sp = d; // assignment from std::string
86 assert(sp == d); // comaprisom with std::string
88 std::string e(sp), f; // check construction-conversion
90 f = sp; // assignment conversion
96 PropertyObject<int> a1("a/bar");
97 PropertyObject<int> a2("b/blah/foo[1]");
99 // ensure assignment between property objects copies values, *not*
100 // copies the property reference
101 a2 = a1 = 88; // a2 should *not* point to a/bar after this!
102 assert(testRoot->getIntValue("b/blah/foo[1]") == 88);
103 assert(a2.node() == testRoot->getNode("b/blah/foo[1]"));
108 PropertyObject<int> a3(a1);
109 assert(a1.node() == a3.node());
113 // Compound assignment ops
136 void testSTLContainer()
138 std::vector<PropertyObject<int> > vec;
139 // enlarging the vec causes the copy-constructor to be called,
140 // when the storage is re-sized
141 vec.push_back(PropertyObject<int>("a/thing[0]"));
142 vec.push_back(PropertyObject<int>("a/thing[1]"));
143 vec.push_back(PropertyObject<int>("a/thing[2]"));
144 vec.push_back(PropertyObject<int>("a/thing[3]"));
151 assert(testRoot->getIntValue("a/thing[2]") == 6789);
152 assert(testRoot->getIntValue("a/thing[3]") == -11);
153 assert(testRoot->getIntValue("a/thing[0]") == 1234);
155 for (int i=0; i<100; ++i) {
157 ::snprintf(path, 128, "d/foo[%d]", i);
158 vec.push_back(PropertyObject<int>(path));
159 testRoot->setIntValue(path, i * 4);
162 assert(vec[0] == 1234);
163 assert(vec[3] == -11);
166 void testReadMissing()
168 PropertyObject<bool> b("not/found/honest");
172 assert(false && "read of missing property didn't throw");
173 (void) v; // don't warn about unused variable
174 } catch (sg_exception& e) {
178 PropertyObject<std::string> s("also/missing");
181 } catch (sg_exception& e) {
188 PropertyObject<bool> a = PropertyObject<bool>::create("a/lemon", true);
190 assert(testRoot->getBoolValue("a/lemon") == true);
193 PropertyObject<int> b(PropertyObject<int>::create("a/pear", 3142));
196 PropertyObject<std::string> c(PropertyObject<std::string>::create("a/lime", "fofofo"));
197 assert(c == "fofofo");
199 // check overloads for string version
200 SGPropertyNode* n = testRoot->getNode("b", true);
201 PropertyObject<std::string> d(PropertyObject<std::string>::create(n, "grape", "xyz"));
202 assert(!strcmp(testRoot->getStringValue("b/grape"), "xyz"));
207 int main(int argc, char* argv[])
209 testRoot = new SGPropertyNode();
210 simgear::PropertyObjectBase::setDefaultRoot(testRoot);
212 // create some properties 'manually'
213 testRoot->setDoubleValue("a/foo", 12.0);
214 testRoot->setIntValue("a/bar", 1234);
215 testRoot->setBoolValue("a/flags[3]", true);
216 testRoot->setStringValue("a/alice", "aaaa");
218 // basic reading / setting