nodetests.cpp 18.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#include "nodetests.h"
#include "yaml-cpp/yaml.h"
#include <boost/foreach.hpp>
#include <iostream>

namespace {
	struct TEST {
		TEST(): ok(false) {}
		TEST(bool ok_): ok(ok_) {}
		TEST(const char *error_): ok(false), error(error_) {}
11
        TEST(const std::string& error_): ok(false), error(error_) {}
12
13
14
15
16
17
		
		bool ok;
		std::string error;
	};
}

18
19
20
21
22
#define YAML_ASSERT(cond)\
    do {\
        if(!(cond))\
            return "  Assert failed: " #cond;\
    } while(false)
23

24
25
26
27
28
29
30
31
32
33
34
35
36
37
#define YAML_ASSERT_THROWS(cond, exc)\
    do {\
        try {\
            (cond);\
            return "  Expression did not throw: " #cond;\
        } catch(const exc&) {\
        } catch(const std::runtime_error& e) {\
            std::stringstream stream;\
            stream << "  Expression threw runtime error ther than " #exc ":\n    " #cond "\n    " << e.what();\
            return stream.str();\
        } catch(...) {\
            return "  Expression threw unknown exception, other than " #exc ":\n    " #cond;\
        }\
    } while(false)
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188

namespace Test
{
	namespace Node
	{
		TEST SimpleScalar()
		{
			YAML::Node node = YAML::Node("Hello, World!");
			YAML_ASSERT(node.IsScalar());
			YAML_ASSERT(node.as<std::string>() == "Hello, World!");
			return true;
		}
		
		TEST IntScalar()
		{
			YAML::Node node = YAML::Node(15);
			YAML_ASSERT(node.IsScalar());
			YAML_ASSERT(node.as<int>() == 15);
			return true;
		}

		TEST SimpleAppendSequence()
		{
			YAML::Node node;
			node.push_back(10);
			node.push_back("foo");
			node.push_back("monkey");
			YAML_ASSERT(node.IsSequence());
			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");
			YAML_ASSERT(node.IsSequence());
			return true;
		}

		TEST SimpleAssignSequence()
		{
			YAML::Node node;
			node[0] = 10;
			node[1] = "foo";
			node[2] = "monkey";
			YAML_ASSERT(node.IsSequence());
			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");
			YAML_ASSERT(node.IsSequence());
			return true;
		}

		TEST SimpleMap()
		{
			YAML::Node node;
			node["key"] = "value";
			YAML_ASSERT(node.IsMap());
			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"];
			YAML_ASSERT(node.IsMap());
			YAML_ASSERT(node["key"].as<std::string>() == "value");
			YAML_ASSERT(node.size() == 1);

			node["undefined"] = "monkey";
			YAML_ASSERT(node["undefined"].as<std::string>() == "monkey");
			YAML_ASSERT(node.size() == 2);
			
			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);
			return true;
		}
		
		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;
		}

		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;
		}

		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;
		}
		
189
190
191
192
193
194
195
196
197
198
199
200
201
		TEST StdPair()
		{
			std::pair<int, std::string> p;
            p.first = 5;
            p.second = "five";
			
			YAML::Node node;
			node["pair"] = p;
			YAML_ASSERT((node["pair"].as<std::pair<int, std::string> >() == p));
			return true;
		}

        TEST SimpleAlias()
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
		{
			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;
		}
		
		TEST SelfReferenceSequence()
		{
			YAML::Node node;
			node[0] = node;
			YAML_ASSERT(node.IsSequence());
			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;
			YAML_ASSERT(node.IsMap());
			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";
			YAML_ASSERT(node.IsMap());
			YAML_ASSERT(node.size() == 1);
			YAML_ASSERT(node[node].as<std::string>() == "value");
			return true;
		}

		TEST SelfReferenceMap()
		{
			YAML::Node node;
			node[node] = node;
			YAML_ASSERT(node.IsMap());
			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;
		}
		
		TEST TempMapVariable()
		{
			YAML::Node node;
			YAML::Node tmp = node["key"];
			tmp = "value";
			YAML_ASSERT(node.IsMap());
			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";
			YAML_ASSERT(node.IsMap());
			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;
		}
		
		TEST Bool()
		{
			YAML::Node node;
			node[true] = false;
			YAML_ASSERT(node.IsMap());
			YAML_ASSERT(node[true].as<bool>() == false);
			return true;
		}
		
		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;
		}
        
        TEST Reassign()
        {
            YAML::Node node = YAML::Load("foo");
            node = YAML::Node();
            return true;
        }
        
        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;
        }
        
        TEST NumericConversion()
        {
            YAML::Node node = YAML::Load("[1.5, 1, .nan, .inf, -.inf, 0x15, 015]");
            YAML_ASSERT(node[0].as<float>() == 1.5f);
            YAML_ASSERT(node[0].as<double>() == 1.5);
340
            YAML_ASSERT_THROWS(node[0].as<int>(), YAML::TypedBadConversion<int>);
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
            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());
            YAML_ASSERT(node[5].as<int>() == 21);
            YAML_ASSERT(node[6].as<int>() == 13);
            return true;
        }
        
        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;
        }
        
        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) {
                YAML_ASSERT(i < 4);
                int x = seq[i++];
                YAML_ASSERT(it->as<int>() == x);
            }
            YAML_ASSERT(i == 4);
            return true;
        }
        
        TEST IterateMap()
        {
            YAML::Node node = YAML::Load("{a: A, b: B, c: C}");
            int i=0;
            for(YAML::const_iterator it=node.begin();it!=node.end();++it) {
                YAML_ASSERT(i < 3);
                i++;
                YAML_ASSERT(it->first.as<char>() + 'A' - 'a' == it->second.as<char>());
            }
            YAML_ASSERT(i == 3);
            return true;
        }
        
        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;
        }

        TEST ForEachMap()
        {
            YAML::Node node = YAML::Load("{a: A, b: B, c: C}");
            BOOST_FOREACH(const YAML::const_iterator::value_type &p, node) {
                YAML_ASSERT(p.first.as<char>() + 'A' - 'a' == p.second.as<char>());
            }
            return true;
        }
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
        
        TEST CloneScalar()
        {
            YAML::Node node = YAML::Load("!foo monkey");
            YAML::Node clone = Clone(node);
            YAML_ASSERT(!(node == clone));
            YAML_ASSERT(node.as<std::string>() == clone.as<std::string>());
            YAML_ASSERT(node.Tag() == clone.Tag());
            return true;
        }

        TEST CloneSeq()
        {
            YAML::Node node = YAML::Load("[1, 3, 5, 7]");
            YAML::Node clone = Clone(node);
            YAML_ASSERT(!(node == clone));
            YAML_ASSERT(clone.Type() == YAML::NodeType::Sequence);
            YAML_ASSERT(node.size() == clone.size());
            for(std::size_t i=0;i<node.size();i++)
                YAML_ASSERT(node[i].as<int>() == clone[i].as<int>());
            return true;
        }

        TEST CloneMap()
        {
            YAML::Node node = YAML::Load("{foo: bar}");
            YAML::Node clone = Clone(node);
            YAML_ASSERT(!(node == clone));
            YAML_ASSERT(clone.Type() == YAML::NodeType::Map);
            YAML_ASSERT(node.size() == clone.size());
            YAML_ASSERT(node["foo"].as<std::string>() == clone["foo"].as<std::string>());
            return true;
        }

        TEST CloneAlias()
        {
            YAML::Node node = YAML::Load("&foo [*foo]");
            YAML::Node clone = Clone(node);
            YAML_ASSERT(!(node == clone));
            YAML_ASSERT(clone.Type() == YAML::NodeType::Sequence);
            YAML_ASSERT(node.size() == clone.size());
            YAML_ASSERT(clone == clone[0]);
            return true;
        }
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
        
        TEST ForceInsertIntoMap()
        {
            YAML::Node node;
            node["a"] = "b";
            node.force_insert("x", "y");
            node.force_insert("a", 5);
            YAML_ASSERT(node.size() == 3);
            YAML_ASSERT(node.Type() == YAML::NodeType::Map);
            bool ab = false;
            bool a5 = false;
            bool xy = false;
            for(YAML::const_iterator it=node.begin();it!=node.end();++it) {
                if(it->first.as<std::string>() == "a") {
                    if(it->second.as<std::string>() == "b")
                        ab = true;
                    else if(it->second.as<std::string>() == "5")
                        a5 = true;
                } else if(it->first.as<std::string>() == "x" && it->second.as<std::string>() == "y")
                    xy = true;
            }
            YAML_ASSERT(ab);
            YAML_ASSERT(a5);
            YAML_ASSERT(xy);
            return true;
        }
Jesse Beder's avatar
Jesse Beder committed
476
        
477
        TEST ResetNode()
Jesse Beder's avatar
Jesse Beder committed
478
479
480
        {
            YAML::Node node = YAML::Load("[1, 2, 3]");
            YAML_ASSERT(!node.IsNull());
481
482
            YAML::Node other = node;
            node.reset();
Jesse Beder's avatar
Jesse Beder committed
483
            YAML_ASSERT(node.IsNull());
484
485
486
487
            YAML_ASSERT(!other.IsNull());
            node.reset(other);
            YAML_ASSERT(!node.IsNull());
            YAML_ASSERT(other == node);
Jesse Beder's avatar
Jesse Beder committed
488
489
            return true;
        }
490
491
492
493
494
495
496
497
498
499
500

        TEST DereferenceIteratorError()
        {
            YAML::Node node = YAML::Load("[{a: b}, 1, 2]");
            YAML_ASSERT_THROWS(node.begin()->first.as<int>(), YAML::InvalidNode);
            YAML_ASSERT((*node.begin()).IsMap() == true);
            YAML_ASSERT(node.begin()->IsMap() == true);
            YAML_ASSERT_THROWS((*node.begin()->begin()).IsDefined(), YAML::InvalidNode);
            YAML_ASSERT_THROWS(node.begin()->begin()->IsDefined(), YAML::InvalidNode);
            return true;
        }
501
502
503
504
505
506
507
508

        TEST FloatingPrecision()
        {
            const double x = 0.123456789;
            YAML::Node node = YAML::Node(x);
            YAML_ASSERT(node.as<double>() == x);
            return true;
        }
509
    }
510
511
512
513
514
	
	void RunNodeTest(TEST (*test)(), const std::string& name, int& passed, int& total) {
		TEST ret;
		try {
			ret = test();
515
		} catch(const std::exception& e) {
516
			ret.ok = false;
517
            ret.error = e.what();
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
		}
		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);
		RunNodeTest(&Node::IntScalar, "int scalar", passed, total);
		RunNodeTest(&Node::SimpleAppendSequence, "simple append sequence", passed, total);
		RunNodeTest(&Node::SimpleAssignSequence, "simple assign sequence", passed, total);
		RunNodeTest(&Node::SimpleMap, "simple map", passed, total);
		RunNodeTest(&Node::MapWithUndefinedValues, "map with undefined values", passed, total);
		RunNodeTest(&Node::MapIteratorWithUndefinedValues, "map iterator with undefined values", passed, total);
		RunNodeTest(&Node::SimpleSubkeys, "simple subkey", passed, total);
		RunNodeTest(&Node::StdVector, "std::vector", passed, total);
		RunNodeTest(&Node::StdList, "std::list", passed, total);
		RunNodeTest(&Node::StdMap, "std::map", passed, total);
545
		RunNodeTest(&Node::StdPair, "std::pair", passed, total);
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
		RunNodeTest(&Node::SimpleAlias, "simple alias", passed, total);
		RunNodeTest(&Node::AliasAsKey, "alias as key", passed, total);
		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);
		RunNodeTest(&Node::TempMapVariable, "temp map variable", passed, total);
		RunNodeTest(&Node::TempMapVariableAlias, "temp map variable alias", passed, total);
		RunNodeTest(&Node::Bool, "bool", passed, total);
		RunNodeTest(&Node::AutoBoolConversion, "auto bool conversion", passed, total);
		RunNodeTest(&Node::Reassign, "reassign", passed, total);
		RunNodeTest(&Node::FallbackValues, "fallback values", passed, total);
		RunNodeTest(&Node::NumericConversion, "numeric conversion", passed, total);
		RunNodeTest(&Node::Binary, "binary", passed, total);
		RunNodeTest(&Node::IterateSequence, "iterate sequence", passed, total);
		RunNodeTest(&Node::IterateMap, "iterate map", passed, total);
		RunNodeTest(&Node::ForEach, "for each", passed, total);
		RunNodeTest(&Node::ForEachMap, "for each map", passed, total);
564
565
566
567
		RunNodeTest(&Node::CloneScalar, "clone scalar", passed, total);
		RunNodeTest(&Node::CloneSeq, "clone seq", passed, total);
		RunNodeTest(&Node::CloneMap, "clone map", passed, total);
		RunNodeTest(&Node::CloneAlias, "clone alias", passed, total);
568
        RunNodeTest(&Node::ForceInsertIntoMap, "force insert into map", passed, total);
569
        RunNodeTest(&Node::ResetNode, "reset node", passed, total);
570
        RunNodeTest(&Node::DereferenceIteratorError, "dereference iterator error", passed, total);
571
        RunNodeTest(&Node::FloatingPrecision, "floating precision", passed, total);
572
573
574
575
576

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