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) {
61 Input yin("---\nfoo: 3\nbar: 5\n...\n");
64 EXPECT_FALSE(yin.error());
65 EXPECT_EQ(doc.foo, 3);
71 // Test the reading of a yaml sequence of mappings
73 TEST(YAMLIO, TestSequenceMapRead) {
75 Input yin("---\n - foo: 3\n bar: 5\n - foo: 7\n bar: 9\n...\n");
78 EXPECT_FALSE(yin.error());
79 EXPECT_EQ(seq.size(), 2UL);
80 FooBar& map1 = seq[0];
81 FooBar& map2 = seq[1];
82 EXPECT_EQ(map1.foo, 3);
83 EXPECT_EQ(map1.bar, 5);
84 EXPECT_EQ(map2.foo, 7);
85 EXPECT_EQ(map2.bar, 9);
90 // Test writing then reading back a sequence of mappings
92 TEST(YAMLIO, TestSequenceMapWriteAndRead) {
93 std::string intermediate;
102 seq.push_back(entry1);
103 seq.push_back(entry2);
105 llvm::raw_string_ostream ostr(intermediate);
111 Input yin(intermediate);
115 EXPECT_FALSE(yin.error());
116 EXPECT_EQ(seq2.size(), 2UL);
117 FooBar& map1 = seq2[0];
118 FooBar& map2 = seq2[1];
119 EXPECT_EQ(map1.foo, 10);
120 EXPECT_EQ(map1.bar, -3);
121 EXPECT_EQ(map2.foo, 257);
122 EXPECT_EQ(map2.bar, 0);
127 //===----------------------------------------------------------------------===//
128 // Test built-in types
129 //===----------------------------------------------------------------------===//
131 struct BuiltInTypes {
153 struct MappingTraits<BuiltInTypes> {
154 static void mapping(IO &io, BuiltInTypes& bt) {
155 io.mapRequired("str", bt.str);
156 io.mapRequired("u64", bt.u64);
157 io.mapRequired("u32", bt.u32);
158 io.mapRequired("u16", bt.u16);
159 io.mapRequired("u8", bt.u8);
160 io.mapRequired("b", bt.b);
161 io.mapRequired("s64", bt.s64);
162 io.mapRequired("s32", bt.s32);
163 io.mapRequired("s16", bt.s16);
164 io.mapRequired("s8", bt.s8);
165 io.mapRequired("f", bt.f);
166 io.mapRequired("d", bt.d);
167 io.mapRequired("h8", bt.h8);
168 io.mapRequired("h16", bt.h16);
169 io.mapRequired("h32", bt.h32);
170 io.mapRequired("h64", bt.h64);
178 // Test the reading of all built-in scalar conversions
180 TEST(YAMLIO, TestReadBuiltInTypes) {
198 "h64: 0xFEDCBA9876543210\n"
202 EXPECT_FALSE(yin.error());
203 EXPECT_TRUE(map.str.equals("hello there"));
204 EXPECT_EQ(map.u64, 5000000000ULL);
205 EXPECT_EQ(map.u32, 4000000000U);
206 EXPECT_EQ(map.u16, 65000);
207 EXPECT_EQ(map.u8, 255);
208 EXPECT_EQ(map.b, false);
209 EXPECT_EQ(map.s64, -5000000000LL);
210 EXPECT_EQ(map.s32, -2000000000L);
211 EXPECT_EQ(map.s16, -32000);
212 EXPECT_EQ(map.s8, -127);
213 EXPECT_EQ(map.f, 137.125);
214 EXPECT_EQ(map.d, -2.8625);
215 EXPECT_EQ(map.h8, Hex8(255));
216 EXPECT_EQ(map.h16, Hex16(0x8765));
217 EXPECT_EQ(map.h32, Hex32(0xFEDCBA98));
218 EXPECT_EQ(map.h64, Hex64(0xFEDCBA9876543210LL));
223 // Test writing then reading back all built-in scalar types
225 TEST(YAMLIO, TestReadWriteBuiltInTypes) {
226 std::string intermediate;
230 map.u64 = 6000000000ULL;
231 map.u32 = 3000000000U;
235 map.s64 = -6000000000LL;
236 map.s32 = -2000000000;
243 map.h32 = 3000000000U;
244 map.h64 = 6000000000LL;
246 llvm::raw_string_ostream ostr(intermediate);
252 Input yin(intermediate);
256 EXPECT_FALSE(yin.error());
257 EXPECT_TRUE(map.str.equals("one two"));
258 EXPECT_EQ(map.u64, 6000000000ULL);
259 EXPECT_EQ(map.u32, 3000000000U);
260 EXPECT_EQ(map.u16, 50000);
261 EXPECT_EQ(map.u8, 254);
262 EXPECT_EQ(map.b, true);
263 EXPECT_EQ(map.s64, -6000000000LL);
264 EXPECT_EQ(map.s32, -2000000000L);
265 EXPECT_EQ(map.s16, -32000);
266 EXPECT_EQ(map.s8, -128);
267 EXPECT_EQ(map.f, 3.25);
268 EXPECT_EQ(map.d, -2.8625);
269 EXPECT_EQ(map.h8, Hex8(254));
270 EXPECT_EQ(map.h16, Hex16(50000));
271 EXPECT_EQ(map.h32, Hex32(3000000000U));
272 EXPECT_EQ(map.h64, Hex64(6000000000LL));
278 //===----------------------------------------------------------------------===//
279 // Test ScalarEnumerationTraits
280 //===----------------------------------------------------------------------===//
301 struct ScalarEnumerationTraits<Colors> {
302 static void enumeration(IO &io, Colors &value) {
303 io.enumCase(value, "red", cRed);
304 io.enumCase(value, "blue", cBlue);
305 io.enumCase(value, "green", cGreen);
306 io.enumCase(value, "yellow",cYellow);
310 struct MappingTraits<ColorMap> {
311 static void mapping(IO &io, ColorMap& c) {
312 io.mapRequired("c1", c.c1);
313 io.mapRequired("c2", c.c2);
314 io.mapRequired("c3", c.c3);
315 io.mapOptional("c4", c.c4, cBlue); // supplies default
316 io.mapOptional("c5", c.c5, cYellow); // supplies default
317 io.mapOptional("c6", c.c6, cRed); // supplies default
325 // Test reading enumerated scalars
327 TEST(YAMLIO, TestEnumRead) {
337 EXPECT_FALSE(yin.error());
338 EXPECT_EQ(cBlue, map.c1);
339 EXPECT_EQ(cRed, map.c2);
340 EXPECT_EQ(cGreen, map.c3);
341 EXPECT_EQ(cBlue, map.c4); // tests default
342 EXPECT_EQ(cYellow,map.c5); // tests overridden
343 EXPECT_EQ(cRed, map.c6); // tests default
348 //===----------------------------------------------------------------------===//
349 // Test ScalarBitSetTraits
350 //===----------------------------------------------------------------------===//
359 inline MyFlags operator|(MyFlags a, MyFlags b) {
360 return static_cast<MyFlags>(
361 static_cast<uint32_t>(a) | static_cast<uint32_t>(b));
375 struct ScalarBitSetTraits<MyFlags> {
376 static void bitset(IO &io, MyFlags &value) {
377 io.bitSetCase(value, "big", flagBig);
378 io.bitSetCase(value, "flat", flagFlat);
379 io.bitSetCase(value, "round", flagRound);
380 io.bitSetCase(value, "pointy",flagPointy);
384 struct MappingTraits<FlagsMap> {
385 static void mapping(IO &io, FlagsMap& c) {
386 io.mapRequired("f1", c.f1);
387 io.mapRequired("f2", c.f2);
388 io.mapRequired("f3", c.f3);
389 io.mapOptional("f4", c.f4, MyFlags(flagRound));
397 // Test reading flow sequence representing bit-mask values
399 TEST(YAMLIO, TestFlagsRead) {
403 "f2: [ round, flat ]\n"
408 EXPECT_FALSE(yin.error());
409 EXPECT_EQ(flagBig, map.f1);
410 EXPECT_EQ(flagRound|flagFlat, map.f2);
411 EXPECT_EQ(flagNone, map.f3); // check empty set
412 EXPECT_EQ(flagRound, map.f4); // check optional key
417 // Test writing then reading back bit-mask values
419 TEST(YAMLIO, TestReadWriteFlags) {
420 std::string intermediate;
424 map.f2 = flagRound | flagFlat;
428 llvm::raw_string_ostream ostr(intermediate);
434 Input yin(intermediate);
438 EXPECT_FALSE(yin.error());
439 EXPECT_EQ(flagBig, map2.f1);
440 EXPECT_EQ(flagRound|flagFlat, map2.f2);
441 EXPECT_EQ(flagNone, map2.f3);
442 //EXPECT_EQ(flagRound, map2.f4); // check optional key
448 //===----------------------------------------------------------------------===//
450 //===----------------------------------------------------------------------===//
452 struct MyCustomType {
457 struct MyCustomTypeMap {
467 struct MappingTraits<MyCustomTypeMap> {
468 static void mapping(IO &io, MyCustomTypeMap& s) {
469 io.mapRequired("f1", s.f1);
470 io.mapRequired("f2", s.f2);
471 io.mapRequired("f3", s.f3);
474 // MyCustomType is formatted as a yaml scalar. A value of
475 // {length=3, width=4} would be represented in yaml as "3 by 4".
477 struct ScalarTraits<MyCustomType> {
478 static void output(const MyCustomType &value, void* ctxt, llvm::raw_ostream &out) {
479 out << llvm::format("%d by %d", value.length, value.width);
481 static StringRef input(StringRef scalar, void* ctxt, MyCustomType &value) {
482 size_t byStart = scalar.find("by");
483 if ( byStart != StringRef::npos ) {
484 StringRef lenStr = scalar.slice(0, byStart);
485 lenStr = lenStr.rtrim();
486 if ( lenStr.getAsInteger(0, value.length) ) {
487 return "malformed length";
489 StringRef widthStr = scalar.drop_front(byStart+2);
490 widthStr = widthStr.ltrim();
491 if ( widthStr.getAsInteger(0, value.width) ) {
492 return "malformed width";
497 return "malformed by";
506 // Test writing then reading back custom values
508 TEST(YAMLIO, TestReadWriteMyCustomType) {
509 std::string intermediate;
518 llvm::raw_string_ostream ostr(intermediate);
524 Input yin(intermediate);
525 MyCustomTypeMap map2;
528 EXPECT_FALSE(yin.error());
529 EXPECT_EQ(1, map2.f1.length);
530 EXPECT_EQ(4, map2.f1.width);
531 EXPECT_EQ(100, map2.f2.length);
532 EXPECT_EQ(400, map2.f2.width);
533 EXPECT_EQ(10, map2.f3);
538 //===----------------------------------------------------------------------===//
539 // Test flow sequences
540 //===----------------------------------------------------------------------===//
542 LLVM_YAML_STRONG_TYPEDEF(int, MyNumber)
543 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(MyNumber)
544 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::StringRef)
549 struct ScalarTraits<MyNumber> {
550 static void output(const MyNumber &value, void *, llvm::raw_ostream &out) {
554 static StringRef input(StringRef scalar, void *, MyNumber &value) {
556 if ( getAsSignedInteger(scalar, 0, n) )
557 return "invalid number";
565 struct NameAndNumbers {
566 llvm::StringRef name;
567 std::vector<llvm::StringRef> strings;
568 std::vector<MyNumber> single;
569 std::vector<MyNumber> numbers;
575 struct MappingTraits<NameAndNumbers> {
576 static void mapping(IO &io, NameAndNumbers& nn) {
577 io.mapRequired("name", nn.name);
578 io.mapRequired("strings", nn.strings);
579 io.mapRequired("single", nn.single);
580 io.mapRequired("numbers", nn.numbers);
588 // Test writing then reading back custom values
590 TEST(YAMLIO, TestReadWriteMyFlowSequence) {
591 std::string intermediate;
595 map.strings.push_back(llvm::StringRef("one"));
596 map.strings.push_back(llvm::StringRef("two"));
597 map.single.push_back(1);
598 map.numbers.push_back(10);
599 map.numbers.push_back(-30);
600 map.numbers.push_back(1024);
602 llvm::raw_string_ostream ostr(intermediate);
606 // Verify sequences were written in flow style
608 llvm::StringRef flowOut(intermediate);
609 EXPECT_NE(llvm::StringRef::npos, flowOut.find("one, two"));
610 EXPECT_NE(llvm::StringRef::npos, flowOut.find("10, -30, 1024"));
614 Input yin(intermediate);
618 EXPECT_FALSE(yin.error());
619 EXPECT_TRUE(map2.name.equals("hello"));
620 EXPECT_EQ(map2.strings.size(), 2UL);
621 EXPECT_TRUE(map2.strings[0].equals("one"));
622 EXPECT_TRUE(map2.strings[1].equals("two"));
623 EXPECT_EQ(map2.single.size(), 1UL);
624 EXPECT_EQ(1, map2.single[0]);
625 EXPECT_EQ(map2.numbers.size(), 3UL);
626 EXPECT_EQ(10, map2.numbers[0]);
627 EXPECT_EQ(-30, map2.numbers[1]);
628 EXPECT_EQ(1024, map2.numbers[2]);
633 //===----------------------------------------------------------------------===//
634 // Test normalizing/denormalizing
635 //===----------------------------------------------------------------------===//
637 LLVM_YAML_STRONG_TYPEDEF(uint32_t, TotalSeconds)
639 typedef std::vector<TotalSeconds> SecondsSequence;
641 LLVM_YAML_IS_SEQUENCE_VECTOR(TotalSeconds)
647 struct MappingTraits<TotalSeconds> {
649 class NormalizedSeconds {
651 NormalizedSeconds(IO &io)
652 : hours(0), minutes(0), seconds(0) {
654 NormalizedSeconds(IO &, TotalSeconds &secs)
656 minutes((secs - (hours*3600))/60),
659 TotalSeconds denormalize(IO &) {
660 return TotalSeconds(hours*3600 + minutes*60 + seconds);
668 static void mapping(IO &io, TotalSeconds &secs) {
669 MappingNormalization<NormalizedSeconds, TotalSeconds> keys(io, secs);
671 io.mapOptional("hours", keys->hours, (uint32_t)0);
672 io.mapOptional("minutes", keys->minutes, (uint8_t)0);
673 io.mapRequired("seconds", keys->seconds);
681 // Test the reading of a yaml sequence of mappings
683 TEST(YAMLIO, TestReadMySecondsSequence) {
685 Input yin("---\n - hours: 1\n seconds: 5\n - seconds: 59\n...\n");
688 EXPECT_FALSE(yin.error());
689 EXPECT_EQ(seq.size(), 2UL);
690 EXPECT_EQ(seq[0], 3605U);
691 EXPECT_EQ(seq[1], 59U);
696 // Test writing then reading back custom values
698 TEST(YAMLIO, TestReadWriteMySecondsSequence) {
699 std::string intermediate;
706 llvm::raw_string_ostream ostr(intermediate);
711 Input yin(intermediate);
712 SecondsSequence seq2;
715 EXPECT_FALSE(yin.error());
716 EXPECT_EQ(seq2.size(), 3UL);
717 EXPECT_EQ(seq2[0], 4000U);
718 EXPECT_EQ(seq2[1], 500U);
719 EXPECT_EQ(seq2[2], 59U);
724 //===----------------------------------------------------------------------===//
725 // Test dynamic typing
726 //===----------------------------------------------------------------------===//
745 struct KindAndFlags {
746 KindAndFlags() : kind(kindA), flags(0) { }
747 KindAndFlags(Kind k, uint32_t f) : kind(k), flags(f) { }
752 typedef std::vector<KindAndFlags> KindAndFlagsSequence;
754 LLVM_YAML_IS_SEQUENCE_VECTOR(KindAndFlags)
759 struct ScalarEnumerationTraits<AFlags> {
760 static void enumeration(IO &io, AFlags &value) {
761 io.enumCase(value, "a1", a1);
762 io.enumCase(value, "a2", a2);
763 io.enumCase(value, "a3", a3);
767 struct ScalarEnumerationTraits<BFlags> {
768 static void enumeration(IO &io, BFlags &value) {
769 io.enumCase(value, "b1", b1);
770 io.enumCase(value, "b2", b2);
771 io.enumCase(value, "b3", b3);
775 struct ScalarEnumerationTraits<Kind> {
776 static void enumeration(IO &io, Kind &value) {
777 io.enumCase(value, "A", kindA);
778 io.enumCase(value, "B", kindB);
782 struct MappingTraits<KindAndFlags> {
783 static void mapping(IO &io, KindAndFlags& kf) {
784 io.mapRequired("kind", kf.kind);
785 // Type of "flags" field varies depending on "kind" field.
786 // Use memcpy here to avoid breaking strict aliasing rules.
787 if (kf.kind == kindA) {
788 AFlags aflags = static_cast<AFlags>(kf.flags);
789 io.mapRequired("flags", aflags);
792 BFlags bflags = static_cast<BFlags>(kf.flags);
793 io.mapRequired("flags", bflags);
803 // Test the reading of a yaml sequence dynamic types
805 TEST(YAMLIO, TestReadKindAndFlagsSequence) {
806 KindAndFlagsSequence seq;
807 Input yin("---\n - kind: A\n flags: a2\n - kind: B\n flags: b1\n...\n");
810 EXPECT_FALSE(yin.error());
811 EXPECT_EQ(seq.size(), 2UL);
812 EXPECT_EQ(seq[0].kind, kindA);
813 EXPECT_EQ(seq[0].flags, (uint32_t)a2);
814 EXPECT_EQ(seq[1].kind, kindB);
815 EXPECT_EQ(seq[1].flags, (uint32_t)b1);
819 // Test writing then reading back dynamic types
821 TEST(YAMLIO, TestReadWriteKindAndFlagsSequence) {
822 std::string intermediate;
824 KindAndFlagsSequence seq;
825 seq.push_back(KindAndFlags(kindA,a1));
826 seq.push_back(KindAndFlags(kindB,b1));
827 seq.push_back(KindAndFlags(kindA,a2));
828 seq.push_back(KindAndFlags(kindB,b2));
829 seq.push_back(KindAndFlags(kindA,a3));
831 llvm::raw_string_ostream ostr(intermediate);
836 Input yin(intermediate);
837 KindAndFlagsSequence seq2;
840 EXPECT_FALSE(yin.error());
841 EXPECT_EQ(seq2.size(), 5UL);
842 EXPECT_EQ(seq2[0].kind, kindA);
843 EXPECT_EQ(seq2[0].flags, (uint32_t)a1);
844 EXPECT_EQ(seq2[1].kind, kindB);
845 EXPECT_EQ(seq2[1].flags, (uint32_t)b1);
846 EXPECT_EQ(seq2[2].kind, kindA);
847 EXPECT_EQ(seq2[2].flags, (uint32_t)a2);
848 EXPECT_EQ(seq2[3].kind, kindB);
849 EXPECT_EQ(seq2[3].flags, (uint32_t)b2);
850 EXPECT_EQ(seq2[4].kind, kindA);
851 EXPECT_EQ(seq2[4].flags, (uint32_t)a3);
856 //===----------------------------------------------------------------------===//
857 // Test document list
858 //===----------------------------------------------------------------------===//
864 typedef std::vector<FooBarMap> FooBarMapDocumentList;
866 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(FooBarMap)
872 struct MappingTraits<FooBarMap> {
873 static void mapping(IO &io, FooBarMap& fb) {
874 io.mapRequired("foo", fb.foo);
875 io.mapRequired("bar", fb.bar);
883 // Test the reading of a yaml mapping
885 TEST(YAMLIO, TestDocRead) {
887 Input yin("---\nfoo: 3\nbar: 5\n...\n");
890 EXPECT_FALSE(yin.error());
891 EXPECT_EQ(doc.foo, 3);
892 EXPECT_EQ(doc.bar,5);
898 // Test writing then reading back a sequence of mappings
900 TEST(YAMLIO, TestSequenceDocListWriteAndRead) {
901 std::string intermediate;
909 std::vector<FooBarMap> docList;
910 docList.push_back(doc1);
911 docList.push_back(doc2);
913 llvm::raw_string_ostream ostr(intermediate);
920 Input yin(intermediate);
921 std::vector<FooBarMap> docList2;
924 EXPECT_FALSE(yin.error());
925 EXPECT_EQ(docList2.size(), 2UL);
926 FooBarMap& map1 = docList2[0];
927 FooBarMap& map2 = docList2[1];
928 EXPECT_EQ(map1.foo, 10);
929 EXPECT_EQ(map1.bar, -3);
930 EXPECT_EQ(map2.foo, 257);
931 EXPECT_EQ(map2.bar, 0);
936 //===----------------------------------------------------------------------===//
937 // Test error handling
938 //===----------------------------------------------------------------------===//
942 static void suppressErrorMessages(const llvm::SMDiagnostic &, void *) {
947 // Test error handling of unknown enumerated scalar
949 TEST(YAMLIO, TestColorsReadError) {
956 yin.setDiagHandler(suppressErrorMessages);
958 EXPECT_TRUE(yin.error());
963 // Test error handling of flow sequence with unknown value
965 TEST(YAMLIO, TestFlagsReadError) {
969 "f2: [ round, hollow ]\n"
972 yin.setDiagHandler(suppressErrorMessages);
975 EXPECT_TRUE(yin.error());
980 // Test error handling reading built-in uint8_t type
982 LLVM_YAML_IS_SEQUENCE_VECTOR(uint8_t)
983 TEST(YAMLIO, TestReadBuiltInTypesUint8Error) {
984 std::vector<uint8_t> seq;
990 yin.setDiagHandler(suppressErrorMessages);
993 EXPECT_TRUE(yin.error());
998 // Test error handling reading built-in uint16_t type
1000 LLVM_YAML_IS_SEQUENCE_VECTOR(uint16_t)
1001 TEST(YAMLIO, TestReadBuiltInTypesUint16Error) {
1002 std::vector<uint16_t> seq;
1008 yin.setDiagHandler(suppressErrorMessages);
1011 EXPECT_TRUE(yin.error());
1016 // Test error handling reading built-in uint32_t type
1018 LLVM_YAML_IS_SEQUENCE_VECTOR(uint32_t)
1019 TEST(YAMLIO, TestReadBuiltInTypesUint32Error) {
1020 std::vector<uint32_t> seq;
1026 yin.setDiagHandler(suppressErrorMessages);
1029 EXPECT_TRUE(yin.error());
1034 // Test error handling reading built-in uint64_t type
1036 LLVM_YAML_IS_SEQUENCE_VECTOR(uint64_t)
1037 TEST(YAMLIO, TestReadBuiltInTypesUint64Error) {
1038 std::vector<uint64_t> seq;
1040 "- 18446744073709551615\n"
1042 "- 19446744073709551615\n"
1044 yin.setDiagHandler(suppressErrorMessages);
1047 EXPECT_TRUE(yin.error());
1052 // Test error handling reading built-in int8_t type
1054 LLVM_YAML_IS_SEQUENCE_VECTOR(int8_t)
1055 TEST(YAMLIO, TestReadBuiltInTypesint8OverError) {
1056 std::vector<int8_t> seq;
1063 yin.setDiagHandler(suppressErrorMessages);
1066 EXPECT_TRUE(yin.error());
1070 // Test error handling reading built-in int8_t type
1072 TEST(YAMLIO, TestReadBuiltInTypesint8UnderError) {
1073 std::vector<int8_t> seq;
1080 yin.setDiagHandler(suppressErrorMessages);
1083 EXPECT_TRUE(yin.error());
1088 // Test error handling reading built-in int16_t type
1090 LLVM_YAML_IS_SEQUENCE_VECTOR(int16_t)
1091 TEST(YAMLIO, TestReadBuiltInTypesint16UnderError) {
1092 std::vector<int16_t> seq;
1099 yin.setDiagHandler(suppressErrorMessages);
1102 EXPECT_TRUE(yin.error());
1107 // Test error handling reading built-in int16_t type
1109 TEST(YAMLIO, TestReadBuiltInTypesint16OverError) {
1110 std::vector<int16_t> seq;
1117 yin.setDiagHandler(suppressErrorMessages);
1120 EXPECT_TRUE(yin.error());
1125 // Test error handling reading built-in int32_t type
1127 LLVM_YAML_IS_SEQUENCE_VECTOR(int32_t)
1128 TEST(YAMLIO, TestReadBuiltInTypesint32UnderError) {
1129 std::vector<int32_t> seq;
1136 yin.setDiagHandler(suppressErrorMessages);
1139 EXPECT_TRUE(yin.error());
1143 // Test error handling reading built-in int32_t type
1145 TEST(YAMLIO, TestReadBuiltInTypesint32OverError) {
1146 std::vector<int32_t> seq;
1153 yin.setDiagHandler(suppressErrorMessages);
1156 EXPECT_TRUE(yin.error());
1161 // Test error handling reading built-in int64_t type
1163 LLVM_YAML_IS_SEQUENCE_VECTOR(int64_t)
1164 TEST(YAMLIO, TestReadBuiltInTypesint64UnderError) {
1165 std::vector<int64_t> seq;
1167 "- -9223372036854775808\n"
1169 "- 9223372036854775807\n"
1170 "- -9223372036854775809\n"
1172 yin.setDiagHandler(suppressErrorMessages);
1175 EXPECT_TRUE(yin.error());
1179 // Test error handling reading built-in int64_t type
1181 TEST(YAMLIO, TestReadBuiltInTypesint64OverError) {
1182 std::vector<int64_t> seq;
1184 "- -9223372036854775808\n"
1186 "- 9223372036854775807\n"
1187 "- 9223372036854775809\n"
1189 yin.setDiagHandler(suppressErrorMessages);
1192 EXPECT_TRUE(yin.error());
1196 // Test error handling reading built-in float type
1198 LLVM_YAML_IS_SEQUENCE_VECTOR(float)
1199 TEST(YAMLIO, TestReadBuiltInTypesFloatError) {
1200 std::vector<float> seq;
1207 yin.setDiagHandler(suppressErrorMessages);
1210 EXPECT_TRUE(yin.error());
1214 // Test error handling reading built-in float type
1216 LLVM_YAML_IS_SEQUENCE_VECTOR(double)
1217 TEST(YAMLIO, TestReadBuiltInTypesDoubleError) {
1218 std::vector<double> seq;
1225 yin.setDiagHandler(suppressErrorMessages);
1228 EXPECT_TRUE(yin.error());
1232 // Test error handling reading built-in Hex8 type
1234 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex8)
1235 TEST(YAMLIO, TestReadBuiltInTypesHex8Error) {
1236 std::vector<Hex8> seq;
1242 yin.setDiagHandler(suppressErrorMessages);
1245 EXPECT_TRUE(yin.error());
1250 // Test error handling reading built-in Hex16 type
1252 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex16)
1253 TEST(YAMLIO, TestReadBuiltInTypesHex16Error) {
1254 std::vector<Hex16> seq;
1260 yin.setDiagHandler(suppressErrorMessages);
1263 EXPECT_TRUE(yin.error());
1267 // Test error handling reading built-in Hex32 type
1269 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex32)
1270 TEST(YAMLIO, TestReadBuiltInTypesHex32Error) {
1271 std::vector<Hex32> seq;
1277 yin.setDiagHandler(suppressErrorMessages);
1280 EXPECT_TRUE(yin.error());
1284 // Test error handling reading built-in Hex64 type
1286 LLVM_YAML_IS_SEQUENCE_VECTOR(Hex64)
1287 TEST(YAMLIO, TestReadBuiltInTypesHex64Error) {
1288 std::vector<Hex64> seq;
1291 "- 0xFFEEDDCCBBAA9988\n"
1292 "- 0x12345567890ABCDEF0\n"
1294 yin.setDiagHandler(suppressErrorMessages);
1297 EXPECT_TRUE(yin.error());
1300 struct OptionalTest {
1301 std::vector<int> Numbers;
1304 struct OptionalTestSeq {
1305 std::vector<OptionalTest> Tests;
1308 LLVM_YAML_IS_SEQUENCE_VECTOR(OptionalTest)
1312 struct MappingTraits<OptionalTest> {
1313 static void mapping(IO& IO, OptionalTest &OT) {
1314 IO.mapOptional("Numbers", OT.Numbers);
1319 struct MappingTraits<OptionalTestSeq> {
1320 static void mapping(IO &IO, OptionalTestSeq &OTS) {
1321 IO.mapOptional("Tests", OTS.Tests);
1327 TEST(YAMLIO, SequenceElideTest) {
1328 // Test that writing out a purely optional structure with its fields set to
1329 // default followed by other data is properly read back in.
1330 OptionalTestSeq Seq;
1331 OptionalTest One, Two, Three, Four;
1332 int N[] = {1, 2, 3};
1333 Three.Numbers.assign(N, N + 3);
1334 Seq.Tests.push_back(One);
1335 Seq.Tests.push_back(Two);
1336 Seq.Tests.push_back(Three);
1337 Seq.Tests.push_back(Four);
1339 std::string intermediate;
1341 llvm::raw_string_ostream ostr(intermediate);
1346 Input yin(intermediate);
1347 OptionalTestSeq Seq2;
1350 EXPECT_FALSE(yin.error());
1352 EXPECT_EQ(4UL, Seq2.Tests.size());
1354 EXPECT_TRUE(Seq2.Tests[0].Numbers.empty());
1355 EXPECT_TRUE(Seq2.Tests[1].Numbers.empty());
1357 EXPECT_EQ(1, Seq2.Tests[2].Numbers[0]);
1358 EXPECT_EQ(2, Seq2.Tests[2].Numbers[1]);
1359 EXPECT_EQ(3, Seq2.Tests[2].Numbers[2]);
1361 EXPECT_TRUE(Seq2.Tests[3].Numbers.empty());