Reverting my r193344 checkin due to build breakage.
[oota-llvm.git] / unittests / Support / YAMLIOTest.cpp
1 //===- 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
18 using llvm::yaml::Input;
19 using llvm::yaml::Output;
20 using llvm::yaml::IO;
21 using llvm::yaml::MappingTraits;
22 using llvm::yaml::MappingNormalization;
23 using llvm::yaml::ScalarTraits;
24 using llvm::yaml::Hex8;
25 using llvm::yaml::Hex16;
26 using llvm::yaml::Hex32;
27 using llvm::yaml::Hex64;
28
29
30 //===----------------------------------------------------------------------===//
31 //  Test MappingTraits
32 //===----------------------------------------------------------------------===//
33
34 struct FooBar {
35   int foo;
36   int bar;
37 };
38 typedef std::vector<FooBar> FooBarSequence;
39
40 LLVM_YAML_IS_SEQUENCE_VECTOR(FooBar)
41
42
43 namespace llvm {
44 namespace 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 //
59 TEST(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 //
73 TEST(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 //
92 TEST(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
131 struct 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
150 namespace llvm {
151 namespace 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 //
180 TEST(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);
205   EXPECT_EQ(map.u32, 4000000000U);
206   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 //
225 TEST(YAMLIO, TestReadWriteBuiltInTypes) {
226   std::string intermediate;
227   {
228     BuiltInTypes map;
229     map.str = "one two";
230     map.u64 = 6000000000ULL;
231     map.u32 = 3000000000U;
232     map.u16 = 50000;
233     map.u8  = 254;
234     map.b   = true;
235     map.s64 = -6000000000LL;
236     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;
243     map.h32 = 3000000000U;
244     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);
259     EXPECT_EQ(map.u32,      3000000000U);
260     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));
271     EXPECT_EQ(map.h32,      Hex32(3000000000U));
272     EXPECT_EQ(map.h64,      Hex64(6000000000LL));
273   }
274 }
275
276 struct StringTypes {
277   llvm::StringRef str1;
278   llvm::StringRef str2;
279   llvm::StringRef str3;
280   llvm::StringRef str4;
281   llvm::StringRef str5;
282 };
283
284 namespace llvm {
285 namespace yaml {
286   template <>
287   struct MappingTraits<StringTypes> {
288     static void mapping(IO &io, StringTypes& st) {
289       io.mapRequired("str1",      st.str1);
290       io.mapRequired("str2",      st.str2);
291       io.mapRequired("str3",      st.str3);
292       io.mapRequired("str4",      st.str4);
293       io.mapRequired("str5",      st.str5);
294     }
295   };
296 }
297 }
298
299 TEST(YAMLIO, TestReadWriteStringTypes) {
300   std::string intermediate;
301   {
302     StringTypes map;
303     map.str1 = "'aaa";
304     map.str2 = "\"bbb";
305     map.str3 = "`ccc";
306     map.str4 = "@ddd";
307     map.str5 = "";
308
309     llvm::raw_string_ostream ostr(intermediate);
310     Output yout(ostr);
311     yout << map;
312   }
313
314   llvm::StringRef flowOut(intermediate);
315   EXPECT_NE(llvm::StringRef::npos, flowOut.find("'''aaa"));
316   EXPECT_NE(llvm::StringRef::npos, flowOut.find("'\"bbb'"));
317   EXPECT_NE(llvm::StringRef::npos, flowOut.find("'`ccc'"));
318   EXPECT_NE(llvm::StringRef::npos, flowOut.find("'@ddd'"));
319   EXPECT_NE(llvm::StringRef::npos, flowOut.find("''\n"));
320
321   {
322     Input yin(intermediate);
323     StringTypes map;
324     yin >> map;
325
326     EXPECT_FALSE(yin.error());
327     EXPECT_TRUE(map.str1.equals("'aaa"));
328     EXPECT_TRUE(map.str2.equals("\"bbb"));
329     EXPECT_TRUE(map.str3.equals("`ccc"));
330     EXPECT_TRUE(map.str4.equals("@ddd"));
331     EXPECT_TRUE(map.str5.equals(""));
332   }
333 }
334
335 //===----------------------------------------------------------------------===//
336 //  Test ScalarEnumerationTraits
337 //===----------------------------------------------------------------------===//
338
339 enum Colors {
340     cRed,
341     cBlue,
342     cGreen,
343     cYellow
344 };
345
346 struct ColorMap {
347   Colors      c1;
348   Colors      c2;
349   Colors      c3;
350   Colors      c4;
351   Colors      c5;
352   Colors      c6;
353 };
354
355 namespace llvm {
356 namespace yaml {
357   template <>
358   struct ScalarEnumerationTraits<Colors> {
359     static void enumeration(IO &io, Colors &value) {
360       io.enumCase(value, "red",   cRed);
361       io.enumCase(value, "blue",  cBlue);
362       io.enumCase(value, "green", cGreen);
363       io.enumCase(value, "yellow",cYellow);
364     }
365   };
366   template <>
367   struct MappingTraits<ColorMap> {
368     static void mapping(IO &io, ColorMap& c) {
369       io.mapRequired("c1", c.c1);
370       io.mapRequired("c2", c.c2);
371       io.mapRequired("c3", c.c3);
372       io.mapOptional("c4", c.c4, cBlue);   // supplies default
373       io.mapOptional("c5", c.c5, cYellow); // supplies default
374       io.mapOptional("c6", c.c6, cRed);    // supplies default
375     }
376   };
377 }
378 }
379
380
381 //
382 // Test reading enumerated scalars
383 //
384 TEST(YAMLIO, TestEnumRead) {
385   ColorMap map;
386   Input yin("---\n"
387             "c1:  blue\n"
388             "c2:  red\n"
389             "c3:  green\n"
390             "c5:  yellow\n"
391             "...\n");
392   yin >> map;
393
394   EXPECT_FALSE(yin.error());
395   EXPECT_EQ(cBlue,  map.c1);
396   EXPECT_EQ(cRed,   map.c2);
397   EXPECT_EQ(cGreen, map.c3);
398   EXPECT_EQ(cBlue,  map.c4);  // tests default
399   EXPECT_EQ(cYellow,map.c5);  // tests overridden
400   EXPECT_EQ(cRed,   map.c6);  // tests default
401 }
402
403
404
405 //===----------------------------------------------------------------------===//
406 //  Test ScalarBitSetTraits
407 //===----------------------------------------------------------------------===//
408
409 enum MyFlags {
410   flagNone    = 0,
411   flagBig     = 1 << 0,
412   flagFlat    = 1 << 1,
413   flagRound   = 1 << 2,
414   flagPointy  = 1 << 3
415 };
416 inline MyFlags operator|(MyFlags a, MyFlags b) {
417   return static_cast<MyFlags>(
418                       static_cast<uint32_t>(a) | static_cast<uint32_t>(b));
419 }
420
421 struct FlagsMap {
422   MyFlags     f1;
423   MyFlags     f2;
424   MyFlags     f3;
425   MyFlags     f4;
426 };
427
428
429 namespace llvm {
430 namespace yaml {
431   template <>
432   struct ScalarBitSetTraits<MyFlags> {
433     static void bitset(IO &io, MyFlags &value) {
434       io.bitSetCase(value, "big",   flagBig);
435       io.bitSetCase(value, "flat",  flagFlat);
436       io.bitSetCase(value, "round", flagRound);
437       io.bitSetCase(value, "pointy",flagPointy);
438     }
439   };
440   template <>
441   struct MappingTraits<FlagsMap> {
442     static void mapping(IO &io, FlagsMap& c) {
443       io.mapRequired("f1", c.f1);
444       io.mapRequired("f2", c.f2);
445       io.mapRequired("f3", c.f3);
446       io.mapOptional("f4", c.f4, MyFlags(flagRound));
447      }
448   };
449 }
450 }
451
452
453 //
454 // Test reading flow sequence representing bit-mask values
455 //
456 TEST(YAMLIO, TestFlagsRead) {
457   FlagsMap map;
458   Input yin("---\n"
459             "f1:  [ big ]\n"
460             "f2:  [ round, flat ]\n"
461             "f3:  []\n"
462             "...\n");
463   yin >> map;
464
465   EXPECT_FALSE(yin.error());
466   EXPECT_EQ(flagBig,              map.f1);
467   EXPECT_EQ(flagRound|flagFlat,   map.f2);
468   EXPECT_EQ(flagNone,             map.f3);  // check empty set
469   EXPECT_EQ(flagRound,            map.f4);  // check optional key
470 }
471
472
473 //
474 // Test writing then reading back bit-mask values
475 //
476 TEST(YAMLIO, TestReadWriteFlags) {
477   std::string intermediate;
478   {
479     FlagsMap map;
480     map.f1 = flagBig;
481     map.f2 = flagRound | flagFlat;
482     map.f3 = flagNone;
483     map.f4 = flagNone;
484
485     llvm::raw_string_ostream ostr(intermediate);
486     Output yout(ostr);
487     yout << map;
488   }
489
490   {
491     Input yin(intermediate);
492     FlagsMap map2;
493     yin >> map2;
494
495     EXPECT_FALSE(yin.error());
496     EXPECT_EQ(flagBig,              map2.f1);
497     EXPECT_EQ(flagRound|flagFlat,   map2.f2);
498     EXPECT_EQ(flagNone,             map2.f3);
499     //EXPECT_EQ(flagRound,            map2.f4);  // check optional key
500   }
501 }
502
503
504
505 //===----------------------------------------------------------------------===//
506 //  Test ScalarTraits
507 //===----------------------------------------------------------------------===//
508
509 struct MyCustomType {
510   int length;
511   int width;
512 };
513
514 struct MyCustomTypeMap {
515   MyCustomType     f1;
516   MyCustomType     f2;
517   int              f3;
518 };
519
520
521 namespace llvm {
522 namespace yaml {
523   template <>
524   struct MappingTraits<MyCustomTypeMap> {
525     static void mapping(IO &io, MyCustomTypeMap& s) {
526       io.mapRequired("f1", s.f1);
527       io.mapRequired("f2", s.f2);
528       io.mapRequired("f3", s.f3);
529      }
530   };
531   // MyCustomType is formatted as a yaml scalar.  A value of
532   // {length=3, width=4} would be represented in yaml as "3 by 4".
533   template<>
534   struct ScalarTraits<MyCustomType> {
535     static void output(const MyCustomType &value, void* ctxt, llvm::raw_ostream &out) {
536       out << llvm::format("%d by %d", value.length, value.width);
537     }
538     static StringRef input(StringRef scalar, void* ctxt, MyCustomType &value) {
539       size_t byStart = scalar.find("by");
540       if ( byStart != StringRef::npos ) {
541         StringRef lenStr = scalar.slice(0, byStart);
542         lenStr = lenStr.rtrim();
543         if ( lenStr.getAsInteger(0, value.length) ) {
544           return "malformed length";
545         }
546         StringRef widthStr = scalar.drop_front(byStart+2);
547         widthStr = widthStr.ltrim();
548         if ( widthStr.getAsInteger(0, value.width) ) {
549           return "malformed width";
550         }
551         return StringRef();
552       }
553       else {
554           return "malformed by";
555       }
556     }
557   };
558 }
559 }
560
561
562 //
563 // Test writing then reading back custom values
564 //
565 TEST(YAMLIO, TestReadWriteMyCustomType) {
566   std::string intermediate;
567   {
568     MyCustomTypeMap map;
569     map.f1.length = 1;
570     map.f1.width  = 4;
571     map.f2.length = 100;
572     map.f2.width  = 400;
573     map.f3 = 10;
574
575     llvm::raw_string_ostream ostr(intermediate);
576     Output yout(ostr);
577     yout << map;
578   }
579
580   {
581     Input yin(intermediate);
582     MyCustomTypeMap map2;
583     yin >> map2;
584
585     EXPECT_FALSE(yin.error());
586     EXPECT_EQ(1,      map2.f1.length);
587     EXPECT_EQ(4,      map2.f1.width);
588     EXPECT_EQ(100,    map2.f2.length);
589     EXPECT_EQ(400,    map2.f2.width);
590     EXPECT_EQ(10,     map2.f3);
591   }
592 }
593
594
595 //===----------------------------------------------------------------------===//
596 //  Test flow sequences
597 //===----------------------------------------------------------------------===//
598
599 LLVM_YAML_STRONG_TYPEDEF(int, MyNumber)
600 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyNumber)
601 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::StringRef)
602
603 namespace llvm {
604 namespace yaml {
605   template<>
606   struct ScalarTraits<MyNumber> {
607     static void output(const MyNumber &value, void *, llvm::raw_ostream &out) {
608       out << value;
609     }
610
611     static StringRef input(StringRef scalar, void *, MyNumber &value) {
612       long long n;
613       if ( getAsSignedInteger(scalar, 0, n) )
614         return "invalid number";
615       value = n;
616       return StringRef();
617     }
618   };
619 }
620 }
621
622 struct NameAndNumbers {
623   llvm::StringRef               name;
624   std::vector<llvm::StringRef>  strings;
625   std::vector<MyNumber>         single;
626   std::vector<MyNumber>         numbers;
627 };
628
629 namespace llvm {
630 namespace yaml {
631   template <>
632   struct MappingTraits<NameAndNumbers> {
633     static void mapping(IO &io, NameAndNumbers& nn) {
634       io.mapRequired("name",     nn.name);
635       io.mapRequired("strings",  nn.strings);
636       io.mapRequired("single",   nn.single);
637       io.mapRequired("numbers",  nn.numbers);
638     }
639   };
640 }
641 }
642
643
644 //
645 // Test writing then reading back custom values
646 //
647 TEST(YAMLIO, TestReadWriteMyFlowSequence) {
648   std::string intermediate;
649   {
650     NameAndNumbers map;
651     map.name  = "hello";
652     map.strings.push_back(llvm::StringRef("one"));
653     map.strings.push_back(llvm::StringRef("two"));
654     map.single.push_back(1);
655     map.numbers.push_back(10);
656     map.numbers.push_back(-30);
657     map.numbers.push_back(1024);
658
659     llvm::raw_string_ostream ostr(intermediate);
660     Output yout(ostr);
661     yout << map;
662
663     // Verify sequences were written in flow style
664     ostr.flush();
665     llvm::StringRef flowOut(intermediate);
666     EXPECT_NE(llvm::StringRef::npos, flowOut.find("one, two"));
667     EXPECT_NE(llvm::StringRef::npos, flowOut.find("10, -30, 1024"));
668   }
669
670   {
671     Input yin(intermediate);
672     NameAndNumbers map2;
673     yin >> map2;
674
675     EXPECT_FALSE(yin.error());
676     EXPECT_TRUE(map2.name.equals("hello"));
677     EXPECT_EQ(map2.strings.size(), 2UL);
678     EXPECT_TRUE(map2.strings[0].equals("one"));
679     EXPECT_TRUE(map2.strings[1].equals("two"));
680     EXPECT_EQ(map2.single.size(), 1UL);
681     EXPECT_EQ(1,       map2.single[0]);
682     EXPECT_EQ(map2.numbers.size(), 3UL);
683     EXPECT_EQ(10,      map2.numbers[0]);
684     EXPECT_EQ(-30,     map2.numbers[1]);
685     EXPECT_EQ(1024,    map2.numbers[2]);
686   }
687 }
688
689
690 //===----------------------------------------------------------------------===//
691 //  Test normalizing/denormalizing
692 //===----------------------------------------------------------------------===//
693
694 LLVM_YAML_STRONG_TYPEDEF(uint32_t, TotalSeconds)
695
696 typedef std::vector<TotalSeconds> SecondsSequence;
697
698 LLVM_YAML_IS_SEQUENCE_VECTOR(TotalSeconds)
699
700
701 namespace llvm {
702 namespace yaml {
703   template <>
704   struct MappingTraits<TotalSeconds> {
705
706     class NormalizedSeconds {
707     public:
708       NormalizedSeconds(IO &io)
709         : hours(0), minutes(0), seconds(0) {
710       }
711       NormalizedSeconds(IO &, TotalSeconds &secs)
712         : hours(secs/3600),
713           minutes((secs - (hours*3600))/60),
714           seconds(secs % 60) {
715       }
716       TotalSeconds denormalize(IO &) {
717         return TotalSeconds(hours*3600 + minutes*60 + seconds);
718       }
719
720       uint32_t     hours;
721       uint8_t      minutes;
722       uint8_t      seconds;
723     };
724
725     static void mapping(IO &io, TotalSeconds &secs) {
726       MappingNormalization<NormalizedSeconds, TotalSeconds> keys(io, secs);
727
728       io.mapOptional("hours",    keys->hours,    (uint32_t)0);
729       io.mapOptional("minutes",  keys->minutes,  (uint8_t)0);
730       io.mapRequired("seconds",  keys->seconds);
731     }
732   };
733 }
734 }
735
736
737 //
738 // Test the reading of a yaml sequence of mappings
739 //
740 TEST(YAMLIO, TestReadMySecondsSequence) {
741   SecondsSequence seq;
742   Input yin("---\n - hours:  1\n   seconds:  5\n - seconds:  59\n...\n");
743   yin >> seq;
744
745   EXPECT_FALSE(yin.error());
746   EXPECT_EQ(seq.size(), 2UL);
747   EXPECT_EQ(seq[0], 3605U);
748   EXPECT_EQ(seq[1], 59U);
749 }
750
751
752 //
753 // Test writing then reading back custom values
754 //
755 TEST(YAMLIO, TestReadWriteMySecondsSequence) {
756   std::string intermediate;
757   {
758     SecondsSequence seq;
759     seq.push_back(4000);
760     seq.push_back(500);
761     seq.push_back(59);
762
763     llvm::raw_string_ostream ostr(intermediate);
764     Output yout(ostr);
765     yout << seq;
766   }
767   {
768     Input yin(intermediate);
769     SecondsSequence seq2;
770     yin >> seq2;
771
772     EXPECT_FALSE(yin.error());
773     EXPECT_EQ(seq2.size(), 3UL);
774     EXPECT_EQ(seq2[0], 4000U);
775     EXPECT_EQ(seq2[1], 500U);
776     EXPECT_EQ(seq2[2], 59U);
777   }
778 }
779
780
781 //===----------------------------------------------------------------------===//
782 //  Test dynamic typing
783 //===----------------------------------------------------------------------===//
784
785 enum AFlags {
786     a1,
787     a2,
788     a3
789 };
790
791 enum BFlags {
792     b1,
793     b2,
794     b3
795 };
796
797 enum Kind {
798     kindA,
799     kindB
800 };
801
802 struct KindAndFlags {
803   KindAndFlags() : kind(kindA), flags(0) { }
804   KindAndFlags(Kind k, uint32_t f) : kind(k), flags(f) { }
805   Kind        kind;
806   uint32_t    flags;
807 };
808
809 typedef std::vector<KindAndFlags> KindAndFlagsSequence;
810
811 LLVM_YAML_IS_SEQUENCE_VECTOR(KindAndFlags)
812
813 namespace llvm {
814 namespace yaml {
815   template <>
816   struct ScalarEnumerationTraits<AFlags> {
817     static void enumeration(IO &io, AFlags &value) {
818       io.enumCase(value, "a1",  a1);
819       io.enumCase(value, "a2",  a2);
820       io.enumCase(value, "a3",  a3);
821     }
822   };
823   template <>
824   struct ScalarEnumerationTraits<BFlags> {
825     static void enumeration(IO &io, BFlags &value) {
826       io.enumCase(value, "b1",  b1);
827       io.enumCase(value, "b2",  b2);
828       io.enumCase(value, "b3",  b3);
829     }
830   };
831   template <>
832   struct ScalarEnumerationTraits<Kind> {
833     static void enumeration(IO &io, Kind &value) {
834       io.enumCase(value, "A",  kindA);
835       io.enumCase(value, "B",  kindB);
836     }
837   };
838   template <>
839   struct MappingTraits<KindAndFlags> {
840     static void mapping(IO &io, KindAndFlags& kf) {
841       io.mapRequired("kind",  kf.kind);
842       // Type of "flags" field varies depending on "kind" field.
843       // Use memcpy here to avoid breaking strict aliasing rules.
844       if (kf.kind == kindA) {
845         AFlags aflags = static_cast<AFlags>(kf.flags);
846         io.mapRequired("flags", aflags);
847         kf.flags = aflags;
848       } else {
849         BFlags bflags = static_cast<BFlags>(kf.flags);
850         io.mapRequired("flags", bflags);
851         kf.flags = bflags;
852       }
853     }
854   };
855 }
856 }
857
858
859 //
860 // Test the reading of a yaml sequence dynamic types
861 //
862 TEST(YAMLIO, TestReadKindAndFlagsSequence) {
863   KindAndFlagsSequence seq;
864   Input yin("---\n - kind:  A\n   flags:  a2\n - kind:  B\n   flags:  b1\n...\n");
865   yin >> seq;
866
867   EXPECT_FALSE(yin.error());
868   EXPECT_EQ(seq.size(), 2UL);
869   EXPECT_EQ(seq[0].kind,  kindA);
870   EXPECT_EQ(seq[0].flags, (uint32_t)a2);
871   EXPECT_EQ(seq[1].kind,  kindB);
872   EXPECT_EQ(seq[1].flags, (uint32_t)b1);
873 }
874
875 //
876 // Test writing then reading back dynamic types
877 //
878 TEST(YAMLIO, TestReadWriteKindAndFlagsSequence) {
879   std::string intermediate;
880   {
881     KindAndFlagsSequence seq;
882     seq.push_back(KindAndFlags(kindA,a1));
883     seq.push_back(KindAndFlags(kindB,b1));
884     seq.push_back(KindAndFlags(kindA,a2));
885     seq.push_back(KindAndFlags(kindB,b2));
886     seq.push_back(KindAndFlags(kindA,a3));
887
888     llvm::raw_string_ostream ostr(intermediate);
889     Output yout(ostr);
890     yout << seq;
891   }
892   {
893     Input yin(intermediate);
894     KindAndFlagsSequence seq2;
895     yin >> seq2;
896
897     EXPECT_FALSE(yin.error());
898     EXPECT_EQ(seq2.size(), 5UL);
899     EXPECT_EQ(seq2[0].kind,  kindA);
900     EXPECT_EQ(seq2[0].flags, (uint32_t)a1);
901     EXPECT_EQ(seq2[1].kind,  kindB);
902     EXPECT_EQ(seq2[1].flags, (uint32_t)b1);
903     EXPECT_EQ(seq2[2].kind,  kindA);
904     EXPECT_EQ(seq2[2].flags, (uint32_t)a2);
905     EXPECT_EQ(seq2[3].kind,  kindB);
906     EXPECT_EQ(seq2[3].flags, (uint32_t)b2);
907     EXPECT_EQ(seq2[4].kind,  kindA);
908     EXPECT_EQ(seq2[4].flags, (uint32_t)a3);
909   }
910 }
911
912
913 //===----------------------------------------------------------------------===//
914 //  Test document list
915 //===----------------------------------------------------------------------===//
916
917 struct FooBarMap {
918   int foo;
919   int bar;
920 };
921 typedef std::vector<FooBarMap> FooBarMapDocumentList;
922
923 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(FooBarMap)
924
925
926 namespace llvm {
927 namespace yaml {
928   template <>
929   struct MappingTraits<FooBarMap> {
930     static void mapping(IO &io, FooBarMap& fb) {
931       io.mapRequired("foo",    fb.foo);
932       io.mapRequired("bar",    fb.bar);
933     }
934   };
935 }
936 }
937
938
939 //
940 // Test the reading of a yaml mapping
941 //
942 TEST(YAMLIO, TestDocRead) {
943   FooBarMap doc;
944   Input yin("---\nfoo:  3\nbar:  5\n...\n");
945   yin >> doc;
946
947   EXPECT_FALSE(yin.error());
948   EXPECT_EQ(doc.foo, 3);
949   EXPECT_EQ(doc.bar,5);
950 }
951
952
953
954 //
955 // Test writing then reading back a sequence of mappings
956 //
957 TEST(YAMLIO, TestSequenceDocListWriteAndRead) {
958   std::string intermediate;
959   {
960     FooBarMap doc1;
961     doc1.foo = 10;
962     doc1.bar = -3;
963     FooBarMap doc2;
964     doc2.foo = 257;
965     doc2.bar = 0;
966     std::vector<FooBarMap> docList;
967     docList.push_back(doc1);
968     docList.push_back(doc2);
969
970     llvm::raw_string_ostream ostr(intermediate);
971     Output yout(ostr);
972     yout << docList;
973   }
974
975
976   {
977     Input yin(intermediate);
978     std::vector<FooBarMap> docList2;
979     yin >> docList2;
980
981     EXPECT_FALSE(yin.error());
982     EXPECT_EQ(docList2.size(), 2UL);
983     FooBarMap& map1 = docList2[0];
984     FooBarMap& map2 = docList2[1];
985     EXPECT_EQ(map1.foo, 10);
986     EXPECT_EQ(map1.bar, -3);
987     EXPECT_EQ(map2.foo, 257);
988     EXPECT_EQ(map2.bar, 0);
989   }
990 }
991
992
993 //===----------------------------------------------------------------------===//
994 //  Test error handling
995 //===----------------------------------------------------------------------===//
996
997
998
999 static void suppressErrorMessages(const llvm::SMDiagnostic &, void *) {
1000 }
1001
1002
1003 //
1004 // Test error handling of unknown enumerated scalar
1005 //
1006 TEST(YAMLIO, TestColorsReadError) {
1007   ColorMap map;
1008   Input yin("---\n"
1009             "c1:  blue\n"
1010             "c2:  purple\n"
1011             "c3:  green\n"
1012             "...\n");
1013   yin.setDiagHandler(suppressErrorMessages);
1014   yin >> map;
1015   EXPECT_TRUE(yin.error());
1016 }
1017
1018
1019 //
1020 // Test error handling of flow sequence with unknown value
1021 //
1022 TEST(YAMLIO, TestFlagsReadError) {
1023   FlagsMap map;
1024   Input yin("---\n"
1025             "f1:  [ big ]\n"
1026             "f2:  [ round, hollow ]\n"
1027             "f3:  []\n"
1028             "...\n");
1029   yin.setDiagHandler(suppressErrorMessages);
1030   yin >> map;
1031
1032   EXPECT_TRUE(yin.error());
1033 }
1034
1035
1036 //
1037 // Test error handling reading built-in uint8_t type
1038 //
1039 LLVM_YAML_IS_SEQUENCE_VECTOR(uint8_t)
1040 TEST(YAMLIO, TestReadBuiltInTypesUint8Error) {
1041   std::vector<uint8_t> seq;
1042   Input yin("---\n"
1043             "- 255\n"
1044             "- 0\n"
1045             "- 257\n"
1046             "...\n");
1047   yin.setDiagHandler(suppressErrorMessages);
1048   yin >> seq;
1049
1050   EXPECT_TRUE(yin.error());
1051 }
1052
1053
1054 //
1055 // Test error handling reading built-in uint16_t type
1056 //
1057 LLVM_YAML_IS_SEQUENCE_VECTOR(uint16_t)
1058 TEST(YAMLIO, TestReadBuiltInTypesUint16Error) {
1059   std::vector<uint16_t> seq;
1060   Input yin("---\n"
1061             "- 65535\n"
1062             "- 0\n"
1063             "- 66000\n"
1064             "...\n");
1065   yin.setDiagHandler(suppressErrorMessages);
1066   yin >> seq;
1067
1068   EXPECT_TRUE(yin.error());
1069 }
1070
1071
1072 //
1073 // Test error handling reading built-in uint32_t type
1074 //
1075 LLVM_YAML_IS_SEQUENCE_VECTOR(uint32_t)
1076 TEST(YAMLIO, TestReadBuiltInTypesUint32Error) {
1077   std::vector<uint32_t> seq;
1078   Input yin("---\n"
1079             "- 4000000000\n"
1080             "- 0\n"
1081             "- 5000000000\n"
1082             "...\n");
1083   yin.setDiagHandler(suppressErrorMessages);
1084   yin >> seq;
1085
1086   EXPECT_TRUE(yin.error());
1087 }
1088
1089
1090 //
1091 // Test error handling reading built-in uint64_t type
1092 //
1093 LLVM_YAML_IS_SEQUENCE_VECTOR(uint64_t)
1094 TEST(YAMLIO, TestReadBuiltInTypesUint64Error) {
1095   std::vector<uint64_t> seq;
1096   Input yin("---\n"
1097             "- 18446744073709551615\n"
1098             "- 0\n"
1099             "- 19446744073709551615\n"
1100             "...\n");
1101   yin.setDiagHandler(suppressErrorMessages);
1102   yin >> seq;
1103
1104   EXPECT_TRUE(yin.error());
1105 }
1106
1107
1108 //
1109 // Test error handling reading built-in int8_t type
1110 //
1111 LLVM_YAML_IS_SEQUENCE_VECTOR(int8_t)
1112 TEST(YAMLIO, TestReadBuiltInTypesint8OverError) {
1113   std::vector<int8_t> seq;
1114   Input yin("---\n"
1115             "- -128\n"
1116             "- 0\n"
1117             "- 127\n"
1118             "- 128\n"
1119            "...\n");
1120   yin.setDiagHandler(suppressErrorMessages);
1121   yin >> seq;
1122
1123   EXPECT_TRUE(yin.error());
1124 }
1125
1126 //
1127 // Test error handling reading built-in int8_t type
1128 //
1129 TEST(YAMLIO, TestReadBuiltInTypesint8UnderError) {
1130   std::vector<int8_t> seq;
1131   Input yin("---\n"
1132             "- -128\n"
1133             "- 0\n"
1134             "- 127\n"
1135             "- -129\n"
1136             "...\n");
1137   yin.setDiagHandler(suppressErrorMessages);
1138   yin >> seq;
1139
1140   EXPECT_TRUE(yin.error());
1141 }
1142
1143
1144 //
1145 // Test error handling reading built-in int16_t type
1146 //
1147 LLVM_YAML_IS_SEQUENCE_VECTOR(int16_t)
1148 TEST(YAMLIO, TestReadBuiltInTypesint16UnderError) {
1149   std::vector<int16_t> seq;
1150   Input yin("---\n"
1151             "- 32767\n"
1152             "- 0\n"
1153             "- -32768\n"
1154             "- -32769\n"
1155             "...\n");
1156   yin.setDiagHandler(suppressErrorMessages);
1157   yin >> seq;
1158
1159   EXPECT_TRUE(yin.error());
1160 }
1161
1162
1163 //
1164 // Test error handling reading built-in int16_t type
1165 //
1166 TEST(YAMLIO, TestReadBuiltInTypesint16OverError) {
1167   std::vector<int16_t> seq;
1168   Input yin("---\n"
1169             "- 32767\n"
1170             "- 0\n"
1171             "- -32768\n"
1172             "- 32768\n"
1173             "...\n");
1174   yin.setDiagHandler(suppressErrorMessages);
1175   yin >> seq;
1176
1177   EXPECT_TRUE(yin.error());
1178 }
1179
1180
1181 //
1182 // Test error handling reading built-in int32_t type
1183 //
1184 LLVM_YAML_IS_SEQUENCE_VECTOR(int32_t)
1185 TEST(YAMLIO, TestReadBuiltInTypesint32UnderError) {
1186   std::vector<int32_t> seq;
1187   Input yin("---\n"
1188             "- 2147483647\n"
1189             "- 0\n"
1190             "- -2147483648\n"
1191             "- -2147483649\n"
1192             "...\n");
1193   yin.setDiagHandler(suppressErrorMessages);
1194   yin >> seq;
1195
1196   EXPECT_TRUE(yin.error());
1197 }
1198
1199 //
1200 // Test error handling reading built-in int32_t type
1201 //
1202 TEST(YAMLIO, TestReadBuiltInTypesint32OverError) {
1203   std::vector<int32_t> seq;
1204   Input yin("---\n"
1205             "- 2147483647\n"
1206             "- 0\n"
1207             "- -2147483648\n"
1208             "- 2147483649\n"
1209             "...\n");
1210   yin.setDiagHandler(suppressErrorMessages);
1211   yin >> seq;
1212
1213   EXPECT_TRUE(yin.error());
1214 }
1215
1216
1217 //
1218 // Test error handling reading built-in int64_t type
1219 //
1220 LLVM_YAML_IS_SEQUENCE_VECTOR(int64_t)
1221 TEST(YAMLIO, TestReadBuiltInTypesint64UnderError) {
1222   std::vector<int64_t> seq;
1223   Input yin("---\n"
1224             "- -9223372036854775808\n"
1225             "- 0\n"
1226             "- 9223372036854775807\n"
1227             "- -9223372036854775809\n"
1228             "...\n");
1229   yin.setDiagHandler(suppressErrorMessages);
1230   yin >> seq;
1231
1232   EXPECT_TRUE(yin.error());
1233 }
1234
1235 //
1236 // Test error handling reading built-in int64_t type
1237 //
1238 TEST(YAMLIO, TestReadBuiltInTypesint64OverError) {
1239   std::vector<int64_t> seq;
1240   Input yin("---\n"
1241             "- -9223372036854775808\n"
1242             "- 0\n"
1243             "- 9223372036854775807\n"
1244             "- 9223372036854775809\n"
1245             "...\n");
1246   yin.setDiagHandler(suppressErrorMessages);
1247   yin >> seq;
1248
1249   EXPECT_TRUE(yin.error());
1250 }
1251
1252 //
1253 // Test error handling reading built-in float type
1254 //
1255 LLVM_YAML_IS_SEQUENCE_VECTOR(float)
1256 TEST(YAMLIO, TestReadBuiltInTypesFloatError) {
1257   std::vector<float> seq;
1258   Input yin("---\n"
1259             "- 0.0\n"
1260             "- 1000.1\n"
1261             "- -123.456\n"
1262             "- 1.2.3\n"
1263             "...\n");
1264   yin.setDiagHandler(suppressErrorMessages);
1265   yin >> seq;
1266
1267   EXPECT_TRUE(yin.error());
1268 }
1269
1270 //
1271 // Test error handling reading built-in float type
1272 //
1273 LLVM_YAML_IS_SEQUENCE_VECTOR(double)
1274 TEST(YAMLIO, TestReadBuiltInTypesDoubleError) {
1275   std::vector<double> seq;
1276   Input yin("---\n"
1277             "- 0.0\n"
1278             "- 1000.1\n"
1279             "- -123.456\n"
1280             "- 1.2.3\n"
1281             "...\n");
1282   yin.setDiagHandler(suppressErrorMessages);
1283   yin >> seq;
1284
1285   EXPECT_TRUE(yin.error());
1286 }
1287
1288 //
1289 // Test error handling reading built-in Hex8 type
1290 //
1291 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex8)
1292 TEST(YAMLIO, TestReadBuiltInTypesHex8Error) {
1293   std::vector<Hex8> seq;
1294   Input yin("---\n"
1295             "- 0x12\n"
1296             "- 0xFE\n"
1297             "- 0x123\n"
1298             "...\n");
1299   yin.setDiagHandler(suppressErrorMessages);
1300   yin >> seq;
1301
1302   EXPECT_TRUE(yin.error());
1303 }
1304
1305
1306 //
1307 // Test error handling reading built-in Hex16 type
1308 //
1309 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex16)
1310 TEST(YAMLIO, TestReadBuiltInTypesHex16Error) {
1311   std::vector<Hex16> seq;
1312   Input yin("---\n"
1313             "- 0x0012\n"
1314             "- 0xFEFF\n"
1315             "- 0x12345\n"
1316             "...\n");
1317   yin.setDiagHandler(suppressErrorMessages);
1318   yin >> seq;
1319
1320   EXPECT_TRUE(yin.error());
1321 }
1322
1323 //
1324 // Test error handling reading built-in Hex32 type
1325 //
1326 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex32)
1327 TEST(YAMLIO, TestReadBuiltInTypesHex32Error) {
1328   std::vector<Hex32> seq;
1329   Input yin("---\n"
1330             "- 0x0012\n"
1331             "- 0xFEFF0000\n"
1332             "- 0x1234556789\n"
1333             "...\n");
1334   yin.setDiagHandler(suppressErrorMessages);
1335   yin >> seq;
1336
1337   EXPECT_TRUE(yin.error());
1338 }
1339
1340 //
1341 // Test error handling reading built-in Hex64 type
1342 //
1343 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex64)
1344 TEST(YAMLIO, TestReadBuiltInTypesHex64Error) {
1345   std::vector<Hex64> seq;
1346   Input yin("---\n"
1347             "- 0x0012\n"
1348             "- 0xFFEEDDCCBBAA9988\n"
1349             "- 0x12345567890ABCDEF0\n"
1350             "...\n");
1351   yin.setDiagHandler(suppressErrorMessages);
1352   yin >> seq;
1353
1354   EXPECT_TRUE(yin.error());
1355 }
1356
1357 struct OptionalTest {
1358   std::vector<int> Numbers;
1359 };
1360
1361 struct OptionalTestSeq {
1362   std::vector<OptionalTest> Tests;
1363 };
1364
1365 LLVM_YAML_IS_SEQUENCE_VECTOR(OptionalTest)
1366 namespace llvm {
1367 namespace yaml {
1368   template <>
1369   struct MappingTraits<OptionalTest> {
1370     static void mapping(IO& IO, OptionalTest &OT) {
1371       IO.mapOptional("Numbers", OT.Numbers);
1372     }
1373   };
1374
1375   template <>
1376   struct MappingTraits<OptionalTestSeq> {
1377     static void mapping(IO &IO, OptionalTestSeq &OTS) {
1378       IO.mapOptional("Tests", OTS.Tests);
1379     }
1380   };
1381 }
1382 }
1383
1384 TEST(YAMLIO, SequenceElideTest) {
1385   // Test that writing out a purely optional structure with its fields set to
1386   // default followed by other data is properly read back in.
1387   OptionalTestSeq Seq;
1388   OptionalTest One, Two, Three, Four;
1389   int N[] = {1, 2, 3};
1390   Three.Numbers.assign(N, N + 3);
1391   Seq.Tests.push_back(One);
1392   Seq.Tests.push_back(Two);
1393   Seq.Tests.push_back(Three);
1394   Seq.Tests.push_back(Four);
1395
1396   std::string intermediate;
1397   {
1398     llvm::raw_string_ostream ostr(intermediate);
1399     Output yout(ostr);
1400     yout << Seq;
1401   }
1402
1403   Input yin(intermediate);
1404   OptionalTestSeq Seq2;
1405   yin >> Seq2;
1406
1407   EXPECT_FALSE(yin.error());
1408
1409   EXPECT_EQ(4UL, Seq2.Tests.size());
1410
1411   EXPECT_TRUE(Seq2.Tests[0].Numbers.empty());
1412   EXPECT_TRUE(Seq2.Tests[1].Numbers.empty());
1413
1414   EXPECT_EQ(1, Seq2.Tests[2].Numbers[0]);
1415   EXPECT_EQ(2, Seq2.Tests[2].Numbers[1]);
1416   EXPECT_EQ(3, Seq2.Tests[2].Numbers[2]);
1417
1418   EXPECT_TRUE(Seq2.Tests[3].Numbers.empty());
1419 }