YAML: Assign a value returned by the default constructor to the value in an optional...
[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
32 static void suppressErrorMessages(const llvm::SMDiagnostic &, void *) {
33 }
34
35
36
37 //===----------------------------------------------------------------------===//
38 //  Test MappingTraits
39 //===----------------------------------------------------------------------===//
40
41 struct FooBar {
42   int foo;
43   int bar;
44 };
45 typedef std::vector<FooBar> FooBarSequence;
46
47 LLVM_YAML_IS_SEQUENCE_VECTOR(FooBar)
48
49 struct FooBarContainer {
50   FooBarSequence fbs;
51 };
52
53 namespace llvm {
54 namespace yaml {
55   template <>
56   struct MappingTraits<FooBar> {
57     static void mapping(IO &io, FooBar& fb) {
58       io.mapRequired("foo",    fb.foo);
59       io.mapRequired("bar",    fb.bar);
60     }
61   };
62
63   template <> struct MappingTraits<FooBarContainer> {
64     static void mapping(IO &io, FooBarContainer &fb) {
65       io.mapRequired("fbs", fb.fbs);
66     }
67   };
68 }
69 }
70
71 struct FooBarOptional {
72   int Foo;
73   int Bar;
74 };
75
76 namespace llvm {
77 namespace yaml {
78 template <> struct MappingTraits<FooBarOptional> {
79   static void mapping(IO &YamlIO, FooBarOptional &Obj) {
80     YamlIO.mapRequired("foo", Obj.Foo);
81     YamlIO.mapOptional("bar", Obj.Bar);
82   }
83 };
84 }
85 }
86
87 //
88 // Test the reading of a yaml mapping
89 //
90 TEST(YAMLIO, TestMapRead) {
91   FooBar doc;
92   {
93     Input yin("---\nfoo:  3\nbar:  5\n...\n");
94     yin >> doc;
95
96     EXPECT_FALSE(yin.error());
97     EXPECT_EQ(doc.foo, 3);
98     EXPECT_EQ(doc.bar, 5);
99   }
100
101   {
102     Input yin("{foo: 3, bar: 5}");
103     yin >> doc;
104
105     EXPECT_FALSE(yin.error());
106     EXPECT_EQ(doc.foo, 3);
107     EXPECT_EQ(doc.bar, 5);
108   }
109 }
110
111 TEST(YAMLIO, TestMapReadOptional) {
112   FooBarOptional Doc;
113   Doc.Bar = 42;
114   {
115     Input In("---\nfoo:  3\n...\n");
116     In >> Doc;
117
118     EXPECT_FALSE(In.error());
119     EXPECT_EQ(Doc.Foo, 3);
120     EXPECT_EQ(Doc.Bar, 0);
121   }
122 }
123
124 TEST(YAMLIO, TestMalformedMapRead) {
125   FooBar doc;
126   Input yin("{foo: 3; bar: 5}", nullptr, suppressErrorMessages);
127   yin >> doc;
128   EXPECT_TRUE(!!yin.error());
129 }
130
131 //
132 // Test the reading of a yaml sequence of mappings
133 //
134 TEST(YAMLIO, TestSequenceMapRead) {
135   FooBarSequence seq;
136   Input yin("---\n - foo:  3\n   bar:  5\n - foo:  7\n   bar:  9\n...\n");
137   yin >> seq;
138
139   EXPECT_FALSE(yin.error());
140   EXPECT_EQ(seq.size(), 2UL);
141   FooBar& map1 = seq[0];
142   FooBar& map2 = seq[1];
143   EXPECT_EQ(map1.foo, 3);
144   EXPECT_EQ(map1.bar, 5);
145   EXPECT_EQ(map2.foo, 7);
146   EXPECT_EQ(map2.bar, 9);
147 }
148
149 //
150 // Test the reading of a map containing a yaml sequence of mappings
151 //
152 TEST(YAMLIO, TestContainerSequenceMapRead) {
153   {
154     FooBarContainer cont;
155     Input yin2("---\nfbs:\n - foo: 3\n   bar: 5\n - foo: 7\n   bar: 9\n...\n");
156     yin2 >> cont;
157
158     EXPECT_FALSE(yin2.error());
159     EXPECT_EQ(cont.fbs.size(), 2UL);
160     EXPECT_EQ(cont.fbs[0].foo, 3);
161     EXPECT_EQ(cont.fbs[0].bar, 5);
162     EXPECT_EQ(cont.fbs[1].foo, 7);
163     EXPECT_EQ(cont.fbs[1].bar, 9);
164   }
165
166   {
167     FooBarContainer cont;
168     Input yin("---\nfbs:\n...\n");
169     yin >> cont;
170     // Okay: Empty node represents an empty array.
171     EXPECT_FALSE(yin.error());
172     EXPECT_EQ(cont.fbs.size(), 0UL);
173   }
174
175   {
176     FooBarContainer cont;
177     Input yin("---\nfbs: !!null null\n...\n");
178     yin >> cont;
179     // Okay: null represents an empty array.
180     EXPECT_FALSE(yin.error());
181     EXPECT_EQ(cont.fbs.size(), 0UL);
182   }
183
184   {
185     FooBarContainer cont;
186     Input yin("---\nfbs: ~\n...\n");
187     yin >> cont;
188     // Okay: null represents an empty array.
189     EXPECT_FALSE(yin.error());
190     EXPECT_EQ(cont.fbs.size(), 0UL);
191   }
192
193   {
194     FooBarContainer cont;
195     Input yin("---\nfbs: null\n...\n");
196     yin >> cont;
197     // Okay: null represents an empty array.
198     EXPECT_FALSE(yin.error());
199     EXPECT_EQ(cont.fbs.size(), 0UL);
200   }
201 }
202
203 //
204 // Test the reading of a map containing a malformed yaml sequence
205 //
206 TEST(YAMLIO, TestMalformedContainerSequenceMapRead) {
207   {
208     FooBarContainer cont;
209     Input yin("---\nfbs:\n   foo: 3\n   bar: 5\n...\n", nullptr,
210               suppressErrorMessages);
211     yin >> cont;
212     // Error: fbs is not a sequence.
213     EXPECT_TRUE(!!yin.error());
214     EXPECT_EQ(cont.fbs.size(), 0UL);
215   }
216
217   {
218     FooBarContainer cont;
219     Input yin("---\nfbs: 'scalar'\n...\n", nullptr, suppressErrorMessages);
220     yin >> cont;
221     // This should be an error.
222     EXPECT_TRUE(!!yin.error());
223     EXPECT_EQ(cont.fbs.size(), 0UL);
224   }
225 }
226
227 //
228 // Test writing then reading back a sequence of mappings
229 //
230 TEST(YAMLIO, TestSequenceMapWriteAndRead) {
231   std::string intermediate;
232   {
233     FooBar entry1;
234     entry1.foo = 10;
235     entry1.bar = -3;
236     FooBar entry2;
237     entry2.foo = 257;
238     entry2.bar = 0;
239     FooBarSequence seq;
240     seq.push_back(entry1);
241     seq.push_back(entry2);
242
243     llvm::raw_string_ostream ostr(intermediate);
244     Output yout(ostr);
245     yout << seq;
246   }
247
248   {
249     Input yin(intermediate);
250     FooBarSequence seq2;
251     yin >> seq2;
252
253     EXPECT_FALSE(yin.error());
254     EXPECT_EQ(seq2.size(), 2UL);
255     FooBar& map1 = seq2[0];
256     FooBar& map2 = seq2[1];
257     EXPECT_EQ(map1.foo, 10);
258     EXPECT_EQ(map1.bar, -3);
259     EXPECT_EQ(map2.foo, 257);
260     EXPECT_EQ(map2.bar, 0);
261   }
262 }
263
264
265 //===----------------------------------------------------------------------===//
266 //  Test built-in types
267 //===----------------------------------------------------------------------===//
268
269 struct BuiltInTypes {
270   llvm::StringRef str;
271   std::string stdstr;
272   uint64_t        u64;
273   uint32_t        u32;
274   uint16_t        u16;
275   uint8_t         u8;
276   bool            b;
277   int64_t         s64;
278   int32_t         s32;
279   int16_t         s16;
280   int8_t          s8;
281   float           f;
282   double          d;
283   Hex8            h8;
284   Hex16           h16;
285   Hex32           h32;
286   Hex64           h64;
287 };
288
289 namespace llvm {
290 namespace yaml {
291   template <>
292   struct MappingTraits<BuiltInTypes> {
293     static void mapping(IO &io, BuiltInTypes& bt) {
294       io.mapRequired("str",      bt.str);
295       io.mapRequired("stdstr",   bt.stdstr);
296       io.mapRequired("u64",      bt.u64);
297       io.mapRequired("u32",      bt.u32);
298       io.mapRequired("u16",      bt.u16);
299       io.mapRequired("u8",       bt.u8);
300       io.mapRequired("b",        bt.b);
301       io.mapRequired("s64",      bt.s64);
302       io.mapRequired("s32",      bt.s32);
303       io.mapRequired("s16",      bt.s16);
304       io.mapRequired("s8",       bt.s8);
305       io.mapRequired("f",        bt.f);
306       io.mapRequired("d",        bt.d);
307       io.mapRequired("h8",       bt.h8);
308       io.mapRequired("h16",      bt.h16);
309       io.mapRequired("h32",      bt.h32);
310       io.mapRequired("h64",      bt.h64);
311     }
312   };
313 }
314 }
315
316
317 //
318 // Test the reading of all built-in scalar conversions
319 //
320 TEST(YAMLIO, TestReadBuiltInTypes) {
321   BuiltInTypes map;
322   Input yin("---\n"
323             "str:      hello there\n"
324             "stdstr:   hello where?\n"
325             "u64:      5000000000\n"
326             "u32:      4000000000\n"
327             "u16:      65000\n"
328             "u8:       255\n"
329             "b:        false\n"
330             "s64:      -5000000000\n"
331             "s32:      -2000000000\n"
332             "s16:      -32000\n"
333             "s8:       -127\n"
334             "f:        137.125\n"
335             "d:        -2.8625\n"
336             "h8:       0xFF\n"
337             "h16:      0x8765\n"
338             "h32:      0xFEDCBA98\n"
339             "h64:      0xFEDCBA9876543210\n"
340            "...\n");
341   yin >> map;
342
343   EXPECT_FALSE(yin.error());
344   EXPECT_TRUE(map.str.equals("hello there"));
345   EXPECT_TRUE(map.stdstr == "hello where?");
346   EXPECT_EQ(map.u64, 5000000000ULL);
347   EXPECT_EQ(map.u32, 4000000000U);
348   EXPECT_EQ(map.u16, 65000);
349   EXPECT_EQ(map.u8,  255);
350   EXPECT_EQ(map.b,   false);
351   EXPECT_EQ(map.s64, -5000000000LL);
352   EXPECT_EQ(map.s32, -2000000000L);
353   EXPECT_EQ(map.s16, -32000);
354   EXPECT_EQ(map.s8,  -127);
355   EXPECT_EQ(map.f,   137.125);
356   EXPECT_EQ(map.d,   -2.8625);
357   EXPECT_EQ(map.h8,  Hex8(255));
358   EXPECT_EQ(map.h16, Hex16(0x8765));
359   EXPECT_EQ(map.h32, Hex32(0xFEDCBA98));
360   EXPECT_EQ(map.h64, Hex64(0xFEDCBA9876543210LL));
361 }
362
363
364 //
365 // Test writing then reading back all built-in scalar types
366 //
367 TEST(YAMLIO, TestReadWriteBuiltInTypes) {
368   std::string intermediate;
369   {
370     BuiltInTypes map;
371     map.str = "one two";
372     map.stdstr = "three four";
373     map.u64 = 6000000000ULL;
374     map.u32 = 3000000000U;
375     map.u16 = 50000;
376     map.u8  = 254;
377     map.b   = true;
378     map.s64 = -6000000000LL;
379     map.s32 = -2000000000;
380     map.s16 = -32000;
381     map.s8  = -128;
382     map.f   = 3.25;
383     map.d   = -2.8625;
384     map.h8  = 254;
385     map.h16 = 50000;
386     map.h32 = 3000000000U;
387     map.h64 = 6000000000LL;
388
389     llvm::raw_string_ostream ostr(intermediate);
390     Output yout(ostr);
391     yout << map;
392   }
393
394   {
395     Input yin(intermediate);
396     BuiltInTypes map;
397     yin >> map;
398
399     EXPECT_FALSE(yin.error());
400     EXPECT_TRUE(map.str.equals("one two"));
401     EXPECT_TRUE(map.stdstr == "three four");
402     EXPECT_EQ(map.u64,      6000000000ULL);
403     EXPECT_EQ(map.u32,      3000000000U);
404     EXPECT_EQ(map.u16,      50000);
405     EXPECT_EQ(map.u8,       254);
406     EXPECT_EQ(map.b,        true);
407     EXPECT_EQ(map.s64,      -6000000000LL);
408     EXPECT_EQ(map.s32,      -2000000000L);
409     EXPECT_EQ(map.s16,      -32000);
410     EXPECT_EQ(map.s8,       -128);
411     EXPECT_EQ(map.f,        3.25);
412     EXPECT_EQ(map.d,        -2.8625);
413     EXPECT_EQ(map.h8,       Hex8(254));
414     EXPECT_EQ(map.h16,      Hex16(50000));
415     EXPECT_EQ(map.h32,      Hex32(3000000000U));
416     EXPECT_EQ(map.h64,      Hex64(6000000000LL));
417   }
418 }
419
420 struct StringTypes {
421   llvm::StringRef str1;
422   llvm::StringRef str2;
423   llvm::StringRef str3;
424   llvm::StringRef str4;
425   llvm::StringRef str5;
426   llvm::StringRef str6;
427   llvm::StringRef str7;
428   llvm::StringRef str8;
429   llvm::StringRef str9;
430   llvm::StringRef str10;
431   llvm::StringRef str11;
432   std::string stdstr1;
433   std::string stdstr2;
434   std::string stdstr3;
435   std::string stdstr4;
436   std::string stdstr5;
437   std::string stdstr6;
438   std::string stdstr7;
439   std::string stdstr8;
440   std::string stdstr9;
441   std::string stdstr10;
442   std::string stdstr11;
443 };
444
445 namespace llvm {
446 namespace yaml {
447   template <>
448   struct MappingTraits<StringTypes> {
449     static void mapping(IO &io, StringTypes& st) {
450       io.mapRequired("str1",      st.str1);
451       io.mapRequired("str2",      st.str2);
452       io.mapRequired("str3",      st.str3);
453       io.mapRequired("str4",      st.str4);
454       io.mapRequired("str5",      st.str5);
455       io.mapRequired("str6",      st.str6);
456       io.mapRequired("str7",      st.str7);
457       io.mapRequired("str8",      st.str8);
458       io.mapRequired("str9",      st.str9);
459       io.mapRequired("str10",     st.str10);
460       io.mapRequired("str11",     st.str11);
461       io.mapRequired("stdstr1",   st.stdstr1);
462       io.mapRequired("stdstr2",   st.stdstr2);
463       io.mapRequired("stdstr3",   st.stdstr3);
464       io.mapRequired("stdstr4",   st.stdstr4);
465       io.mapRequired("stdstr5",   st.stdstr5);
466       io.mapRequired("stdstr6",   st.stdstr6);
467       io.mapRequired("stdstr7",   st.stdstr7);
468       io.mapRequired("stdstr8",   st.stdstr8);
469       io.mapRequired("stdstr9",   st.stdstr9);
470       io.mapRequired("stdstr10",  st.stdstr10);
471       io.mapRequired("stdstr11",  st.stdstr11);
472     }
473   };
474 }
475 }
476
477 TEST(YAMLIO, TestReadWriteStringTypes) {
478   std::string intermediate;
479   {
480     StringTypes map;
481     map.str1 = "'aaa";
482     map.str2 = "\"bbb";
483     map.str3 = "`ccc";
484     map.str4 = "@ddd";
485     map.str5 = "";
486     map.str6 = "0000000004000000";
487     map.str7 = "true";
488     map.str8 = "FALSE";
489     map.str9 = "~";
490     map.str10 = "0.2e20";
491     map.str11 = "0x30";
492     map.stdstr1 = "'eee";
493     map.stdstr2 = "\"fff";
494     map.stdstr3 = "`ggg";
495     map.stdstr4 = "@hhh";
496     map.stdstr5 = "";
497     map.stdstr6 = "0000000004000000";
498     map.stdstr7 = "true";
499     map.stdstr8 = "FALSE";
500     map.stdstr9 = "~";
501     map.stdstr10 = "0.2e20";
502     map.stdstr11 = "0x30";
503
504     llvm::raw_string_ostream ostr(intermediate);
505     Output yout(ostr);
506     yout << map;
507   }
508
509   llvm::StringRef flowOut(intermediate);
510   EXPECT_NE(llvm::StringRef::npos, flowOut.find("'''aaa"));
511   EXPECT_NE(llvm::StringRef::npos, flowOut.find("'\"bbb'"));
512   EXPECT_NE(llvm::StringRef::npos, flowOut.find("'`ccc'"));
513   EXPECT_NE(llvm::StringRef::npos, flowOut.find("'@ddd'"));
514   EXPECT_NE(llvm::StringRef::npos, flowOut.find("''\n"));
515   EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0000000004000000'\n"));
516   EXPECT_NE(llvm::StringRef::npos, flowOut.find("'true'\n"));
517   EXPECT_NE(llvm::StringRef::npos, flowOut.find("'FALSE'\n"));
518   EXPECT_NE(llvm::StringRef::npos, flowOut.find("'~'\n"));
519   EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0.2e20'\n"));
520   EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0x30'\n"));
521   EXPECT_NE(std::string::npos, flowOut.find("'''eee"));
522   EXPECT_NE(std::string::npos, flowOut.find("'\"fff'"));
523   EXPECT_NE(std::string::npos, flowOut.find("'`ggg'"));
524   EXPECT_NE(std::string::npos, flowOut.find("'@hhh'"));
525   EXPECT_NE(std::string::npos, flowOut.find("''\n"));
526   EXPECT_NE(std::string::npos, flowOut.find("'0000000004000000'\n"));
527
528   {
529     Input yin(intermediate);
530     StringTypes map;
531     yin >> map;
532
533     EXPECT_FALSE(yin.error());
534     EXPECT_TRUE(map.str1.equals("'aaa"));
535     EXPECT_TRUE(map.str2.equals("\"bbb"));
536     EXPECT_TRUE(map.str3.equals("`ccc"));
537     EXPECT_TRUE(map.str4.equals("@ddd"));
538     EXPECT_TRUE(map.str5.equals(""));
539     EXPECT_TRUE(map.str6.equals("0000000004000000"));
540     EXPECT_TRUE(map.stdstr1 == "'eee");
541     EXPECT_TRUE(map.stdstr2 == "\"fff");
542     EXPECT_TRUE(map.stdstr3 == "`ggg");
543     EXPECT_TRUE(map.stdstr4 == "@hhh");
544     EXPECT_TRUE(map.stdstr5 == "");
545     EXPECT_TRUE(map.stdstr6 == "0000000004000000");
546   }
547 }
548
549 //===----------------------------------------------------------------------===//
550 //  Test ScalarEnumerationTraits
551 //===----------------------------------------------------------------------===//
552
553 enum Colors {
554     cRed,
555     cBlue,
556     cGreen,
557     cYellow
558 };
559
560 struct ColorMap {
561   Colors      c1;
562   Colors      c2;
563   Colors      c3;
564   Colors      c4;
565   Colors      c5;
566   Colors      c6;
567 };
568
569 namespace llvm {
570 namespace yaml {
571   template <>
572   struct ScalarEnumerationTraits<Colors> {
573     static void enumeration(IO &io, Colors &value) {
574       io.enumCase(value, "red",   cRed);
575       io.enumCase(value, "blue",  cBlue);
576       io.enumCase(value, "green", cGreen);
577       io.enumCase(value, "yellow",cYellow);
578     }
579   };
580   template <>
581   struct MappingTraits<ColorMap> {
582     static void mapping(IO &io, ColorMap& c) {
583       io.mapRequired("c1", c.c1);
584       io.mapRequired("c2", c.c2);
585       io.mapRequired("c3", c.c3);
586       io.mapOptional("c4", c.c4, cBlue);   // supplies default
587       io.mapOptional("c5", c.c5, cYellow); // supplies default
588       io.mapOptional("c6", c.c6, cRed);    // supplies default
589     }
590   };
591 }
592 }
593
594
595 //
596 // Test reading enumerated scalars
597 //
598 TEST(YAMLIO, TestEnumRead) {
599   ColorMap map;
600   Input yin("---\n"
601             "c1:  blue\n"
602             "c2:  red\n"
603             "c3:  green\n"
604             "c5:  yellow\n"
605             "...\n");
606   yin >> map;
607
608   EXPECT_FALSE(yin.error());
609   EXPECT_EQ(cBlue,  map.c1);
610   EXPECT_EQ(cRed,   map.c2);
611   EXPECT_EQ(cGreen, map.c3);
612   EXPECT_EQ(cBlue,  map.c4);  // tests default
613   EXPECT_EQ(cYellow,map.c5);  // tests overridden
614   EXPECT_EQ(cRed,   map.c6);  // tests default
615 }
616
617
618
619 //===----------------------------------------------------------------------===//
620 //  Test ScalarBitSetTraits
621 //===----------------------------------------------------------------------===//
622
623 enum MyFlags {
624   flagNone    = 0,
625   flagBig     = 1 << 0,
626   flagFlat    = 1 << 1,
627   flagRound   = 1 << 2,
628   flagPointy  = 1 << 3
629 };
630 inline MyFlags operator|(MyFlags a, MyFlags b) {
631   return static_cast<MyFlags>(
632                       static_cast<uint32_t>(a) | static_cast<uint32_t>(b));
633 }
634
635 struct FlagsMap {
636   MyFlags     f1;
637   MyFlags     f2;
638   MyFlags     f3;
639   MyFlags     f4;
640 };
641
642
643 namespace llvm {
644 namespace yaml {
645   template <>
646   struct ScalarBitSetTraits<MyFlags> {
647     static void bitset(IO &io, MyFlags &value) {
648       io.bitSetCase(value, "big",   flagBig);
649       io.bitSetCase(value, "flat",  flagFlat);
650       io.bitSetCase(value, "round", flagRound);
651       io.bitSetCase(value, "pointy",flagPointy);
652     }
653   };
654   template <>
655   struct MappingTraits<FlagsMap> {
656     static void mapping(IO &io, FlagsMap& c) {
657       io.mapRequired("f1", c.f1);
658       io.mapRequired("f2", c.f2);
659       io.mapRequired("f3", c.f3);
660       io.mapOptional("f4", c.f4, MyFlags(flagRound));
661      }
662   };
663 }
664 }
665
666
667 //
668 // Test reading flow sequence representing bit-mask values
669 //
670 TEST(YAMLIO, TestFlagsRead) {
671   FlagsMap map;
672   Input yin("---\n"
673             "f1:  [ big ]\n"
674             "f2:  [ round, flat ]\n"
675             "f3:  []\n"
676             "...\n");
677   yin >> map;
678
679   EXPECT_FALSE(yin.error());
680   EXPECT_EQ(flagBig,              map.f1);
681   EXPECT_EQ(flagRound|flagFlat,   map.f2);
682   EXPECT_EQ(flagNone,             map.f3);  // check empty set
683   EXPECT_EQ(flagRound,            map.f4);  // check optional key
684 }
685
686
687 //
688 // Test writing then reading back bit-mask values
689 //
690 TEST(YAMLIO, TestReadWriteFlags) {
691   std::string intermediate;
692   {
693     FlagsMap map;
694     map.f1 = flagBig;
695     map.f2 = flagRound | flagFlat;
696     map.f3 = flagNone;
697     map.f4 = flagNone;
698
699     llvm::raw_string_ostream ostr(intermediate);
700     Output yout(ostr);
701     yout << map;
702   }
703
704   {
705     Input yin(intermediate);
706     FlagsMap map2;
707     yin >> map2;
708
709     EXPECT_FALSE(yin.error());
710     EXPECT_EQ(flagBig,              map2.f1);
711     EXPECT_EQ(flagRound|flagFlat,   map2.f2);
712     EXPECT_EQ(flagNone,             map2.f3);
713     //EXPECT_EQ(flagRound,            map2.f4);  // check optional key
714   }
715 }
716
717
718
719 //===----------------------------------------------------------------------===//
720 //  Test ScalarTraits
721 //===----------------------------------------------------------------------===//
722
723 struct MyCustomType {
724   int length;
725   int width;
726 };
727
728 struct MyCustomTypeMap {
729   MyCustomType     f1;
730   MyCustomType     f2;
731   int              f3;
732 };
733
734
735 namespace llvm {
736 namespace yaml {
737   template <>
738   struct MappingTraits<MyCustomTypeMap> {
739     static void mapping(IO &io, MyCustomTypeMap& s) {
740       io.mapRequired("f1", s.f1);
741       io.mapRequired("f2", s.f2);
742       io.mapRequired("f3", s.f3);
743      }
744   };
745   // MyCustomType is formatted as a yaml scalar.  A value of
746   // {length=3, width=4} would be represented in yaml as "3 by 4".
747   template<>
748   struct ScalarTraits<MyCustomType> {
749     static void output(const MyCustomType &value, void* ctxt, llvm::raw_ostream &out) {
750       out << llvm::format("%d by %d", value.length, value.width);
751     }
752     static StringRef input(StringRef scalar, void* ctxt, MyCustomType &value) {
753       size_t byStart = scalar.find("by");
754       if ( byStart != StringRef::npos ) {
755         StringRef lenStr = scalar.slice(0, byStart);
756         lenStr = lenStr.rtrim();
757         if ( lenStr.getAsInteger(0, value.length) ) {
758           return "malformed length";
759         }
760         StringRef widthStr = scalar.drop_front(byStart+2);
761         widthStr = widthStr.ltrim();
762         if ( widthStr.getAsInteger(0, value.width) ) {
763           return "malformed width";
764         }
765         return StringRef();
766       }
767       else {
768           return "malformed by";
769       }
770     }
771     static bool mustQuote(StringRef) { return true; }
772   };
773 }
774 }
775
776
777 //
778 // Test writing then reading back custom values
779 //
780 TEST(YAMLIO, TestReadWriteMyCustomType) {
781   std::string intermediate;
782   {
783     MyCustomTypeMap map;
784     map.f1.length = 1;
785     map.f1.width  = 4;
786     map.f2.length = 100;
787     map.f2.width  = 400;
788     map.f3 = 10;
789
790     llvm::raw_string_ostream ostr(intermediate);
791     Output yout(ostr);
792     yout << map;
793   }
794
795   {
796     Input yin(intermediate);
797     MyCustomTypeMap map2;
798     yin >> map2;
799
800     EXPECT_FALSE(yin.error());
801     EXPECT_EQ(1,      map2.f1.length);
802     EXPECT_EQ(4,      map2.f1.width);
803     EXPECT_EQ(100,    map2.f2.length);
804     EXPECT_EQ(400,    map2.f2.width);
805     EXPECT_EQ(10,     map2.f3);
806   }
807 }
808
809
810 //===----------------------------------------------------------------------===//
811 //  Test BlockScalarTraits
812 //===----------------------------------------------------------------------===//
813
814 struct MultilineStringType {
815   std::string str;
816 };
817
818 struct MultilineStringTypeMap {
819   MultilineStringType name;
820   MultilineStringType description;
821   MultilineStringType ingredients;
822   MultilineStringType recipes;
823   MultilineStringType warningLabels;
824   MultilineStringType documentation;
825   int price;
826 };
827
828 namespace llvm {
829 namespace yaml {
830   template <>
831   struct MappingTraits<MultilineStringTypeMap> {
832     static void mapping(IO &io, MultilineStringTypeMap& s) {
833       io.mapRequired("name", s.name);
834       io.mapRequired("description", s.description);
835       io.mapRequired("ingredients", s.ingredients);
836       io.mapRequired("recipes", s.recipes);
837       io.mapRequired("warningLabels", s.warningLabels);
838       io.mapRequired("documentation", s.documentation);
839       io.mapRequired("price", s.price);
840      }
841   };
842
843   // MultilineStringType is formatted as a yaml block literal scalar. A value of
844   // "Hello\nWorld" would be represented in yaml as
845   //  |
846   //    Hello
847   //    World
848   template <>
849   struct BlockScalarTraits<MultilineStringType> {
850     static void output(const MultilineStringType &value, void *ctxt,
851                        llvm::raw_ostream &out) {
852       out << value.str;
853     }
854     static StringRef input(StringRef scalar, void *ctxt,
855                            MultilineStringType &value) {
856       value.str = scalar.str();
857       return StringRef();
858     }
859   };
860 }
861 }
862
863 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MultilineStringType)
864
865 //
866 // Test writing then reading back custom values
867 //
868 TEST(YAMLIO, TestReadWriteMultilineStringType) {
869   std::string intermediate;
870   {
871     MultilineStringTypeMap map;
872     map.name.str = "An Item";
873     map.description.str = "Hello\nWorld";
874     map.ingredients.str = "SubItem 1\nSub Item 2\n\nSub Item 3\n";
875     map.recipes.str = "\n\nTest 1\n\n\n";
876     map.warningLabels.str = "";
877     map.documentation.str = "\n\n";
878     map.price = 350;
879
880     llvm::raw_string_ostream ostr(intermediate);
881     Output yout(ostr);
882     yout << map;
883   }
884   {
885     Input yin(intermediate);
886     MultilineStringTypeMap map2;
887     yin >> map2;
888
889     EXPECT_FALSE(yin.error());
890     EXPECT_EQ(map2.name.str, "An Item\n");
891     EXPECT_EQ(map2.description.str, "Hello\nWorld\n");
892     EXPECT_EQ(map2.ingredients.str, "SubItem 1\nSub Item 2\n\nSub Item 3\n");
893     EXPECT_EQ(map2.recipes.str, "\n\nTest 1\n");
894     EXPECT_TRUE(map2.warningLabels.str.empty());
895     EXPECT_TRUE(map2.documentation.str.empty());
896     EXPECT_EQ(map2.price, 350);
897   }
898 }
899
900 //
901 // Test writing then reading back custom values
902 //
903 TEST(YAMLIO, TestReadWriteBlockScalarDocuments) {
904   std::string intermediate;
905   {
906     std::vector<MultilineStringType> documents;
907     MultilineStringType doc;
908     doc.str = "Hello\nWorld";
909     documents.push_back(doc);
910
911     llvm::raw_string_ostream ostr(intermediate);
912     Output yout(ostr);
913     yout << documents;
914
915     // Verify that the block scalar header was written out on the same line
916     // as the document marker.
917     EXPECT_NE(llvm::StringRef::npos, llvm::StringRef(ostr.str()).find("--- |"));
918   }
919   {
920     Input yin(intermediate);
921     std::vector<MultilineStringType> documents2;
922     yin >> documents2;
923
924     EXPECT_FALSE(yin.error());
925     EXPECT_EQ(documents2.size(), size_t(1));
926     EXPECT_EQ(documents2[0].str, "Hello\nWorld\n");
927   }
928 }
929
930 TEST(YAMLIO, TestReadWriteBlockScalarValue) {
931   std::string intermediate;
932   {
933     MultilineStringType doc;
934     doc.str = "Just a block\nscalar doc";
935
936     llvm::raw_string_ostream ostr(intermediate);
937     Output yout(ostr);
938     yout << doc;
939   }
940   {
941     Input yin(intermediate);
942     MultilineStringType doc;
943     yin >> doc;
944
945     EXPECT_FALSE(yin.error());
946     EXPECT_EQ(doc.str, "Just a block\nscalar doc\n");
947   }
948 }
949
950 //===----------------------------------------------------------------------===//
951 //  Test flow sequences
952 //===----------------------------------------------------------------------===//
953
954 LLVM_YAML_STRONG_TYPEDEF(int, MyNumber)
955 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyNumber)
956 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::StringRef)
957
958 namespace llvm {
959 namespace yaml {
960   template<>
961   struct ScalarTraits<MyNumber> {
962     static void output(const MyNumber &value, void *, llvm::raw_ostream &out) {
963       out << value;
964     }
965
966     static StringRef input(StringRef scalar, void *, MyNumber &value) {
967       long long n;
968       if ( getAsSignedInteger(scalar, 0, n) )
969         return "invalid number";
970       value = n;
971       return StringRef();
972     }
973
974     static bool mustQuote(StringRef) { return false; }
975   };
976 }
977 }
978
979 struct NameAndNumbers {
980   llvm::StringRef               name;
981   std::vector<llvm::StringRef>  strings;
982   std::vector<MyNumber>         single;
983   std::vector<MyNumber>         numbers;
984 };
985
986 namespace llvm {
987 namespace yaml {
988   template <>
989   struct MappingTraits<NameAndNumbers> {
990     static void mapping(IO &io, NameAndNumbers& nn) {
991       io.mapRequired("name",     nn.name);
992       io.mapRequired("strings",  nn.strings);
993       io.mapRequired("single",   nn.single);
994       io.mapRequired("numbers",  nn.numbers);
995     }
996   };
997 }
998 }
999
1000 typedef std::vector<MyNumber> MyNumberFlowSequence;
1001
1002 LLVM_YAML_IS_SEQUENCE_VECTOR(MyNumberFlowSequence)
1003
1004 struct NameAndNumbersFlow {
1005   llvm::StringRef                    name;
1006   std::vector<MyNumberFlowSequence>  sequenceOfNumbers;
1007 };
1008
1009 namespace llvm {
1010 namespace yaml {
1011   template <>
1012   struct MappingTraits<NameAndNumbersFlow> {
1013     static void mapping(IO &io, NameAndNumbersFlow& nn) {
1014       io.mapRequired("name",     nn.name);
1015       io.mapRequired("sequenceOfNumbers",  nn.sequenceOfNumbers);
1016     }
1017   };
1018 }
1019 }
1020
1021 //
1022 // Test writing then reading back custom values
1023 //
1024 TEST(YAMLIO, TestReadWriteMyFlowSequence) {
1025   std::string intermediate;
1026   {
1027     NameAndNumbers map;
1028     map.name  = "hello";
1029     map.strings.push_back(llvm::StringRef("one"));
1030     map.strings.push_back(llvm::StringRef("two"));
1031     map.single.push_back(1);
1032     map.numbers.push_back(10);
1033     map.numbers.push_back(-30);
1034     map.numbers.push_back(1024);
1035
1036     llvm::raw_string_ostream ostr(intermediate);
1037     Output yout(ostr);
1038     yout << map;
1039
1040     // Verify sequences were written in flow style
1041     ostr.flush();
1042     llvm::StringRef flowOut(intermediate);
1043     EXPECT_NE(llvm::StringRef::npos, flowOut.find("one, two"));
1044     EXPECT_NE(llvm::StringRef::npos, flowOut.find("10, -30, 1024"));
1045   }
1046
1047   {
1048     Input yin(intermediate);
1049     NameAndNumbers map2;
1050     yin >> map2;
1051
1052     EXPECT_FALSE(yin.error());
1053     EXPECT_TRUE(map2.name.equals("hello"));
1054     EXPECT_EQ(map2.strings.size(), 2UL);
1055     EXPECT_TRUE(map2.strings[0].equals("one"));
1056     EXPECT_TRUE(map2.strings[1].equals("two"));
1057     EXPECT_EQ(map2.single.size(), 1UL);
1058     EXPECT_EQ(1,       map2.single[0]);
1059     EXPECT_EQ(map2.numbers.size(), 3UL);
1060     EXPECT_EQ(10,      map2.numbers[0]);
1061     EXPECT_EQ(-30,     map2.numbers[1]);
1062     EXPECT_EQ(1024,    map2.numbers[2]);
1063   }
1064 }
1065
1066
1067 //
1068 // Test writing then reading back a sequence of flow sequences.
1069 //
1070 TEST(YAMLIO, TestReadWriteSequenceOfMyFlowSequence) {
1071   std::string intermediate;
1072   {
1073     NameAndNumbersFlow map;
1074     map.name  = "hello";
1075     MyNumberFlowSequence single = { 0 };
1076     MyNumberFlowSequence numbers = { 12, 1, -512 };
1077     map.sequenceOfNumbers.push_back(single);
1078     map.sequenceOfNumbers.push_back(numbers);
1079     map.sequenceOfNumbers.push_back(MyNumberFlowSequence());
1080
1081     llvm::raw_string_ostream ostr(intermediate);
1082     Output yout(ostr);
1083     yout << map;
1084
1085     // Verify sequences were written in flow style
1086     // and that the parent sequence used '-'.
1087     ostr.flush();
1088     llvm::StringRef flowOut(intermediate);
1089     EXPECT_NE(llvm::StringRef::npos, flowOut.find("- [ 0 ]"));
1090     EXPECT_NE(llvm::StringRef::npos, flowOut.find("- [ 12, 1, -512 ]"));
1091     EXPECT_NE(llvm::StringRef::npos, flowOut.find("- [  ]"));
1092   }
1093
1094   {
1095     Input yin(intermediate);
1096     NameAndNumbersFlow map2;
1097     yin >> map2;
1098
1099     EXPECT_FALSE(yin.error());
1100     EXPECT_TRUE(map2.name.equals("hello"));
1101     EXPECT_EQ(map2.sequenceOfNumbers.size(), 3UL);
1102     EXPECT_EQ(map2.sequenceOfNumbers[0].size(), 1UL);
1103     EXPECT_EQ(0,    map2.sequenceOfNumbers[0][0]);
1104     EXPECT_EQ(map2.sequenceOfNumbers[1].size(), 3UL);
1105     EXPECT_EQ(12,   map2.sequenceOfNumbers[1][0]);
1106     EXPECT_EQ(1,    map2.sequenceOfNumbers[1][1]);
1107     EXPECT_EQ(-512, map2.sequenceOfNumbers[1][2]);
1108     EXPECT_TRUE(map2.sequenceOfNumbers[2].empty());
1109   }
1110 }
1111
1112 //===----------------------------------------------------------------------===//
1113 //  Test normalizing/denormalizing
1114 //===----------------------------------------------------------------------===//
1115
1116 LLVM_YAML_STRONG_TYPEDEF(uint32_t, TotalSeconds)
1117
1118 typedef std::vector<TotalSeconds> SecondsSequence;
1119
1120 LLVM_YAML_IS_SEQUENCE_VECTOR(TotalSeconds)
1121
1122
1123 namespace llvm {
1124 namespace yaml {
1125   template <>
1126   struct MappingTraits<TotalSeconds> {
1127
1128     class NormalizedSeconds {
1129     public:
1130       NormalizedSeconds(IO &io)
1131         : hours(0), minutes(0), seconds(0) {
1132       }
1133       NormalizedSeconds(IO &, TotalSeconds &secs)
1134         : hours(secs/3600),
1135           minutes((secs - (hours*3600))/60),
1136           seconds(secs % 60) {
1137       }
1138       TotalSeconds denormalize(IO &) {
1139         return TotalSeconds(hours*3600 + minutes*60 + seconds);
1140       }
1141
1142       uint32_t     hours;
1143       uint8_t      minutes;
1144       uint8_t      seconds;
1145     };
1146
1147     static void mapping(IO &io, TotalSeconds &secs) {
1148       MappingNormalization<NormalizedSeconds, TotalSeconds> keys(io, secs);
1149
1150       io.mapOptional("hours",    keys->hours,    (uint32_t)0);
1151       io.mapOptional("minutes",  keys->minutes,  (uint8_t)0);
1152       io.mapRequired("seconds",  keys->seconds);
1153     }
1154   };
1155 }
1156 }
1157
1158
1159 //
1160 // Test the reading of a yaml sequence of mappings
1161 //
1162 TEST(YAMLIO, TestReadMySecondsSequence) {
1163   SecondsSequence seq;
1164   Input yin("---\n - hours:  1\n   seconds:  5\n - seconds:  59\n...\n");
1165   yin >> seq;
1166
1167   EXPECT_FALSE(yin.error());
1168   EXPECT_EQ(seq.size(), 2UL);
1169   EXPECT_EQ(seq[0], 3605U);
1170   EXPECT_EQ(seq[1], 59U);
1171 }
1172
1173
1174 //
1175 // Test writing then reading back custom values
1176 //
1177 TEST(YAMLIO, TestReadWriteMySecondsSequence) {
1178   std::string intermediate;
1179   {
1180     SecondsSequence seq;
1181     seq.push_back(4000);
1182     seq.push_back(500);
1183     seq.push_back(59);
1184
1185     llvm::raw_string_ostream ostr(intermediate);
1186     Output yout(ostr);
1187     yout << seq;
1188   }
1189   {
1190     Input yin(intermediate);
1191     SecondsSequence seq2;
1192     yin >> seq2;
1193
1194     EXPECT_FALSE(yin.error());
1195     EXPECT_EQ(seq2.size(), 3UL);
1196     EXPECT_EQ(seq2[0], 4000U);
1197     EXPECT_EQ(seq2[1], 500U);
1198     EXPECT_EQ(seq2[2], 59U);
1199   }
1200 }
1201
1202
1203 //===----------------------------------------------------------------------===//
1204 //  Test dynamic typing
1205 //===----------------------------------------------------------------------===//
1206
1207 enum AFlags {
1208     a1,
1209     a2,
1210     a3
1211 };
1212
1213 enum BFlags {
1214     b1,
1215     b2,
1216     b3
1217 };
1218
1219 enum Kind {
1220     kindA,
1221     kindB
1222 };
1223
1224 struct KindAndFlags {
1225   KindAndFlags() : kind(kindA), flags(0) { }
1226   KindAndFlags(Kind k, uint32_t f) : kind(k), flags(f) { }
1227   Kind        kind;
1228   uint32_t    flags;
1229 };
1230
1231 typedef std::vector<KindAndFlags> KindAndFlagsSequence;
1232
1233 LLVM_YAML_IS_SEQUENCE_VECTOR(KindAndFlags)
1234
1235 namespace llvm {
1236 namespace yaml {
1237   template <>
1238   struct ScalarEnumerationTraits<AFlags> {
1239     static void enumeration(IO &io, AFlags &value) {
1240       io.enumCase(value, "a1",  a1);
1241       io.enumCase(value, "a2",  a2);
1242       io.enumCase(value, "a3",  a3);
1243     }
1244   };
1245   template <>
1246   struct ScalarEnumerationTraits<BFlags> {
1247     static void enumeration(IO &io, BFlags &value) {
1248       io.enumCase(value, "b1",  b1);
1249       io.enumCase(value, "b2",  b2);
1250       io.enumCase(value, "b3",  b3);
1251     }
1252   };
1253   template <>
1254   struct ScalarEnumerationTraits<Kind> {
1255     static void enumeration(IO &io, Kind &value) {
1256       io.enumCase(value, "A",  kindA);
1257       io.enumCase(value, "B",  kindB);
1258     }
1259   };
1260   template <>
1261   struct MappingTraits<KindAndFlags> {
1262     static void mapping(IO &io, KindAndFlags& kf) {
1263       io.mapRequired("kind",  kf.kind);
1264       // Type of "flags" field varies depending on "kind" field.
1265       // Use memcpy here to avoid breaking strict aliasing rules.
1266       if (kf.kind == kindA) {
1267         AFlags aflags = static_cast<AFlags>(kf.flags);
1268         io.mapRequired("flags", aflags);
1269         kf.flags = aflags;
1270       } else {
1271         BFlags bflags = static_cast<BFlags>(kf.flags);
1272         io.mapRequired("flags", bflags);
1273         kf.flags = bflags;
1274       }
1275     }
1276   };
1277 }
1278 }
1279
1280
1281 //
1282 // Test the reading of a yaml sequence dynamic types
1283 //
1284 TEST(YAMLIO, TestReadKindAndFlagsSequence) {
1285   KindAndFlagsSequence seq;
1286   Input yin("---\n - kind:  A\n   flags:  a2\n - kind:  B\n   flags:  b1\n...\n");
1287   yin >> seq;
1288
1289   EXPECT_FALSE(yin.error());
1290   EXPECT_EQ(seq.size(), 2UL);
1291   EXPECT_EQ(seq[0].kind,  kindA);
1292   EXPECT_EQ(seq[0].flags, (uint32_t)a2);
1293   EXPECT_EQ(seq[1].kind,  kindB);
1294   EXPECT_EQ(seq[1].flags, (uint32_t)b1);
1295 }
1296
1297 //
1298 // Test writing then reading back dynamic types
1299 //
1300 TEST(YAMLIO, TestReadWriteKindAndFlagsSequence) {
1301   std::string intermediate;
1302   {
1303     KindAndFlagsSequence seq;
1304     seq.push_back(KindAndFlags(kindA,a1));
1305     seq.push_back(KindAndFlags(kindB,b1));
1306     seq.push_back(KindAndFlags(kindA,a2));
1307     seq.push_back(KindAndFlags(kindB,b2));
1308     seq.push_back(KindAndFlags(kindA,a3));
1309
1310     llvm::raw_string_ostream ostr(intermediate);
1311     Output yout(ostr);
1312     yout << seq;
1313   }
1314   {
1315     Input yin(intermediate);
1316     KindAndFlagsSequence seq2;
1317     yin >> seq2;
1318
1319     EXPECT_FALSE(yin.error());
1320     EXPECT_EQ(seq2.size(), 5UL);
1321     EXPECT_EQ(seq2[0].kind,  kindA);
1322     EXPECT_EQ(seq2[0].flags, (uint32_t)a1);
1323     EXPECT_EQ(seq2[1].kind,  kindB);
1324     EXPECT_EQ(seq2[1].flags, (uint32_t)b1);
1325     EXPECT_EQ(seq2[2].kind,  kindA);
1326     EXPECT_EQ(seq2[2].flags, (uint32_t)a2);
1327     EXPECT_EQ(seq2[3].kind,  kindB);
1328     EXPECT_EQ(seq2[3].flags, (uint32_t)b2);
1329     EXPECT_EQ(seq2[4].kind,  kindA);
1330     EXPECT_EQ(seq2[4].flags, (uint32_t)a3);
1331   }
1332 }
1333
1334
1335 //===----------------------------------------------------------------------===//
1336 //  Test document list
1337 //===----------------------------------------------------------------------===//
1338
1339 struct FooBarMap {
1340   int foo;
1341   int bar;
1342 };
1343 typedef std::vector<FooBarMap> FooBarMapDocumentList;
1344
1345 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(FooBarMap)
1346
1347
1348 namespace llvm {
1349 namespace yaml {
1350   template <>
1351   struct MappingTraits<FooBarMap> {
1352     static void mapping(IO &io, FooBarMap& fb) {
1353       io.mapRequired("foo",    fb.foo);
1354       io.mapRequired("bar",    fb.bar);
1355     }
1356   };
1357 }
1358 }
1359
1360
1361 //
1362 // Test the reading of a yaml mapping
1363 //
1364 TEST(YAMLIO, TestDocRead) {
1365   FooBarMap doc;
1366   Input yin("---\nfoo:  3\nbar:  5\n...\n");
1367   yin >> doc;
1368
1369   EXPECT_FALSE(yin.error());
1370   EXPECT_EQ(doc.foo, 3);
1371   EXPECT_EQ(doc.bar,5);
1372 }
1373
1374
1375
1376 //
1377 // Test writing then reading back a sequence of mappings
1378 //
1379 TEST(YAMLIO, TestSequenceDocListWriteAndRead) {
1380   std::string intermediate;
1381   {
1382     FooBarMap doc1;
1383     doc1.foo = 10;
1384     doc1.bar = -3;
1385     FooBarMap doc2;
1386     doc2.foo = 257;
1387     doc2.bar = 0;
1388     std::vector<FooBarMap> docList;
1389     docList.push_back(doc1);
1390     docList.push_back(doc2);
1391
1392     llvm::raw_string_ostream ostr(intermediate);
1393     Output yout(ostr);
1394     yout << docList;
1395   }
1396
1397
1398   {
1399     Input yin(intermediate);
1400     std::vector<FooBarMap> docList2;
1401     yin >> docList2;
1402
1403     EXPECT_FALSE(yin.error());
1404     EXPECT_EQ(docList2.size(), 2UL);
1405     FooBarMap& map1 = docList2[0];
1406     FooBarMap& map2 = docList2[1];
1407     EXPECT_EQ(map1.foo, 10);
1408     EXPECT_EQ(map1.bar, -3);
1409     EXPECT_EQ(map2.foo, 257);
1410     EXPECT_EQ(map2.bar, 0);
1411   }
1412 }
1413
1414 //===----------------------------------------------------------------------===//
1415 //  Test document tags
1416 //===----------------------------------------------------------------------===//
1417
1418 struct MyDouble {
1419   MyDouble() : value(0.0) { }
1420   MyDouble(double x) : value(x) { }
1421   double value;
1422 };
1423
1424 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyDouble)
1425
1426
1427 namespace llvm {
1428 namespace yaml {
1429   template <>
1430   struct MappingTraits<MyDouble> {
1431     static void mapping(IO &io, MyDouble &d) {
1432       if (io.mapTag("!decimal", true)) {
1433         mappingDecimal(io, d);
1434       } else if (io.mapTag("!fraction")) {
1435         mappingFraction(io, d);
1436       }
1437     }
1438     static void mappingDecimal(IO &io, MyDouble &d) {
1439       io.mapRequired("value", d.value);
1440     }
1441     static void mappingFraction(IO &io, MyDouble &d) {
1442         double num, denom;
1443         io.mapRequired("numerator",      num);
1444         io.mapRequired("denominator",    denom);
1445         // convert fraction to double
1446         d.value = num/denom;
1447     }
1448   };
1449  }
1450 }
1451
1452
1453 //
1454 // Test the reading of two different tagged yaml documents.
1455 //
1456 TEST(YAMLIO, TestTaggedDocuments) {
1457   std::vector<MyDouble> docList;
1458   Input yin("--- !decimal\nvalue:  3.0\n"
1459             "--- !fraction\nnumerator:  9.0\ndenominator:  2\n...\n");
1460   yin >> docList;
1461   EXPECT_FALSE(yin.error());
1462   EXPECT_EQ(docList.size(), 2UL);
1463   EXPECT_EQ(docList[0].value, 3.0);
1464   EXPECT_EQ(docList[1].value, 4.5);
1465 }
1466
1467
1468
1469 //
1470 // Test writing then reading back tagged documents
1471 //
1472 TEST(YAMLIO, TestTaggedDocumentsWriteAndRead) {
1473   std::string intermediate;
1474   {
1475     MyDouble a(10.25);
1476     MyDouble b(-3.75);
1477     std::vector<MyDouble> docList;
1478     docList.push_back(a);
1479     docList.push_back(b);
1480
1481     llvm::raw_string_ostream ostr(intermediate);
1482     Output yout(ostr);
1483     yout << docList;
1484   }
1485
1486   {
1487     Input yin(intermediate);
1488     std::vector<MyDouble> docList2;
1489     yin >> docList2;
1490
1491     EXPECT_FALSE(yin.error());
1492     EXPECT_EQ(docList2.size(), 2UL);
1493     EXPECT_EQ(docList2[0].value, 10.25);
1494     EXPECT_EQ(docList2[1].value, -3.75);
1495   }
1496 }
1497
1498
1499 //===----------------------------------------------------------------------===//
1500 //  Test mapping validation
1501 //===----------------------------------------------------------------------===//
1502
1503 struct MyValidation {
1504   double value;
1505 };
1506
1507 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyValidation)
1508
1509 namespace llvm {
1510 namespace yaml {
1511   template <>
1512   struct MappingTraits<MyValidation> {
1513     static void mapping(IO &io, MyValidation &d) {
1514         io.mapRequired("value", d.value);
1515     }
1516     static StringRef validate(IO &io, MyValidation &d) {
1517         if (d.value < 0)
1518           return "negative value";
1519         return StringRef();
1520     }
1521   };
1522  }
1523 }
1524
1525
1526 //
1527 // Test that validate() is called and complains about the negative value.
1528 //
1529 TEST(YAMLIO, TestValidatingInput) {
1530   std::vector<MyValidation> docList;
1531   Input yin("--- \nvalue:  3.0\n"
1532             "--- \nvalue:  -1.0\n...\n",
1533             nullptr, suppressErrorMessages);
1534   yin >> docList;
1535   EXPECT_TRUE(!!yin.error());
1536 }
1537
1538 //===----------------------------------------------------------------------===//
1539 //  Test flow mapping
1540 //===----------------------------------------------------------------------===//
1541
1542 struct FlowFooBar {
1543   int foo;
1544   int bar;
1545
1546   FlowFooBar() : foo(0), bar(0) {}
1547   FlowFooBar(int foo, int bar) : foo(foo), bar(bar) {}
1548 };
1549
1550 typedef std::vector<FlowFooBar> FlowFooBarSequence;
1551
1552 LLVM_YAML_IS_SEQUENCE_VECTOR(FlowFooBar)
1553
1554 struct FlowFooBarDoc {
1555   FlowFooBar attribute;
1556   FlowFooBarSequence seq;
1557 };
1558
1559 namespace llvm {
1560 namespace yaml {
1561   template <>
1562   struct MappingTraits<FlowFooBar> {
1563     static void mapping(IO &io, FlowFooBar &fb) {
1564       io.mapRequired("foo", fb.foo);
1565       io.mapRequired("bar", fb.bar);
1566     }
1567
1568     static const bool flow = true;
1569   };
1570
1571   template <>
1572   struct MappingTraits<FlowFooBarDoc> {
1573     static void mapping(IO &io, FlowFooBarDoc &fb) {
1574       io.mapRequired("attribute", fb.attribute);
1575       io.mapRequired("seq", fb.seq);
1576     }
1577   };
1578 }
1579 }
1580
1581 //
1582 // Test writing then reading back custom mappings
1583 //
1584 TEST(YAMLIO, TestReadWriteMyFlowMapping) {
1585   std::string intermediate;
1586   {
1587     FlowFooBarDoc doc;
1588     doc.attribute = FlowFooBar(42, 907);
1589     doc.seq.push_back(FlowFooBar(1, 2));
1590     doc.seq.push_back(FlowFooBar(0, 0));
1591     doc.seq.push_back(FlowFooBar(-1, 1024));
1592
1593     llvm::raw_string_ostream ostr(intermediate);
1594     Output yout(ostr);
1595     yout << doc;
1596
1597     // Verify that mappings were written in flow style
1598     ostr.flush();
1599     llvm::StringRef flowOut(intermediate);
1600     EXPECT_NE(llvm::StringRef::npos, flowOut.find("{ foo: 42, bar: 907 }"));
1601     EXPECT_NE(llvm::StringRef::npos, flowOut.find("- { foo: 1, bar: 2 }"));
1602     EXPECT_NE(llvm::StringRef::npos, flowOut.find("- { foo: 0, bar: 0 }"));
1603     EXPECT_NE(llvm::StringRef::npos, flowOut.find("- { foo: -1, bar: 1024 }"));
1604   }
1605
1606   {
1607     Input yin(intermediate);
1608     FlowFooBarDoc doc2;
1609     yin >> doc2;
1610
1611     EXPECT_FALSE(yin.error());
1612     EXPECT_EQ(doc2.attribute.foo, 42);
1613     EXPECT_EQ(doc2.attribute.bar, 907);
1614     EXPECT_EQ(doc2.seq.size(), 3UL);
1615     EXPECT_EQ(doc2.seq[0].foo, 1);
1616     EXPECT_EQ(doc2.seq[0].bar, 2);
1617     EXPECT_EQ(doc2.seq[1].foo, 0);
1618     EXPECT_EQ(doc2.seq[1].bar, 0);
1619     EXPECT_EQ(doc2.seq[2].foo, -1);
1620     EXPECT_EQ(doc2.seq[2].bar, 1024);
1621   }
1622 }
1623
1624 //===----------------------------------------------------------------------===//
1625 //  Test error handling
1626 //===----------------------------------------------------------------------===//
1627
1628 //
1629 // Test error handling of unknown enumerated scalar
1630 //
1631 TEST(YAMLIO, TestColorsReadError) {
1632   ColorMap map;
1633   Input yin("---\n"
1634             "c1:  blue\n"
1635             "c2:  purple\n"
1636             "c3:  green\n"
1637             "...\n",
1638             /*Ctxt=*/nullptr,
1639             suppressErrorMessages);
1640   yin >> map;
1641   EXPECT_TRUE(!!yin.error());
1642 }
1643
1644
1645 //
1646 // Test error handling of flow sequence with unknown value
1647 //
1648 TEST(YAMLIO, TestFlagsReadError) {
1649   FlagsMap map;
1650   Input yin("---\n"
1651             "f1:  [ big ]\n"
1652             "f2:  [ round, hollow ]\n"
1653             "f3:  []\n"
1654             "...\n",
1655             /*Ctxt=*/nullptr,
1656             suppressErrorMessages);
1657   yin >> map;
1658
1659   EXPECT_TRUE(!!yin.error());
1660 }
1661
1662
1663 //
1664 // Test error handling reading built-in uint8_t type
1665 //
1666 LLVM_YAML_IS_SEQUENCE_VECTOR(uint8_t)
1667 TEST(YAMLIO, TestReadBuiltInTypesUint8Error) {
1668   std::vector<uint8_t> seq;
1669   Input yin("---\n"
1670             "- 255\n"
1671             "- 0\n"
1672             "- 257\n"
1673             "...\n",
1674             /*Ctxt=*/nullptr,
1675             suppressErrorMessages);
1676   yin >> seq;
1677
1678   EXPECT_TRUE(!!yin.error());
1679 }
1680
1681
1682 //
1683 // Test error handling reading built-in uint16_t type
1684 //
1685 LLVM_YAML_IS_SEQUENCE_VECTOR(uint16_t)
1686 TEST(YAMLIO, TestReadBuiltInTypesUint16Error) {
1687   std::vector<uint16_t> seq;
1688   Input yin("---\n"
1689             "- 65535\n"
1690             "- 0\n"
1691             "- 66000\n"
1692             "...\n",
1693             /*Ctxt=*/nullptr,
1694             suppressErrorMessages);
1695   yin >> seq;
1696
1697   EXPECT_TRUE(!!yin.error());
1698 }
1699
1700
1701 //
1702 // Test error handling reading built-in uint32_t type
1703 //
1704 LLVM_YAML_IS_SEQUENCE_VECTOR(uint32_t)
1705 TEST(YAMLIO, TestReadBuiltInTypesUint32Error) {
1706   std::vector<uint32_t> seq;
1707   Input yin("---\n"
1708             "- 4000000000\n"
1709             "- 0\n"
1710             "- 5000000000\n"
1711             "...\n",
1712             /*Ctxt=*/nullptr,
1713             suppressErrorMessages);
1714   yin >> seq;
1715
1716   EXPECT_TRUE(!!yin.error());
1717 }
1718
1719
1720 //
1721 // Test error handling reading built-in uint64_t type
1722 //
1723 LLVM_YAML_IS_SEQUENCE_VECTOR(uint64_t)
1724 TEST(YAMLIO, TestReadBuiltInTypesUint64Error) {
1725   std::vector<uint64_t> seq;
1726   Input yin("---\n"
1727             "- 18446744073709551615\n"
1728             "- 0\n"
1729             "- 19446744073709551615\n"
1730             "...\n",
1731             /*Ctxt=*/nullptr,
1732             suppressErrorMessages);
1733   yin >> seq;
1734
1735   EXPECT_TRUE(!!yin.error());
1736 }
1737
1738
1739 //
1740 // Test error handling reading built-in int8_t type
1741 //
1742 LLVM_YAML_IS_SEQUENCE_VECTOR(int8_t)
1743 TEST(YAMLIO, TestReadBuiltInTypesint8OverError) {
1744   std::vector<int8_t> seq;
1745   Input yin("---\n"
1746             "- -128\n"
1747             "- 0\n"
1748             "- 127\n"
1749             "- 128\n"
1750            "...\n",
1751             /*Ctxt=*/nullptr,
1752             suppressErrorMessages);
1753   yin >> seq;
1754
1755   EXPECT_TRUE(!!yin.error());
1756 }
1757
1758 //
1759 // Test error handling reading built-in int8_t type
1760 //
1761 TEST(YAMLIO, TestReadBuiltInTypesint8UnderError) {
1762   std::vector<int8_t> seq;
1763   Input yin("---\n"
1764             "- -128\n"
1765             "- 0\n"
1766             "- 127\n"
1767             "- -129\n"
1768             "...\n",
1769             /*Ctxt=*/nullptr,
1770             suppressErrorMessages);
1771   yin >> seq;
1772
1773   EXPECT_TRUE(!!yin.error());
1774 }
1775
1776
1777 //
1778 // Test error handling reading built-in int16_t type
1779 //
1780 LLVM_YAML_IS_SEQUENCE_VECTOR(int16_t)
1781 TEST(YAMLIO, TestReadBuiltInTypesint16UnderError) {
1782   std::vector<int16_t> seq;
1783   Input yin("---\n"
1784             "- 32767\n"
1785             "- 0\n"
1786             "- -32768\n"
1787             "- -32769\n"
1788             "...\n",
1789             /*Ctxt=*/nullptr,
1790             suppressErrorMessages);
1791   yin >> seq;
1792
1793   EXPECT_TRUE(!!yin.error());
1794 }
1795
1796
1797 //
1798 // Test error handling reading built-in int16_t type
1799 //
1800 TEST(YAMLIO, TestReadBuiltInTypesint16OverError) {
1801   std::vector<int16_t> seq;
1802   Input yin("---\n"
1803             "- 32767\n"
1804             "- 0\n"
1805             "- -32768\n"
1806             "- 32768\n"
1807             "...\n",
1808             /*Ctxt=*/nullptr,
1809             suppressErrorMessages);
1810   yin >> seq;
1811
1812   EXPECT_TRUE(!!yin.error());
1813 }
1814
1815
1816 //
1817 // Test error handling reading built-in int32_t type
1818 //
1819 LLVM_YAML_IS_SEQUENCE_VECTOR(int32_t)
1820 TEST(YAMLIO, TestReadBuiltInTypesint32UnderError) {
1821   std::vector<int32_t> seq;
1822   Input yin("---\n"
1823             "- 2147483647\n"
1824             "- 0\n"
1825             "- -2147483648\n"
1826             "- -2147483649\n"
1827             "...\n",
1828             /*Ctxt=*/nullptr,
1829             suppressErrorMessages);
1830   yin >> seq;
1831
1832   EXPECT_TRUE(!!yin.error());
1833 }
1834
1835 //
1836 // Test error handling reading built-in int32_t type
1837 //
1838 TEST(YAMLIO, TestReadBuiltInTypesint32OverError) {
1839   std::vector<int32_t> seq;
1840   Input yin("---\n"
1841             "- 2147483647\n"
1842             "- 0\n"
1843             "- -2147483648\n"
1844             "- 2147483649\n"
1845             "...\n",
1846             /*Ctxt=*/nullptr,
1847             suppressErrorMessages);
1848   yin >> seq;
1849
1850   EXPECT_TRUE(!!yin.error());
1851 }
1852
1853
1854 //
1855 // Test error handling reading built-in int64_t type
1856 //
1857 LLVM_YAML_IS_SEQUENCE_VECTOR(int64_t)
1858 TEST(YAMLIO, TestReadBuiltInTypesint64UnderError) {
1859   std::vector<int64_t> seq;
1860   Input yin("---\n"
1861             "- -9223372036854775808\n"
1862             "- 0\n"
1863             "- 9223372036854775807\n"
1864             "- -9223372036854775809\n"
1865             "...\n",
1866             /*Ctxt=*/nullptr,
1867             suppressErrorMessages);
1868   yin >> seq;
1869
1870   EXPECT_TRUE(!!yin.error());
1871 }
1872
1873 //
1874 // Test error handling reading built-in int64_t type
1875 //
1876 TEST(YAMLIO, TestReadBuiltInTypesint64OverError) {
1877   std::vector<int64_t> seq;
1878   Input yin("---\n"
1879             "- -9223372036854775808\n"
1880             "- 0\n"
1881             "- 9223372036854775807\n"
1882             "- 9223372036854775809\n"
1883             "...\n",
1884             /*Ctxt=*/nullptr,
1885             suppressErrorMessages);
1886   yin >> seq;
1887
1888   EXPECT_TRUE(!!yin.error());
1889 }
1890
1891 //
1892 // Test error handling reading built-in float type
1893 //
1894 LLVM_YAML_IS_SEQUENCE_VECTOR(float)
1895 TEST(YAMLIO, TestReadBuiltInTypesFloatError) {
1896   std::vector<float> seq;
1897   Input yin("---\n"
1898             "- 0.0\n"
1899             "- 1000.1\n"
1900             "- -123.456\n"
1901             "- 1.2.3\n"
1902             "...\n",
1903             /*Ctxt=*/nullptr,
1904             suppressErrorMessages);
1905   yin >> seq;
1906
1907   EXPECT_TRUE(!!yin.error());
1908 }
1909
1910 //
1911 // Test error handling reading built-in float type
1912 //
1913 LLVM_YAML_IS_SEQUENCE_VECTOR(double)
1914 TEST(YAMLIO, TestReadBuiltInTypesDoubleError) {
1915   std::vector<double> seq;
1916   Input yin("---\n"
1917             "- 0.0\n"
1918             "- 1000.1\n"
1919             "- -123.456\n"
1920             "- 1.2.3\n"
1921             "...\n",
1922             /*Ctxt=*/nullptr,
1923             suppressErrorMessages);
1924   yin >> seq;
1925
1926   EXPECT_TRUE(!!yin.error());
1927 }
1928
1929 //
1930 // Test error handling reading built-in Hex8 type
1931 //
1932 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex8)
1933 TEST(YAMLIO, TestReadBuiltInTypesHex8Error) {
1934   std::vector<Hex8> seq;
1935   Input yin("---\n"
1936             "- 0x12\n"
1937             "- 0xFE\n"
1938             "- 0x123\n"
1939             "...\n",
1940             /*Ctxt=*/nullptr,
1941             suppressErrorMessages);
1942   yin >> seq;
1943
1944   EXPECT_TRUE(!!yin.error());
1945 }
1946
1947
1948 //
1949 // Test error handling reading built-in Hex16 type
1950 //
1951 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex16)
1952 TEST(YAMLIO, TestReadBuiltInTypesHex16Error) {
1953   std::vector<Hex16> seq;
1954   Input yin("---\n"
1955             "- 0x0012\n"
1956             "- 0xFEFF\n"
1957             "- 0x12345\n"
1958             "...\n",
1959             /*Ctxt=*/nullptr,
1960             suppressErrorMessages);
1961   yin >> seq;
1962
1963   EXPECT_TRUE(!!yin.error());
1964 }
1965
1966 //
1967 // Test error handling reading built-in Hex32 type
1968 //
1969 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex32)
1970 TEST(YAMLIO, TestReadBuiltInTypesHex32Error) {
1971   std::vector<Hex32> seq;
1972   Input yin("---\n"
1973             "- 0x0012\n"
1974             "- 0xFEFF0000\n"
1975             "- 0x1234556789\n"
1976             "...\n",
1977             /*Ctxt=*/nullptr,
1978             suppressErrorMessages);
1979   yin >> seq;
1980
1981   EXPECT_TRUE(!!yin.error());
1982 }
1983
1984 //
1985 // Test error handling reading built-in Hex64 type
1986 //
1987 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex64)
1988 TEST(YAMLIO, TestReadBuiltInTypesHex64Error) {
1989   std::vector<Hex64> seq;
1990   Input yin("---\n"
1991             "- 0x0012\n"
1992             "- 0xFFEEDDCCBBAA9988\n"
1993             "- 0x12345567890ABCDEF0\n"
1994             "...\n",
1995             /*Ctxt=*/nullptr,
1996             suppressErrorMessages);
1997   yin >> seq;
1998
1999   EXPECT_TRUE(!!yin.error());
2000 }
2001
2002 TEST(YAMLIO, TestMalformedMapFailsGracefully) {
2003   FooBar doc;
2004   {
2005     // We pass the suppressErrorMessages handler to handle the error
2006     // message generated in the constructor of Input.
2007     Input yin("{foo:3, bar: 5}", /*Ctxt=*/nullptr, suppressErrorMessages);
2008     yin >> doc;
2009     EXPECT_TRUE(!!yin.error());
2010   }
2011
2012   {
2013     Input yin("---\nfoo:3\nbar: 5\n...\n", /*Ctxt=*/nullptr, suppressErrorMessages);
2014     yin >> doc;
2015     EXPECT_TRUE(!!yin.error());
2016   }
2017 }
2018
2019 struct OptionalTest {
2020   std::vector<int> Numbers;
2021 };
2022
2023 struct OptionalTestSeq {
2024   std::vector<OptionalTest> Tests;
2025 };
2026
2027 LLVM_YAML_IS_SEQUENCE_VECTOR(OptionalTest)
2028 namespace llvm {
2029 namespace yaml {
2030   template <>
2031   struct MappingTraits<OptionalTest> {
2032     static void mapping(IO& IO, OptionalTest &OT) {
2033       IO.mapOptional("Numbers", OT.Numbers);
2034     }
2035   };
2036
2037   template <>
2038   struct MappingTraits<OptionalTestSeq> {
2039     static void mapping(IO &IO, OptionalTestSeq &OTS) {
2040       IO.mapOptional("Tests", OTS.Tests);
2041     }
2042   };
2043 }
2044 }
2045
2046 TEST(YAMLIO, SequenceElideTest) {
2047   // Test that writing out a purely optional structure with its fields set to
2048   // default followed by other data is properly read back in.
2049   OptionalTestSeq Seq;
2050   OptionalTest One, Two, Three, Four;
2051   int N[] = {1, 2, 3};
2052   Three.Numbers.assign(N, N + 3);
2053   Seq.Tests.push_back(One);
2054   Seq.Tests.push_back(Two);
2055   Seq.Tests.push_back(Three);
2056   Seq.Tests.push_back(Four);
2057
2058   std::string intermediate;
2059   {
2060     llvm::raw_string_ostream ostr(intermediate);
2061     Output yout(ostr);
2062     yout << Seq;
2063   }
2064
2065   Input yin(intermediate);
2066   OptionalTestSeq Seq2;
2067   yin >> Seq2;
2068
2069   EXPECT_FALSE(yin.error());
2070
2071   EXPECT_EQ(4UL, Seq2.Tests.size());
2072
2073   EXPECT_TRUE(Seq2.Tests[0].Numbers.empty());
2074   EXPECT_TRUE(Seq2.Tests[1].Numbers.empty());
2075
2076   EXPECT_EQ(1, Seq2.Tests[2].Numbers[0]);
2077   EXPECT_EQ(2, Seq2.Tests[2].Numbers[1]);
2078   EXPECT_EQ(3, Seq2.Tests[2].Numbers[2]);
2079
2080   EXPECT_TRUE(Seq2.Tests[3].Numbers.empty());
2081 }
2082
2083 TEST(YAMLIO, TestEmptyStringFailsForMapWithRequiredFields) {
2084   FooBar doc;
2085   Input yin("");
2086   yin >> doc;
2087   EXPECT_TRUE(!!yin.error());
2088 }
2089
2090 TEST(YAMLIO, TestEmptyStringSucceedsForMapWithOptionalFields) {
2091   OptionalTest doc;
2092   Input yin("");
2093   yin >> doc;
2094   EXPECT_FALSE(yin.error());
2095 }
2096
2097 TEST(YAMLIO, TestEmptyStringSucceedsForSequence) {
2098   std::vector<uint8_t> seq;
2099   Input yin("", /*Ctxt=*/nullptr, suppressErrorMessages);
2100   yin >> seq;
2101
2102   EXPECT_FALSE(yin.error());
2103   EXPECT_TRUE(seq.empty());
2104 }
2105
2106 struct FlowMap {
2107   llvm::StringRef str1, str2, str3;
2108   FlowMap(llvm::StringRef str1, llvm::StringRef str2, llvm::StringRef str3)
2109     : str1(str1), str2(str2), str3(str3) {}
2110 };
2111
2112 struct FlowSeq {
2113   llvm::StringRef str;
2114   FlowSeq(llvm::StringRef S) : str(S) {}
2115   FlowSeq() = default;
2116 };
2117
2118 namespace llvm {
2119 namespace yaml {
2120   template <>
2121   struct MappingTraits<FlowMap> {
2122     static void mapping(IO &io, FlowMap &fm) {
2123       io.mapRequired("str1", fm.str1);
2124       io.mapRequired("str2", fm.str2);
2125       io.mapRequired("str3", fm.str3);
2126     }
2127
2128     static const bool flow = true;
2129   };
2130
2131 template <>
2132 struct ScalarTraits<FlowSeq> {
2133   static void output(const FlowSeq &value, void*, llvm::raw_ostream &out) {
2134     out << value.str;
2135   }
2136   static StringRef input(StringRef scalar, void*, FlowSeq &value) {
2137     value.str = scalar;
2138     return "";
2139   }
2140
2141   static bool mustQuote(StringRef S) { return false; }
2142 };
2143 }
2144 }
2145
2146 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(FlowSeq)
2147
2148 TEST(YAMLIO, TestWrapFlow) {
2149   std::string out;
2150   llvm::raw_string_ostream ostr(out);
2151   FlowMap Map("This is str1", "This is str2", "This is str3");
2152   std::vector<FlowSeq> Seq;
2153   Seq.emplace_back("This is str1");
2154   Seq.emplace_back("This is str2");
2155   Seq.emplace_back("This is str3");
2156
2157   {
2158     // 20 is just bellow the total length of the first mapping field.
2159     // We should wreap at every element.
2160     Output yout(ostr, nullptr, 15);
2161
2162     yout << Map;
2163     ostr.flush();
2164     EXPECT_EQ(out,
2165               "---\n"
2166               "{ str1: This is str1, \n"
2167               "  str2: This is str2, \n"
2168               "  str3: This is str3 }\n"
2169               "...\n");
2170     out.clear();
2171
2172     yout << Seq;
2173     ostr.flush();
2174     EXPECT_EQ(out,
2175               "---\n"
2176               "[ This is str1, \n"
2177               "  This is str2, \n"
2178               "  This is str3 ]\n"
2179               "...\n");
2180     out.clear();
2181   }
2182   {
2183     // 25 will allow the second field to be output on the first line.
2184     Output yout(ostr, nullptr, 25);
2185
2186     yout << Map;
2187     ostr.flush();
2188     EXPECT_EQ(out,
2189               "---\n"
2190               "{ str1: This is str1, str2: This is str2, \n"
2191               "  str3: This is str3 }\n"
2192               "...\n");
2193     out.clear();
2194
2195     yout << Seq;
2196     ostr.flush();
2197     EXPECT_EQ(out,
2198               "---\n"
2199               "[ This is str1, This is str2, \n"
2200               "  This is str3 ]\n"
2201               "...\n");
2202     out.clear();
2203   }
2204   {
2205     // 0 means no wrapping.
2206     Output yout(ostr, nullptr, 0);
2207
2208     yout << Map;
2209     ostr.flush();
2210     EXPECT_EQ(out,
2211               "---\n"
2212               "{ str1: This is str1, str2: This is str2, str3: This is str3 }\n"
2213               "...\n");
2214     out.clear();
2215
2216     yout << Seq;
2217     ostr.flush();
2218     EXPECT_EQ(out,
2219               "---\n"
2220               "[ This is str1, This is str2, This is str3 ]\n"
2221               "...\n");
2222     out.clear();
2223   }
2224 }