1 //===- unittest/Support/YAMLIOTest.cpp ------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
18 using llvm::yaml::Input;
19 using llvm::yaml::Output;
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;
32 static void suppressErrorMessages(const llvm::SMDiagnostic &, void *) {
37 //===----------------------------------------------------------------------===//
39 //===----------------------------------------------------------------------===//
45 typedef std::vector<FooBar> FooBarSequence;
47 LLVM_YAML_IS_SEQUENCE_VECTOR(FooBar)
49 struct FooBarContainer {
56 struct MappingTraits<FooBar> {
57 static void mapping(IO &io, FooBar& fb) {
58 io.mapRequired("foo", fb.foo);
59 io.mapRequired("bar", fb.bar);
63 template <> struct MappingTraits<FooBarContainer> {
64 static void mapping(IO &io, FooBarContainer &fb) {
65 io.mapRequired("fbs", fb.fbs);
73 // Test the reading of a yaml mapping
75 TEST(YAMLIO, TestMapRead) {
78 Input yin("---\nfoo: 3\nbar: 5\n...\n");
81 EXPECT_FALSE(yin.error());
82 EXPECT_EQ(doc.foo, 3);
83 EXPECT_EQ(doc.bar, 5);
87 Input yin("{foo: 3, bar: 5}");
90 EXPECT_FALSE(yin.error());
91 EXPECT_EQ(doc.foo, 3);
92 EXPECT_EQ(doc.bar, 5);
96 TEST(YAMLIO, TestMalformedMapRead) {
98 Input yin("{foo: 3; bar: 5}", nullptr, suppressErrorMessages);
100 EXPECT_TRUE(!!yin.error());
104 // Test the reading of a yaml sequence of mappings
106 TEST(YAMLIO, TestSequenceMapRead) {
108 Input yin("---\n - foo: 3\n bar: 5\n - foo: 7\n bar: 9\n...\n");
111 EXPECT_FALSE(yin.error());
112 EXPECT_EQ(seq.size(), 2UL);
113 FooBar& map1 = seq[0];
114 FooBar& map2 = seq[1];
115 EXPECT_EQ(map1.foo, 3);
116 EXPECT_EQ(map1.bar, 5);
117 EXPECT_EQ(map2.foo, 7);
118 EXPECT_EQ(map2.bar, 9);
122 // Test the reading of a map containing a yaml sequence of mappings
124 TEST(YAMLIO, TestContainerSequenceMapRead) {
126 FooBarContainer cont;
127 Input yin2("---\nfbs:\n - foo: 3\n bar: 5\n - foo: 7\n bar: 9\n...\n");
130 EXPECT_FALSE(yin2.error());
131 EXPECT_EQ(cont.fbs.size(), 2UL);
132 EXPECT_EQ(cont.fbs[0].foo, 3);
133 EXPECT_EQ(cont.fbs[0].bar, 5);
134 EXPECT_EQ(cont.fbs[1].foo, 7);
135 EXPECT_EQ(cont.fbs[1].bar, 9);
139 FooBarContainer cont;
140 Input yin("---\nfbs:\n...\n");
142 // Okay: Empty node represents an empty array.
143 EXPECT_FALSE(yin.error());
144 EXPECT_EQ(cont.fbs.size(), 0UL);
148 FooBarContainer cont;
149 Input yin("---\nfbs: !!null null\n...\n");
151 // Okay: null represents an empty array.
152 EXPECT_FALSE(yin.error());
153 EXPECT_EQ(cont.fbs.size(), 0UL);
157 FooBarContainer cont;
158 Input yin("---\nfbs: ~\n...\n");
160 // Okay: null represents an empty array.
161 EXPECT_FALSE(yin.error());
162 EXPECT_EQ(cont.fbs.size(), 0UL);
166 FooBarContainer cont;
167 Input yin("---\nfbs: null\n...\n");
169 // Okay: null represents an empty array.
170 EXPECT_FALSE(yin.error());
171 EXPECT_EQ(cont.fbs.size(), 0UL);
176 // Test the reading of a map containing a malformed yaml sequence
178 TEST(YAMLIO, TestMalformedContainerSequenceMapRead) {
180 FooBarContainer cont;
181 Input yin("---\nfbs:\n foo: 3\n bar: 5\n...\n", nullptr,
182 suppressErrorMessages);
184 // Error: fbs is not a sequence.
185 EXPECT_TRUE(!!yin.error());
186 EXPECT_EQ(cont.fbs.size(), 0UL);
190 FooBarContainer cont;
191 Input yin("---\nfbs: 'scalar'\n...\n", nullptr, suppressErrorMessages);
193 // This should be an error.
194 EXPECT_TRUE(!!yin.error());
195 EXPECT_EQ(cont.fbs.size(), 0UL);
200 // Test writing then reading back a sequence of mappings
202 TEST(YAMLIO, TestSequenceMapWriteAndRead) {
203 std::string intermediate;
212 seq.push_back(entry1);
213 seq.push_back(entry2);
215 llvm::raw_string_ostream ostr(intermediate);
221 Input yin(intermediate);
225 EXPECT_FALSE(yin.error());
226 EXPECT_EQ(seq2.size(), 2UL);
227 FooBar& map1 = seq2[0];
228 FooBar& map2 = seq2[1];
229 EXPECT_EQ(map1.foo, 10);
230 EXPECT_EQ(map1.bar, -3);
231 EXPECT_EQ(map2.foo, 257);
232 EXPECT_EQ(map2.bar, 0);
237 //===----------------------------------------------------------------------===//
238 // Test built-in types
239 //===----------------------------------------------------------------------===//
241 struct BuiltInTypes {
264 struct MappingTraits<BuiltInTypes> {
265 static void mapping(IO &io, BuiltInTypes& bt) {
266 io.mapRequired("str", bt.str);
267 io.mapRequired("stdstr", bt.stdstr);
268 io.mapRequired("u64", bt.u64);
269 io.mapRequired("u32", bt.u32);
270 io.mapRequired("u16", bt.u16);
271 io.mapRequired("u8", bt.u8);
272 io.mapRequired("b", bt.b);
273 io.mapRequired("s64", bt.s64);
274 io.mapRequired("s32", bt.s32);
275 io.mapRequired("s16", bt.s16);
276 io.mapRequired("s8", bt.s8);
277 io.mapRequired("f", bt.f);
278 io.mapRequired("d", bt.d);
279 io.mapRequired("h8", bt.h8);
280 io.mapRequired("h16", bt.h16);
281 io.mapRequired("h32", bt.h32);
282 io.mapRequired("h64", bt.h64);
290 // Test the reading of all built-in scalar conversions
292 TEST(YAMLIO, TestReadBuiltInTypes) {
296 "stdstr: hello where?\n"
311 "h64: 0xFEDCBA9876543210\n"
315 EXPECT_FALSE(yin.error());
316 EXPECT_TRUE(map.str.equals("hello there"));
317 EXPECT_TRUE(map.stdstr == "hello where?");
318 EXPECT_EQ(map.u64, 5000000000ULL);
319 EXPECT_EQ(map.u32, 4000000000U);
320 EXPECT_EQ(map.u16, 65000);
321 EXPECT_EQ(map.u8, 255);
322 EXPECT_EQ(map.b, false);
323 EXPECT_EQ(map.s64, -5000000000LL);
324 EXPECT_EQ(map.s32, -2000000000L);
325 EXPECT_EQ(map.s16, -32000);
326 EXPECT_EQ(map.s8, -127);
327 EXPECT_EQ(map.f, 137.125);
328 EXPECT_EQ(map.d, -2.8625);
329 EXPECT_EQ(map.h8, Hex8(255));
330 EXPECT_EQ(map.h16, Hex16(0x8765));
331 EXPECT_EQ(map.h32, Hex32(0xFEDCBA98));
332 EXPECT_EQ(map.h64, Hex64(0xFEDCBA9876543210LL));
337 // Test writing then reading back all built-in scalar types
339 TEST(YAMLIO, TestReadWriteBuiltInTypes) {
340 std::string intermediate;
344 map.stdstr = "three four";
345 map.u64 = 6000000000ULL;
346 map.u32 = 3000000000U;
350 map.s64 = -6000000000LL;
351 map.s32 = -2000000000;
358 map.h32 = 3000000000U;
359 map.h64 = 6000000000LL;
361 llvm::raw_string_ostream ostr(intermediate);
367 Input yin(intermediate);
371 EXPECT_FALSE(yin.error());
372 EXPECT_TRUE(map.str.equals("one two"));
373 EXPECT_TRUE(map.stdstr == "three four");
374 EXPECT_EQ(map.u64, 6000000000ULL);
375 EXPECT_EQ(map.u32, 3000000000U);
376 EXPECT_EQ(map.u16, 50000);
377 EXPECT_EQ(map.u8, 254);
378 EXPECT_EQ(map.b, true);
379 EXPECT_EQ(map.s64, -6000000000LL);
380 EXPECT_EQ(map.s32, -2000000000L);
381 EXPECT_EQ(map.s16, -32000);
382 EXPECT_EQ(map.s8, -128);
383 EXPECT_EQ(map.f, 3.25);
384 EXPECT_EQ(map.d, -2.8625);
385 EXPECT_EQ(map.h8, Hex8(254));
386 EXPECT_EQ(map.h16, Hex16(50000));
387 EXPECT_EQ(map.h32, Hex32(3000000000U));
388 EXPECT_EQ(map.h64, Hex64(6000000000LL));
393 llvm::StringRef str1;
394 llvm::StringRef str2;
395 llvm::StringRef str3;
396 llvm::StringRef str4;
397 llvm::StringRef str5;
398 llvm::StringRef str6;
399 llvm::StringRef str7;
400 llvm::StringRef str8;
401 llvm::StringRef str9;
402 llvm::StringRef str10;
403 llvm::StringRef str11;
413 std::string stdstr10;
414 std::string stdstr11;
420 struct MappingTraits<StringTypes> {
421 static void mapping(IO &io, StringTypes& st) {
422 io.mapRequired("str1", st.str1);
423 io.mapRequired("str2", st.str2);
424 io.mapRequired("str3", st.str3);
425 io.mapRequired("str4", st.str4);
426 io.mapRequired("str5", st.str5);
427 io.mapRequired("str6", st.str6);
428 io.mapRequired("str7", st.str7);
429 io.mapRequired("str8", st.str8);
430 io.mapRequired("str9", st.str9);
431 io.mapRequired("str10", st.str10);
432 io.mapRequired("str11", st.str11);
433 io.mapRequired("stdstr1", st.stdstr1);
434 io.mapRequired("stdstr2", st.stdstr2);
435 io.mapRequired("stdstr3", st.stdstr3);
436 io.mapRequired("stdstr4", st.stdstr4);
437 io.mapRequired("stdstr5", st.stdstr5);
438 io.mapRequired("stdstr6", st.stdstr6);
439 io.mapRequired("stdstr7", st.stdstr7);
440 io.mapRequired("stdstr8", st.stdstr8);
441 io.mapRequired("stdstr9", st.stdstr9);
442 io.mapRequired("stdstr10", st.stdstr10);
443 io.mapRequired("stdstr11", st.stdstr11);
449 TEST(YAMLIO, TestReadWriteStringTypes) {
450 std::string intermediate;
458 map.str6 = "0000000004000000";
462 map.str10 = "0.2e20";
464 map.stdstr1 = "'eee";
465 map.stdstr2 = "\"fff";
466 map.stdstr3 = "`ggg";
467 map.stdstr4 = "@hhh";
469 map.stdstr6 = "0000000004000000";
470 map.stdstr7 = "true";
471 map.stdstr8 = "FALSE";
473 map.stdstr10 = "0.2e20";
474 map.stdstr11 = "0x30";
476 llvm::raw_string_ostream ostr(intermediate);
481 llvm::StringRef flowOut(intermediate);
482 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'''aaa"));
483 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'\"bbb'"));
484 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'`ccc'"));
485 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'@ddd'"));
486 EXPECT_NE(llvm::StringRef::npos, flowOut.find("''\n"));
487 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0000000004000000'\n"));
488 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'true'\n"));
489 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'FALSE'\n"));
490 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'~'\n"));
491 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0.2e20'\n"));
492 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'0x30'\n"));
493 EXPECT_NE(std::string::npos, flowOut.find("'''eee"));
494 EXPECT_NE(std::string::npos, flowOut.find("'\"fff'"));
495 EXPECT_NE(std::string::npos, flowOut.find("'`ggg'"));
496 EXPECT_NE(std::string::npos, flowOut.find("'@hhh'"));
497 EXPECT_NE(std::string::npos, flowOut.find("''\n"));
498 EXPECT_NE(std::string::npos, flowOut.find("'0000000004000000'\n"));
501 Input yin(intermediate);
505 EXPECT_FALSE(yin.error());
506 EXPECT_TRUE(map.str1.equals("'aaa"));
507 EXPECT_TRUE(map.str2.equals("\"bbb"));
508 EXPECT_TRUE(map.str3.equals("`ccc"));
509 EXPECT_TRUE(map.str4.equals("@ddd"));
510 EXPECT_TRUE(map.str5.equals(""));
511 EXPECT_TRUE(map.str6.equals("0000000004000000"));
512 EXPECT_TRUE(map.stdstr1 == "'eee");
513 EXPECT_TRUE(map.stdstr2 == "\"fff");
514 EXPECT_TRUE(map.stdstr3 == "`ggg");
515 EXPECT_TRUE(map.stdstr4 == "@hhh");
516 EXPECT_TRUE(map.stdstr5 == "");
517 EXPECT_TRUE(map.stdstr6 == "0000000004000000");
521 //===----------------------------------------------------------------------===//
522 // Test ScalarEnumerationTraits
523 //===----------------------------------------------------------------------===//
544 struct ScalarEnumerationTraits<Colors> {
545 static void enumeration(IO &io, Colors &value) {
546 io.enumCase(value, "red", cRed);
547 io.enumCase(value, "blue", cBlue);
548 io.enumCase(value, "green", cGreen);
549 io.enumCase(value, "yellow",cYellow);
553 struct MappingTraits<ColorMap> {
554 static void mapping(IO &io, ColorMap& c) {
555 io.mapRequired("c1", c.c1);
556 io.mapRequired("c2", c.c2);
557 io.mapRequired("c3", c.c3);
558 io.mapOptional("c4", c.c4, cBlue); // supplies default
559 io.mapOptional("c5", c.c5, cYellow); // supplies default
560 io.mapOptional("c6", c.c6, cRed); // supplies default
568 // Test reading enumerated scalars
570 TEST(YAMLIO, TestEnumRead) {
580 EXPECT_FALSE(yin.error());
581 EXPECT_EQ(cBlue, map.c1);
582 EXPECT_EQ(cRed, map.c2);
583 EXPECT_EQ(cGreen, map.c3);
584 EXPECT_EQ(cBlue, map.c4); // tests default
585 EXPECT_EQ(cYellow,map.c5); // tests overridden
586 EXPECT_EQ(cRed, map.c6); // tests default
591 //===----------------------------------------------------------------------===//
592 // Test ScalarBitSetTraits
593 //===----------------------------------------------------------------------===//
602 inline MyFlags operator|(MyFlags a, MyFlags b) {
603 return static_cast<MyFlags>(
604 static_cast<uint32_t>(a) | static_cast<uint32_t>(b));
618 struct ScalarBitSetTraits<MyFlags> {
619 static void bitset(IO &io, MyFlags &value) {
620 io.bitSetCase(value, "big", flagBig);
621 io.bitSetCase(value, "flat", flagFlat);
622 io.bitSetCase(value, "round", flagRound);
623 io.bitSetCase(value, "pointy",flagPointy);
627 struct MappingTraits<FlagsMap> {
628 static void mapping(IO &io, FlagsMap& c) {
629 io.mapRequired("f1", c.f1);
630 io.mapRequired("f2", c.f2);
631 io.mapRequired("f3", c.f3);
632 io.mapOptional("f4", c.f4, MyFlags(flagRound));
640 // Test reading flow sequence representing bit-mask values
642 TEST(YAMLIO, TestFlagsRead) {
646 "f2: [ round, flat ]\n"
651 EXPECT_FALSE(yin.error());
652 EXPECT_EQ(flagBig, map.f1);
653 EXPECT_EQ(flagRound|flagFlat, map.f2);
654 EXPECT_EQ(flagNone, map.f3); // check empty set
655 EXPECT_EQ(flagRound, map.f4); // check optional key
660 // Test writing then reading back bit-mask values
662 TEST(YAMLIO, TestReadWriteFlags) {
663 std::string intermediate;
667 map.f2 = flagRound | flagFlat;
671 llvm::raw_string_ostream ostr(intermediate);
677 Input yin(intermediate);
681 EXPECT_FALSE(yin.error());
682 EXPECT_EQ(flagBig, map2.f1);
683 EXPECT_EQ(flagRound|flagFlat, map2.f2);
684 EXPECT_EQ(flagNone, map2.f3);
685 //EXPECT_EQ(flagRound, map2.f4); // check optional key
691 //===----------------------------------------------------------------------===//
693 //===----------------------------------------------------------------------===//
695 struct MyCustomType {
700 struct MyCustomTypeMap {
710 struct MappingTraits<MyCustomTypeMap> {
711 static void mapping(IO &io, MyCustomTypeMap& s) {
712 io.mapRequired("f1", s.f1);
713 io.mapRequired("f2", s.f2);
714 io.mapRequired("f3", s.f3);
717 // MyCustomType is formatted as a yaml scalar. A value of
718 // {length=3, width=4} would be represented in yaml as "3 by 4".
720 struct ScalarTraits<MyCustomType> {
721 static void output(const MyCustomType &value, void* ctxt, llvm::raw_ostream &out) {
722 out << llvm::format("%d by %d", value.length, value.width);
724 static StringRef input(StringRef scalar, void* ctxt, MyCustomType &value) {
725 size_t byStart = scalar.find("by");
726 if ( byStart != StringRef::npos ) {
727 StringRef lenStr = scalar.slice(0, byStart);
728 lenStr = lenStr.rtrim();
729 if ( lenStr.getAsInteger(0, value.length) ) {
730 return "malformed length";
732 StringRef widthStr = scalar.drop_front(byStart+2);
733 widthStr = widthStr.ltrim();
734 if ( widthStr.getAsInteger(0, value.width) ) {
735 return "malformed width";
740 return "malformed by";
743 static bool mustQuote(StringRef) { return true; }
750 // Test writing then reading back custom values
752 TEST(YAMLIO, TestReadWriteMyCustomType) {
753 std::string intermediate;
762 llvm::raw_string_ostream ostr(intermediate);
768 Input yin(intermediate);
769 MyCustomTypeMap map2;
772 EXPECT_FALSE(yin.error());
773 EXPECT_EQ(1, map2.f1.length);
774 EXPECT_EQ(4, map2.f1.width);
775 EXPECT_EQ(100, map2.f2.length);
776 EXPECT_EQ(400, map2.f2.width);
777 EXPECT_EQ(10, map2.f3);
782 //===----------------------------------------------------------------------===//
783 // Test flow sequences
784 //===----------------------------------------------------------------------===//
786 LLVM_YAML_STRONG_TYPEDEF(int, MyNumber)
787 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyNumber)
788 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::StringRef)
793 struct ScalarTraits<MyNumber> {
794 static void output(const MyNumber &value, void *, llvm::raw_ostream &out) {
798 static StringRef input(StringRef scalar, void *, MyNumber &value) {
800 if ( getAsSignedInteger(scalar, 0, n) )
801 return "invalid number";
806 static bool mustQuote(StringRef) { return false; }
811 struct NameAndNumbers {
812 llvm::StringRef name;
813 std::vector<llvm::StringRef> strings;
814 std::vector<MyNumber> single;
815 std::vector<MyNumber> numbers;
821 struct MappingTraits<NameAndNumbers> {
822 static void mapping(IO &io, NameAndNumbers& nn) {
823 io.mapRequired("name", nn.name);
824 io.mapRequired("strings", nn.strings);
825 io.mapRequired("single", nn.single);
826 io.mapRequired("numbers", nn.numbers);
834 // Test writing then reading back custom values
836 TEST(YAMLIO, TestReadWriteMyFlowSequence) {
837 std::string intermediate;
841 map.strings.push_back(llvm::StringRef("one"));
842 map.strings.push_back(llvm::StringRef("two"));
843 map.single.push_back(1);
844 map.numbers.push_back(10);
845 map.numbers.push_back(-30);
846 map.numbers.push_back(1024);
848 llvm::raw_string_ostream ostr(intermediate);
852 // Verify sequences were written in flow style
854 llvm::StringRef flowOut(intermediate);
855 EXPECT_NE(llvm::StringRef::npos, flowOut.find("one, two"));
856 EXPECT_NE(llvm::StringRef::npos, flowOut.find("10, -30, 1024"));
860 Input yin(intermediate);
864 EXPECT_FALSE(yin.error());
865 EXPECT_TRUE(map2.name.equals("hello"));
866 EXPECT_EQ(map2.strings.size(), 2UL);
867 EXPECT_TRUE(map2.strings[0].equals("one"));
868 EXPECT_TRUE(map2.strings[1].equals("two"));
869 EXPECT_EQ(map2.single.size(), 1UL);
870 EXPECT_EQ(1, map2.single[0]);
871 EXPECT_EQ(map2.numbers.size(), 3UL);
872 EXPECT_EQ(10, map2.numbers[0]);
873 EXPECT_EQ(-30, map2.numbers[1]);
874 EXPECT_EQ(1024, map2.numbers[2]);
879 //===----------------------------------------------------------------------===//
880 // Test normalizing/denormalizing
881 //===----------------------------------------------------------------------===//
883 LLVM_YAML_STRONG_TYPEDEF(uint32_t, TotalSeconds)
885 typedef std::vector<TotalSeconds> SecondsSequence;
887 LLVM_YAML_IS_SEQUENCE_VECTOR(TotalSeconds)
893 struct MappingTraits<TotalSeconds> {
895 class NormalizedSeconds {
897 NormalizedSeconds(IO &io)
898 : hours(0), minutes(0), seconds(0) {
900 NormalizedSeconds(IO &, TotalSeconds &secs)
902 minutes((secs - (hours*3600))/60),
905 TotalSeconds denormalize(IO &) {
906 return TotalSeconds(hours*3600 + minutes*60 + seconds);
914 static void mapping(IO &io, TotalSeconds &secs) {
915 MappingNormalization<NormalizedSeconds, TotalSeconds> keys(io, secs);
917 io.mapOptional("hours", keys->hours, (uint32_t)0);
918 io.mapOptional("minutes", keys->minutes, (uint8_t)0);
919 io.mapRequired("seconds", keys->seconds);
927 // Test the reading of a yaml sequence of mappings
929 TEST(YAMLIO, TestReadMySecondsSequence) {
931 Input yin("---\n - hours: 1\n seconds: 5\n - seconds: 59\n...\n");
934 EXPECT_FALSE(yin.error());
935 EXPECT_EQ(seq.size(), 2UL);
936 EXPECT_EQ(seq[0], 3605U);
937 EXPECT_EQ(seq[1], 59U);
942 // Test writing then reading back custom values
944 TEST(YAMLIO, TestReadWriteMySecondsSequence) {
945 std::string intermediate;
952 llvm::raw_string_ostream ostr(intermediate);
957 Input yin(intermediate);
958 SecondsSequence seq2;
961 EXPECT_FALSE(yin.error());
962 EXPECT_EQ(seq2.size(), 3UL);
963 EXPECT_EQ(seq2[0], 4000U);
964 EXPECT_EQ(seq2[1], 500U);
965 EXPECT_EQ(seq2[2], 59U);
970 //===----------------------------------------------------------------------===//
971 // Test dynamic typing
972 //===----------------------------------------------------------------------===//
991 struct KindAndFlags {
992 KindAndFlags() : kind(kindA), flags(0) { }
993 KindAndFlags(Kind k, uint32_t f) : kind(k), flags(f) { }
998 typedef std::vector<KindAndFlags> KindAndFlagsSequence;
1000 LLVM_YAML_IS_SEQUENCE_VECTOR(KindAndFlags)
1005 struct ScalarEnumerationTraits<AFlags> {
1006 static void enumeration(IO &io, AFlags &value) {
1007 io.enumCase(value, "a1", a1);
1008 io.enumCase(value, "a2", a2);
1009 io.enumCase(value, "a3", a3);
1013 struct ScalarEnumerationTraits<BFlags> {
1014 static void enumeration(IO &io, BFlags &value) {
1015 io.enumCase(value, "b1", b1);
1016 io.enumCase(value, "b2", b2);
1017 io.enumCase(value, "b3", b3);
1021 struct ScalarEnumerationTraits<Kind> {
1022 static void enumeration(IO &io, Kind &value) {
1023 io.enumCase(value, "A", kindA);
1024 io.enumCase(value, "B", kindB);
1028 struct MappingTraits<KindAndFlags> {
1029 static void mapping(IO &io, KindAndFlags& kf) {
1030 io.mapRequired("kind", kf.kind);
1031 // Type of "flags" field varies depending on "kind" field.
1032 // Use memcpy here to avoid breaking strict aliasing rules.
1033 if (kf.kind == kindA) {
1034 AFlags aflags = static_cast<AFlags>(kf.flags);
1035 io.mapRequired("flags", aflags);
1038 BFlags bflags = static_cast<BFlags>(kf.flags);
1039 io.mapRequired("flags", bflags);
1049 // Test the reading of a yaml sequence dynamic types
1051 TEST(YAMLIO, TestReadKindAndFlagsSequence) {
1052 KindAndFlagsSequence seq;
1053 Input yin("---\n - kind: A\n flags: a2\n - kind: B\n flags: b1\n...\n");
1056 EXPECT_FALSE(yin.error());
1057 EXPECT_EQ(seq.size(), 2UL);
1058 EXPECT_EQ(seq[0].kind, kindA);
1059 EXPECT_EQ(seq[0].flags, (uint32_t)a2);
1060 EXPECT_EQ(seq[1].kind, kindB);
1061 EXPECT_EQ(seq[1].flags, (uint32_t)b1);
1065 // Test writing then reading back dynamic types
1067 TEST(YAMLIO, TestReadWriteKindAndFlagsSequence) {
1068 std::string intermediate;
1070 KindAndFlagsSequence seq;
1071 seq.push_back(KindAndFlags(kindA,a1));
1072 seq.push_back(KindAndFlags(kindB,b1));
1073 seq.push_back(KindAndFlags(kindA,a2));
1074 seq.push_back(KindAndFlags(kindB,b2));
1075 seq.push_back(KindAndFlags(kindA,a3));
1077 llvm::raw_string_ostream ostr(intermediate);
1082 Input yin(intermediate);
1083 KindAndFlagsSequence seq2;
1086 EXPECT_FALSE(yin.error());
1087 EXPECT_EQ(seq2.size(), 5UL);
1088 EXPECT_EQ(seq2[0].kind, kindA);
1089 EXPECT_EQ(seq2[0].flags, (uint32_t)a1);
1090 EXPECT_EQ(seq2[1].kind, kindB);
1091 EXPECT_EQ(seq2[1].flags, (uint32_t)b1);
1092 EXPECT_EQ(seq2[2].kind, kindA);
1093 EXPECT_EQ(seq2[2].flags, (uint32_t)a2);
1094 EXPECT_EQ(seq2[3].kind, kindB);
1095 EXPECT_EQ(seq2[3].flags, (uint32_t)b2);
1096 EXPECT_EQ(seq2[4].kind, kindA);
1097 EXPECT_EQ(seq2[4].flags, (uint32_t)a3);
1102 //===----------------------------------------------------------------------===//
1103 // Test document list
1104 //===----------------------------------------------------------------------===//
1110 typedef std::vector<FooBarMap> FooBarMapDocumentList;
1112 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(FooBarMap)
1118 struct MappingTraits<FooBarMap> {
1119 static void mapping(IO &io, FooBarMap& fb) {
1120 io.mapRequired("foo", fb.foo);
1121 io.mapRequired("bar", fb.bar);
1129 // Test the reading of a yaml mapping
1131 TEST(YAMLIO, TestDocRead) {
1133 Input yin("---\nfoo: 3\nbar: 5\n...\n");
1136 EXPECT_FALSE(yin.error());
1137 EXPECT_EQ(doc.foo, 3);
1138 EXPECT_EQ(doc.bar,5);
1144 // Test writing then reading back a sequence of mappings
1146 TEST(YAMLIO, TestSequenceDocListWriteAndRead) {
1147 std::string intermediate;
1155 std::vector<FooBarMap> docList;
1156 docList.push_back(doc1);
1157 docList.push_back(doc2);
1159 llvm::raw_string_ostream ostr(intermediate);
1166 Input yin(intermediate);
1167 std::vector<FooBarMap> docList2;
1170 EXPECT_FALSE(yin.error());
1171 EXPECT_EQ(docList2.size(), 2UL);
1172 FooBarMap& map1 = docList2[0];
1173 FooBarMap& map2 = docList2[1];
1174 EXPECT_EQ(map1.foo, 10);
1175 EXPECT_EQ(map1.bar, -3);
1176 EXPECT_EQ(map2.foo, 257);
1177 EXPECT_EQ(map2.bar, 0);
1181 //===----------------------------------------------------------------------===//
1182 // Test document tags
1183 //===----------------------------------------------------------------------===//
1186 MyDouble() : value(0.0) { }
1187 MyDouble(double x) : value(x) { }
1191 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyDouble)
1197 struct MappingTraits<MyDouble> {
1198 static void mapping(IO &io, MyDouble &d) {
1199 if (io.mapTag("!decimal", true)) {
1200 mappingDecimal(io, d);
1201 } else if (io.mapTag("!fraction")) {
1202 mappingFraction(io, d);
1205 static void mappingDecimal(IO &io, MyDouble &d) {
1206 io.mapRequired("value", d.value);
1208 static void mappingFraction(IO &io, MyDouble &d) {
1210 io.mapRequired("numerator", num);
1211 io.mapRequired("denominator", denom);
1212 // convert fraction to double
1213 d.value = num/denom;
1221 // Test the reading of two different tagged yaml documents.
1223 TEST(YAMLIO, TestTaggedDocuments) {
1224 std::vector<MyDouble> docList;
1225 Input yin("--- !decimal\nvalue: 3.0\n"
1226 "--- !fraction\nnumerator: 9.0\ndenominator: 2\n...\n");
1228 EXPECT_FALSE(yin.error());
1229 EXPECT_EQ(docList.size(), 2UL);
1230 EXPECT_EQ(docList[0].value, 3.0);
1231 EXPECT_EQ(docList[1].value, 4.5);
1237 // Test writing then reading back tagged documents
1239 TEST(YAMLIO, TestTaggedDocumentsWriteAndRead) {
1240 std::string intermediate;
1244 std::vector<MyDouble> docList;
1245 docList.push_back(a);
1246 docList.push_back(b);
1248 llvm::raw_string_ostream ostr(intermediate);
1254 Input yin(intermediate);
1255 std::vector<MyDouble> docList2;
1258 EXPECT_FALSE(yin.error());
1259 EXPECT_EQ(docList2.size(), 2UL);
1260 EXPECT_EQ(docList2[0].value, 10.25);
1261 EXPECT_EQ(docList2[1].value, -3.75);
1266 //===----------------------------------------------------------------------===//
1267 // Test mapping validation
1268 //===----------------------------------------------------------------------===//
1270 struct MyValidation {
1274 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyValidation)
1279 struct MappingTraits<MyValidation> {
1280 static void mapping(IO &io, MyValidation &d) {
1281 io.mapRequired("value", d.value);
1283 static StringRef validate(IO &io, MyValidation &d) {
1285 return "negative value";
1294 // Test that validate() is called and complains about the negative value.
1296 TEST(YAMLIO, TestValidatingInput) {
1297 std::vector<MyValidation> docList;
1298 Input yin("--- \nvalue: 3.0\n"
1299 "--- \nvalue: -1.0\n...\n",
1300 nullptr, suppressErrorMessages);
1302 EXPECT_TRUE(!!yin.error());
1306 //===----------------------------------------------------------------------===//
1307 // Test error handling
1308 //===----------------------------------------------------------------------===//
1311 // Test error handling of unknown enumerated scalar
1313 TEST(YAMLIO, TestColorsReadError) {
1321 suppressErrorMessages);
1323 EXPECT_TRUE(!!yin.error());
1328 // Test error handling of flow sequence with unknown value
1330 TEST(YAMLIO, TestFlagsReadError) {
1334 "f2: [ round, hollow ]\n"
1338 suppressErrorMessages);
1341 EXPECT_TRUE(!!yin.error());
1346 // Test error handling reading built-in uint8_t type
1348 LLVM_YAML_IS_SEQUENCE_VECTOR(uint8_t)
1349 TEST(YAMLIO, TestReadBuiltInTypesUint8Error) {
1350 std::vector<uint8_t> seq;
1357 suppressErrorMessages);
1360 EXPECT_TRUE(!!yin.error());
1365 // Test error handling reading built-in uint16_t type
1367 LLVM_YAML_IS_SEQUENCE_VECTOR(uint16_t)
1368 TEST(YAMLIO, TestReadBuiltInTypesUint16Error) {
1369 std::vector<uint16_t> seq;
1376 suppressErrorMessages);
1379 EXPECT_TRUE(!!yin.error());
1384 // Test error handling reading built-in uint32_t type
1386 LLVM_YAML_IS_SEQUENCE_VECTOR(uint32_t)
1387 TEST(YAMLIO, TestReadBuiltInTypesUint32Error) {
1388 std::vector<uint32_t> seq;
1395 suppressErrorMessages);
1398 EXPECT_TRUE(!!yin.error());
1403 // Test error handling reading built-in uint64_t type
1405 LLVM_YAML_IS_SEQUENCE_VECTOR(uint64_t)
1406 TEST(YAMLIO, TestReadBuiltInTypesUint64Error) {
1407 std::vector<uint64_t> seq;
1409 "- 18446744073709551615\n"
1411 "- 19446744073709551615\n"
1414 suppressErrorMessages);
1417 EXPECT_TRUE(!!yin.error());
1422 // Test error handling reading built-in int8_t type
1424 LLVM_YAML_IS_SEQUENCE_VECTOR(int8_t)
1425 TEST(YAMLIO, TestReadBuiltInTypesint8OverError) {
1426 std::vector<int8_t> seq;
1434 suppressErrorMessages);
1437 EXPECT_TRUE(!!yin.error());
1441 // Test error handling reading built-in int8_t type
1443 TEST(YAMLIO, TestReadBuiltInTypesint8UnderError) {
1444 std::vector<int8_t> seq;
1452 suppressErrorMessages);
1455 EXPECT_TRUE(!!yin.error());
1460 // Test error handling reading built-in int16_t type
1462 LLVM_YAML_IS_SEQUENCE_VECTOR(int16_t)
1463 TEST(YAMLIO, TestReadBuiltInTypesint16UnderError) {
1464 std::vector<int16_t> seq;
1472 suppressErrorMessages);
1475 EXPECT_TRUE(!!yin.error());
1480 // Test error handling reading built-in int16_t type
1482 TEST(YAMLIO, TestReadBuiltInTypesint16OverError) {
1483 std::vector<int16_t> seq;
1491 suppressErrorMessages);
1494 EXPECT_TRUE(!!yin.error());
1499 // Test error handling reading built-in int32_t type
1501 LLVM_YAML_IS_SEQUENCE_VECTOR(int32_t)
1502 TEST(YAMLIO, TestReadBuiltInTypesint32UnderError) {
1503 std::vector<int32_t> seq;
1511 suppressErrorMessages);
1514 EXPECT_TRUE(!!yin.error());
1518 // Test error handling reading built-in int32_t type
1520 TEST(YAMLIO, TestReadBuiltInTypesint32OverError) {
1521 std::vector<int32_t> seq;
1529 suppressErrorMessages);
1532 EXPECT_TRUE(!!yin.error());
1537 // Test error handling reading built-in int64_t type
1539 LLVM_YAML_IS_SEQUENCE_VECTOR(int64_t)
1540 TEST(YAMLIO, TestReadBuiltInTypesint64UnderError) {
1541 std::vector<int64_t> seq;
1543 "- -9223372036854775808\n"
1545 "- 9223372036854775807\n"
1546 "- -9223372036854775809\n"
1549 suppressErrorMessages);
1552 EXPECT_TRUE(!!yin.error());
1556 // Test error handling reading built-in int64_t type
1558 TEST(YAMLIO, TestReadBuiltInTypesint64OverError) {
1559 std::vector<int64_t> seq;
1561 "- -9223372036854775808\n"
1563 "- 9223372036854775807\n"
1564 "- 9223372036854775809\n"
1567 suppressErrorMessages);
1570 EXPECT_TRUE(!!yin.error());
1574 // Test error handling reading built-in float type
1576 LLVM_YAML_IS_SEQUENCE_VECTOR(float)
1577 TEST(YAMLIO, TestReadBuiltInTypesFloatError) {
1578 std::vector<float> seq;
1586 suppressErrorMessages);
1589 EXPECT_TRUE(!!yin.error());
1593 // Test error handling reading built-in float type
1595 LLVM_YAML_IS_SEQUENCE_VECTOR(double)
1596 TEST(YAMLIO, TestReadBuiltInTypesDoubleError) {
1597 std::vector<double> seq;
1605 suppressErrorMessages);
1608 EXPECT_TRUE(!!yin.error());
1612 // Test error handling reading built-in Hex8 type
1614 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex8)
1615 TEST(YAMLIO, TestReadBuiltInTypesHex8Error) {
1616 std::vector<Hex8> seq;
1623 suppressErrorMessages);
1626 EXPECT_TRUE(!!yin.error());
1631 // Test error handling reading built-in Hex16 type
1633 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex16)
1634 TEST(YAMLIO, TestReadBuiltInTypesHex16Error) {
1635 std::vector<Hex16> seq;
1642 suppressErrorMessages);
1645 EXPECT_TRUE(!!yin.error());
1649 // Test error handling reading built-in Hex32 type
1651 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex32)
1652 TEST(YAMLIO, TestReadBuiltInTypesHex32Error) {
1653 std::vector<Hex32> seq;
1660 suppressErrorMessages);
1663 EXPECT_TRUE(!!yin.error());
1667 // Test error handling reading built-in Hex64 type
1669 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex64)
1670 TEST(YAMLIO, TestReadBuiltInTypesHex64Error) {
1671 std::vector<Hex64> seq;
1674 "- 0xFFEEDDCCBBAA9988\n"
1675 "- 0x12345567890ABCDEF0\n"
1678 suppressErrorMessages);
1681 EXPECT_TRUE(!!yin.error());
1684 TEST(YAMLIO, TestMalformedMapFailsGracefully) {
1687 // We pass the suppressErrorMessages handler to handle the error
1688 // message generated in the constructor of Input.
1689 Input yin("{foo:3, bar: 5}", /*Ctxt=*/nullptr, suppressErrorMessages);
1691 EXPECT_TRUE(!!yin.error());
1695 Input yin("---\nfoo:3\nbar: 5\n...\n", /*Ctxt=*/nullptr, suppressErrorMessages);
1697 EXPECT_TRUE(!!yin.error());
1701 struct OptionalTest {
1702 std::vector<int> Numbers;
1705 struct OptionalTestSeq {
1706 std::vector<OptionalTest> Tests;
1709 LLVM_YAML_IS_SEQUENCE_VECTOR(OptionalTest)
1713 struct MappingTraits<OptionalTest> {
1714 static void mapping(IO& IO, OptionalTest &OT) {
1715 IO.mapOptional("Numbers", OT.Numbers);
1720 struct MappingTraits<OptionalTestSeq> {
1721 static void mapping(IO &IO, OptionalTestSeq &OTS) {
1722 IO.mapOptional("Tests", OTS.Tests);
1728 TEST(YAMLIO, SequenceElideTest) {
1729 // Test that writing out a purely optional structure with its fields set to
1730 // default followed by other data is properly read back in.
1731 OptionalTestSeq Seq;
1732 OptionalTest One, Two, Three, Four;
1733 int N[] = {1, 2, 3};
1734 Three.Numbers.assign(N, N + 3);
1735 Seq.Tests.push_back(One);
1736 Seq.Tests.push_back(Two);
1737 Seq.Tests.push_back(Three);
1738 Seq.Tests.push_back(Four);
1740 std::string intermediate;
1742 llvm::raw_string_ostream ostr(intermediate);
1747 Input yin(intermediate);
1748 OptionalTestSeq Seq2;
1751 EXPECT_FALSE(yin.error());
1753 EXPECT_EQ(4UL, Seq2.Tests.size());
1755 EXPECT_TRUE(Seq2.Tests[0].Numbers.empty());
1756 EXPECT_TRUE(Seq2.Tests[1].Numbers.empty());
1758 EXPECT_EQ(1, Seq2.Tests[2].Numbers[0]);
1759 EXPECT_EQ(2, Seq2.Tests[2].Numbers[1]);
1760 EXPECT_EQ(3, Seq2.Tests[2].Numbers[2]);
1762 EXPECT_TRUE(Seq2.Tests[3].Numbers.empty());
1765 TEST(YAMLIO, TestEmptyStringFailsForMapWithRequiredFields) {
1769 EXPECT_TRUE(!!yin.error());
1772 TEST(YAMLIO, TestEmptyStringSucceedsForMapWithOptionalFields) {
1776 EXPECT_FALSE(yin.error());
1779 TEST(YAMLIO, TestEmptyStringSucceedsForSequence) {
1780 std::vector<uint8_t> seq;
1781 Input yin("", /*Ctxt=*/nullptr, suppressErrorMessages);
1784 EXPECT_FALSE(yin.error());
1785 EXPECT_TRUE(seq.empty());