blob: 5c188e09360f41e78161958d0206e294bece829f [file] [log] [blame]
Nick Kledzik8ceb8b72012-12-12 20:46:15 +00001//===- unittest/Support/YAMLIOTest.cpp ------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/ADT/SmallString.h"
11#include "llvm/ADT/Twine.h"
12#include "llvm/Support/Casting.h"
13#include "llvm/Support/Format.h"
14#include "llvm/Support/YAMLTraits.h"
15#include "gtest/gtest.h"
16
Nick Kledzik3cf080d2012-12-12 22:39:45 +000017// To keep build bots going, disable tests until I figure out
18// why gcc complains there is no match for these traits.
19#if 0
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000020
21using llvm::yaml::Input;
22using llvm::yaml::Output;
23using llvm::yaml::IO;
24using llvm::yaml::MappingTraits;
25using llvm::yaml::MappingNormalization;
26using llvm::yaml::ScalarTraits;
27using llvm::yaml::Hex8;
28using llvm::yaml::Hex16;
29using llvm::yaml::Hex32;
30using llvm::yaml::Hex64;
31
32
33//===----------------------------------------------------------------------===//
34// Test MappingTraits
35//===----------------------------------------------------------------------===//
36
37struct FooBar {
38 int foo;
39 int bar;
40};
41typedef std::vector<FooBar> FooBarSequence;
42
43LLVM_YAML_IS_SEQUENCE_VECTOR(FooBar)
44
45
46namespace llvm {
47namespace yaml {
48 template <>
49 struct MappingTraits<FooBar> {
50 static void mapping(IO &io, FooBar& fb) {
51 io.mapRequired("foo", fb.foo);
52 io.mapRequired("bar", fb.bar);
53 }
54 };
55}
56}
57
58
59//
60// Test the reading of a yaml mapping
61//
62TEST(YAMLIO, TestMapRead) {
63 FooBar doc;
64 Input yin("---\nfoo: 3\nbar: 5\n...\n");
65 yin >> doc;
66
67 EXPECT_FALSE(yin.error());
68 EXPECT_EQ(doc.foo, 3);
69 EXPECT_EQ(doc.bar,5);
70}
71
72
73//
74// Test the reading of a yaml sequence of mappings
75//
76TEST(YAMLIO, TestSequenceMapRead) {
77 FooBarSequence seq;
78 Input yin("---\n - foo: 3\n bar: 5\n - foo: 7\n bar: 9\n...\n");
79 yin >> seq;
80
81 EXPECT_FALSE(yin.error());
82 EXPECT_EQ(seq.size(), 2UL);
83 FooBar& map1 = seq[0];
84 FooBar& map2 = seq[1];
85 EXPECT_EQ(map1.foo, 3);
86 EXPECT_EQ(map1.bar, 5);
87 EXPECT_EQ(map2.foo, 7);
88 EXPECT_EQ(map2.bar, 9);
89}
90
91
92//
93// Test writing then reading back a sequence of mappings
94//
95TEST(YAMLIO, TestSequenceMapWriteAndRead) {
96 std::string intermediate;
97 {
98 FooBar entry1;
99 entry1.foo = 10;
100 entry1.bar = -3;
101 FooBar entry2;
102 entry2.foo = 257;
103 entry2.bar = 0;
104 FooBarSequence seq;
105 seq.push_back(entry1);
106 seq.push_back(entry2);
107
108 llvm::raw_string_ostream ostr(intermediate);
109 Output yout(ostr);
110 yout << seq;
111 }
112
113 {
114 Input yin(intermediate);
115 FooBarSequence seq2;
116 yin >> seq2;
117
118 EXPECT_FALSE(yin.error());
119 EXPECT_EQ(seq2.size(), 2UL);
120 FooBar& map1 = seq2[0];
121 FooBar& map2 = seq2[1];
122 EXPECT_EQ(map1.foo, 10);
123 EXPECT_EQ(map1.bar, -3);
124 EXPECT_EQ(map2.foo, 257);
125 EXPECT_EQ(map2.bar, 0);
126 }
127}
128
129
130//===----------------------------------------------------------------------===//
131// Test built-in types
132//===----------------------------------------------------------------------===//
133
134struct BuiltInTypes {
135 llvm::StringRef str;
136 uint64_t u64;
137 uint32_t u32;
138 uint16_t u16;
139 uint8_t u8;
140 bool b;
141 int64_t s64;
142 int32_t s32;
143 int16_t s16;
144 int8_t s8;
145 float f;
146 double d;
147 Hex8 h8;
148 Hex16 h16;
149 Hex32 h32;
150 Hex64 h64;
151};
152
153namespace llvm {
154namespace yaml {
155 template <>
156 struct MappingTraits<BuiltInTypes> {
157 static void mapping(IO &io, BuiltInTypes& bt) {
158 io.mapRequired("str", bt.str);
159 io.mapRequired("u64", bt.u64);
160 io.mapRequired("u32", bt.u32);
161 io.mapRequired("u16", bt.u16);
162 io.mapRequired("u8", bt.u8);
163 io.mapRequired("b", bt.b);
164 io.mapRequired("s64", bt.s64);
165 io.mapRequired("s32", bt.s32);
166 io.mapRequired("s16", bt.s16);
167 io.mapRequired("s8", bt.s8);
168 io.mapRequired("f", bt.f);
169 io.mapRequired("d", bt.d);
170 io.mapRequired("h8", bt.h8);
171 io.mapRequired("h16", bt.h16);
172 io.mapRequired("h32", bt.h32);
173 io.mapRequired("h64", bt.h64);
174 }
175 };
176}
177}
178
179
180//
181// Test the reading of all built-in scalar conversions
182//
183TEST(YAMLIO, TestReadBuiltInTypes) {
184 BuiltInTypes map;
185 Input yin("---\n"
186 "str: hello there\n"
187 "u64: 5000000000\n"
188 "u32: 4000000000\n"
189 "u16: 65000\n"
190 "u8: 255\n"
191 "b: false\n"
192 "s64: -5000000000\n"
193 "s32: -2000000000\n"
194 "s16: -32000\n"
195 "s8: -127\n"
196 "f: 137.125\n"
197 "d: -2.8625\n"
198 "h8: 0xFF\n"
199 "h16: 0x8765\n"
200 "h32: 0xFEDCBA98\n"
201 "h64: 0xFEDCBA9876543210\n"
202 "...\n");
203 yin >> map;
204
205 EXPECT_FALSE(yin.error());
206 EXPECT_TRUE(map.str.equals("hello there"));
207 EXPECT_EQ(map.u64, 5000000000ULL);
208 EXPECT_EQ(map.u32, 4000000000);
209 EXPECT_EQ(map.u16, 65000);
210 EXPECT_EQ(map.u8, 255);
211 EXPECT_EQ(map.b, false);
212 EXPECT_EQ(map.s64, -5000000000LL);
213 EXPECT_EQ(map.s32, -2000000000L);
214 EXPECT_EQ(map.s16, -32000);
215 EXPECT_EQ(map.s8, -127);
216 EXPECT_EQ(map.f, 137.125);
217 EXPECT_EQ(map.d, -2.8625);
218 EXPECT_EQ(map.h8, Hex8(255));
219 EXPECT_EQ(map.h16, Hex16(0x8765));
220 EXPECT_EQ(map.h32, Hex32(0xFEDCBA98));
221 EXPECT_EQ(map.h64, Hex64(0xFEDCBA9876543210LL));
222}
223
224
225//
226// Test writing then reading back all built-in scalar types
227//
228TEST(YAMLIO, TestReadWriteBuiltInTypes) {
229 std::string intermediate;
230 {
231 BuiltInTypes map;
232 map.str = "one two";
233 map.u64 = 6000000000;
234 map.u32 = 3000000000;
235 map.u16 = 50000;
236 map.u8 = 254;
237 map.b = true;
238 map.s64 = -6000000000;
239 map.s32 = -2000000000;
240 map.s16 = -32000;
241 map.s8 = -128;
242 map.f = 3.25;
243 map.d = -2.8625;
244 map.h8 = 254;
245 map.h16 = 50000;
246 map.h32 = 3000000000;
247 map.h64 = 6000000000LL;
248
249 llvm::raw_string_ostream ostr(intermediate);
250 Output yout(ostr);
251 yout << map;
252 }
253
254 {
255 Input yin(intermediate);
256 BuiltInTypes map;
257 yin >> map;
258
259 EXPECT_FALSE(yin.error());
260 EXPECT_TRUE(map.str.equals("one two"));
261 EXPECT_EQ(map.u64, 6000000000ULL);
262 EXPECT_EQ(map.u32, 3000000000UL);
263 EXPECT_EQ(map.u16, 50000);
264 EXPECT_EQ(map.u8, 254);
265 EXPECT_EQ(map.b, true);
266 EXPECT_EQ(map.s64, -6000000000LL);
267 EXPECT_EQ(map.s32, -2000000000L);
268 EXPECT_EQ(map.s16, -32000);
269 EXPECT_EQ(map.s8, -128);
270 EXPECT_EQ(map.f, 3.25);
271 EXPECT_EQ(map.d, -2.8625);
272 EXPECT_EQ(map.h8, Hex8(254));
273 EXPECT_EQ(map.h16, Hex16(50000));
274 EXPECT_EQ(map.h32, Hex32(3000000000));
275 EXPECT_EQ(map.h64, Hex64(6000000000LL));
276 }
277}
278
279
280
281//===----------------------------------------------------------------------===//
282// Test ScalarEnumerationTraits
283//===----------------------------------------------------------------------===//
284
285enum Colors {
286 cRed,
287 cBlue,
288 cGreen,
289 cYellow
290};
291
292struct ColorMap {
293 Colors c1;
294 Colors c2;
295 Colors c3;
296 Colors c4;
297 Colors c5;
298 Colors c6;
299};
300
301namespace llvm {
302namespace yaml {
303 template <>
304 struct ScalarEnumerationTraits<Colors> {
305 static void enumeration(IO &io, Colors &value) {
306 io.enumCase(value, "red", cRed);
307 io.enumCase(value, "blue", cBlue);
308 io.enumCase(value, "green", cGreen);
309 io.enumCase(value, "yellow",cYellow);
310 }
311 };
312 template <>
313 struct MappingTraits<ColorMap> {
314 static void mapping(IO &io, ColorMap& c) {
315 io.mapRequired("c1", c.c1);
316 io.mapRequired("c2", c.c2);
317 io.mapRequired("c3", c.c3);
318 io.mapOptional("c4", c.c4, cBlue); // supplies default
319 io.mapOptional("c5", c.c5, cYellow); // supplies default
320 io.mapOptional("c6", c.c6, cRed); // supplies default
321 }
322 };
323}
324}
325
326
327//
328// Test reading enumerated scalars
329//
330TEST(YAMLIO, TestEnumRead) {
331 ColorMap map;
332 Input yin("---\n"
333 "c1: blue\n"
334 "c2: red\n"
335 "c3: green\n"
336 "c5: yellow\n"
337 "...\n");
338 yin >> map;
339
340 EXPECT_FALSE(yin.error());
341 EXPECT_EQ(cBlue, map.c1);
342 EXPECT_EQ(cRed, map.c2);
343 EXPECT_EQ(cGreen, map.c3);
344 EXPECT_EQ(cBlue, map.c4); // tests default
345 EXPECT_EQ(cYellow,map.c5); // tests overridden
346 EXPECT_EQ(cRed, map.c6); // tests default
347}
348
349
350
351//===----------------------------------------------------------------------===//
352// Test ScalarBitSetTraits
353//===----------------------------------------------------------------------===//
354
355enum MyFlags {
356 flagNone = 0,
357 flagBig = 1 << 0,
358 flagFlat = 1 << 1,
359 flagRound = 1 << 2,
360 flagPointy = 1 << 3
361};
362inline MyFlags operator|(MyFlags a, MyFlags b) {
363 return static_cast<MyFlags>(
364 static_cast<uint32_t>(a) | static_cast<uint32_t>(b));
365}
366
367struct FlagsMap {
368 MyFlags f1;
369 MyFlags f2;
370 MyFlags f3;
371 MyFlags f4;
372};
373
374
375namespace llvm {
376namespace yaml {
377 template <>
378 struct ScalarBitSetTraits<MyFlags> {
379 static void bitset(IO &io, MyFlags &value) {
380 io.bitSetCase(value, "big", flagBig);
381 io.bitSetCase(value, "flat", flagFlat);
382 io.bitSetCase(value, "round", flagRound);
383 io.bitSetCase(value, "pointy",flagPointy);
384 }
385 };
386 template <>
387 struct MappingTraits<FlagsMap> {
388 static void mapping(IO &io, FlagsMap& c) {
389 io.mapRequired("f1", c.f1);
390 io.mapRequired("f2", c.f2);
391 io.mapRequired("f3", c.f3);
392 io.mapOptional("f4", c.f4, MyFlags(flagRound));
393 }
394 };
395}
396}
397
398
399//
400// Test reading flow sequence representing bit-mask values
401//
402TEST(YAMLIO, TestFlagsRead) {
403 FlagsMap map;
404 Input yin("---\n"
405 "f1: [ big ]\n"
406 "f2: [ round, flat ]\n"
407 "f3: []\n"
408 "...\n");
409 yin >> map;
410
411 EXPECT_FALSE(yin.error());
412 EXPECT_EQ(flagBig, map.f1);
413 EXPECT_EQ(flagRound|flagFlat, map.f2);
414 EXPECT_EQ(flagNone, map.f3); // check empty set
415 EXPECT_EQ(flagRound, map.f4); // check optional key
416}
417
418
419//
420// Test writing then reading back bit-mask values
421//
422TEST(YAMLIO, TestReadWriteFlags) {
423 std::string intermediate;
424 {
425 FlagsMap map;
426 map.f1 = flagBig;
427 map.f2 = flagRound | flagFlat;
428 map.f3 = flagNone;
429 map.f4 = flagNone;
430
431 llvm::raw_string_ostream ostr(intermediate);
432 Output yout(ostr);
433 yout << map;
434 }
435
436 {
437 Input yin(intermediate);
438 FlagsMap map2;
439 yin >> map2;
440
441 EXPECT_FALSE(yin.error());
442 EXPECT_EQ(flagBig, map2.f1);
443 EXPECT_EQ(flagRound|flagFlat, map2.f2);
444 EXPECT_EQ(flagNone, map2.f3);
445 //EXPECT_EQ(flagRound, map2.f4); // check optional key
446 }
447}
448
449
450
451//===----------------------------------------------------------------------===//
452// Test ScalarTraits
453//===----------------------------------------------------------------------===//
454
455struct MyCustomType {
456 int length;
457 int width;
458};
459
460struct MyCustomTypeMap {
461 MyCustomType f1;
462 MyCustomType f2;
463 int f3;
464};
465
466
467namespace llvm {
468namespace yaml {
469 template <>
470 struct MappingTraits<MyCustomTypeMap> {
471 static void mapping(IO &io, MyCustomTypeMap& s) {
472 io.mapRequired("f1", s.f1);
473 io.mapRequired("f2", s.f2);
474 io.mapRequired("f3", s.f3);
475 }
476 };
477 // MyCustomType is formatted as a yaml scalar. A value of
478 // {length=3, width=4} would be represented in yaml as "3 by 4".
479 template<>
480 struct ScalarTraits<MyCustomType> {
481 static void output(const MyCustomType &value, void* ctxt, llvm::raw_ostream &out) {
482 out << llvm::format("%d by %d", value.length, value.width);
483 }
484 static StringRef input(StringRef scalar, void* ctxt, MyCustomType &value) {
485 size_t byStart = scalar.find("by");
486 if ( byStart != StringRef::npos ) {
487 StringRef lenStr = scalar.slice(0, byStart);
488 lenStr = lenStr.rtrim();
489 if ( lenStr.getAsInteger(0, value.length) ) {
490 return "malformed length";
491 }
492 StringRef widthStr = scalar.drop_front(byStart+2);
493 widthStr = widthStr.ltrim();
494 if ( widthStr.getAsInteger(0, value.width) ) {
495 return "malformed width";
496 }
497 return StringRef();
498 }
499 else {
500 return "malformed by";
501 }
502 }
503 };
504}
505}
506
507
508//
509// Test writing then reading back custom values
510//
511TEST(YAMLIO, TestReadWriteMyCustomType) {
512 std::string intermediate;
513 {
514 MyCustomTypeMap map;
515 map.f1.length = 1;
516 map.f1.width = 4;
517 map.f2.length = 100;
518 map.f2.width = 400;
519 map.f3 = 10;
520
521 llvm::raw_string_ostream ostr(intermediate);
522 Output yout(ostr);
523 yout << map;
524 }
525
526 {
527 Input yin(intermediate);
528 MyCustomTypeMap map2;
529 yin >> map2;
530
531 EXPECT_FALSE(yin.error());
532 EXPECT_EQ(1, map2.f1.length);
533 EXPECT_EQ(4, map2.f1.width);
534 EXPECT_EQ(100, map2.f2.length);
535 EXPECT_EQ(400, map2.f2.width);
536 EXPECT_EQ(10, map2.f3);
537 }
538}
539
540
541//===----------------------------------------------------------------------===//
542// Test flow sequences
543//===----------------------------------------------------------------------===//
544
545LLVM_YAML_STRONG_TYPEDEF(int, MyNumber)
546LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyNumber)
547LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::StringRef)
548
549namespace llvm {
550namespace yaml {
551 template<>
552 struct ScalarTraits<MyNumber> {
553 static void output(const MyNumber &value, void *, llvm::raw_ostream &out) {
554 out << value;
555 }
556
557 static StringRef input(StringRef scalar, void *, MyNumber &value) {
David Blaikief2150b62012-12-12 22:14:32 +0000558 long long n;
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000559 if ( getAsSignedInteger(scalar, 0, n) )
560 return "invalid number";
561 value = n;
562 return StringRef();
563 }
564 };
565}
566}
567
568struct NameAndNumbers {
569 llvm::StringRef name;
570 std::vector<llvm::StringRef> strings;
571 std::vector<MyNumber> single;
572 std::vector<MyNumber> numbers;
573};
574
575namespace llvm {
576namespace yaml {
577 template <>
578 struct MappingTraits<NameAndNumbers> {
579 static void mapping(IO &io, NameAndNumbers& nn) {
580 io.mapRequired("name", nn.name);
581 io.mapRequired("strings", nn.strings);
582 io.mapRequired("single", nn.single);
583 io.mapRequired("numbers", nn.numbers);
584 }
585 };
586}
587}
588
589
590//
591// Test writing then reading back custom values
592//
593TEST(YAMLIO, TestReadWriteMyFlowSequence) {
594 std::string intermediate;
595 {
596 NameAndNumbers map;
597 map.name = "hello";
598 map.strings.push_back(llvm::StringRef("one"));
599 map.strings.push_back(llvm::StringRef("two"));
600 map.single.push_back(1);
601 map.numbers.push_back(10);
602 map.numbers.push_back(-30);
603 map.numbers.push_back(1024);
604
605 llvm::raw_string_ostream ostr(intermediate);
606 Output yout(ostr);
607 yout << map;
608 }
609
610 {
611 Input yin(intermediate);
612 NameAndNumbers map2;
613 yin >> map2;
614
615 EXPECT_FALSE(yin.error());
616 EXPECT_TRUE(map2.name.equals("hello"));
617 EXPECT_EQ(map2.strings.size(), 2UL);
618 EXPECT_TRUE(map2.strings[0].equals("one"));
619 EXPECT_TRUE(map2.strings[1].equals("two"));
620 EXPECT_EQ(map2.single.size(), 1UL);
621 EXPECT_EQ(1, map2.single[0]);
622 EXPECT_EQ(map2.numbers.size(), 3UL);
623 EXPECT_EQ(10, map2.numbers[0]);
624 EXPECT_EQ(-30, map2.numbers[1]);
625 EXPECT_EQ(1024, map2.numbers[2]);
626 }
627}
628
629
630//===----------------------------------------------------------------------===//
631// Test normalizing/denormalizing
632//===----------------------------------------------------------------------===//
633
634LLVM_YAML_STRONG_TYPEDEF(uint32_t, TotalSeconds)
635
636typedef std::vector<TotalSeconds> SecondsSequence;
637
638LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(TotalSeconds)
639
640
641namespace llvm {
642namespace yaml {
643 template <>
644 struct MappingTraits<TotalSeconds> {
645
646 class NormalizedSeconds {
647 public:
648 NormalizedSeconds(IO &io)
649 : hours(0), minutes(0), seconds(0) {
650 }
651 NormalizedSeconds(IO &, TotalSeconds &secs)
652 : hours(secs/3600),
653 minutes((secs - (hours*3600))/60),
654 seconds(secs % 60) {
655 }
656 TotalSeconds denormalize(IO &) {
657 return TotalSeconds(hours*3600 + minutes*60 + seconds);
658 }
659
660 uint32_t hours;
661 uint8_t minutes;
662 uint8_t seconds;
663 };
664
665 static void mapping(IO &io, TotalSeconds &secs) {
666 MappingNormalization<NormalizedSeconds, TotalSeconds> keys(io, secs);
667
668 io.mapOptional("hours", keys->hours, (uint32_t)0);
669 io.mapOptional("minutes", keys->minutes, (uint8_t)0);
670 io.mapRequired("seconds", keys->seconds);
671 }
672 };
673}
674}
675
676
677//
678// Test the reading of a yaml sequence of mappings
679//
680TEST(YAMLIO, TestReadMySecondsSequence) {
681 SecondsSequence seq;
682 Input yin("---\n - hours: 1\n seconds: 5\n - seconds: 59\n...\n");
683 yin >> seq;
684
685 EXPECT_FALSE(yin.error());
686 EXPECT_EQ(seq.size(), 2UL);
687 EXPECT_EQ(seq[0], 3605U);
688 EXPECT_EQ(seq[1], 59U);
689}
690
691
692//
693// Test writing then reading back custom values
694//
695TEST(YAMLIO, TestReadWriteMySecondsSequence) {
696 std::string intermediate;
697 {
698 SecondsSequence seq;
699 seq.push_back(4000);
700 seq.push_back(500);
701 seq.push_back(59);
702
703 llvm::raw_string_ostream ostr(intermediate);
704 Output yout(ostr);
705 yout << seq;
706 }
707 {
708 Input yin(intermediate);
709 SecondsSequence seq2;
710 yin >> seq2;
711
712 EXPECT_FALSE(yin.error());
713 EXPECT_EQ(seq2.size(), 3UL);
714 EXPECT_EQ(seq2[0], 4000U);
715 EXPECT_EQ(seq2[1], 500U);
716 EXPECT_EQ(seq2[2], 59U);
717 }
718}
719
720
721//===----------------------------------------------------------------------===//
722// Test dynamic typing
723//===----------------------------------------------------------------------===//
724
725enum AFlags {
726 a1,
727 a2,
728 a3
729};
730
731enum BFlags {
732 b1,
733 b2,
734 b3
735};
736
737enum Kind {
738 kindA,
739 kindB
740};
741
742struct KindAndFlags {
743 KindAndFlags() : kind(kindA), flags(0) { }
744 KindAndFlags(Kind k, uint32_t f) : kind(k), flags(f) { }
745 Kind kind;
746 uint32_t flags;
747};
748
749typedef std::vector<KindAndFlags> KindAndFlagsSequence;
750
751LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(KindAndFlags)
752
753namespace llvm {
754namespace yaml {
755 template <>
756 struct ScalarEnumerationTraits<AFlags> {
757 static void enumeration(IO &io, AFlags &value) {
758 io.enumCase(value, "a1", a1);
759 io.enumCase(value, "a2", a2);
760 io.enumCase(value, "a3", a3);
761 }
762 };
763 template <>
764 struct ScalarEnumerationTraits<BFlags> {
765 static void enumeration(IO &io, BFlags &value) {
766 io.enumCase(value, "b1", b1);
767 io.enumCase(value, "b2", b2);
768 io.enumCase(value, "b3", b3);
769 }
770 };
771 template <>
772 struct ScalarEnumerationTraits<Kind> {
773 static void enumeration(IO &io, Kind &value) {
774 io.enumCase(value, "A", kindA);
775 io.enumCase(value, "B", kindB);
776 }
777 };
778 template <>
779 struct MappingTraits<KindAndFlags> {
780 static void mapping(IO &io, KindAndFlags& kf) {
781 io.mapRequired("kind", kf.kind);
782 // type of flags field varies depending on kind field
783 if ( kf.kind == kindA )
784 io.mapRequired("flags", *((AFlags*)&kf.flags));
785 else
786 io.mapRequired("flags", *((BFlags*)&kf.flags));
787 }
788 };
789}
790}
791
792
793//
794// Test the reading of a yaml sequence dynamic types
795//
796TEST(YAMLIO, TestReadKindAndFlagsSequence) {
797 KindAndFlagsSequence seq;
798 Input yin("---\n - kind: A\n flags: a2\n - kind: B\n flags: b1\n...\n");
799 yin >> seq;
800
801 EXPECT_FALSE(yin.error());
802 EXPECT_EQ(seq.size(), 2UL);
803 EXPECT_EQ(seq[0].kind, kindA);
804 EXPECT_EQ(seq[0].flags, a2);
805 EXPECT_EQ(seq[1].kind, kindB);
806 EXPECT_EQ(seq[1].flags, b1);
807}
808
809//
810// Test writing then reading back dynamic types
811//
812TEST(YAMLIO, TestReadWriteKindAndFlagsSequence) {
813 std::string intermediate;
814 {
815 KindAndFlagsSequence seq;
816 seq.push_back(KindAndFlags(kindA,a1));
817 seq.push_back(KindAndFlags(kindB,b1));
818 seq.push_back(KindAndFlags(kindA,a2));
819 seq.push_back(KindAndFlags(kindB,b2));
820 seq.push_back(KindAndFlags(kindA,a3));
821
822 llvm::raw_string_ostream ostr(intermediate);
823 Output yout(ostr);
824 yout << seq;
825 }
826 {
827 Input yin(intermediate);
828 KindAndFlagsSequence seq2;
829 yin >> seq2;
830
831 EXPECT_FALSE(yin.error());
832 EXPECT_EQ(seq2.size(), 5UL);
833 EXPECT_EQ(seq2[0].kind, kindA);
834 EXPECT_EQ(seq2[0].flags, a1);
835 EXPECT_EQ(seq2[1].kind, kindB);
836 EXPECT_EQ(seq2[1].flags, b1);
837 EXPECT_EQ(seq2[2].kind, kindA);
838 EXPECT_EQ(seq2[2].flags, a2);
839 EXPECT_EQ(seq2[3].kind, kindB);
840 EXPECT_EQ(seq2[3].flags, b2);
841 EXPECT_EQ(seq2[4].kind, kindA);
842 EXPECT_EQ(seq2[4].flags, a3);
843 }
844}
845
846
847//===----------------------------------------------------------------------===//
848// Test document list
849//===----------------------------------------------------------------------===//
850
851struct FooBarMap {
852 int foo;
853 int bar;
854};
855typedef std::vector<FooBarMap> FooBarMapDocumentList;
856
857LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(FooBarMap)
858
859
860namespace llvm {
861namespace yaml {
862 template <>
863 struct MappingTraits<FooBarMap> {
864 static void mapping(IO &io, FooBarMap& fb) {
865 io.mapRequired("foo", fb.foo);
866 io.mapRequired("bar", fb.bar);
867 }
868 };
869}
870}
871
872
873//
874// Test the reading of a yaml mapping
875//
876TEST(YAMLIO, TestDocRead) {
877 FooBarMap doc;
878 Input yin("---\nfoo: 3\nbar: 5\n...\n");
879 yin >> doc;
880
881 EXPECT_FALSE(yin.error());
882 EXPECT_EQ(doc.foo, 3);
883 EXPECT_EQ(doc.bar,5);
884}
885
886
887
888//
889// Test writing then reading back a sequence of mappings
890//
891TEST(YAMLIO, TestSequenceDocListWriteAndRead) {
892 std::string intermediate;
893 {
894 FooBarMap doc1;
895 doc1.foo = 10;
896 doc1.bar = -3;
897 FooBarMap doc2;
898 doc2.foo = 257;
899 doc2.bar = 0;
900 std::vector<FooBarMap> docList;
901 docList.push_back(doc1);
902 docList.push_back(doc2);
903
904 llvm::raw_string_ostream ostr(intermediate);
905 Output yout(ostr);
906 yout << docList;
907 }
908
909
910 {
911 Input yin(intermediate);
912 std::vector<FooBarMap> docList2;
913 yin >> docList2;
914
915 EXPECT_FALSE(yin.error());
916 EXPECT_EQ(docList2.size(), 2UL);
917 FooBarMap& map1 = docList2[0];
918 FooBarMap& map2 = docList2[1];
919 EXPECT_EQ(map1.foo, 10);
920 EXPECT_EQ(map1.bar, -3);
921 EXPECT_EQ(map2.foo, 257);
922 EXPECT_EQ(map2.bar, 0);
923 }
924}
925
926
927//===----------------------------------------------------------------------===//
928// Test error handling
929//===----------------------------------------------------------------------===//
930
931
932
933static void suppressErrorMessages(const llvm::SMDiagnostic &, void *) {
934}
935
936
937//
938// Test error handling of unknown enumerated scalar
939//
940TEST(YAMLIO, TestColorsReadError) {
941 ColorMap map;
942 Input yin("---\n"
943 "c1: blue\n"
944 "c2: purple\n"
945 "c3: green\n"
946 "...\n");
947 yin.setDiagHandler(suppressErrorMessages);
948 yin >> map;
949 EXPECT_TRUE(yin.error());
950}
951
952
953//
954// Test error handling of flow sequence with unknown value
955//
956TEST(YAMLIO, TestFlagsReadError) {
957 FlagsMap map;
958 Input yin("---\n"
959 "f1: [ big ]\n"
960 "f2: [ round, hollow ]\n"
961 "f3: []\n"
962 "...\n");
963 yin.setDiagHandler(suppressErrorMessages);
964 yin >> map;
965
966 EXPECT_TRUE(yin.error());
967}
968
969
970//
971// Test error handling reading built-in uint8_t type
972//
973LLVM_YAML_IS_SEQUENCE_VECTOR(uint8_t)
974TEST(YAMLIO, TestReadBuiltInTypesUint8Error) {
975 std::vector<uint8_t> seq;
976 Input yin("---\n"
977 "- 255\n"
978 "- 0\n"
979 "- 257\n"
980 "...\n");
981 yin.setDiagHandler(suppressErrorMessages);
982 yin >> seq;
983
984 EXPECT_TRUE(yin.error());
985}
986
987
988//
989// Test error handling reading built-in uint16_t type
990//
991LLVM_YAML_IS_SEQUENCE_VECTOR(uint16_t)
992TEST(YAMLIO, TestReadBuiltInTypesUint16Error) {
993 std::vector<uint16_t> seq;
994 Input yin("---\n"
995 "- 65535\n"
996 "- 0\n"
997 "- 66000\n"
998 "...\n");
999 yin.setDiagHandler(suppressErrorMessages);
1000 yin >> seq;
1001
1002 EXPECT_TRUE(yin.error());
1003}
1004
1005
1006//
1007// Test error handling reading built-in uint32_t type
1008//
1009LLVM_YAML_IS_SEQUENCE_VECTOR(uint32_t)
1010TEST(YAMLIO, TestReadBuiltInTypesUint32Error) {
1011 std::vector<uint32_t> seq;
1012 Input yin("---\n"
1013 "- 4000000000\n"
1014 "- 0\n"
1015 "- 5000000000\n"
1016 "...\n");
1017 yin.setDiagHandler(suppressErrorMessages);
1018 yin >> seq;
1019
1020 EXPECT_TRUE(yin.error());
1021}
1022
1023
1024//
1025// Test error handling reading built-in uint64_t type
1026//
1027LLVM_YAML_IS_SEQUENCE_VECTOR(uint64_t)
1028TEST(YAMLIO, TestReadBuiltInTypesUint64Error) {
1029 std::vector<uint64_t> seq;
1030 Input yin("---\n"
1031 "- 18446744073709551615\n"
1032 "- 0\n"
1033 "- 19446744073709551615\n"
1034 "...\n");
1035 yin.setDiagHandler(suppressErrorMessages);
1036 yin >> seq;
1037
1038 EXPECT_TRUE(yin.error());
1039}
1040
1041
1042//
1043// Test error handling reading built-in int8_t type
1044//
1045LLVM_YAML_IS_SEQUENCE_VECTOR(int8_t)
1046TEST(YAMLIO, TestReadBuiltInTypesint8OverError) {
1047 std::vector<int8_t> seq;
1048 Input yin("---\n"
1049 "- -128\n"
1050 "- 0\n"
1051 "- 127\n"
1052 "- 128\n"
1053 "...\n");
1054 yin.setDiagHandler(suppressErrorMessages);
1055 yin >> seq;
1056
1057 EXPECT_TRUE(yin.error());
1058}
1059
1060//
1061// Test error handling reading built-in int8_t type
1062//
1063TEST(YAMLIO, TestReadBuiltInTypesint8UnderError) {
1064 std::vector<int8_t> seq;
1065 Input yin("---\n"
1066 "- -128\n"
1067 "- 0\n"
1068 "- 127\n"
1069 "- -129\n"
1070 "...\n");
1071 yin.setDiagHandler(suppressErrorMessages);
1072 yin >> seq;
1073
1074 EXPECT_TRUE(yin.error());
1075}
1076
1077
1078//
1079// Test error handling reading built-in int16_t type
1080//
1081LLVM_YAML_IS_SEQUENCE_VECTOR(int16_t)
1082TEST(YAMLIO, TestReadBuiltInTypesint16UnderError) {
1083 std::vector<int16_t> seq;
1084 Input yin("---\n"
1085 "- 32767\n"
1086 "- 0\n"
1087 "- -32768\n"
1088 "- -32769\n"
1089 "...\n");
1090 yin.setDiagHandler(suppressErrorMessages);
1091 yin >> seq;
1092
1093 EXPECT_TRUE(yin.error());
1094}
1095
1096
1097//
1098// Test error handling reading built-in int16_t type
1099//
1100TEST(YAMLIO, TestReadBuiltInTypesint16OverError) {
1101 std::vector<int16_t> seq;
1102 Input yin("---\n"
1103 "- 32767\n"
1104 "- 0\n"
1105 "- -32768\n"
1106 "- 32768\n"
1107 "...\n");
1108 yin.setDiagHandler(suppressErrorMessages);
1109 yin >> seq;
1110
1111 EXPECT_TRUE(yin.error());
1112}
1113
1114
1115//
1116// Test error handling reading built-in int32_t type
1117//
1118LLVM_YAML_IS_SEQUENCE_VECTOR(int32_t)
1119TEST(YAMLIO, TestReadBuiltInTypesint32UnderError) {
1120 std::vector<int32_t> seq;
1121 Input yin("---\n"
1122 "- 2147483647\n"
1123 "- 0\n"
1124 "- -2147483648\n"
1125 "- -2147483649\n"
1126 "...\n");
1127 yin.setDiagHandler(suppressErrorMessages);
1128 yin >> seq;
1129
1130 EXPECT_TRUE(yin.error());
1131}
1132
1133//
1134// Test error handling reading built-in int32_t type
1135//
1136TEST(YAMLIO, TestReadBuiltInTypesint32OverError) {
1137 std::vector<int32_t> seq;
1138 Input yin("---\n"
1139 "- 2147483647\n"
1140 "- 0\n"
1141 "- -2147483648\n"
1142 "- 2147483649\n"
1143 "...\n");
1144 yin.setDiagHandler(suppressErrorMessages);
1145 yin >> seq;
1146
1147 EXPECT_TRUE(yin.error());
1148}
1149
1150
1151//
1152// Test error handling reading built-in int64_t type
1153//
1154LLVM_YAML_IS_SEQUENCE_VECTOR(int64_t)
1155TEST(YAMLIO, TestReadBuiltInTypesint64UnderError) {
1156 std::vector<int64_t> seq;
1157 Input yin("---\n"
1158 "- -9223372036854775808\n"
1159 "- 0\n"
1160 "- 9223372036854775807\n"
1161 "- -9223372036854775809\n"
1162 "...\n");
1163 yin.setDiagHandler(suppressErrorMessages);
1164 yin >> seq;
1165
1166 EXPECT_TRUE(yin.error());
1167}
1168
1169//
1170// Test error handling reading built-in int64_t type
1171//
1172TEST(YAMLIO, TestReadBuiltInTypesint64OverError) {
1173 std::vector<int64_t> seq;
1174 Input yin("---\n"
1175 "- -9223372036854775808\n"
1176 "- 0\n"
1177 "- 9223372036854775807\n"
1178 "- 9223372036854775809\n"
1179 "...\n");
1180 yin.setDiagHandler(suppressErrorMessages);
1181 yin >> seq;
1182
1183 EXPECT_TRUE(yin.error());
1184}
1185
1186//
1187// Test error handling reading built-in float type
1188//
1189LLVM_YAML_IS_SEQUENCE_VECTOR(float)
1190TEST(YAMLIO, TestReadBuiltInTypesFloatError) {
1191 std::vector<float> seq;
1192 Input yin("---\n"
1193 "- 0.0\n"
1194 "- 1000.1\n"
1195 "- -123.456\n"
1196 "- 1.2.3\n"
1197 "...\n");
1198 yin.setDiagHandler(suppressErrorMessages);
1199 yin >> seq;
1200
1201 EXPECT_TRUE(yin.error());
1202}
1203
1204//
1205// Test error handling reading built-in float type
1206//
1207LLVM_YAML_IS_SEQUENCE_VECTOR(double)
1208TEST(YAMLIO, TestReadBuiltInTypesDoubleError) {
1209 std::vector<double> seq;
1210 Input yin("---\n"
1211 "- 0.0\n"
1212 "- 1000.1\n"
1213 "- -123.456\n"
1214 "- 1.2.3\n"
1215 "...\n");
1216 yin.setDiagHandler(suppressErrorMessages);
1217 yin >> seq;
1218
1219 EXPECT_TRUE(yin.error());
1220}
1221
1222//
1223// Test error handling reading built-in Hex8 type
1224//
1225LLVM_YAML_IS_SEQUENCE_VECTOR(Hex8)
1226TEST(YAMLIO, TestReadBuiltInTypesHex8Error) {
1227 std::vector<Hex8> seq;
1228 Input yin("---\n"
1229 "- 0x12\n"
1230 "- 0xFE\n"
1231 "- 0x123\n"
1232 "...\n");
1233 yin.setDiagHandler(suppressErrorMessages);
1234 yin >> seq;
1235
1236 EXPECT_TRUE(yin.error());
1237}
1238
1239
1240//
1241// Test error handling reading built-in Hex16 type
1242//
1243LLVM_YAML_IS_SEQUENCE_VECTOR(Hex16)
1244TEST(YAMLIO, TestReadBuiltInTypesHex16Error) {
1245 std::vector<Hex16> seq;
1246 Input yin("---\n"
1247 "- 0x0012\n"
1248 "- 0xFEFF\n"
1249 "- 0x12345\n"
1250 "...\n");
1251 yin.setDiagHandler(suppressErrorMessages);
1252 yin >> seq;
1253
1254 EXPECT_TRUE(yin.error());
1255}
1256
1257//
1258// Test error handling reading built-in Hex32 type
1259//
1260LLVM_YAML_IS_SEQUENCE_VECTOR(Hex32)
1261TEST(YAMLIO, TestReadBuiltInTypesHex32Error) {
1262 std::vector<Hex32> seq;
1263 Input yin("---\n"
1264 "- 0x0012\n"
1265 "- 0xFEFF0000\n"
1266 "- 0x1234556789\n"
1267 "...\n");
1268 yin.setDiagHandler(suppressErrorMessages);
1269 yin >> seq;
1270
1271 EXPECT_TRUE(yin.error());
1272}
1273
1274//
1275// Test error handling reading built-in Hex64 type
1276//
1277LLVM_YAML_IS_SEQUENCE_VECTOR(Hex64)
1278TEST(YAMLIO, TestReadBuiltInTypesHex64Error) {
1279 std::vector<Hex64> seq;
1280 Input yin("---\n"
1281 "- 0x0012\n"
1282 "- 0xFFEEDDCCBBAA9988\n"
1283 "- 0x12345567890ABCDEF0\n"
1284 "...\n");
1285 yin.setDiagHandler(suppressErrorMessages);
1286 yin >> seq;
1287
1288 EXPECT_TRUE(yin.error());
1289}
1290
1291
Nick Kledzik3cf080d2012-12-12 22:39:45 +00001292#endif