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)
53 struct MappingTraits<FooBar> {
54 static void mapping(IO &io, FooBar& fb) {
55 io.mapRequired("foo", fb.foo);
56 io.mapRequired("bar", fb.bar);
64 // Test the reading of a yaml mapping
66 TEST(YAMLIO, TestMapRead) {
69 Input yin("---\nfoo: 3\nbar: 5\n...\n");
72 EXPECT_FALSE(yin.error());
73 EXPECT_EQ(doc.foo, 3);
74 EXPECT_EQ(doc.bar, 5);
78 Input yin("{foo: 3, bar: 5}");
81 EXPECT_FALSE(yin.error());
82 EXPECT_EQ(doc.foo, 3);
83 EXPECT_EQ(doc.bar, 5);
88 // Test the reading of a yaml sequence of mappings
90 TEST(YAMLIO, TestSequenceMapRead) {
92 Input yin("---\n - foo: 3\n bar: 5\n - foo: 7\n bar: 9\n...\n");
95 EXPECT_FALSE(yin.error());
96 EXPECT_EQ(seq.size(), 2UL);
97 FooBar& map1 = seq[0];
98 FooBar& map2 = seq[1];
99 EXPECT_EQ(map1.foo, 3);
100 EXPECT_EQ(map1.bar, 5);
101 EXPECT_EQ(map2.foo, 7);
102 EXPECT_EQ(map2.bar, 9);
107 // Test writing then reading back a sequence of mappings
109 TEST(YAMLIO, TestSequenceMapWriteAndRead) {
110 std::string intermediate;
119 seq.push_back(entry1);
120 seq.push_back(entry2);
122 llvm::raw_string_ostream ostr(intermediate);
128 Input yin(intermediate);
132 EXPECT_FALSE(yin.error());
133 EXPECT_EQ(seq2.size(), 2UL);
134 FooBar& map1 = seq2[0];
135 FooBar& map2 = seq2[1];
136 EXPECT_EQ(map1.foo, 10);
137 EXPECT_EQ(map1.bar, -3);
138 EXPECT_EQ(map2.foo, 257);
139 EXPECT_EQ(map2.bar, 0);
144 //===----------------------------------------------------------------------===//
145 // Test built-in types
146 //===----------------------------------------------------------------------===//
148 struct BuiltInTypes {
171 struct MappingTraits<BuiltInTypes> {
172 static void mapping(IO &io, BuiltInTypes& bt) {
173 io.mapRequired("str", bt.str);
174 io.mapRequired("stdstr", bt.stdstr);
175 io.mapRequired("u64", bt.u64);
176 io.mapRequired("u32", bt.u32);
177 io.mapRequired("u16", bt.u16);
178 io.mapRequired("u8", bt.u8);
179 io.mapRequired("b", bt.b);
180 io.mapRequired("s64", bt.s64);
181 io.mapRequired("s32", bt.s32);
182 io.mapRequired("s16", bt.s16);
183 io.mapRequired("s8", bt.s8);
184 io.mapRequired("f", bt.f);
185 io.mapRequired("d", bt.d);
186 io.mapRequired("h8", bt.h8);
187 io.mapRequired("h16", bt.h16);
188 io.mapRequired("h32", bt.h32);
189 io.mapRequired("h64", bt.h64);
197 // Test the reading of all built-in scalar conversions
199 TEST(YAMLIO, TestReadBuiltInTypes) {
203 "stdstr: hello where?\n"
218 "h64: 0xFEDCBA9876543210\n"
222 EXPECT_FALSE(yin.error());
223 EXPECT_TRUE(map.str.equals("hello there"));
224 EXPECT_TRUE(map.stdstr == "hello where?");
225 EXPECT_EQ(map.u64, 5000000000ULL);
226 EXPECT_EQ(map.u32, 4000000000U);
227 EXPECT_EQ(map.u16, 65000);
228 EXPECT_EQ(map.u8, 255);
229 EXPECT_EQ(map.b, false);
230 EXPECT_EQ(map.s64, -5000000000LL);
231 EXPECT_EQ(map.s32, -2000000000L);
232 EXPECT_EQ(map.s16, -32000);
233 EXPECT_EQ(map.s8, -127);
234 EXPECT_EQ(map.f, 137.125);
235 EXPECT_EQ(map.d, -2.8625);
236 EXPECT_EQ(map.h8, Hex8(255));
237 EXPECT_EQ(map.h16, Hex16(0x8765));
238 EXPECT_EQ(map.h32, Hex32(0xFEDCBA98));
239 EXPECT_EQ(map.h64, Hex64(0xFEDCBA9876543210LL));
244 // Test writing then reading back all built-in scalar types
246 TEST(YAMLIO, TestReadWriteBuiltInTypes) {
247 std::string intermediate;
251 map.stdstr = "three four";
252 map.u64 = 6000000000ULL;
253 map.u32 = 3000000000U;
257 map.s64 = -6000000000LL;
258 map.s32 = -2000000000;
265 map.h32 = 3000000000U;
266 map.h64 = 6000000000LL;
268 llvm::raw_string_ostream ostr(intermediate);
274 Input yin(intermediate);
278 EXPECT_FALSE(yin.error());
279 EXPECT_TRUE(map.str.equals("one two"));
280 EXPECT_TRUE(map.stdstr == "three four");
281 EXPECT_EQ(map.u64, 6000000000ULL);
282 EXPECT_EQ(map.u32, 3000000000U);
283 EXPECT_EQ(map.u16, 50000);
284 EXPECT_EQ(map.u8, 254);
285 EXPECT_EQ(map.b, true);
286 EXPECT_EQ(map.s64, -6000000000LL);
287 EXPECT_EQ(map.s32, -2000000000L);
288 EXPECT_EQ(map.s16, -32000);
289 EXPECT_EQ(map.s8, -128);
290 EXPECT_EQ(map.f, 3.25);
291 EXPECT_EQ(map.d, -2.8625);
292 EXPECT_EQ(map.h8, Hex8(254));
293 EXPECT_EQ(map.h16, Hex16(50000));
294 EXPECT_EQ(map.h32, Hex32(3000000000U));
295 EXPECT_EQ(map.h64, Hex64(6000000000LL));
300 llvm::StringRef str1;
301 llvm::StringRef str2;
302 llvm::StringRef str3;
303 llvm::StringRef str4;
304 llvm::StringRef str5;
315 struct MappingTraits<StringTypes> {
316 static void mapping(IO &io, StringTypes& st) {
317 io.mapRequired("str1", st.str1);
318 io.mapRequired("str2", st.str2);
319 io.mapRequired("str3", st.str3);
320 io.mapRequired("str4", st.str4);
321 io.mapRequired("str5", st.str5);
322 io.mapRequired("stdstr1", st.stdstr1);
323 io.mapRequired("stdstr2", st.stdstr2);
324 io.mapRequired("stdstr3", st.stdstr3);
325 io.mapRequired("stdstr4", st.stdstr4);
326 io.mapRequired("stdstr5", st.stdstr5);
332 TEST(YAMLIO, TestReadWriteStringTypes) {
333 std::string intermediate;
341 map.stdstr1 = "'eee";
342 map.stdstr2 = "\"fff";
343 map.stdstr3 = "`ggg";
344 map.stdstr4 = "@hhh";
347 llvm::raw_string_ostream ostr(intermediate);
352 llvm::StringRef flowOut(intermediate);
353 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'''aaa"));
354 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'\"bbb'"));
355 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'`ccc'"));
356 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'@ddd'"));
357 EXPECT_NE(llvm::StringRef::npos, flowOut.find("''\n"));
358 EXPECT_NE(std::string::npos, flowOut.find("'''eee"));
359 EXPECT_NE(std::string::npos, flowOut.find("'\"fff'"));
360 EXPECT_NE(std::string::npos, flowOut.find("'`ggg'"));
361 EXPECT_NE(std::string::npos, flowOut.find("'@hhh'"));
362 EXPECT_NE(std::string::npos, flowOut.find("''\n"));
365 Input yin(intermediate);
369 EXPECT_FALSE(yin.error());
370 EXPECT_TRUE(map.str1.equals("'aaa"));
371 EXPECT_TRUE(map.str2.equals("\"bbb"));
372 EXPECT_TRUE(map.str3.equals("`ccc"));
373 EXPECT_TRUE(map.str4.equals("@ddd"));
374 EXPECT_TRUE(map.str5.equals(""));
375 EXPECT_TRUE(map.stdstr1 == "'eee");
376 EXPECT_TRUE(map.stdstr2 == "\"fff");
377 EXPECT_TRUE(map.stdstr3 == "`ggg");
378 EXPECT_TRUE(map.stdstr4 == "@hhh");
379 EXPECT_TRUE(map.stdstr5 == "");
383 //===----------------------------------------------------------------------===//
384 // Test ScalarEnumerationTraits
385 //===----------------------------------------------------------------------===//
406 struct ScalarEnumerationTraits<Colors> {
407 static void enumeration(IO &io, Colors &value) {
408 io.enumCase(value, "red", cRed);
409 io.enumCase(value, "blue", cBlue);
410 io.enumCase(value, "green", cGreen);
411 io.enumCase(value, "yellow",cYellow);
415 struct MappingTraits<ColorMap> {
416 static void mapping(IO &io, ColorMap& c) {
417 io.mapRequired("c1", c.c1);
418 io.mapRequired("c2", c.c2);
419 io.mapRequired("c3", c.c3);
420 io.mapOptional("c4", c.c4, cBlue); // supplies default
421 io.mapOptional("c5", c.c5, cYellow); // supplies default
422 io.mapOptional("c6", c.c6, cRed); // supplies default
430 // Test reading enumerated scalars
432 TEST(YAMLIO, TestEnumRead) {
442 EXPECT_FALSE(yin.error());
443 EXPECT_EQ(cBlue, map.c1);
444 EXPECT_EQ(cRed, map.c2);
445 EXPECT_EQ(cGreen, map.c3);
446 EXPECT_EQ(cBlue, map.c4); // tests default
447 EXPECT_EQ(cYellow,map.c5); // tests overridden
448 EXPECT_EQ(cRed, map.c6); // tests default
453 //===----------------------------------------------------------------------===//
454 // Test ScalarBitSetTraits
455 //===----------------------------------------------------------------------===//
464 inline MyFlags operator|(MyFlags a, MyFlags b) {
465 return static_cast<MyFlags>(
466 static_cast<uint32_t>(a) | static_cast<uint32_t>(b));
480 struct ScalarBitSetTraits<MyFlags> {
481 static void bitset(IO &io, MyFlags &value) {
482 io.bitSetCase(value, "big", flagBig);
483 io.bitSetCase(value, "flat", flagFlat);
484 io.bitSetCase(value, "round", flagRound);
485 io.bitSetCase(value, "pointy",flagPointy);
489 struct MappingTraits<FlagsMap> {
490 static void mapping(IO &io, FlagsMap& c) {
491 io.mapRequired("f1", c.f1);
492 io.mapRequired("f2", c.f2);
493 io.mapRequired("f3", c.f3);
494 io.mapOptional("f4", c.f4, MyFlags(flagRound));
502 // Test reading flow sequence representing bit-mask values
504 TEST(YAMLIO, TestFlagsRead) {
508 "f2: [ round, flat ]\n"
513 EXPECT_FALSE(yin.error());
514 EXPECT_EQ(flagBig, map.f1);
515 EXPECT_EQ(flagRound|flagFlat, map.f2);
516 EXPECT_EQ(flagNone, map.f3); // check empty set
517 EXPECT_EQ(flagRound, map.f4); // check optional key
522 // Test writing then reading back bit-mask values
524 TEST(YAMLIO, TestReadWriteFlags) {
525 std::string intermediate;
529 map.f2 = flagRound | flagFlat;
533 llvm::raw_string_ostream ostr(intermediate);
539 Input yin(intermediate);
543 EXPECT_FALSE(yin.error());
544 EXPECT_EQ(flagBig, map2.f1);
545 EXPECT_EQ(flagRound|flagFlat, map2.f2);
546 EXPECT_EQ(flagNone, map2.f3);
547 //EXPECT_EQ(flagRound, map2.f4); // check optional key
553 //===----------------------------------------------------------------------===//
555 //===----------------------------------------------------------------------===//
557 struct MyCustomType {
562 struct MyCustomTypeMap {
572 struct MappingTraits<MyCustomTypeMap> {
573 static void mapping(IO &io, MyCustomTypeMap& s) {
574 io.mapRequired("f1", s.f1);
575 io.mapRequired("f2", s.f2);
576 io.mapRequired("f3", s.f3);
579 // MyCustomType is formatted as a yaml scalar. A value of
580 // {length=3, width=4} would be represented in yaml as "3 by 4".
582 struct ScalarTraits<MyCustomType> {
583 static void output(const MyCustomType &value, void* ctxt, llvm::raw_ostream &out) {
584 out << llvm::format("%d by %d", value.length, value.width);
586 static StringRef input(StringRef scalar, void* ctxt, MyCustomType &value) {
587 size_t byStart = scalar.find("by");
588 if ( byStart != StringRef::npos ) {
589 StringRef lenStr = scalar.slice(0, byStart);
590 lenStr = lenStr.rtrim();
591 if ( lenStr.getAsInteger(0, value.length) ) {
592 return "malformed length";
594 StringRef widthStr = scalar.drop_front(byStart+2);
595 widthStr = widthStr.ltrim();
596 if ( widthStr.getAsInteger(0, value.width) ) {
597 return "malformed width";
602 return "malformed by";
611 // Test writing then reading back custom values
613 TEST(YAMLIO, TestReadWriteMyCustomType) {
614 std::string intermediate;
623 llvm::raw_string_ostream ostr(intermediate);
629 Input yin(intermediate);
630 MyCustomTypeMap map2;
633 EXPECT_FALSE(yin.error());
634 EXPECT_EQ(1, map2.f1.length);
635 EXPECT_EQ(4, map2.f1.width);
636 EXPECT_EQ(100, map2.f2.length);
637 EXPECT_EQ(400, map2.f2.width);
638 EXPECT_EQ(10, map2.f3);
643 //===----------------------------------------------------------------------===//
644 // Test flow sequences
645 //===----------------------------------------------------------------------===//
647 LLVM_YAML_STRONG_TYPEDEF(int, MyNumber)
648 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyNumber)
649 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::StringRef)
654 struct ScalarTraits<MyNumber> {
655 static void output(const MyNumber &value, void *, llvm::raw_ostream &out) {
659 static StringRef input(StringRef scalar, void *, MyNumber &value) {
661 if ( getAsSignedInteger(scalar, 0, n) )
662 return "invalid number";
670 struct NameAndNumbers {
671 llvm::StringRef name;
672 std::vector<llvm::StringRef> strings;
673 std::vector<MyNumber> single;
674 std::vector<MyNumber> numbers;
680 struct MappingTraits<NameAndNumbers> {
681 static void mapping(IO &io, NameAndNumbers& nn) {
682 io.mapRequired("name", nn.name);
683 io.mapRequired("strings", nn.strings);
684 io.mapRequired("single", nn.single);
685 io.mapRequired("numbers", nn.numbers);
693 // Test writing then reading back custom values
695 TEST(YAMLIO, TestReadWriteMyFlowSequence) {
696 std::string intermediate;
700 map.strings.push_back(llvm::StringRef("one"));
701 map.strings.push_back(llvm::StringRef("two"));
702 map.single.push_back(1);
703 map.numbers.push_back(10);
704 map.numbers.push_back(-30);
705 map.numbers.push_back(1024);
707 llvm::raw_string_ostream ostr(intermediate);
711 // Verify sequences were written in flow style
713 llvm::StringRef flowOut(intermediate);
714 EXPECT_NE(llvm::StringRef::npos, flowOut.find("one, two"));
715 EXPECT_NE(llvm::StringRef::npos, flowOut.find("10, -30, 1024"));
719 Input yin(intermediate);
723 EXPECT_FALSE(yin.error());
724 EXPECT_TRUE(map2.name.equals("hello"));
725 EXPECT_EQ(map2.strings.size(), 2UL);
726 EXPECT_TRUE(map2.strings[0].equals("one"));
727 EXPECT_TRUE(map2.strings[1].equals("two"));
728 EXPECT_EQ(map2.single.size(), 1UL);
729 EXPECT_EQ(1, map2.single[0]);
730 EXPECT_EQ(map2.numbers.size(), 3UL);
731 EXPECT_EQ(10, map2.numbers[0]);
732 EXPECT_EQ(-30, map2.numbers[1]);
733 EXPECT_EQ(1024, map2.numbers[2]);
738 //===----------------------------------------------------------------------===//
739 // Test normalizing/denormalizing
740 //===----------------------------------------------------------------------===//
742 LLVM_YAML_STRONG_TYPEDEF(uint32_t, TotalSeconds)
744 typedef std::vector<TotalSeconds> SecondsSequence;
746 LLVM_YAML_IS_SEQUENCE_VECTOR(TotalSeconds)
752 struct MappingTraits<TotalSeconds> {
754 class NormalizedSeconds {
756 NormalizedSeconds(IO &io)
757 : hours(0), minutes(0), seconds(0) {
759 NormalizedSeconds(IO &, TotalSeconds &secs)
761 minutes((secs - (hours*3600))/60),
764 TotalSeconds denormalize(IO &) {
765 return TotalSeconds(hours*3600 + minutes*60 + seconds);
773 static void mapping(IO &io, TotalSeconds &secs) {
774 MappingNormalization<NormalizedSeconds, TotalSeconds> keys(io, secs);
776 io.mapOptional("hours", keys->hours, (uint32_t)0);
777 io.mapOptional("minutes", keys->minutes, (uint8_t)0);
778 io.mapRequired("seconds", keys->seconds);
786 // Test the reading of a yaml sequence of mappings
788 TEST(YAMLIO, TestReadMySecondsSequence) {
790 Input yin("---\n - hours: 1\n seconds: 5\n - seconds: 59\n...\n");
793 EXPECT_FALSE(yin.error());
794 EXPECT_EQ(seq.size(), 2UL);
795 EXPECT_EQ(seq[0], 3605U);
796 EXPECT_EQ(seq[1], 59U);
801 // Test writing then reading back custom values
803 TEST(YAMLIO, TestReadWriteMySecondsSequence) {
804 std::string intermediate;
811 llvm::raw_string_ostream ostr(intermediate);
816 Input yin(intermediate);
817 SecondsSequence seq2;
820 EXPECT_FALSE(yin.error());
821 EXPECT_EQ(seq2.size(), 3UL);
822 EXPECT_EQ(seq2[0], 4000U);
823 EXPECT_EQ(seq2[1], 500U);
824 EXPECT_EQ(seq2[2], 59U);
829 //===----------------------------------------------------------------------===//
830 // Test dynamic typing
831 //===----------------------------------------------------------------------===//
850 struct KindAndFlags {
851 KindAndFlags() : kind(kindA), flags(0) { }
852 KindAndFlags(Kind k, uint32_t f) : kind(k), flags(f) { }
857 typedef std::vector<KindAndFlags> KindAndFlagsSequence;
859 LLVM_YAML_IS_SEQUENCE_VECTOR(KindAndFlags)
864 struct ScalarEnumerationTraits<AFlags> {
865 static void enumeration(IO &io, AFlags &value) {
866 io.enumCase(value, "a1", a1);
867 io.enumCase(value, "a2", a2);
868 io.enumCase(value, "a3", a3);
872 struct ScalarEnumerationTraits<BFlags> {
873 static void enumeration(IO &io, BFlags &value) {
874 io.enumCase(value, "b1", b1);
875 io.enumCase(value, "b2", b2);
876 io.enumCase(value, "b3", b3);
880 struct ScalarEnumerationTraits<Kind> {
881 static void enumeration(IO &io, Kind &value) {
882 io.enumCase(value, "A", kindA);
883 io.enumCase(value, "B", kindB);
887 struct MappingTraits<KindAndFlags> {
888 static void mapping(IO &io, KindAndFlags& kf) {
889 io.mapRequired("kind", kf.kind);
890 // Type of "flags" field varies depending on "kind" field.
891 // Use memcpy here to avoid breaking strict aliasing rules.
892 if (kf.kind == kindA) {
893 AFlags aflags = static_cast<AFlags>(kf.flags);
894 io.mapRequired("flags", aflags);
897 BFlags bflags = static_cast<BFlags>(kf.flags);
898 io.mapRequired("flags", bflags);
908 // Test the reading of a yaml sequence dynamic types
910 TEST(YAMLIO, TestReadKindAndFlagsSequence) {
911 KindAndFlagsSequence seq;
912 Input yin("---\n - kind: A\n flags: a2\n - kind: B\n flags: b1\n...\n");
915 EXPECT_FALSE(yin.error());
916 EXPECT_EQ(seq.size(), 2UL);
917 EXPECT_EQ(seq[0].kind, kindA);
918 EXPECT_EQ(seq[0].flags, (uint32_t)a2);
919 EXPECT_EQ(seq[1].kind, kindB);
920 EXPECT_EQ(seq[1].flags, (uint32_t)b1);
924 // Test writing then reading back dynamic types
926 TEST(YAMLIO, TestReadWriteKindAndFlagsSequence) {
927 std::string intermediate;
929 KindAndFlagsSequence seq;
930 seq.push_back(KindAndFlags(kindA,a1));
931 seq.push_back(KindAndFlags(kindB,b1));
932 seq.push_back(KindAndFlags(kindA,a2));
933 seq.push_back(KindAndFlags(kindB,b2));
934 seq.push_back(KindAndFlags(kindA,a3));
936 llvm::raw_string_ostream ostr(intermediate);
941 Input yin(intermediate);
942 KindAndFlagsSequence seq2;
945 EXPECT_FALSE(yin.error());
946 EXPECT_EQ(seq2.size(), 5UL);
947 EXPECT_EQ(seq2[0].kind, kindA);
948 EXPECT_EQ(seq2[0].flags, (uint32_t)a1);
949 EXPECT_EQ(seq2[1].kind, kindB);
950 EXPECT_EQ(seq2[1].flags, (uint32_t)b1);
951 EXPECT_EQ(seq2[2].kind, kindA);
952 EXPECT_EQ(seq2[2].flags, (uint32_t)a2);
953 EXPECT_EQ(seq2[3].kind, kindB);
954 EXPECT_EQ(seq2[3].flags, (uint32_t)b2);
955 EXPECT_EQ(seq2[4].kind, kindA);
956 EXPECT_EQ(seq2[4].flags, (uint32_t)a3);
961 //===----------------------------------------------------------------------===//
962 // Test document list
963 //===----------------------------------------------------------------------===//
969 typedef std::vector<FooBarMap> FooBarMapDocumentList;
971 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(FooBarMap)
977 struct MappingTraits<FooBarMap> {
978 static void mapping(IO &io, FooBarMap& fb) {
979 io.mapRequired("foo", fb.foo);
980 io.mapRequired("bar", fb.bar);
988 // Test the reading of a yaml mapping
990 TEST(YAMLIO, TestDocRead) {
992 Input yin("---\nfoo: 3\nbar: 5\n...\n");
995 EXPECT_FALSE(yin.error());
996 EXPECT_EQ(doc.foo, 3);
997 EXPECT_EQ(doc.bar,5);
1003 // Test writing then reading back a sequence of mappings
1005 TEST(YAMLIO, TestSequenceDocListWriteAndRead) {
1006 std::string intermediate;
1014 std::vector<FooBarMap> docList;
1015 docList.push_back(doc1);
1016 docList.push_back(doc2);
1018 llvm::raw_string_ostream ostr(intermediate);
1025 Input yin(intermediate);
1026 std::vector<FooBarMap> docList2;
1029 EXPECT_FALSE(yin.error());
1030 EXPECT_EQ(docList2.size(), 2UL);
1031 FooBarMap& map1 = docList2[0];
1032 FooBarMap& map2 = docList2[1];
1033 EXPECT_EQ(map1.foo, 10);
1034 EXPECT_EQ(map1.bar, -3);
1035 EXPECT_EQ(map2.foo, 257);
1036 EXPECT_EQ(map2.bar, 0);
1040 //===----------------------------------------------------------------------===//
1041 // Test document tags
1042 //===----------------------------------------------------------------------===//
1045 MyDouble() : value(0.0) { }
1046 MyDouble(double x) : value(x) { }
1050 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyDouble)
1056 struct MappingTraits<MyDouble> {
1057 static void mapping(IO &io, MyDouble &d) {
1058 if (io.mapTag("!decimal", true)) {
1059 mappingDecimal(io, d);
1060 } else if (io.mapTag("!fraction")) {
1061 mappingFraction(io, d);
1064 static void mappingDecimal(IO &io, MyDouble &d) {
1065 io.mapRequired("value", d.value);
1067 static void mappingFraction(IO &io, MyDouble &d) {
1069 io.mapRequired("numerator", num);
1070 io.mapRequired("denominator", denom);
1071 // convert fraction to double
1072 d.value = num/denom;
1080 // Test the reading of two different tagged yaml documents.
1082 TEST(YAMLIO, TestTaggedDocuments) {
1083 std::vector<MyDouble> docList;
1084 Input yin("--- !decimal\nvalue: 3.0\n"
1085 "--- !fraction\nnumerator: 9.0\ndenominator: 2\n...\n");
1087 EXPECT_FALSE(yin.error());
1088 EXPECT_EQ(docList.size(), 2UL);
1089 EXPECT_EQ(docList[0].value, 3.0);
1090 EXPECT_EQ(docList[1].value, 4.5);
1096 // Test writing then reading back tagged documents
1098 TEST(YAMLIO, TestTaggedDocumentsWriteAndRead) {
1099 std::string intermediate;
1103 std::vector<MyDouble> docList;
1104 docList.push_back(a);
1105 docList.push_back(b);
1107 llvm::raw_string_ostream ostr(intermediate);
1113 Input yin(intermediate);
1114 std::vector<MyDouble> docList2;
1117 EXPECT_FALSE(yin.error());
1118 EXPECT_EQ(docList2.size(), 2UL);
1119 EXPECT_EQ(docList2[0].value, 10.25);
1120 EXPECT_EQ(docList2[1].value, -3.75);
1125 //===----------------------------------------------------------------------===//
1126 // Test mapping validation
1127 //===----------------------------------------------------------------------===//
1129 struct MyValidation {
1133 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyValidation)
1138 struct MappingTraits<MyValidation> {
1139 static void mapping(IO &io, MyValidation &d) {
1140 io.mapRequired("value", d.value);
1142 static StringRef validate(IO &io, MyValidation &d) {
1144 return "negative value";
1153 // Test that validate() is called and complains about the negative value.
1155 TEST(YAMLIO, TestValidatingInput) {
1156 std::vector<MyValidation> docList;
1157 Input yin("--- \nvalue: 3.0\n"
1158 "--- \nvalue: -1.0\n...\n",
1159 NULL, suppressErrorMessages);
1161 EXPECT_TRUE(yin.error());
1165 //===----------------------------------------------------------------------===//
1166 // Test error handling
1167 //===----------------------------------------------------------------------===//
1170 // Test error handling of unknown enumerated scalar
1172 TEST(YAMLIO, TestColorsReadError) {
1180 suppressErrorMessages);
1182 EXPECT_TRUE(yin.error());
1187 // Test error handling of flow sequence with unknown value
1189 TEST(YAMLIO, TestFlagsReadError) {
1193 "f2: [ round, hollow ]\n"
1197 suppressErrorMessages);
1200 EXPECT_TRUE(yin.error());
1205 // Test error handling reading built-in uint8_t type
1207 LLVM_YAML_IS_SEQUENCE_VECTOR(uint8_t)
1208 TEST(YAMLIO, TestReadBuiltInTypesUint8Error) {
1209 std::vector<uint8_t> seq;
1216 suppressErrorMessages);
1219 EXPECT_TRUE(yin.error());
1224 // Test error handling reading built-in uint16_t type
1226 LLVM_YAML_IS_SEQUENCE_VECTOR(uint16_t)
1227 TEST(YAMLIO, TestReadBuiltInTypesUint16Error) {
1228 std::vector<uint16_t> seq;
1235 suppressErrorMessages);
1238 EXPECT_TRUE(yin.error());
1243 // Test error handling reading built-in uint32_t type
1245 LLVM_YAML_IS_SEQUENCE_VECTOR(uint32_t)
1246 TEST(YAMLIO, TestReadBuiltInTypesUint32Error) {
1247 std::vector<uint32_t> seq;
1254 suppressErrorMessages);
1257 EXPECT_TRUE(yin.error());
1262 // Test error handling reading built-in uint64_t type
1264 LLVM_YAML_IS_SEQUENCE_VECTOR(uint64_t)
1265 TEST(YAMLIO, TestReadBuiltInTypesUint64Error) {
1266 std::vector<uint64_t> seq;
1268 "- 18446744073709551615\n"
1270 "- 19446744073709551615\n"
1273 suppressErrorMessages);
1276 EXPECT_TRUE(yin.error());
1281 // Test error handling reading built-in int8_t type
1283 LLVM_YAML_IS_SEQUENCE_VECTOR(int8_t)
1284 TEST(YAMLIO, TestReadBuiltInTypesint8OverError) {
1285 std::vector<int8_t> seq;
1293 suppressErrorMessages);
1296 EXPECT_TRUE(yin.error());
1300 // Test error handling reading built-in int8_t type
1302 TEST(YAMLIO, TestReadBuiltInTypesint8UnderError) {
1303 std::vector<int8_t> seq;
1311 suppressErrorMessages);
1314 EXPECT_TRUE(yin.error());
1319 // Test error handling reading built-in int16_t type
1321 LLVM_YAML_IS_SEQUENCE_VECTOR(int16_t)
1322 TEST(YAMLIO, TestReadBuiltInTypesint16UnderError) {
1323 std::vector<int16_t> seq;
1331 suppressErrorMessages);
1334 EXPECT_TRUE(yin.error());
1339 // Test error handling reading built-in int16_t type
1341 TEST(YAMLIO, TestReadBuiltInTypesint16OverError) {
1342 std::vector<int16_t> seq;
1350 suppressErrorMessages);
1353 EXPECT_TRUE(yin.error());
1358 // Test error handling reading built-in int32_t type
1360 LLVM_YAML_IS_SEQUENCE_VECTOR(int32_t)
1361 TEST(YAMLIO, TestReadBuiltInTypesint32UnderError) {
1362 std::vector<int32_t> seq;
1370 suppressErrorMessages);
1373 EXPECT_TRUE(yin.error());
1377 // Test error handling reading built-in int32_t type
1379 TEST(YAMLIO, TestReadBuiltInTypesint32OverError) {
1380 std::vector<int32_t> seq;
1388 suppressErrorMessages);
1391 EXPECT_TRUE(yin.error());
1396 // Test error handling reading built-in int64_t type
1398 LLVM_YAML_IS_SEQUENCE_VECTOR(int64_t)
1399 TEST(YAMLIO, TestReadBuiltInTypesint64UnderError) {
1400 std::vector<int64_t> seq;
1402 "- -9223372036854775808\n"
1404 "- 9223372036854775807\n"
1405 "- -9223372036854775809\n"
1408 suppressErrorMessages);
1411 EXPECT_TRUE(yin.error());
1415 // Test error handling reading built-in int64_t type
1417 TEST(YAMLIO, TestReadBuiltInTypesint64OverError) {
1418 std::vector<int64_t> seq;
1420 "- -9223372036854775808\n"
1422 "- 9223372036854775807\n"
1423 "- 9223372036854775809\n"
1426 suppressErrorMessages);
1429 EXPECT_TRUE(yin.error());
1433 // Test error handling reading built-in float type
1435 LLVM_YAML_IS_SEQUENCE_VECTOR(float)
1436 TEST(YAMLIO, TestReadBuiltInTypesFloatError) {
1437 std::vector<float> seq;
1445 suppressErrorMessages);
1448 EXPECT_TRUE(yin.error());
1452 // Test error handling reading built-in float type
1454 LLVM_YAML_IS_SEQUENCE_VECTOR(double)
1455 TEST(YAMLIO, TestReadBuiltInTypesDoubleError) {
1456 std::vector<double> seq;
1464 suppressErrorMessages);
1467 EXPECT_TRUE(yin.error());
1471 // Test error handling reading built-in Hex8 type
1473 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex8)
1474 TEST(YAMLIO, TestReadBuiltInTypesHex8Error) {
1475 std::vector<Hex8> seq;
1482 suppressErrorMessages);
1485 EXPECT_TRUE(yin.error());
1490 // Test error handling reading built-in Hex16 type
1492 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex16)
1493 TEST(YAMLIO, TestReadBuiltInTypesHex16Error) {
1494 std::vector<Hex16> seq;
1501 suppressErrorMessages);
1504 EXPECT_TRUE(yin.error());
1508 // Test error handling reading built-in Hex32 type
1510 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex32)
1511 TEST(YAMLIO, TestReadBuiltInTypesHex32Error) {
1512 std::vector<Hex32> seq;
1519 suppressErrorMessages);
1522 EXPECT_TRUE(yin.error());
1526 // Test error handling reading built-in Hex64 type
1528 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex64)
1529 TEST(YAMLIO, TestReadBuiltInTypesHex64Error) {
1530 std::vector<Hex64> seq;
1533 "- 0xFFEEDDCCBBAA9988\n"
1534 "- 0x12345567890ABCDEF0\n"
1537 suppressErrorMessages);
1540 EXPECT_TRUE(yin.error());
1543 TEST(YAMLIO, TestMalformedMapFailsGracefully) {
1546 // We pass the suppressErrorMessages handler to handle the error
1547 // message generated in the constructor of Input.
1548 Input yin("{foo:3, bar: 5}", /*Ctxt=*/NULL, suppressErrorMessages);
1550 EXPECT_TRUE(yin.error());
1554 Input yin("---\nfoo:3\nbar: 5\n...\n", /*Ctxt=*/NULL, suppressErrorMessages);
1556 EXPECT_TRUE(yin.error());
1560 struct OptionalTest {
1561 std::vector<int> Numbers;
1564 struct OptionalTestSeq {
1565 std::vector<OptionalTest> Tests;
1568 LLVM_YAML_IS_SEQUENCE_VECTOR(OptionalTest)
1572 struct MappingTraits<OptionalTest> {
1573 static void mapping(IO& IO, OptionalTest &OT) {
1574 IO.mapOptional("Numbers", OT.Numbers);
1579 struct MappingTraits<OptionalTestSeq> {
1580 static void mapping(IO &IO, OptionalTestSeq &OTS) {
1581 IO.mapOptional("Tests", OTS.Tests);
1587 TEST(YAMLIO, SequenceElideTest) {
1588 // Test that writing out a purely optional structure with its fields set to
1589 // default followed by other data is properly read back in.
1590 OptionalTestSeq Seq;
1591 OptionalTest One, Two, Three, Four;
1592 int N[] = {1, 2, 3};
1593 Three.Numbers.assign(N, N + 3);
1594 Seq.Tests.push_back(One);
1595 Seq.Tests.push_back(Two);
1596 Seq.Tests.push_back(Three);
1597 Seq.Tests.push_back(Four);
1599 std::string intermediate;
1601 llvm::raw_string_ostream ostr(intermediate);
1606 Input yin(intermediate);
1607 OptionalTestSeq Seq2;
1610 EXPECT_FALSE(yin.error());
1612 EXPECT_EQ(4UL, Seq2.Tests.size());
1614 EXPECT_TRUE(Seq2.Tests[0].Numbers.empty());
1615 EXPECT_TRUE(Seq2.Tests[1].Numbers.empty());
1617 EXPECT_EQ(1, Seq2.Tests[2].Numbers[0]);
1618 EXPECT_EQ(2, Seq2.Tests[2].Numbers[1]);
1619 EXPECT_EQ(3, Seq2.Tests[2].Numbers[2]);
1621 EXPECT_TRUE(Seq2.Tests[3].Numbers.empty());
1624 TEST(YAMLIO, TestEmptyStringFailsForMapWithRequiredFields) {
1628 EXPECT_TRUE(yin.error());
1631 TEST(YAMLIO, TestEmptyStringSucceedsForMapWithOptionalFields) {
1635 EXPECT_FALSE(yin.error());
1638 TEST(YAMLIO, TestEmptyStringSucceedsForSequence) {
1639 std::vector<uint8_t> seq;
1640 Input yin("", /*Ctxt=*/NULL, suppressErrorMessages);
1643 EXPECT_FALSE(yin.error());
1644 EXPECT_TRUE(seq.empty());