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