YAMLIOTest.cpp 59 KB

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