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;
30 //===----------------------------------------------------------------------===//
32 //===----------------------------------------------------------------------===//
38 typedef std::vector<FooBar> FooBarSequence;
40 LLVM_YAML_IS_SEQUENCE_VECTOR(FooBar)
46 struct MappingTraits<FooBar> {
47 static void mapping(IO &io, FooBar& fb) {
48 io.mapRequired("foo", fb.foo);
49 io.mapRequired("bar", fb.bar);
57 // Test the reading of a yaml mapping
59 TEST(YAMLIO, TestMapRead) {
62 Input yin("---\nfoo: 3\nbar: 5\n...\n");
65 EXPECT_FALSE(yin.error());
66 EXPECT_EQ(doc.foo, 3);
67 EXPECT_EQ(doc.bar, 5);
71 Input yin("{foo: 3, bar: 5}");
74 EXPECT_FALSE(yin.error());
75 EXPECT_EQ(doc.foo, 3);
76 EXPECT_EQ(doc.bar, 5);
81 // Test the reading of a yaml sequence of mappings
83 TEST(YAMLIO, TestSequenceMapRead) {
85 Input yin("---\n - foo: 3\n bar: 5\n - foo: 7\n bar: 9\n...\n");
88 EXPECT_FALSE(yin.error());
89 EXPECT_EQ(seq.size(), 2UL);
90 FooBar& map1 = seq[0];
91 FooBar& map2 = seq[1];
92 EXPECT_EQ(map1.foo, 3);
93 EXPECT_EQ(map1.bar, 5);
94 EXPECT_EQ(map2.foo, 7);
95 EXPECT_EQ(map2.bar, 9);
100 // Test writing then reading back a sequence of mappings
102 TEST(YAMLIO, TestSequenceMapWriteAndRead) {
103 std::string intermediate;
112 seq.push_back(entry1);
113 seq.push_back(entry2);
115 llvm::raw_string_ostream ostr(intermediate);
121 Input yin(intermediate);
125 EXPECT_FALSE(yin.error());
126 EXPECT_EQ(seq2.size(), 2UL);
127 FooBar& map1 = seq2[0];
128 FooBar& map2 = seq2[1];
129 EXPECT_EQ(map1.foo, 10);
130 EXPECT_EQ(map1.bar, -3);
131 EXPECT_EQ(map2.foo, 257);
132 EXPECT_EQ(map2.bar, 0);
137 //===----------------------------------------------------------------------===//
138 // Test built-in types
139 //===----------------------------------------------------------------------===//
141 struct BuiltInTypes {
163 struct MappingTraits<BuiltInTypes> {
164 static void mapping(IO &io, BuiltInTypes& bt) {
165 io.mapRequired("str", bt.str);
166 io.mapRequired("u64", bt.u64);
167 io.mapRequired("u32", bt.u32);
168 io.mapRequired("u16", bt.u16);
169 io.mapRequired("u8", bt.u8);
170 io.mapRequired("b", bt.b);
171 io.mapRequired("s64", bt.s64);
172 io.mapRequired("s32", bt.s32);
173 io.mapRequired("s16", bt.s16);
174 io.mapRequired("s8", bt.s8);
175 io.mapRequired("f", bt.f);
176 io.mapRequired("d", bt.d);
177 io.mapRequired("h8", bt.h8);
178 io.mapRequired("h16", bt.h16);
179 io.mapRequired("h32", bt.h32);
180 io.mapRequired("h64", bt.h64);
188 // Test the reading of all built-in scalar conversions
190 TEST(YAMLIO, TestReadBuiltInTypes) {
208 "h64: 0xFEDCBA9876543210\n"
212 EXPECT_FALSE(yin.error());
213 EXPECT_TRUE(map.str.equals("hello there"));
214 EXPECT_EQ(map.u64, 5000000000ULL);
215 EXPECT_EQ(map.u32, 4000000000U);
216 EXPECT_EQ(map.u16, 65000);
217 EXPECT_EQ(map.u8, 255);
218 EXPECT_EQ(map.b, false);
219 EXPECT_EQ(map.s64, -5000000000LL);
220 EXPECT_EQ(map.s32, -2000000000L);
221 EXPECT_EQ(map.s16, -32000);
222 EXPECT_EQ(map.s8, -127);
223 EXPECT_EQ(map.f, 137.125);
224 EXPECT_EQ(map.d, -2.8625);
225 EXPECT_EQ(map.h8, Hex8(255));
226 EXPECT_EQ(map.h16, Hex16(0x8765));
227 EXPECT_EQ(map.h32, Hex32(0xFEDCBA98));
228 EXPECT_EQ(map.h64, Hex64(0xFEDCBA9876543210LL));
233 // Test writing then reading back all built-in scalar types
235 TEST(YAMLIO, TestReadWriteBuiltInTypes) {
236 std::string intermediate;
240 map.u64 = 6000000000ULL;
241 map.u32 = 3000000000U;
245 map.s64 = -6000000000LL;
246 map.s32 = -2000000000;
253 map.h32 = 3000000000U;
254 map.h64 = 6000000000LL;
256 llvm::raw_string_ostream ostr(intermediate);
262 Input yin(intermediate);
266 EXPECT_FALSE(yin.error());
267 EXPECT_TRUE(map.str.equals("one two"));
268 EXPECT_EQ(map.u64, 6000000000ULL);
269 EXPECT_EQ(map.u32, 3000000000U);
270 EXPECT_EQ(map.u16, 50000);
271 EXPECT_EQ(map.u8, 254);
272 EXPECT_EQ(map.b, true);
273 EXPECT_EQ(map.s64, -6000000000LL);
274 EXPECT_EQ(map.s32, -2000000000L);
275 EXPECT_EQ(map.s16, -32000);
276 EXPECT_EQ(map.s8, -128);
277 EXPECT_EQ(map.f, 3.25);
278 EXPECT_EQ(map.d, -2.8625);
279 EXPECT_EQ(map.h8, Hex8(254));
280 EXPECT_EQ(map.h16, Hex16(50000));
281 EXPECT_EQ(map.h32, Hex32(3000000000U));
282 EXPECT_EQ(map.h64, Hex64(6000000000LL));
287 llvm::StringRef str1;
288 llvm::StringRef str2;
289 llvm::StringRef str3;
290 llvm::StringRef str4;
291 llvm::StringRef str5;
297 struct MappingTraits<StringTypes> {
298 static void mapping(IO &io, StringTypes& st) {
299 io.mapRequired("str1", st.str1);
300 io.mapRequired("str2", st.str2);
301 io.mapRequired("str3", st.str3);
302 io.mapRequired("str4", st.str4);
303 io.mapRequired("str5", st.str5);
309 TEST(YAMLIO, TestReadWriteStringTypes) {
310 std::string intermediate;
319 llvm::raw_string_ostream ostr(intermediate);
324 llvm::StringRef flowOut(intermediate);
325 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'''aaa"));
326 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'\"bbb'"));
327 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'`ccc'"));
328 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'@ddd'"));
329 EXPECT_NE(llvm::StringRef::npos, flowOut.find("''\n"));
332 Input yin(intermediate);
336 EXPECT_FALSE(yin.error());
337 EXPECT_TRUE(map.str1.equals("'aaa"));
338 EXPECT_TRUE(map.str2.equals("\"bbb"));
339 EXPECT_TRUE(map.str3.equals("`ccc"));
340 EXPECT_TRUE(map.str4.equals("@ddd"));
341 EXPECT_TRUE(map.str5.equals(""));
345 //===----------------------------------------------------------------------===//
346 // Test ScalarEnumerationTraits
347 //===----------------------------------------------------------------------===//
368 struct ScalarEnumerationTraits<Colors> {
369 static void enumeration(IO &io, Colors &value) {
370 io.enumCase(value, "red", cRed);
371 io.enumCase(value, "blue", cBlue);
372 io.enumCase(value, "green", cGreen);
373 io.enumCase(value, "yellow",cYellow);
377 struct MappingTraits<ColorMap> {
378 static void mapping(IO &io, ColorMap& c) {
379 io.mapRequired("c1", c.c1);
380 io.mapRequired("c2", c.c2);
381 io.mapRequired("c3", c.c3);
382 io.mapOptional("c4", c.c4, cBlue); // supplies default
383 io.mapOptional("c5", c.c5, cYellow); // supplies default
384 io.mapOptional("c6", c.c6, cRed); // supplies default
392 // Test reading enumerated scalars
394 TEST(YAMLIO, TestEnumRead) {
404 EXPECT_FALSE(yin.error());
405 EXPECT_EQ(cBlue, map.c1);
406 EXPECT_EQ(cRed, map.c2);
407 EXPECT_EQ(cGreen, map.c3);
408 EXPECT_EQ(cBlue, map.c4); // tests default
409 EXPECT_EQ(cYellow,map.c5); // tests overridden
410 EXPECT_EQ(cRed, map.c6); // tests default
415 //===----------------------------------------------------------------------===//
416 // Test ScalarBitSetTraits
417 //===----------------------------------------------------------------------===//
426 inline MyFlags operator|(MyFlags a, MyFlags b) {
427 return static_cast<MyFlags>(
428 static_cast<uint32_t>(a) | static_cast<uint32_t>(b));
442 struct ScalarBitSetTraits<MyFlags> {
443 static void bitset(IO &io, MyFlags &value) {
444 io.bitSetCase(value, "big", flagBig);
445 io.bitSetCase(value, "flat", flagFlat);
446 io.bitSetCase(value, "round", flagRound);
447 io.bitSetCase(value, "pointy",flagPointy);
451 struct MappingTraits<FlagsMap> {
452 static void mapping(IO &io, FlagsMap& c) {
453 io.mapRequired("f1", c.f1);
454 io.mapRequired("f2", c.f2);
455 io.mapRequired("f3", c.f3);
456 io.mapOptional("f4", c.f4, MyFlags(flagRound));
464 // Test reading flow sequence representing bit-mask values
466 TEST(YAMLIO, TestFlagsRead) {
470 "f2: [ round, flat ]\n"
475 EXPECT_FALSE(yin.error());
476 EXPECT_EQ(flagBig, map.f1);
477 EXPECT_EQ(flagRound|flagFlat, map.f2);
478 EXPECT_EQ(flagNone, map.f3); // check empty set
479 EXPECT_EQ(flagRound, map.f4); // check optional key
484 // Test writing then reading back bit-mask values
486 TEST(YAMLIO, TestReadWriteFlags) {
487 std::string intermediate;
491 map.f2 = flagRound | flagFlat;
495 llvm::raw_string_ostream ostr(intermediate);
501 Input yin(intermediate);
505 EXPECT_FALSE(yin.error());
506 EXPECT_EQ(flagBig, map2.f1);
507 EXPECT_EQ(flagRound|flagFlat, map2.f2);
508 EXPECT_EQ(flagNone, map2.f3);
509 //EXPECT_EQ(flagRound, map2.f4); // check optional key
515 //===----------------------------------------------------------------------===//
517 //===----------------------------------------------------------------------===//
519 struct MyCustomType {
524 struct MyCustomTypeMap {
534 struct MappingTraits<MyCustomTypeMap> {
535 static void mapping(IO &io, MyCustomTypeMap& s) {
536 io.mapRequired("f1", s.f1);
537 io.mapRequired("f2", s.f2);
538 io.mapRequired("f3", s.f3);
541 // MyCustomType is formatted as a yaml scalar. A value of
542 // {length=3, width=4} would be represented in yaml as "3 by 4".
544 struct ScalarTraits<MyCustomType> {
545 static void output(const MyCustomType &value, void* ctxt, llvm::raw_ostream &out) {
546 out << llvm::format("%d by %d", value.length, value.width);
548 static StringRef input(StringRef scalar, void* ctxt, MyCustomType &value) {
549 size_t byStart = scalar.find("by");
550 if ( byStart != StringRef::npos ) {
551 StringRef lenStr = scalar.slice(0, byStart);
552 lenStr = lenStr.rtrim();
553 if ( lenStr.getAsInteger(0, value.length) ) {
554 return "malformed length";
556 StringRef widthStr = scalar.drop_front(byStart+2);
557 widthStr = widthStr.ltrim();
558 if ( widthStr.getAsInteger(0, value.width) ) {
559 return "malformed width";
564 return "malformed by";
573 // Test writing then reading back custom values
575 TEST(YAMLIO, TestReadWriteMyCustomType) {
576 std::string intermediate;
585 llvm::raw_string_ostream ostr(intermediate);
591 Input yin(intermediate);
592 MyCustomTypeMap map2;
595 EXPECT_FALSE(yin.error());
596 EXPECT_EQ(1, map2.f1.length);
597 EXPECT_EQ(4, map2.f1.width);
598 EXPECT_EQ(100, map2.f2.length);
599 EXPECT_EQ(400, map2.f2.width);
600 EXPECT_EQ(10, map2.f3);
605 //===----------------------------------------------------------------------===//
606 // Test flow sequences
607 //===----------------------------------------------------------------------===//
609 LLVM_YAML_STRONG_TYPEDEF(int, MyNumber)
610 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyNumber)
611 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::StringRef)
616 struct ScalarTraits<MyNumber> {
617 static void output(const MyNumber &value, void *, llvm::raw_ostream &out) {
621 static StringRef input(StringRef scalar, void *, MyNumber &value) {
623 if ( getAsSignedInteger(scalar, 0, n) )
624 return "invalid number";
632 struct NameAndNumbers {
633 llvm::StringRef name;
634 std::vector<llvm::StringRef> strings;
635 std::vector<MyNumber> single;
636 std::vector<MyNumber> numbers;
642 struct MappingTraits<NameAndNumbers> {
643 static void mapping(IO &io, NameAndNumbers& nn) {
644 io.mapRequired("name", nn.name);
645 io.mapRequired("strings", nn.strings);
646 io.mapRequired("single", nn.single);
647 io.mapRequired("numbers", nn.numbers);
655 // Test writing then reading back custom values
657 TEST(YAMLIO, TestReadWriteMyFlowSequence) {
658 std::string intermediate;
662 map.strings.push_back(llvm::StringRef("one"));
663 map.strings.push_back(llvm::StringRef("two"));
664 map.single.push_back(1);
665 map.numbers.push_back(10);
666 map.numbers.push_back(-30);
667 map.numbers.push_back(1024);
669 llvm::raw_string_ostream ostr(intermediate);
673 // Verify sequences were written in flow style
675 llvm::StringRef flowOut(intermediate);
676 EXPECT_NE(llvm::StringRef::npos, flowOut.find("one, two"));
677 EXPECT_NE(llvm::StringRef::npos, flowOut.find("10, -30, 1024"));
681 Input yin(intermediate);
685 EXPECT_FALSE(yin.error());
686 EXPECT_TRUE(map2.name.equals("hello"));
687 EXPECT_EQ(map2.strings.size(), 2UL);
688 EXPECT_TRUE(map2.strings[0].equals("one"));
689 EXPECT_TRUE(map2.strings[1].equals("two"));
690 EXPECT_EQ(map2.single.size(), 1UL);
691 EXPECT_EQ(1, map2.single[0]);
692 EXPECT_EQ(map2.numbers.size(), 3UL);
693 EXPECT_EQ(10, map2.numbers[0]);
694 EXPECT_EQ(-30, map2.numbers[1]);
695 EXPECT_EQ(1024, map2.numbers[2]);
700 //===----------------------------------------------------------------------===//
701 // Test normalizing/denormalizing
702 //===----------------------------------------------------------------------===//
704 LLVM_YAML_STRONG_TYPEDEF(uint32_t, TotalSeconds)
706 typedef std::vector<TotalSeconds> SecondsSequence;
708 LLVM_YAML_IS_SEQUENCE_VECTOR(TotalSeconds)
714 struct MappingTraits<TotalSeconds> {
716 class NormalizedSeconds {
718 NormalizedSeconds(IO &io)
719 : hours(0), minutes(0), seconds(0) {
721 NormalizedSeconds(IO &, TotalSeconds &secs)
723 minutes((secs - (hours*3600))/60),
726 TotalSeconds denormalize(IO &) {
727 return TotalSeconds(hours*3600 + minutes*60 + seconds);
735 static void mapping(IO &io, TotalSeconds &secs) {
736 MappingNormalization<NormalizedSeconds, TotalSeconds> keys(io, secs);
738 io.mapOptional("hours", keys->hours, (uint32_t)0);
739 io.mapOptional("minutes", keys->minutes, (uint8_t)0);
740 io.mapRequired("seconds", keys->seconds);
748 // Test the reading of a yaml sequence of mappings
750 TEST(YAMLIO, TestReadMySecondsSequence) {
752 Input yin("---\n - hours: 1\n seconds: 5\n - seconds: 59\n...\n");
755 EXPECT_FALSE(yin.error());
756 EXPECT_EQ(seq.size(), 2UL);
757 EXPECT_EQ(seq[0], 3605U);
758 EXPECT_EQ(seq[1], 59U);
763 // Test writing then reading back custom values
765 TEST(YAMLIO, TestReadWriteMySecondsSequence) {
766 std::string intermediate;
773 llvm::raw_string_ostream ostr(intermediate);
778 Input yin(intermediate);
779 SecondsSequence seq2;
782 EXPECT_FALSE(yin.error());
783 EXPECT_EQ(seq2.size(), 3UL);
784 EXPECT_EQ(seq2[0], 4000U);
785 EXPECT_EQ(seq2[1], 500U);
786 EXPECT_EQ(seq2[2], 59U);
791 //===----------------------------------------------------------------------===//
792 // Test dynamic typing
793 //===----------------------------------------------------------------------===//
812 struct KindAndFlags {
813 KindAndFlags() : kind(kindA), flags(0) { }
814 KindAndFlags(Kind k, uint32_t f) : kind(k), flags(f) { }
819 typedef std::vector<KindAndFlags> KindAndFlagsSequence;
821 LLVM_YAML_IS_SEQUENCE_VECTOR(KindAndFlags)
826 struct ScalarEnumerationTraits<AFlags> {
827 static void enumeration(IO &io, AFlags &value) {
828 io.enumCase(value, "a1", a1);
829 io.enumCase(value, "a2", a2);
830 io.enumCase(value, "a3", a3);
834 struct ScalarEnumerationTraits<BFlags> {
835 static void enumeration(IO &io, BFlags &value) {
836 io.enumCase(value, "b1", b1);
837 io.enumCase(value, "b2", b2);
838 io.enumCase(value, "b3", b3);
842 struct ScalarEnumerationTraits<Kind> {
843 static void enumeration(IO &io, Kind &value) {
844 io.enumCase(value, "A", kindA);
845 io.enumCase(value, "B", kindB);
849 struct MappingTraits<KindAndFlags> {
850 static void mapping(IO &io, KindAndFlags& kf) {
851 io.mapRequired("kind", kf.kind);
852 // Type of "flags" field varies depending on "kind" field.
853 // Use memcpy here to avoid breaking strict aliasing rules.
854 if (kf.kind == kindA) {
855 AFlags aflags = static_cast<AFlags>(kf.flags);
856 io.mapRequired("flags", aflags);
859 BFlags bflags = static_cast<BFlags>(kf.flags);
860 io.mapRequired("flags", bflags);
870 // Test the reading of a yaml sequence dynamic types
872 TEST(YAMLIO, TestReadKindAndFlagsSequence) {
873 KindAndFlagsSequence seq;
874 Input yin("---\n - kind: A\n flags: a2\n - kind: B\n flags: b1\n...\n");
877 EXPECT_FALSE(yin.error());
878 EXPECT_EQ(seq.size(), 2UL);
879 EXPECT_EQ(seq[0].kind, kindA);
880 EXPECT_EQ(seq[0].flags, (uint32_t)a2);
881 EXPECT_EQ(seq[1].kind, kindB);
882 EXPECT_EQ(seq[1].flags, (uint32_t)b1);
886 // Test writing then reading back dynamic types
888 TEST(YAMLIO, TestReadWriteKindAndFlagsSequence) {
889 std::string intermediate;
891 KindAndFlagsSequence seq;
892 seq.push_back(KindAndFlags(kindA,a1));
893 seq.push_back(KindAndFlags(kindB,b1));
894 seq.push_back(KindAndFlags(kindA,a2));
895 seq.push_back(KindAndFlags(kindB,b2));
896 seq.push_back(KindAndFlags(kindA,a3));
898 llvm::raw_string_ostream ostr(intermediate);
903 Input yin(intermediate);
904 KindAndFlagsSequence seq2;
907 EXPECT_FALSE(yin.error());
908 EXPECT_EQ(seq2.size(), 5UL);
909 EXPECT_EQ(seq2[0].kind, kindA);
910 EXPECT_EQ(seq2[0].flags, (uint32_t)a1);
911 EXPECT_EQ(seq2[1].kind, kindB);
912 EXPECT_EQ(seq2[1].flags, (uint32_t)b1);
913 EXPECT_EQ(seq2[2].kind, kindA);
914 EXPECT_EQ(seq2[2].flags, (uint32_t)a2);
915 EXPECT_EQ(seq2[3].kind, kindB);
916 EXPECT_EQ(seq2[3].flags, (uint32_t)b2);
917 EXPECT_EQ(seq2[4].kind, kindA);
918 EXPECT_EQ(seq2[4].flags, (uint32_t)a3);
923 //===----------------------------------------------------------------------===//
924 // Test document list
925 //===----------------------------------------------------------------------===//
931 typedef std::vector<FooBarMap> FooBarMapDocumentList;
933 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(FooBarMap)
939 struct MappingTraits<FooBarMap> {
940 static void mapping(IO &io, FooBarMap& fb) {
941 io.mapRequired("foo", fb.foo);
942 io.mapRequired("bar", fb.bar);
950 // Test the reading of a yaml mapping
952 TEST(YAMLIO, TestDocRead) {
954 Input yin("---\nfoo: 3\nbar: 5\n...\n");
957 EXPECT_FALSE(yin.error());
958 EXPECT_EQ(doc.foo, 3);
959 EXPECT_EQ(doc.bar,5);
965 // Test writing then reading back a sequence of mappings
967 TEST(YAMLIO, TestSequenceDocListWriteAndRead) {
968 std::string intermediate;
976 std::vector<FooBarMap> docList;
977 docList.push_back(doc1);
978 docList.push_back(doc2);
980 llvm::raw_string_ostream ostr(intermediate);
987 Input yin(intermediate);
988 std::vector<FooBarMap> docList2;
991 EXPECT_FALSE(yin.error());
992 EXPECT_EQ(docList2.size(), 2UL);
993 FooBarMap& map1 = docList2[0];
994 FooBarMap& map2 = docList2[1];
995 EXPECT_EQ(map1.foo, 10);
996 EXPECT_EQ(map1.bar, -3);
997 EXPECT_EQ(map2.foo, 257);
998 EXPECT_EQ(map2.bar, 0);
1002 //===----------------------------------------------------------------------===//
1003 // Test document tags
1004 //===----------------------------------------------------------------------===//
1007 MyDouble() : value(0.0) { }
1008 MyDouble(double x) : value(x) { }
1012 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyDouble)
1018 struct MappingTraits<MyDouble> {
1019 static void mapping(IO &io, MyDouble &d) {
1020 if (io.mapTag("!decimal", true)) {
1021 mappingDecimal(io, d);
1022 } else if (io.mapTag("!fraction")) {
1023 mappingFraction(io, d);
1026 static void mappingDecimal(IO &io, MyDouble &d) {
1027 io.mapRequired("value", d.value);
1029 static void mappingFraction(IO &io, MyDouble &d) {
1031 io.mapRequired("numerator", num);
1032 io.mapRequired("denominator", denom);
1033 // convert fraction to double
1034 d.value = num/denom;
1042 // Test the reading of two different tagged yaml documents.
1044 TEST(YAMLIO, TestTaggedDocuments) {
1045 std::vector<MyDouble> docList;
1046 Input yin("--- !decimal\nvalue: 3.0\n"
1047 "--- !fraction\nnumerator: 9.0\ndenominator: 2\n...\n");
1049 EXPECT_FALSE(yin.error());
1050 EXPECT_EQ(docList.size(), 2UL);
1051 EXPECT_EQ(docList[0].value, 3.0);
1052 EXPECT_EQ(docList[1].value, 4.5);
1058 // Test writing then reading back tagged documents
1060 TEST(YAMLIO, TestTaggedDocumentsWriteAndRead) {
1061 std::string intermediate;
1065 std::vector<MyDouble> docList;
1066 docList.push_back(a);
1067 docList.push_back(b);
1069 llvm::raw_string_ostream ostr(intermediate);
1075 Input yin(intermediate);
1076 std::vector<MyDouble> docList2;
1079 EXPECT_FALSE(yin.error());
1080 EXPECT_EQ(docList2.size(), 2UL);
1081 EXPECT_EQ(docList2[0].value, 10.25);
1082 EXPECT_EQ(docList2[1].value, -3.75);
1087 //===----------------------------------------------------------------------===//
1088 // Test dyn_cast<> on IO object
1089 //===----------------------------------------------------------------------===//
1094 typedef std::vector<DynCast> DynCastSequence;
1096 LLVM_YAML_IS_SEQUENCE_VECTOR(DynCast)
1101 struct MappingTraits<DynCast> {
1102 static void mapping(IO &io, DynCast& info) {
1103 // Change 10 to 13 when writing yaml.
1104 if (Output *output = dyn_cast<Output>(&io)) {
1106 if (info.value == 10)
1109 io.mapRequired("value", info.value);
1110 // Change 20 to 23 when parsing yaml.
1111 if (Input *input = dyn_cast<Input>(&io)) {
1113 if (info.value == 20)
1122 // Test writing then reading back a sequence of mappings
1124 TEST(YAMLIO, TestDynCast) {
1125 std::string intermediate;
1133 DynCastSequence seq;
1134 seq.push_back(entry1);
1135 seq.push_back(entry2);
1136 seq.push_back(entry3);
1138 llvm::raw_string_ostream ostr(intermediate);
1144 Input yin(intermediate);
1145 DynCastSequence seq2;
1148 EXPECT_FALSE(yin.error());
1149 EXPECT_EQ(seq2.size(), 3UL);
1150 EXPECT_EQ(seq2[0].value, 13); // Verify changed to 13.
1151 EXPECT_EQ(seq2[1].value, 23); // Verify changed to 23.
1152 EXPECT_EQ(seq2[2].value, 30); // Verify stays same.
1158 //===----------------------------------------------------------------------===//
1159 // Test error handling
1160 //===----------------------------------------------------------------------===//
1164 static void suppressErrorMessages(const llvm::SMDiagnostic &, void *) {
1169 // Test error handling of unknown enumerated scalar
1171 TEST(YAMLIO, TestColorsReadError) {
1179 suppressErrorMessages);
1181 EXPECT_TRUE(yin.error());
1186 // Test error handling of flow sequence with unknown value
1188 TEST(YAMLIO, TestFlagsReadError) {
1192 "f2: [ round, hollow ]\n"
1196 suppressErrorMessages);
1199 EXPECT_TRUE(yin.error());
1204 // Test error handling reading built-in uint8_t type
1206 LLVM_YAML_IS_SEQUENCE_VECTOR(uint8_t)
1207 TEST(YAMLIO, TestReadBuiltInTypesUint8Error) {
1208 std::vector<uint8_t> seq;
1215 suppressErrorMessages);
1218 EXPECT_TRUE(yin.error());
1223 // Test error handling reading built-in uint16_t type
1225 LLVM_YAML_IS_SEQUENCE_VECTOR(uint16_t)
1226 TEST(YAMLIO, TestReadBuiltInTypesUint16Error) {
1227 std::vector<uint16_t> seq;
1234 suppressErrorMessages);
1237 EXPECT_TRUE(yin.error());
1242 // Test error handling reading built-in uint32_t type
1244 LLVM_YAML_IS_SEQUENCE_VECTOR(uint32_t)
1245 TEST(YAMLIO, TestReadBuiltInTypesUint32Error) {
1246 std::vector<uint32_t> seq;
1253 suppressErrorMessages);
1256 EXPECT_TRUE(yin.error());
1261 // Test error handling reading built-in uint64_t type
1263 LLVM_YAML_IS_SEQUENCE_VECTOR(uint64_t)
1264 TEST(YAMLIO, TestReadBuiltInTypesUint64Error) {
1265 std::vector<uint64_t> seq;
1267 "- 18446744073709551615\n"
1269 "- 19446744073709551615\n"
1272 suppressErrorMessages);
1275 EXPECT_TRUE(yin.error());
1280 // Test error handling reading built-in int8_t type
1282 LLVM_YAML_IS_SEQUENCE_VECTOR(int8_t)
1283 TEST(YAMLIO, TestReadBuiltInTypesint8OverError) {
1284 std::vector<int8_t> seq;
1292 suppressErrorMessages);
1295 EXPECT_TRUE(yin.error());
1299 // Test error handling reading built-in int8_t type
1301 TEST(YAMLIO, TestReadBuiltInTypesint8UnderError) {
1302 std::vector<int8_t> seq;
1310 suppressErrorMessages);
1313 EXPECT_TRUE(yin.error());
1318 // Test error handling reading built-in int16_t type
1320 LLVM_YAML_IS_SEQUENCE_VECTOR(int16_t)
1321 TEST(YAMLIO, TestReadBuiltInTypesint16UnderError) {
1322 std::vector<int16_t> seq;
1330 suppressErrorMessages);
1333 EXPECT_TRUE(yin.error());
1338 // Test error handling reading built-in int16_t type
1340 TEST(YAMLIO, TestReadBuiltInTypesint16OverError) {
1341 std::vector<int16_t> seq;
1349 suppressErrorMessages);
1352 EXPECT_TRUE(yin.error());
1357 // Test error handling reading built-in int32_t type
1359 LLVM_YAML_IS_SEQUENCE_VECTOR(int32_t)
1360 TEST(YAMLIO, TestReadBuiltInTypesint32UnderError) {
1361 std::vector<int32_t> seq;
1369 suppressErrorMessages);
1372 EXPECT_TRUE(yin.error());
1376 // Test error handling reading built-in int32_t type
1378 TEST(YAMLIO, TestReadBuiltInTypesint32OverError) {
1379 std::vector<int32_t> seq;
1387 suppressErrorMessages);
1390 EXPECT_TRUE(yin.error());
1395 // Test error handling reading built-in int64_t type
1397 LLVM_YAML_IS_SEQUENCE_VECTOR(int64_t)
1398 TEST(YAMLIO, TestReadBuiltInTypesint64UnderError) {
1399 std::vector<int64_t> seq;
1401 "- -9223372036854775808\n"
1403 "- 9223372036854775807\n"
1404 "- -9223372036854775809\n"
1407 suppressErrorMessages);
1410 EXPECT_TRUE(yin.error());
1414 // Test error handling reading built-in int64_t type
1416 TEST(YAMLIO, TestReadBuiltInTypesint64OverError) {
1417 std::vector<int64_t> seq;
1419 "- -9223372036854775808\n"
1421 "- 9223372036854775807\n"
1422 "- 9223372036854775809\n"
1425 suppressErrorMessages);
1428 EXPECT_TRUE(yin.error());
1432 // Test error handling reading built-in float type
1434 LLVM_YAML_IS_SEQUENCE_VECTOR(float)
1435 TEST(YAMLIO, TestReadBuiltInTypesFloatError) {
1436 std::vector<float> seq;
1444 suppressErrorMessages);
1447 EXPECT_TRUE(yin.error());
1451 // Test error handling reading built-in float type
1453 LLVM_YAML_IS_SEQUENCE_VECTOR(double)
1454 TEST(YAMLIO, TestReadBuiltInTypesDoubleError) {
1455 std::vector<double> seq;
1463 suppressErrorMessages);
1466 EXPECT_TRUE(yin.error());
1470 // Test error handling reading built-in Hex8 type
1472 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex8)
1473 TEST(YAMLIO, TestReadBuiltInTypesHex8Error) {
1474 std::vector<Hex8> seq;
1481 suppressErrorMessages);
1484 EXPECT_TRUE(yin.error());
1489 // Test error handling reading built-in Hex16 type
1491 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex16)
1492 TEST(YAMLIO, TestReadBuiltInTypesHex16Error) {
1493 std::vector<Hex16> seq;
1500 suppressErrorMessages);
1503 EXPECT_TRUE(yin.error());
1507 // Test error handling reading built-in Hex32 type
1509 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex32)
1510 TEST(YAMLIO, TestReadBuiltInTypesHex32Error) {
1511 std::vector<Hex32> seq;
1518 suppressErrorMessages);
1521 EXPECT_TRUE(yin.error());
1525 // Test error handling reading built-in Hex64 type
1527 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex64)
1528 TEST(YAMLIO, TestReadBuiltInTypesHex64Error) {
1529 std::vector<Hex64> seq;
1532 "- 0xFFEEDDCCBBAA9988\n"
1533 "- 0x12345567890ABCDEF0\n"
1536 suppressErrorMessages);
1539 EXPECT_TRUE(yin.error());
1542 TEST(YAMLIO, TestMalformedMapFailsGracefully) {
1545 // We pass the suppressErrorMessages handler to handle the error
1546 // message generated in the constructor of Input.
1547 Input yin("{foo:3, bar: 5}", /*Ctxt=*/NULL, suppressErrorMessages);
1549 EXPECT_TRUE(yin.error());
1553 Input yin("---\nfoo:3\nbar: 5\n...\n", /*Ctxt=*/NULL, suppressErrorMessages);
1555 EXPECT_TRUE(yin.error());
1559 struct OptionalTest {
1560 std::vector<int> Numbers;
1563 struct OptionalTestSeq {
1564 std::vector<OptionalTest> Tests;
1567 LLVM_YAML_IS_SEQUENCE_VECTOR(OptionalTest)
1571 struct MappingTraits<OptionalTest> {
1572 static void mapping(IO& IO, OptionalTest &OT) {
1573 IO.mapOptional("Numbers", OT.Numbers);
1578 struct MappingTraits<OptionalTestSeq> {
1579 static void mapping(IO &IO, OptionalTestSeq &OTS) {
1580 IO.mapOptional("Tests", OTS.Tests);
1586 TEST(YAMLIO, SequenceElideTest) {
1587 // Test that writing out a purely optional structure with its fields set to
1588 // default followed by other data is properly read back in.
1589 OptionalTestSeq Seq;
1590 OptionalTest One, Two, Three, Four;
1591 int N[] = {1, 2, 3};
1592 Three.Numbers.assign(N, N + 3);
1593 Seq.Tests.push_back(One);
1594 Seq.Tests.push_back(Two);
1595 Seq.Tests.push_back(Three);
1596 Seq.Tests.push_back(Four);
1598 std::string intermediate;
1600 llvm::raw_string_ostream ostr(intermediate);
1605 Input yin(intermediate);
1606 OptionalTestSeq Seq2;
1609 EXPECT_FALSE(yin.error());
1611 EXPECT_EQ(4UL, Seq2.Tests.size());
1613 EXPECT_TRUE(Seq2.Tests[0].Numbers.empty());
1614 EXPECT_TRUE(Seq2.Tests[1].Numbers.empty());
1616 EXPECT_EQ(1, Seq2.Tests[2].Numbers[0]);
1617 EXPECT_EQ(2, Seq2.Tests[2].Numbers[1]);
1618 EXPECT_EQ(3, Seq2.Tests[2].Numbers[2]);
1620 EXPECT_TRUE(Seq2.Tests[3].Numbers.empty());
1623 TEST(YAMLIO, TestEmptyStringFailsForMapWithRequiredFields) {
1627 EXPECT_TRUE(yin.error());
1630 TEST(YAMLIO, TestEmptyStringSucceedsForMapWithOptionalFields) {
1634 EXPECT_FALSE(yin.error());
1637 TEST(YAMLIO, TestEmptyStringSucceedsForSequence) {
1638 std::vector<uint8_t> seq;
1639 Input yin("", /*Ctxt=*/NULL, suppressErrorMessages);
1642 EXPECT_FALSE(yin.error());
1643 EXPECT_TRUE(seq.empty());