blob: afa71cc25ea2b1b695805e1d7534c5c27ec6ab68 [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);
Nick Kledzik50c30422013-01-04 19:32:00 +0000603 Output yout(ostr);
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000604 yout << map;
Nick Kledzik50c30422013-01-04 19:32:00 +0000605
606 // Verify sequences were written in flow style
607 ostr.flush();
608 llvm::StringRef flowOut(intermediate);
609 EXPECT_NE(llvm::StringRef::npos, flowOut.find("one, two"));
610 EXPECT_NE(llvm::StringRef::npos, flowOut.find("10, -30, 1024"));
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000611 }
612
613 {
614 Input yin(intermediate);
615 NameAndNumbers map2;
616 yin >> map2;
617
618 EXPECT_FALSE(yin.error());
619 EXPECT_TRUE(map2.name.equals("hello"));
620 EXPECT_EQ(map2.strings.size(), 2UL);
621 EXPECT_TRUE(map2.strings[0].equals("one"));
622 EXPECT_TRUE(map2.strings[1].equals("two"));
623 EXPECT_EQ(map2.single.size(), 1UL);
624 EXPECT_EQ(1, map2.single[0]);
625 EXPECT_EQ(map2.numbers.size(), 3UL);
626 EXPECT_EQ(10, map2.numbers[0]);
627 EXPECT_EQ(-30, map2.numbers[1]);
628 EXPECT_EQ(1024, map2.numbers[2]);
629 }
630}
631
632
633//===----------------------------------------------------------------------===//
634// Test normalizing/denormalizing
635//===----------------------------------------------------------------------===//
636
637LLVM_YAML_STRONG_TYPEDEF(uint32_t, TotalSeconds)
638
639typedef std::vector<TotalSeconds> SecondsSequence;
640
Nick Kledzik50c30422013-01-04 19:32:00 +0000641LLVM_YAML_IS_SEQUENCE_VECTOR(TotalSeconds)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000642
643
644namespace llvm {
645namespace yaml {
646 template <>
647 struct MappingTraits<TotalSeconds> {
648
649 class NormalizedSeconds {
650 public:
651 NormalizedSeconds(IO &io)
652 : hours(0), minutes(0), seconds(0) {
653 }
654 NormalizedSeconds(IO &, TotalSeconds &secs)
655 : hours(secs/3600),
656 minutes((secs - (hours*3600))/60),
657 seconds(secs % 60) {
658 }
659 TotalSeconds denormalize(IO &) {
660 return TotalSeconds(hours*3600 + minutes*60 + seconds);
661 }
662
663 uint32_t hours;
664 uint8_t minutes;
665 uint8_t seconds;
666 };
667
668 static void mapping(IO &io, TotalSeconds &secs) {
669 MappingNormalization<NormalizedSeconds, TotalSeconds> keys(io, secs);
670
671 io.mapOptional("hours", keys->hours, (uint32_t)0);
672 io.mapOptional("minutes", keys->minutes, (uint8_t)0);
673 io.mapRequired("seconds", keys->seconds);
674 }
675 };
676}
677}
678
679
680//
681// Test the reading of a yaml sequence of mappings
682//
683TEST(YAMLIO, TestReadMySecondsSequence) {
684 SecondsSequence seq;
685 Input yin("---\n - hours: 1\n seconds: 5\n - seconds: 59\n...\n");
686 yin >> seq;
687
688 EXPECT_FALSE(yin.error());
689 EXPECT_EQ(seq.size(), 2UL);
690 EXPECT_EQ(seq[0], 3605U);
691 EXPECT_EQ(seq[1], 59U);
692}
693
694
695//
696// Test writing then reading back custom values
697//
698TEST(YAMLIO, TestReadWriteMySecondsSequence) {
699 std::string intermediate;
700 {
701 SecondsSequence seq;
702 seq.push_back(4000);
703 seq.push_back(500);
704 seq.push_back(59);
705
706 llvm::raw_string_ostream ostr(intermediate);
707 Output yout(ostr);
708 yout << seq;
709 }
710 {
711 Input yin(intermediate);
712 SecondsSequence seq2;
713 yin >> seq2;
714
715 EXPECT_FALSE(yin.error());
716 EXPECT_EQ(seq2.size(), 3UL);
717 EXPECT_EQ(seq2[0], 4000U);
718 EXPECT_EQ(seq2[1], 500U);
719 EXPECT_EQ(seq2[2], 59U);
720 }
721}
722
723
724//===----------------------------------------------------------------------===//
725// Test dynamic typing
726//===----------------------------------------------------------------------===//
727
728enum AFlags {
729 a1,
730 a2,
731 a3
732};
733
734enum BFlags {
735 b1,
736 b2,
737 b3
738};
739
740enum Kind {
741 kindA,
742 kindB
743};
744
745struct KindAndFlags {
746 KindAndFlags() : kind(kindA), flags(0) { }
747 KindAndFlags(Kind k, uint32_t f) : kind(k), flags(f) { }
748 Kind kind;
749 uint32_t flags;
750};
751
752typedef std::vector<KindAndFlags> KindAndFlagsSequence;
753
Nick Kledzik50c30422013-01-04 19:32:00 +0000754LLVM_YAML_IS_SEQUENCE_VECTOR(KindAndFlags)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000755
756namespace llvm {
757namespace yaml {
758 template <>
759 struct ScalarEnumerationTraits<AFlags> {
760 static void enumeration(IO &io, AFlags &value) {
761 io.enumCase(value, "a1", a1);
762 io.enumCase(value, "a2", a2);
763 io.enumCase(value, "a3", a3);
764 }
765 };
766 template <>
767 struct ScalarEnumerationTraits<BFlags> {
768 static void enumeration(IO &io, BFlags &value) {
769 io.enumCase(value, "b1", b1);
770 io.enumCase(value, "b2", b2);
771 io.enumCase(value, "b3", b3);
772 }
773 };
774 template <>
775 struct ScalarEnumerationTraits<Kind> {
776 static void enumeration(IO &io, Kind &value) {
777 io.enumCase(value, "A", kindA);
778 io.enumCase(value, "B", kindB);
779 }
780 };
781 template <>
782 struct MappingTraits<KindAndFlags> {
783 static void mapping(IO &io, KindAndFlags& kf) {
784 io.mapRequired("kind", kf.kind);
785 // type of flags field varies depending on kind field
786 if ( kf.kind == kindA )
787 io.mapRequired("flags", *((AFlags*)&kf.flags));
788 else
789 io.mapRequired("flags", *((BFlags*)&kf.flags));
790 }
791 };
792}
793}
794
795
796//
797// Test the reading of a yaml sequence dynamic types
798//
799TEST(YAMLIO, TestReadKindAndFlagsSequence) {
800 KindAndFlagsSequence seq;
801 Input yin("---\n - kind: A\n flags: a2\n - kind: B\n flags: b1\n...\n");
802 yin >> seq;
803
804 EXPECT_FALSE(yin.error());
805 EXPECT_EQ(seq.size(), 2UL);
806 EXPECT_EQ(seq[0].kind, kindA);
Nick Kledzikc83b8fe2012-12-17 20:43:53 +0000807 EXPECT_EQ(seq[0].flags, (uint32_t)a2);
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000808 EXPECT_EQ(seq[1].kind, kindB);
Nick Kledzikc83b8fe2012-12-17 20:43:53 +0000809 EXPECT_EQ(seq[1].flags, (uint32_t)b1);
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000810}
811
812//
813// Test writing then reading back dynamic types
814//
815TEST(YAMLIO, TestReadWriteKindAndFlagsSequence) {
816 std::string intermediate;
817 {
818 KindAndFlagsSequence seq;
819 seq.push_back(KindAndFlags(kindA,a1));
820 seq.push_back(KindAndFlags(kindB,b1));
821 seq.push_back(KindAndFlags(kindA,a2));
822 seq.push_back(KindAndFlags(kindB,b2));
823 seq.push_back(KindAndFlags(kindA,a3));
824
825 llvm::raw_string_ostream ostr(intermediate);
826 Output yout(ostr);
827 yout << seq;
828 }
829 {
830 Input yin(intermediate);
831 KindAndFlagsSequence seq2;
832 yin >> seq2;
833
834 EXPECT_FALSE(yin.error());
835 EXPECT_EQ(seq2.size(), 5UL);
836 EXPECT_EQ(seq2[0].kind, kindA);
Nick Kledzikc83b8fe2012-12-17 20:43:53 +0000837 EXPECT_EQ(seq2[0].flags, (uint32_t)a1);
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000838 EXPECT_EQ(seq2[1].kind, kindB);
Nick Kledzikc83b8fe2012-12-17 20:43:53 +0000839 EXPECT_EQ(seq2[1].flags, (uint32_t)b1);
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000840 EXPECT_EQ(seq2[2].kind, kindA);
Nick Kledzikc83b8fe2012-12-17 20:43:53 +0000841 EXPECT_EQ(seq2[2].flags, (uint32_t)a2);
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000842 EXPECT_EQ(seq2[3].kind, kindB);
Nick Kledzikc83b8fe2012-12-17 20:43:53 +0000843 EXPECT_EQ(seq2[3].flags, (uint32_t)b2);
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000844 EXPECT_EQ(seq2[4].kind, kindA);
Nick Kledzikc83b8fe2012-12-17 20:43:53 +0000845 EXPECT_EQ(seq2[4].flags, (uint32_t)a3);
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000846 }
847}
848
849
850//===----------------------------------------------------------------------===//
851// Test document list
852//===----------------------------------------------------------------------===//
853
854struct FooBarMap {
855 int foo;
856 int bar;
857};
858typedef std::vector<FooBarMap> FooBarMapDocumentList;
859
860LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(FooBarMap)
861
862
863namespace llvm {
864namespace yaml {
865 template <>
866 struct MappingTraits<FooBarMap> {
867 static void mapping(IO &io, FooBarMap& fb) {
868 io.mapRequired("foo", fb.foo);
869 io.mapRequired("bar", fb.bar);
870 }
871 };
872}
873}
874
875
876//
877// Test the reading of a yaml mapping
878//
879TEST(YAMLIO, TestDocRead) {
880 FooBarMap doc;
881 Input yin("---\nfoo: 3\nbar: 5\n...\n");
882 yin >> doc;
883
884 EXPECT_FALSE(yin.error());
885 EXPECT_EQ(doc.foo, 3);
886 EXPECT_EQ(doc.bar,5);
887}
888
889
890
891//
892// Test writing then reading back a sequence of mappings
893//
894TEST(YAMLIO, TestSequenceDocListWriteAndRead) {
895 std::string intermediate;
896 {
897 FooBarMap doc1;
898 doc1.foo = 10;
899 doc1.bar = -3;
900 FooBarMap doc2;
901 doc2.foo = 257;
902 doc2.bar = 0;
903 std::vector<FooBarMap> docList;
904 docList.push_back(doc1);
905 docList.push_back(doc2);
906
907 llvm::raw_string_ostream ostr(intermediate);
908 Output yout(ostr);
909 yout << docList;
910 }
911
912
913 {
914 Input yin(intermediate);
915 std::vector<FooBarMap> docList2;
916 yin >> docList2;
917
918 EXPECT_FALSE(yin.error());
919 EXPECT_EQ(docList2.size(), 2UL);
920 FooBarMap& map1 = docList2[0];
921 FooBarMap& map2 = docList2[1];
922 EXPECT_EQ(map1.foo, 10);
923 EXPECT_EQ(map1.bar, -3);
924 EXPECT_EQ(map2.foo, 257);
925 EXPECT_EQ(map2.bar, 0);
926 }
927}
928
929
930//===----------------------------------------------------------------------===//
931// Test error handling
932//===----------------------------------------------------------------------===//
933
934
935
936static void suppressErrorMessages(const llvm::SMDiagnostic &, void *) {
937}
938
939
940//
941// Test error handling of unknown enumerated scalar
942//
943TEST(YAMLIO, TestColorsReadError) {
944 ColorMap map;
945 Input yin("---\n"
946 "c1: blue\n"
947 "c2: purple\n"
948 "c3: green\n"
949 "...\n");
950 yin.setDiagHandler(suppressErrorMessages);
951 yin >> map;
952 EXPECT_TRUE(yin.error());
953}
954
955
956//
957// Test error handling of flow sequence with unknown value
958//
959TEST(YAMLIO, TestFlagsReadError) {
960 FlagsMap map;
961 Input yin("---\n"
962 "f1: [ big ]\n"
963 "f2: [ round, hollow ]\n"
964 "f3: []\n"
965 "...\n");
966 yin.setDiagHandler(suppressErrorMessages);
967 yin >> map;
968
969 EXPECT_TRUE(yin.error());
970}
971
972
973//
974// Test error handling reading built-in uint8_t type
975//
976LLVM_YAML_IS_SEQUENCE_VECTOR(uint8_t)
977TEST(YAMLIO, TestReadBuiltInTypesUint8Error) {
978 std::vector<uint8_t> seq;
979 Input yin("---\n"
980 "- 255\n"
981 "- 0\n"
982 "- 257\n"
983 "...\n");
984 yin.setDiagHandler(suppressErrorMessages);
985 yin >> seq;
986
987 EXPECT_TRUE(yin.error());
988}
989
990
991//
992// Test error handling reading built-in uint16_t type
993//
994LLVM_YAML_IS_SEQUENCE_VECTOR(uint16_t)
995TEST(YAMLIO, TestReadBuiltInTypesUint16Error) {
996 std::vector<uint16_t> seq;
997 Input yin("---\n"
998 "- 65535\n"
999 "- 0\n"
1000 "- 66000\n"
1001 "...\n");
1002 yin.setDiagHandler(suppressErrorMessages);
1003 yin >> seq;
1004
1005 EXPECT_TRUE(yin.error());
1006}
1007
1008
1009//
1010// Test error handling reading built-in uint32_t type
1011//
1012LLVM_YAML_IS_SEQUENCE_VECTOR(uint32_t)
1013TEST(YAMLIO, TestReadBuiltInTypesUint32Error) {
1014 std::vector<uint32_t> seq;
1015 Input yin("---\n"
1016 "- 4000000000\n"
1017 "- 0\n"
1018 "- 5000000000\n"
1019 "...\n");
1020 yin.setDiagHandler(suppressErrorMessages);
1021 yin >> seq;
1022
1023 EXPECT_TRUE(yin.error());
1024}
1025
1026
1027//
1028// Test error handling reading built-in uint64_t type
1029//
1030LLVM_YAML_IS_SEQUENCE_VECTOR(uint64_t)
1031TEST(YAMLIO, TestReadBuiltInTypesUint64Error) {
1032 std::vector<uint64_t> seq;
1033 Input yin("---\n"
1034 "- 18446744073709551615\n"
1035 "- 0\n"
1036 "- 19446744073709551615\n"
1037 "...\n");
1038 yin.setDiagHandler(suppressErrorMessages);
1039 yin >> seq;
1040
1041 EXPECT_TRUE(yin.error());
1042}
1043
1044
1045//
1046// Test error handling reading built-in int8_t type
1047//
1048LLVM_YAML_IS_SEQUENCE_VECTOR(int8_t)
1049TEST(YAMLIO, TestReadBuiltInTypesint8OverError) {
1050 std::vector<int8_t> seq;
1051 Input yin("---\n"
1052 "- -128\n"
1053 "- 0\n"
1054 "- 127\n"
1055 "- 128\n"
1056 "...\n");
1057 yin.setDiagHandler(suppressErrorMessages);
1058 yin >> seq;
1059
1060 EXPECT_TRUE(yin.error());
1061}
1062
1063//
1064// Test error handling reading built-in int8_t type
1065//
1066TEST(YAMLIO, TestReadBuiltInTypesint8UnderError) {
1067 std::vector<int8_t> seq;
1068 Input yin("---\n"
1069 "- -128\n"
1070 "- 0\n"
1071 "- 127\n"
1072 "- -129\n"
1073 "...\n");
1074 yin.setDiagHandler(suppressErrorMessages);
1075 yin >> seq;
1076
1077 EXPECT_TRUE(yin.error());
1078}
1079
1080
1081//
1082// Test error handling reading built-in int16_t type
1083//
1084LLVM_YAML_IS_SEQUENCE_VECTOR(int16_t)
1085TEST(YAMLIO, TestReadBuiltInTypesint16UnderError) {
1086 std::vector<int16_t> seq;
1087 Input yin("---\n"
1088 "- 32767\n"
1089 "- 0\n"
1090 "- -32768\n"
1091 "- -32769\n"
1092 "...\n");
1093 yin.setDiagHandler(suppressErrorMessages);
1094 yin >> seq;
1095
1096 EXPECT_TRUE(yin.error());
1097}
1098
1099
1100//
1101// Test error handling reading built-in int16_t type
1102//
1103TEST(YAMLIO, TestReadBuiltInTypesint16OverError) {
1104 std::vector<int16_t> seq;
1105 Input yin("---\n"
1106 "- 32767\n"
1107 "- 0\n"
1108 "- -32768\n"
1109 "- 32768\n"
1110 "...\n");
1111 yin.setDiagHandler(suppressErrorMessages);
1112 yin >> seq;
1113
1114 EXPECT_TRUE(yin.error());
1115}
1116
1117
1118//
1119// Test error handling reading built-in int32_t type
1120//
1121LLVM_YAML_IS_SEQUENCE_VECTOR(int32_t)
1122TEST(YAMLIO, TestReadBuiltInTypesint32UnderError) {
1123 std::vector<int32_t> seq;
1124 Input yin("---\n"
1125 "- 2147483647\n"
1126 "- 0\n"
1127 "- -2147483648\n"
1128 "- -2147483649\n"
1129 "...\n");
1130 yin.setDiagHandler(suppressErrorMessages);
1131 yin >> seq;
1132
1133 EXPECT_TRUE(yin.error());
1134}
1135
1136//
1137// Test error handling reading built-in int32_t type
1138//
1139TEST(YAMLIO, TestReadBuiltInTypesint32OverError) {
1140 std::vector<int32_t> seq;
1141 Input yin("---\n"
1142 "- 2147483647\n"
1143 "- 0\n"
1144 "- -2147483648\n"
1145 "- 2147483649\n"
1146 "...\n");
1147 yin.setDiagHandler(suppressErrorMessages);
1148 yin >> seq;
1149
1150 EXPECT_TRUE(yin.error());
1151}
1152
1153
1154//
1155// Test error handling reading built-in int64_t type
1156//
1157LLVM_YAML_IS_SEQUENCE_VECTOR(int64_t)
1158TEST(YAMLIO, TestReadBuiltInTypesint64UnderError) {
1159 std::vector<int64_t> seq;
1160 Input yin("---\n"
1161 "- -9223372036854775808\n"
1162 "- 0\n"
1163 "- 9223372036854775807\n"
1164 "- -9223372036854775809\n"
1165 "...\n");
1166 yin.setDiagHandler(suppressErrorMessages);
1167 yin >> seq;
1168
1169 EXPECT_TRUE(yin.error());
1170}
1171
1172//
1173// Test error handling reading built-in int64_t type
1174//
1175TEST(YAMLIO, TestReadBuiltInTypesint64OverError) {
1176 std::vector<int64_t> seq;
1177 Input yin("---\n"
1178 "- -9223372036854775808\n"
1179 "- 0\n"
1180 "- 9223372036854775807\n"
1181 "- 9223372036854775809\n"
1182 "...\n");
1183 yin.setDiagHandler(suppressErrorMessages);
1184 yin >> seq;
1185
1186 EXPECT_TRUE(yin.error());
1187}
1188
1189//
1190// Test error handling reading built-in float type
1191//
1192LLVM_YAML_IS_SEQUENCE_VECTOR(float)
1193TEST(YAMLIO, TestReadBuiltInTypesFloatError) {
1194 std::vector<float> seq;
1195 Input yin("---\n"
1196 "- 0.0\n"
1197 "- 1000.1\n"
1198 "- -123.456\n"
1199 "- 1.2.3\n"
1200 "...\n");
1201 yin.setDiagHandler(suppressErrorMessages);
1202 yin >> seq;
1203
1204 EXPECT_TRUE(yin.error());
1205}
1206
1207//
1208// Test error handling reading built-in float type
1209//
1210LLVM_YAML_IS_SEQUENCE_VECTOR(double)
1211TEST(YAMLIO, TestReadBuiltInTypesDoubleError) {
1212 std::vector<double> seq;
1213 Input yin("---\n"
1214 "- 0.0\n"
1215 "- 1000.1\n"
1216 "- -123.456\n"
1217 "- 1.2.3\n"
1218 "...\n");
1219 yin.setDiagHandler(suppressErrorMessages);
1220 yin >> seq;
1221
1222 EXPECT_TRUE(yin.error());
1223}
1224
1225//
1226// Test error handling reading built-in Hex8 type
1227//
1228LLVM_YAML_IS_SEQUENCE_VECTOR(Hex8)
1229TEST(YAMLIO, TestReadBuiltInTypesHex8Error) {
1230 std::vector<Hex8> seq;
1231 Input yin("---\n"
1232 "- 0x12\n"
1233 "- 0xFE\n"
1234 "- 0x123\n"
1235 "...\n");
1236 yin.setDiagHandler(suppressErrorMessages);
1237 yin >> seq;
1238
1239 EXPECT_TRUE(yin.error());
1240}
1241
1242
1243//
1244// Test error handling reading built-in Hex16 type
1245//
1246LLVM_YAML_IS_SEQUENCE_VECTOR(Hex16)
1247TEST(YAMLIO, TestReadBuiltInTypesHex16Error) {
1248 std::vector<Hex16> seq;
1249 Input yin("---\n"
1250 "- 0x0012\n"
1251 "- 0xFEFF\n"
1252 "- 0x12345\n"
1253 "...\n");
1254 yin.setDiagHandler(suppressErrorMessages);
1255 yin >> seq;
1256
1257 EXPECT_TRUE(yin.error());
1258}
1259
1260//
1261// Test error handling reading built-in Hex32 type
1262//
1263LLVM_YAML_IS_SEQUENCE_VECTOR(Hex32)
1264TEST(YAMLIO, TestReadBuiltInTypesHex32Error) {
1265 std::vector<Hex32> seq;
1266 Input yin("---\n"
1267 "- 0x0012\n"
1268 "- 0xFEFF0000\n"
1269 "- 0x1234556789\n"
1270 "...\n");
1271 yin.setDiagHandler(suppressErrorMessages);
1272 yin >> seq;
1273
1274 EXPECT_TRUE(yin.error());
1275}
1276
1277//
1278// Test error handling reading built-in Hex64 type
1279//
1280LLVM_YAML_IS_SEQUENCE_VECTOR(Hex64)
1281TEST(YAMLIO, TestReadBuiltInTypesHex64Error) {
1282 std::vector<Hex64> seq;
1283 Input yin("---\n"
1284 "- 0x0012\n"
1285 "- 0xFFEEDDCCBBAA9988\n"
1286 "- 0x12345567890ABCDEF0\n"
1287 "...\n");
1288 yin.setDiagHandler(suppressErrorMessages);
1289 yin >> seq;
1290
1291 EXPECT_TRUE(yin.error());
1292}
1293