nodetests.cpp 12.6 KB
Newer Older
1
2
#include "nodetests.h"
#include "yaml-cpp/yaml.h"
3
#include <boost/foreach.hpp>
4
#include <iostream>
5
6
7
8
9
10
11
12
13
14
15
16
17
18

namespace {
	struct TEST {
		TEST(): ok(false) {}
		TEST(bool ok_): ok(ok_) {}
		TEST(const char *error_): ok(false), error(error_) {}
		
		bool ok;
		std::string error;
	};
}

#define YAML_ASSERT(cond) do { if(!(cond)) return "  Assert failed: " #cond; } while(false)

19
20
#define YAML_ASSERT_THROWS(cond, exc) do { try { (cond); return "  Expression did not throw: " #cond; } catch(const exc&) {} catch(...) { return "  Expression threw something other than " #exc ": " #cond; } } while(false)

21
22
23
24
25
26
27
namespace Test
{
	namespace Node
	{
		TEST SimpleScalar()
		{
			YAML::Node node = YAML::Node("Hello, World!");
28
			YAML_ASSERT(node.IsScalar());
29
30
31
			YAML_ASSERT(node.as<std::string>() == "Hello, World!");
			return true;
		}
32
33
34
35
		
		TEST IntScalar()
		{
			YAML::Node node = YAML::Node(15);
36
			YAML_ASSERT(node.IsScalar());
37
38
39
40
			YAML_ASSERT(node.as<int>() == 15);
			return true;
		}

41
		TEST SimpleAppendSequence()
42
43
		{
			YAML::Node node;
44
45
46
			node.push_back(10);
			node.push_back("foo");
			node.push_back("monkey");
47
			YAML_ASSERT(node.IsSequence());
48
49
50
			YAML_ASSERT(node.size() == 3);
			YAML_ASSERT(node[0].as<int>() == 10);
			YAML_ASSERT(node[1].as<std::string>() == "foo");
51
			YAML_ASSERT(node[2].as<std::string>() == "monkey");
52
			YAML_ASSERT(node.IsSequence());
53
54
			return true;
		}
beder's avatar
beder committed
55

56
57
58
59
60
61
		TEST SimpleAssignSequence()
		{
			YAML::Node node;
			node[0] = 10;
			node[1] = "foo";
			node[2] = "monkey";
62
			YAML_ASSERT(node.IsSequence());
63
64
65
66
			YAML_ASSERT(node.size() == 3);
			YAML_ASSERT(node[0].as<int>() == 10);
			YAML_ASSERT(node[1].as<std::string>() == "foo");
			YAML_ASSERT(node[2].as<std::string>() == "monkey");
67
			YAML_ASSERT(node.IsSequence());
68
69
70
			return true;
		}

beder's avatar
beder committed
71
72
73
74
		TEST SimpleMap()
		{
			YAML::Node node;
			node["key"] = "value";
75
			YAML_ASSERT(node.IsMap());
beder's avatar
beder committed
76
77
78
79
80
81
82
83
84
85
			YAML_ASSERT(node["key"].as<std::string>() == "value");
			YAML_ASSERT(node.size() == 1);
			return true;
		}

		TEST MapWithUndefinedValues()
		{
			YAML::Node node;
			node["key"] = "value";
			node["undefined"];
86
			YAML_ASSERT(node.IsMap());
beder's avatar
beder committed
87
88
			YAML_ASSERT(node["key"].as<std::string>() == "value");
			YAML_ASSERT(node.size() == 1);
beder's avatar
beder committed
89

beder's avatar
beder committed
90
91
92
			node["undefined"] = "monkey";
			YAML_ASSERT(node["undefined"].as<std::string>() == "monkey");
			YAML_ASSERT(node.size() == 2);
beder's avatar
beder committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
			
			return true;
		}
		
		TEST MapIteratorWithUndefinedValues()
		{
			YAML::Node node;
			node["key"] = "value";
			node["undefined"];
			
			std::size_t count = 0;
			for(YAML::const_iterator it=node.begin();it!=node.end();++it)
				count++;
			YAML_ASSERT(count == 1);
beder's avatar
beder committed
107
108
			return true;
		}
beder's avatar
beder committed
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
		
		TEST SimpleSubkeys()
		{
			YAML::Node node;
			node["device"]["udid"] = "12345";
			node["device"]["name"] = "iPhone";
			node["device"]["os"] = "4.0";
			node["username"] = "monkey";
			YAML_ASSERT(node["device"]["udid"].as<std::string>() == "12345");
			YAML_ASSERT(node["device"]["name"].as<std::string>() == "iPhone");
			YAML_ASSERT(node["device"]["os"].as<std::string>() == "4.0");
			YAML_ASSERT(node["username"].as<std::string>() == "monkey");
			return true;
		}
		
		TEST StdVector()
		{
			std::vector<int> primes;
			primes.push_back(2);
			primes.push_back(3);
			primes.push_back(5);
			primes.push_back(7);
			primes.push_back(11);
			primes.push_back(13);
			
			YAML::Node node;
			node["primes"] = primes;
			YAML_ASSERT(node["primes"].as<std::vector<int> >() == primes);
			return true;
		}
beder's avatar
beder committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154

		TEST StdList()
		{
			std::list<int> primes;
			primes.push_back(2);
			primes.push_back(3);
			primes.push_back(5);
			primes.push_back(7);
			primes.push_back(11);
			primes.push_back(13);
			
			YAML::Node node;
			node["primes"] = primes;
			YAML_ASSERT(node["primes"].as<std::list<int> >() == primes);
			return true;
		}
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169

		TEST StdMap()
		{
			std::map<int, int> squares;
			squares[0] = 0;
			squares[1] = 1;
			squares[2] = 4;
			squares[3] = 9;
			squares[4] = 16;
			
			YAML::Node node;
			node["squares"] = squares;
			YAML_ASSERT((node["squares"].as<std::map<int, int> >() == squares));
			return true;
		}
beder's avatar
beder committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
		
		TEST SimpleAlias()
		{
			YAML::Node node;
			node["foo"] = "value";
			node["bar"] = node["foo"];
			YAML_ASSERT(node["foo"].as<std::string>() == "value");
			YAML_ASSERT(node["bar"].as<std::string>() == "value");
			YAML_ASSERT(node["foo"] == node["bar"]);
			YAML_ASSERT(node.size() == 2);
			return true;
		}

		TEST AliasAsKey()
		{
			YAML::Node node;
			node["foo"] = "value";
			YAML::Node value = node["foo"];
			node[value] = "foo";
			YAML_ASSERT(node["foo"].as<std::string>() == "value");
			YAML_ASSERT(node[value].as<std::string>() == "foo");
			YAML_ASSERT(node["value"].as<std::string>() == "foo");
			YAML_ASSERT(node.size() == 2);
			return true;
		}
beder's avatar
beder committed
195
196
197
198
199
		
		TEST SelfReferenceSequence()
		{
			YAML::Node node;
			node[0] = node;
200
			YAML_ASSERT(node.IsSequence());
beder's avatar
beder committed
201
202
203
204
205
206
207
208
209
210
211
			YAML_ASSERT(node.size() == 1);
			YAML_ASSERT(node[0] == node);
			YAML_ASSERT(node[0][0] == node);
			YAML_ASSERT(node[0][0] == node[0]);
			return true;
		}

		TEST ValueSelfReferenceMap()
		{
			YAML::Node node;
			node["key"] = node;
212
			YAML_ASSERT(node.IsMap());
beder's avatar
beder committed
213
214
215
216
217
218
219
220
221
222
223
			YAML_ASSERT(node.size() == 1);
			YAML_ASSERT(node["key"] == node);
			YAML_ASSERT(node["key"]["key"] == node);
			YAML_ASSERT(node["key"]["key"] == node["key"]);
			return true;
		}

		TEST KeySelfReferenceMap()
		{
			YAML::Node node;
			node[node] = "value";
224
			YAML_ASSERT(node.IsMap());
beder's avatar
beder committed
225
226
227
228
229
230
231
232
233
			YAML_ASSERT(node.size() == 1);
			YAML_ASSERT(node[node].as<std::string>() == "value");
			return true;
		}

		TEST SelfReferenceMap()
		{
			YAML::Node node;
			node[node] = node;
234
			YAML_ASSERT(node.IsMap());
beder's avatar
beder committed
235
236
237
238
239
240
			YAML_ASSERT(node.size() == 1);
			YAML_ASSERT(node[node] == node);
			YAML_ASSERT(node[node][node] == node);
			YAML_ASSERT(node[node][node] == node[node]);
			return true;
		}
beder's avatar
beder committed
241
242
243
244
245
246
		
		TEST TempMapVariable()
		{
			YAML::Node node;
			YAML::Node tmp = node["key"];
			tmp = "value";
247
			YAML_ASSERT(node.IsMap());
beder's avatar
beder committed
248
249
250
251
252
253
254
255
256
257
258
			YAML_ASSERT(node.size() == 1);
			YAML_ASSERT(node["key"].as<std::string>() == "value");
			return true;
		}

		TEST TempMapVariableAlias()
		{
			YAML::Node node;
			YAML::Node tmp = node["key"];
			tmp = node["other"];
			node["other"] = "value";
259
			YAML_ASSERT(node.IsMap());
beder's avatar
beder committed
260
261
262
263
264
265
			YAML_ASSERT(node.size() == 2);
			YAML_ASSERT(node["key"].as<std::string>() == "value");
			YAML_ASSERT(node["other"].as<std::string>() == "value");
			YAML_ASSERT(node["other"] == node["key"]);
			return true;
		}
beder's avatar
beder committed
266
267
268
269
270
271
272
273
274
		
		TEST Bool()
		{
			YAML::Node node;
			node[true] = false;
			YAML_ASSERT(node.IsMap());
			YAML_ASSERT(node[true].as<bool>() == false);
			return true;
		}
275
276
277
278
279
280
281
282
283
284
		
		TEST AutoBoolConversion()
		{
			YAML::Node node;
			node["foo"] = "bar";
			YAML_ASSERT(static_cast<bool>(node["foo"]));
			YAML_ASSERT(!node["monkey"]);
			YAML_ASSERT(!!node["foo"]);
			return true;
		}
285
286
287
288
289
290
291
        
        TEST Reassign()
        {
            YAML::Node node = YAML::Load("foo");
            node = YAML::Node();
            return true;
        }
292
293
294
295
296
297
298
299
300
301
302
303
        
        TEST FallbackValues()
        {
            YAML::Node node = YAML::Load("foo: bar\nx: 2");
            YAML_ASSERT(node["foo"].as<std::string>() == "bar");
            YAML_ASSERT(node["foo"].as<std::string>("hello") == "bar");
            YAML_ASSERT(node["baz"].as<std::string>("hello") == "hello");
            YAML_ASSERT(node["x"].as<int>() == 2);
            YAML_ASSERT(node["x"].as<int>(5) == 2);
            YAML_ASSERT(node["y"].as<int>(5) == 5);
            return true;
        }
304
305
306
        
        TEST NumericConversion()
        {
307
            YAML::Node node = YAML::Load("[1.5, 1, .nan, .inf, -.inf, 0x15, 015]");
308
309
310
311
312
313
314
315
            YAML_ASSERT(node[0].as<float>() == 1.5f);
            YAML_ASSERT(node[0].as<double>() == 1.5);
            YAML_ASSERT_THROWS(node[0].as<int>(), std::runtime_error);
            YAML_ASSERT(node[1].as<int>() == 1);
            YAML_ASSERT(node[1].as<float>() == 1.0f);
            YAML_ASSERT(node[2].as<float>() != node[2].as<float>());
            YAML_ASSERT(node[3].as<float>() == std::numeric_limits<float>::infinity());
            YAML_ASSERT(node[4].as<float>() == -std::numeric_limits<float>::infinity());
316
317
            YAML_ASSERT(node[5].as<int>() == 21);
            YAML_ASSERT(node[6].as<int>() == 13);
318
319
            return true;
        }
320
321
322
323
324
325
326
327
        
        TEST Binary()
        {
            YAML::Node node = YAML::Load("[!!binary \"SGVsbG8sIFdvcmxkIQ==\", !!binary \"TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4K\"]");
            YAML_ASSERT(node[0].as<YAML::Binary>() == YAML::Binary(reinterpret_cast<const unsigned char*>("Hello, World!"), 13));
            YAML_ASSERT(node[1].as<YAML::Binary>() == YAML::Binary(reinterpret_cast<const unsigned char*>("Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.\n"), 270));
            return true;
        }
328
        
beder's avatar
beder committed
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
        TEST IterateSequence()
        {
            YAML::Node node = YAML::Load("[1, 3, 5, 7]");
            int seq[] = {1, 3, 5, 7};
            int i=0;
            for(YAML::const_iterator it=node.begin();it!=node.end();++it) {
                int x = seq[i++];
                YAML_ASSERT(it->as<int>() == x);
            }
            return true;
        }
        
        TEST IterateMap()
        {
            YAML::Node node = YAML::Load("{a: A, b: B, c: C}");
            for(YAML::const_iterator it=node.begin();it!=node.end();++it) {
                YAML_ASSERT(it->first.as<char>() + 'A' - 'a' == it->second.as<char>());
            }
            return true;
        }
        
350
351
352
353
354
355
356
357
358
359
360
        TEST ForEach()
        {
            YAML::Node node = YAML::Load("[1, 3, 5, 7]");
            int seq[] = {1, 3, 5, 7};
            int i = 0;
            BOOST_FOREACH(const YAML::Node &item, node) {
                int x = seq[i++];
                YAML_ASSERT(item.as<int>() == x);
            }
            return true;
        }
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
	}
	
	void RunNodeTest(TEST (*test)(), const std::string& name, int& passed, int& total) {
		TEST ret;
		try {
			ret = test();
		} catch(...) {
			ret.ok = false;
		}
		if(ret.ok) {
			passed++;
		} else {
			std::cout << "Node test failed: " << name << "\n";
			if(ret.error != "")
				std::cout << ret.error << "\n";
		}
		total++;
	}
	
	bool RunNodeTests()
	{
		int passed = 0;
		int total = 0;

		RunNodeTest(&Node::SimpleScalar, "simple scalar", passed, total);
386
		RunNodeTest(&Node::IntScalar, "int scalar", passed, total);
387
388
		RunNodeTest(&Node::SimpleAppendSequence, "simple append sequence", passed, total);
		RunNodeTest(&Node::SimpleAssignSequence, "simple assign sequence", passed, total);
beder's avatar
beder committed
389
390
		RunNodeTest(&Node::SimpleMap, "simple map", passed, total);
		RunNodeTest(&Node::MapWithUndefinedValues, "map with undefined values", passed, total);
beder's avatar
beder committed
391
		RunNodeTest(&Node::MapIteratorWithUndefinedValues, "map iterator with undefined values", passed, total);
beder's avatar
beder committed
392
393
		RunNodeTest(&Node::SimpleSubkeys, "simple subkey", passed, total);
		RunNodeTest(&Node::StdVector, "std::vector", passed, total);
beder's avatar
beder committed
394
		RunNodeTest(&Node::StdList, "std::list", passed, total);
395
		RunNodeTest(&Node::StdMap, "std::map", passed, total);
beder's avatar
beder committed
396
397
		RunNodeTest(&Node::SimpleAlias, "simple alias", passed, total);
		RunNodeTest(&Node::AliasAsKey, "alias as key", passed, total);
beder's avatar
beder committed
398
399
400
401
		RunNodeTest(&Node::SelfReferenceSequence, "self reference sequence", passed, total);
		RunNodeTest(&Node::ValueSelfReferenceMap, "value self reference map", passed, total);
		RunNodeTest(&Node::KeySelfReferenceMap, "key self reference map", passed, total);
		RunNodeTest(&Node::SelfReferenceMap, "self reference map", passed, total);
beder's avatar
beder committed
402
403
		RunNodeTest(&Node::TempMapVariable, "temp map variable", passed, total);
		RunNodeTest(&Node::TempMapVariableAlias, "temp map variable alias", passed, total);
beder's avatar
beder committed
404
		RunNodeTest(&Node::Bool, "bool", passed, total);
405
		RunNodeTest(&Node::AutoBoolConversion, "auto bool conversion", passed, total);
406
		RunNodeTest(&Node::Reassign, "reassign", passed, total);
407
		RunNodeTest(&Node::FallbackValues, "fallback values", passed, total);
408
		RunNodeTest(&Node::NumericConversion, "numeric conversion", passed, total);
409
		RunNodeTest(&Node::Binary, "binary", passed, total);
beder's avatar
beder committed
410
411
		RunNodeTest(&Node::IterateSequence, "iterate sequence", passed, total);
		RunNodeTest(&Node::IterateMap, "iterate map", passed, total);
412
		RunNodeTest(&Node::ForEach, "for each", passed, total);
413
414
415
416
417

		std::cout << "Node tests: " << passed << "/" << total << " passed\n";
		return passed == total;
	}
}