2
0

test_variant.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. /*************************************************************************/
  2. /* test_variant.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef TEST_VARIANT_H
  31. #define TEST_VARIANT_H
  32. #include "core/variant/variant.h"
  33. #include "core/variant/variant_parser.h"
  34. #include "tests/test_macros.h"
  35. namespace TestVariant {
  36. static inline Array build_array() {
  37. return Array();
  38. }
  39. template <typename... Targs>
  40. static inline Array build_array(Variant item, Targs... Fargs) {
  41. Array a = build_array(Fargs...);
  42. a.push_front(item);
  43. return a;
  44. }
  45. static inline Dictionary build_dictionary() {
  46. return Dictionary();
  47. }
  48. template <typename... Targs>
  49. static inline Dictionary build_dictionary(Variant key, Variant item, Targs... Fargs) {
  50. Dictionary d = build_dictionary(Fargs...);
  51. d[key] = item;
  52. return d;
  53. }
  54. TEST_CASE("[Variant] Writer and parser integer") {
  55. int64_t a32 = 2147483648; // 2^31, so out of bounds for 32-bit signed int [-2^31, +2^31-1].
  56. String a32_str;
  57. VariantWriter::write_to_string(a32, a32_str);
  58. CHECK_MESSAGE(a32_str != "-2147483648", "Should not wrap around");
  59. int64_t b64 = 9223372036854775807; // 2^63-1, upper bound for signed 64-bit int.
  60. String b64_str;
  61. VariantWriter::write_to_string(b64, b64_str);
  62. CHECK_MESSAGE(b64_str == "9223372036854775807", "Should not wrap around.");
  63. VariantParser::StreamString ss;
  64. String errs;
  65. int line;
  66. Variant b64_parsed;
  67. int64_t b64_int_parsed;
  68. ss.s = b64_str;
  69. VariantParser::parse(&ss, b64_parsed, errs, line);
  70. b64_int_parsed = b64_parsed;
  71. CHECK_MESSAGE(b64_int_parsed == 9223372036854775807, "Should parse back.");
  72. ss.s = "9223372036854775808"; // Overflowed by one.
  73. VariantParser::parse(&ss, b64_parsed, errs, line);
  74. b64_int_parsed = b64_parsed;
  75. CHECK_MESSAGE(b64_int_parsed == 9223372036854775807, "The result should be clamped to max value.");
  76. ss.s = "1e100"; // Googol! Scientific notation.
  77. VariantParser::parse(&ss, b64_parsed, errs, line);
  78. b64_int_parsed = b64_parsed;
  79. CHECK_MESSAGE(b64_int_parsed == 9223372036854775807, "The result should be clamped to max value.");
  80. }
  81. TEST_CASE("[Variant] Writer and parser Variant::FLOAT") {
  82. // Variant::FLOAT is always 64-bit (C++ double).
  83. // This is the maximum non-infinity double-precision float.
  84. double a64 = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
  85. String a64_str;
  86. VariantWriter::write_to_string(a64, a64_str);
  87. CHECK_MESSAGE(a64_str == "1.79769e+308", "Writes in scientific notation.");
  88. CHECK_MESSAGE(a64_str != "inf", "Should not overflow.");
  89. CHECK_MESSAGE(a64_str != "nan", "The result should be defined.");
  90. String errs;
  91. int line;
  92. Variant variant_parsed;
  93. double float_parsed;
  94. VariantParser::StreamString bss;
  95. bss.s = a64_str;
  96. VariantParser::parse(&bss, variant_parsed, errs, line);
  97. float_parsed = variant_parsed;
  98. // Loses precision, but that's alright.
  99. CHECK_MESSAGE(float_parsed == 1.79769e+308, "Should parse back.");
  100. // Approximation of Googol with a double-precision float.
  101. VariantParser::StreamString css;
  102. css.s = "1.0e+100";
  103. VariantParser::parse(&css, variant_parsed, errs, line);
  104. float_parsed = variant_parsed;
  105. CHECK_MESSAGE(float_parsed == 1.0e+100, "Should match the double literal.");
  106. }
  107. TEST_CASE("[Variant] Assignment To Bool from Int,Float,String,Vec2,Vec2i,Vec3,Vec3i and Color") {
  108. Variant int_v = 0;
  109. Variant bool_v = true;
  110. int_v = bool_v; // int_v is now a bool
  111. CHECK(int_v == Variant(true));
  112. bool_v = false;
  113. int_v = bool_v;
  114. CHECK(int_v.get_type() == Variant::BOOL);
  115. Variant float_v = 0.0f;
  116. bool_v = true;
  117. float_v = bool_v;
  118. CHECK(float_v == Variant(true));
  119. bool_v = false;
  120. float_v = bool_v;
  121. CHECK(float_v.get_type() == Variant::BOOL);
  122. Variant string_v = "";
  123. bool_v = true;
  124. string_v = bool_v;
  125. CHECK(string_v == Variant(true));
  126. bool_v = false;
  127. string_v = bool_v;
  128. CHECK(string_v.get_type() == Variant::BOOL);
  129. Variant vec2_v = Vector2(0, 0);
  130. bool_v = true;
  131. vec2_v = bool_v;
  132. CHECK(vec2_v == Variant(true));
  133. bool_v = false;
  134. vec2_v = bool_v;
  135. CHECK(vec2_v.get_type() == Variant::BOOL);
  136. Variant vec2i_v = Vector2i(0, 0);
  137. bool_v = true;
  138. vec2i_v = bool_v;
  139. CHECK(vec2i_v == Variant(true));
  140. bool_v = false;
  141. vec2i_v = bool_v;
  142. CHECK(vec2i_v.get_type() == Variant::BOOL);
  143. Variant vec3_v = Vector3(0, 0, 0);
  144. bool_v = true;
  145. vec3_v = bool_v;
  146. CHECK(vec3_v == Variant(true));
  147. bool_v = false;
  148. vec3_v = bool_v;
  149. CHECK(vec3_v.get_type() == Variant::BOOL);
  150. Variant vec3i_v = Vector3i(0, 0, 0);
  151. bool_v = true;
  152. vec3i_v = bool_v;
  153. CHECK(vec3i_v == Variant(true));
  154. bool_v = false;
  155. vec3i_v = bool_v;
  156. CHECK(vec3i_v.get_type() == Variant::BOOL);
  157. Variant col_v = Color(0.5f, 0.2f, 0.75f);
  158. bool_v = true;
  159. col_v = bool_v;
  160. CHECK(col_v == Variant(true));
  161. bool_v = false;
  162. col_v = bool_v;
  163. CHECK(col_v.get_type() == Variant::BOOL);
  164. }
  165. TEST_CASE("[Variant] Assignment To Int from Bool,Float,String,Vec2,Vec2i,Vec3,Vec3i and Color") {
  166. Variant bool_v = false;
  167. Variant int_v = 2;
  168. bool_v = int_v; // Now bool_v is int
  169. CHECK(bool_v == Variant(2));
  170. int_v = -3;
  171. bool_v = int_v;
  172. CHECK(bool_v.get_type() == Variant::INT);
  173. Variant float_v = 0.0f;
  174. int_v = 2;
  175. float_v = int_v;
  176. CHECK(float_v == Variant(2));
  177. int_v = -3;
  178. float_v = int_v;
  179. CHECK(float_v.get_type() == Variant::INT);
  180. Variant string_v = "";
  181. int_v = 2;
  182. string_v = int_v;
  183. CHECK(string_v == Variant(2));
  184. int_v = -3;
  185. string_v = int_v;
  186. CHECK(string_v.get_type() == Variant::INT);
  187. Variant vec2_v = Vector2(0, 0);
  188. int_v = 2;
  189. vec2_v = int_v;
  190. CHECK(vec2_v == Variant(2));
  191. int_v = -3;
  192. vec2_v = int_v;
  193. CHECK(vec2_v.get_type() == Variant::INT);
  194. Variant vec2i_v = Vector2i(0, 0);
  195. int_v = 2;
  196. vec2i_v = int_v;
  197. CHECK(vec2i_v == Variant(2));
  198. int_v = -3;
  199. vec2i_v = int_v;
  200. CHECK(vec2i_v.get_type() == Variant::INT);
  201. Variant vec3_v = Vector3(0, 0, 0);
  202. int_v = 2;
  203. vec3_v = int_v;
  204. CHECK(vec3_v == Variant(2));
  205. int_v = -3;
  206. vec3_v = int_v;
  207. CHECK(vec3_v.get_type() == Variant::INT);
  208. Variant vec3i_v = Vector3i(0, 0, 0);
  209. int_v = 2;
  210. vec3i_v = int_v;
  211. CHECK(vec3i_v == Variant(2));
  212. int_v = -3;
  213. vec3i_v = int_v;
  214. CHECK(vec3i_v.get_type() == Variant::INT);
  215. Variant col_v = Color(0.5f, 0.2f, 0.75f);
  216. int_v = 2;
  217. col_v = int_v;
  218. CHECK(col_v == Variant(2));
  219. int_v = -3;
  220. col_v = int_v;
  221. CHECK(col_v.get_type() == Variant::INT);
  222. }
  223. TEST_CASE("[Variant] Assignment To Float from Bool,Int,String,Vec2,Vec2i,Vec3,Vec3i and Color") {
  224. Variant bool_v = false;
  225. Variant float_v = 1.5f;
  226. bool_v = float_v; // Now bool_v is float
  227. CHECK(bool_v == Variant(1.5f));
  228. float_v = -4.6f;
  229. bool_v = float_v;
  230. CHECK(bool_v.get_type() == Variant::FLOAT);
  231. Variant int_v = 1;
  232. float_v = 1.5f;
  233. int_v = float_v;
  234. CHECK(int_v == Variant(1.5f));
  235. float_v = -4.6f;
  236. int_v = float_v;
  237. CHECK(int_v.get_type() == Variant::FLOAT);
  238. Variant string_v = "";
  239. float_v = 1.5f;
  240. string_v = float_v;
  241. CHECK(string_v == Variant(1.5f));
  242. float_v = -4.6f;
  243. string_v = float_v;
  244. CHECK(string_v.get_type() == Variant::FLOAT);
  245. Variant vec2_v = Vector2(0, 0);
  246. float_v = 1.5f;
  247. vec2_v = float_v;
  248. CHECK(vec2_v == Variant(1.5f));
  249. float_v = -4.6f;
  250. vec2_v = float_v;
  251. CHECK(vec2_v.get_type() == Variant::FLOAT);
  252. Variant vec2i_v = Vector2i(0, 0);
  253. float_v = 1.5f;
  254. vec2i_v = float_v;
  255. CHECK(vec2i_v == Variant(1.5f));
  256. float_v = -4.6f;
  257. vec2i_v = float_v;
  258. CHECK(vec2i_v.get_type() == Variant::FLOAT);
  259. Variant vec3_v = Vector3(0, 0, 0);
  260. float_v = 1.5f;
  261. vec3_v = float_v;
  262. CHECK(vec3_v == Variant(1.5f));
  263. float_v = -4.6f;
  264. vec3_v = float_v;
  265. CHECK(vec3_v.get_type() == Variant::FLOAT);
  266. Variant vec3i_v = Vector3i(0, 0, 0);
  267. float_v = 1.5f;
  268. vec3i_v = float_v;
  269. CHECK(vec3i_v == Variant(1.5f));
  270. float_v = -4.6f;
  271. vec3i_v = float_v;
  272. CHECK(vec3i_v.get_type() == Variant::FLOAT);
  273. Variant col_v = Color(0.5f, 0.2f, 0.75f);
  274. float_v = 1.5f;
  275. col_v = float_v;
  276. CHECK(col_v == Variant(1.5f));
  277. float_v = -4.6f;
  278. col_v = float_v;
  279. CHECK(col_v.get_type() == Variant::FLOAT);
  280. }
  281. TEST_CASE("[Variant] Assignment To String from Bool,Int,Float,Vec2,Vec2i,Vec3,Vec3i and Color") {
  282. Variant bool_v = false;
  283. Variant string_v = "Hello";
  284. bool_v = string_v; // Now bool_v is string
  285. CHECK(bool_v == Variant("Hello"));
  286. string_v = "Hello there";
  287. bool_v = string_v;
  288. CHECK(bool_v.get_type() == Variant::STRING);
  289. Variant int_v = 0;
  290. string_v = "Hello";
  291. int_v = string_v;
  292. CHECK(int_v == Variant("Hello"));
  293. string_v = "Hello there";
  294. int_v = string_v;
  295. CHECK(int_v.get_type() == Variant::STRING);
  296. Variant float_v = 0.0f;
  297. string_v = "Hello";
  298. float_v = string_v;
  299. CHECK(float_v == Variant("Hello"));
  300. string_v = "Hello there";
  301. float_v = string_v;
  302. CHECK(float_v.get_type() == Variant::STRING);
  303. Variant vec2_v = Vector2(0, 0);
  304. string_v = "Hello";
  305. vec2_v = string_v;
  306. CHECK(vec2_v == Variant("Hello"));
  307. string_v = "Hello there";
  308. vec2_v = string_v;
  309. CHECK(vec2_v.get_type() == Variant::STRING);
  310. Variant vec2i_v = Vector2i(0, 0);
  311. string_v = "Hello";
  312. vec2i_v = string_v;
  313. CHECK(vec2i_v == Variant("Hello"));
  314. string_v = "Hello there";
  315. vec2i_v = string_v;
  316. CHECK(vec2i_v.get_type() == Variant::STRING);
  317. Variant vec3_v = Vector3(0, 0, 0);
  318. string_v = "Hello";
  319. vec3_v = string_v;
  320. CHECK(vec3_v == Variant("Hello"));
  321. string_v = "Hello there";
  322. vec3_v = string_v;
  323. CHECK(vec3_v.get_type() == Variant::STRING);
  324. Variant vec3i_v = Vector3i(0, 0, 0);
  325. string_v = "Hello";
  326. vec3i_v = string_v;
  327. CHECK(vec3i_v == Variant("Hello"));
  328. string_v = "Hello there";
  329. vec3i_v = string_v;
  330. CHECK(vec3i_v.get_type() == Variant::STRING);
  331. Variant col_v = Color(0.5f, 0.2f, 0.75f);
  332. string_v = "Hello";
  333. col_v = string_v;
  334. CHECK(col_v == Variant("Hello"));
  335. string_v = "Hello there";
  336. col_v = string_v;
  337. CHECK(col_v.get_type() == Variant::STRING);
  338. }
  339. TEST_CASE("[Variant] Assignment To Vec2 from Bool,Int,Float,String,Vec2i,Vec3,Vec3i and Color") {
  340. Variant bool_v = false;
  341. Variant vec2_v = Vector2(2.2f, 3.5f);
  342. bool_v = vec2_v; // Now bool_v is Vector2
  343. CHECK(bool_v == Variant(Vector2(2.2f, 3.5f)));
  344. vec2_v = Vector2(-5.4f, -7.9f);
  345. bool_v = vec2_v;
  346. CHECK(bool_v.get_type() == Variant::VECTOR2);
  347. Variant int_v = 0;
  348. vec2_v = Vector2(2.2f, 3.5f);
  349. int_v = vec2_v;
  350. CHECK(int_v == Variant(Vector2(2.2f, 3.5f)));
  351. vec2_v = Vector2(-5.4f, -7.9f);
  352. int_v = vec2_v;
  353. CHECK(int_v.get_type() == Variant::VECTOR2);
  354. Variant float_v = 0.0f;
  355. vec2_v = Vector2(2.2f, 3.5f);
  356. float_v = vec2_v;
  357. CHECK(float_v == Variant(Vector2(2.2f, 3.5f)));
  358. vec2_v = Vector2(-5.4f, -7.9f);
  359. float_v = vec2_v;
  360. CHECK(float_v.get_type() == Variant::VECTOR2);
  361. Variant string_v = "";
  362. vec2_v = Vector2(2.2f, 3.5f);
  363. string_v = vec2_v;
  364. CHECK(string_v == Variant(Vector2(2.2f, 3.5f)));
  365. vec2_v = Vector2(-5.4f, -7.9f);
  366. string_v = vec2_v;
  367. CHECK(string_v.get_type() == Variant::VECTOR2);
  368. Variant vec2i_v = Vector2i(0, 0);
  369. vec2_v = Vector2(2.2f, 3.5f);
  370. vec2i_v = vec2_v;
  371. CHECK(vec2i_v == Variant(Vector2(2.2f, 3.5f)));
  372. vec2_v = Vector2(-5.4f, -7.9f);
  373. vec2i_v = vec2_v;
  374. CHECK(vec2i_v.get_type() == Variant::VECTOR2);
  375. Variant vec3_v = Vector3(0, 0, 0);
  376. vec2_v = Vector2(2.2f, 3.5f);
  377. vec3_v = vec2_v;
  378. CHECK(vec3_v == Variant(Vector2(2.2f, 3.5f)));
  379. vec2_v = Vector2(-5.4f, -7.9f);
  380. vec3_v = vec2_v;
  381. CHECK(vec3_v.get_type() == Variant::VECTOR2);
  382. Variant vec3i_v = Vector3i(0, 0, 0);
  383. vec2_v = Vector2(2.2f, 3.5f);
  384. vec3i_v = vec2_v;
  385. CHECK(vec3i_v == Variant(Vector2(2.2f, 3.5f)));
  386. vec2_v = Vector2(-5.4f, -7.9f);
  387. vec3i_v = vec2_v;
  388. CHECK(vec3i_v.get_type() == Variant::VECTOR2);
  389. Variant col_v = Color(0.5f, 0.2f, 0.75f);
  390. vec2_v = Vector2(2.2f, 3.5f);
  391. col_v = vec2_v;
  392. CHECK(col_v == Variant(Vector2(2.2f, 3.5f)));
  393. vec2_v = Vector2(-5.4f, -7.9f);
  394. col_v = vec2_v;
  395. CHECK(col_v.get_type() == Variant::VECTOR2);
  396. }
  397. TEST_CASE("[Variant] Assignment To Vec2i from Bool,Int,Float,String,Vec2,Vec3,Vec3i and Color") {
  398. Variant bool_v = false;
  399. Variant vec2i_v = Vector2i(2, 3);
  400. bool_v = vec2i_v; // Now bool_v is Vector2i
  401. CHECK(bool_v == Variant(Vector2i(2, 3)));
  402. vec2i_v = Vector2i(-5, -7);
  403. bool_v = vec2i_v;
  404. CHECK(bool_v.get_type() == Variant::VECTOR2I);
  405. Variant int_v = 0;
  406. vec2i_v = Vector2i(2, 3);
  407. int_v = vec2i_v;
  408. CHECK(int_v == Variant(Vector2i(2, 3)));
  409. vec2i_v = Vector2i(-5, -7);
  410. int_v = vec2i_v;
  411. CHECK(int_v.get_type() == Variant::VECTOR2I);
  412. Variant float_v = 0.0f;
  413. vec2i_v = Vector2i(2, 3);
  414. float_v = vec2i_v;
  415. CHECK(float_v == Variant(Vector2i(2, 3)));
  416. vec2i_v = Vector2i(-5, -7);
  417. float_v = vec2i_v;
  418. CHECK(float_v.get_type() == Variant::VECTOR2I);
  419. Variant string_v = "";
  420. vec2i_v = Vector2i(2, 3);
  421. string_v = vec2i_v;
  422. CHECK(string_v == Variant(Vector2i(2, 3)));
  423. vec2i_v = Vector2i(-5, -7);
  424. string_v = vec2i_v;
  425. CHECK(string_v.get_type() == Variant::VECTOR2I);
  426. Variant vec2_v = Vector2(0, 0);
  427. vec2i_v = Vector2i(2, 3);
  428. vec2_v = vec2i_v;
  429. CHECK(vec2_v == Variant(Vector2i(2, 3)));
  430. vec2i_v = Vector2i(-5, -7);
  431. vec2_v = vec2i_v;
  432. CHECK(vec2i_v.get_type() == Variant::VECTOR2I);
  433. Variant vec3_v = Vector3(0, 0, 0);
  434. vec2i_v = Vector2i(2, 3);
  435. vec3_v = vec2i_v;
  436. CHECK(vec3_v == Variant(Vector2i(2, 3)));
  437. vec2i_v = Vector2i(-5, -7);
  438. vec3_v = vec2i_v;
  439. CHECK(vec3_v.get_type() == Variant::VECTOR2I);
  440. Variant vec3i_v = Vector3i(0, 0, 0);
  441. vec2i_v = Vector2i(2, 3);
  442. vec3i_v = vec2i_v;
  443. CHECK(vec3i_v == Variant(Vector2i(2, 3)));
  444. vec2i_v = Vector2i(-5, -7);
  445. vec3i_v = vec2i_v;
  446. CHECK(vec3i_v.get_type() == Variant::VECTOR2I);
  447. Variant col_v = Color(0.5f, 0.2f, 0.75f);
  448. vec2i_v = Vector2i(2, 3);
  449. col_v = vec2i_v;
  450. CHECK(col_v == Variant(Vector2i(2, 3)));
  451. vec2i_v = Vector2i(-5, -7);
  452. col_v = vec2i_v;
  453. CHECK(col_v.get_type() == Variant::VECTOR2I);
  454. }
  455. TEST_CASE("[Variant] Assignment To Vec3 from Bool,Int,Float,String,Vec2,Vec2i,Vec3i and Color") {
  456. Variant bool_v = false;
  457. Variant vec3_v = Vector3(2.2f, 3.5f, 5.3f);
  458. bool_v = vec3_v; // Now bool_v is Vector3
  459. CHECK(bool_v == Variant(Vector3(2.2f, 3.5f, 5.3f)));
  460. vec3_v = Vector3(-5.4f, -7.9f, -2.1f);
  461. bool_v = vec3_v;
  462. CHECK(bool_v.get_type() == Variant::VECTOR3);
  463. Variant int_v = 0;
  464. vec3_v = Vector3(2.2f, 3.5f, 5.3f);
  465. int_v = vec3_v;
  466. CHECK(int_v == Variant(Vector3(2.2f, 3.5f, 5.3f)));
  467. vec3_v = Vector3(-5.4f, -7.9f, -2.1f);
  468. int_v = vec3_v;
  469. CHECK(int_v.get_type() == Variant::VECTOR3);
  470. Variant float_v = 0.0f;
  471. vec3_v = Vector3(2.2f, 3.5f, 5.3f);
  472. float_v = vec3_v;
  473. CHECK(float_v == Variant(Vector3(2.2f, 3.5f, 5.3f)));
  474. vec3_v = Vector3(-5.4f, -7.9f, -2.1f);
  475. float_v = vec3_v;
  476. CHECK(float_v.get_type() == Variant::VECTOR3);
  477. Variant string_v = "";
  478. vec3_v = Vector3(2.2f, 3.5f, 5.3f);
  479. string_v = vec3_v;
  480. CHECK(string_v == Variant(Vector3(2.2f, 3.5f, 5.3f)));
  481. vec3_v = Vector3(-5.4f, -7.9f, -2.1f);
  482. string_v = vec3_v;
  483. CHECK(string_v.get_type() == Variant::VECTOR3);
  484. Variant vec2_v = Vector2(0, 0);
  485. vec3_v = Vector3(2.2f, 3.5f, 5.3f);
  486. vec2_v = vec3_v;
  487. CHECK(vec2_v == Variant(Vector3(2.2f, 3.5f, 5.3f)));
  488. vec3_v = Vector3(-5.4f, -7.9f, -2.1f);
  489. vec2_v = vec3_v;
  490. CHECK(vec2_v.get_type() == Variant::VECTOR3);
  491. Variant vec2i_v = Vector2i(0, 0);
  492. vec3_v = Vector3(2.2f, 3.5f, 5.3f);
  493. vec2i_v = vec3_v;
  494. CHECK(vec2i_v == Variant(Vector3(2.2f, 3.5f, 5.3f)));
  495. vec3_v = Vector3(-5.4f, -7.9f, -2.1f);
  496. vec2i_v = vec3_v;
  497. CHECK(vec2i_v.get_type() == Variant::VECTOR3);
  498. Variant vec3i_v = Vector3i(0, 0, 0);
  499. vec3_v = Vector3(2.2f, 3.5f, 5.3f);
  500. vec3i_v = vec3_v;
  501. CHECK(vec3i_v == Variant(Vector3(2.2f, 3.5f, 5.3f)));
  502. vec3_v = Vector3(-5.4f, -7.9f, -2.1f);
  503. vec3i_v = vec3_v;
  504. CHECK(vec3i_v.get_type() == Variant::VECTOR3);
  505. Variant col_v = Color(0.5f, 0.2f, 0.75f);
  506. vec3_v = Vector3(2.2f, 3.5f, 5.3f);
  507. col_v = vec3_v;
  508. CHECK(col_v == Variant(Vector3(2.2f, 3.5f, 5.3f)));
  509. vec3_v = Vector3(-5.4f, -7.9f, -2.1f);
  510. col_v = vec3_v;
  511. CHECK(col_v.get_type() == Variant::VECTOR3);
  512. }
  513. TEST_CASE("[Variant] Assignment To Vec3i from Bool,Int,Float,String,Vec2,Vec2i,Vec3 and Color") {
  514. Variant bool_v = false;
  515. Variant vec3i_v = Vector3i(2, 3, 5);
  516. bool_v = vec3i_v; // Now bool_v is Vector3i
  517. CHECK(bool_v == Variant(Vector3i(2, 3, 5)));
  518. vec3i_v = Vector3i(-5, -7, -2);
  519. bool_v = vec3i_v;
  520. CHECK(bool_v.get_type() == Variant::VECTOR3I);
  521. Variant int_v = 0;
  522. vec3i_v = Vector3i(2, 3, 5);
  523. int_v = vec3i_v;
  524. CHECK(int_v == Variant(Vector3i(2, 3, 5)));
  525. vec3i_v = Vector3i(-5, -7, -2);
  526. int_v = vec3i_v;
  527. CHECK(int_v.get_type() == Variant::VECTOR3I);
  528. Variant float_v = 0.0f;
  529. vec3i_v = Vector3i(2, 3, 5);
  530. float_v = vec3i_v;
  531. CHECK(float_v == Variant(Vector3i(2, 3, 5)));
  532. vec3i_v = Vector3i(-5, -7, -2);
  533. float_v = vec3i_v;
  534. CHECK(float_v.get_type() == Variant::VECTOR3I);
  535. Variant string_v = "";
  536. vec3i_v = Vector3i(2, 3, 5);
  537. string_v = vec3i_v;
  538. CHECK(string_v == Variant(Vector3i(2, 3, 5)));
  539. vec3i_v = Vector3i(-5, -7, -2);
  540. string_v = vec3i_v;
  541. CHECK(string_v.get_type() == Variant::VECTOR3I);
  542. Variant vec2_v = Vector2(0, 0);
  543. vec3i_v = Vector3i(2, 3, 5);
  544. vec2_v = vec3i_v;
  545. CHECK(vec2_v == Variant(Vector3i(2, 3, 5)));
  546. vec3i_v = Vector3i(-5, -7, -2);
  547. vec2_v = vec3i_v;
  548. CHECK(vec2_v.get_type() == Variant::VECTOR3I);
  549. Variant vec2i_v = Vector2i(0, 0);
  550. vec3i_v = Vector3i(2, 3, 5);
  551. vec2i_v = vec3i_v;
  552. CHECK(vec2i_v == Variant(Vector3i(2, 3, 5)));
  553. vec3i_v = Vector3i(-5, -7, -2);
  554. vec2i_v = vec3i_v;
  555. CHECK(vec2i_v.get_type() == Variant::VECTOR3I);
  556. Variant vec3_v = Vector3(0, 0, 0);
  557. vec3i_v = Vector3i(2, 3, 5);
  558. vec3_v = vec3i_v;
  559. CHECK(vec3_v == Variant(Vector3i(2, 3, 5)));
  560. vec3i_v = Vector3i(-5, -7, -2);
  561. vec3_v = vec3i_v;
  562. CHECK(vec3_v.get_type() == Variant::VECTOR3I);
  563. Variant col_v = Color(0.5f, 0.2f, 0.75f);
  564. vec3i_v = Vector3i(2, 3, 5);
  565. col_v = vec3i_v;
  566. CHECK(col_v == Variant(Vector3i(2, 3, 5)));
  567. vec3i_v = Vector3i(-5, -7, -2);
  568. col_v = vec3i_v;
  569. CHECK(col_v.get_type() == Variant::VECTOR3I);
  570. }
  571. TEST_CASE("[Variant] Assignment To Color from Bool,Int,Float,String,Vec2,Vec2i,Vec3 and Vec3i") {
  572. Variant bool_v = false;
  573. Variant col_v = Color(0.25f, 0.4f, 0.78f);
  574. bool_v = col_v; // Now bool_v is Color
  575. CHECK(bool_v == Variant(Color(0.25f, 0.4f, 0.78f)));
  576. col_v = Color(0.33f, 0.75f, 0.21f);
  577. bool_v = col_v;
  578. CHECK(bool_v.get_type() == Variant::COLOR);
  579. Variant int_v = 0;
  580. col_v = Color(0.25f, 0.4f, 0.78f);
  581. int_v = col_v;
  582. CHECK(int_v == Variant(Color(0.25f, 0.4f, 0.78f)));
  583. col_v = Color(0.33f, 0.75f, 0.21f);
  584. int_v = col_v;
  585. CHECK(int_v.get_type() == Variant::COLOR);
  586. Variant float_v = 0.0f;
  587. col_v = Color(0.25f, 0.4f, 0.78f);
  588. float_v = col_v;
  589. CHECK(float_v == Variant(Color(0.25f, 0.4f, 0.78f)));
  590. col_v = Color(0.33f, 0.75f, 0.21f);
  591. float_v = col_v;
  592. CHECK(float_v.get_type() == Variant::COLOR);
  593. Variant string_v = "";
  594. col_v = Color(0.25f, 0.4f, 0.78f);
  595. string_v = col_v;
  596. CHECK(string_v == Variant(Color(0.25f, 0.4f, 0.78f)));
  597. col_v = Color(0.33f, 0.75f, 0.21f);
  598. string_v = col_v;
  599. CHECK(string_v.get_type() == Variant::COLOR);
  600. Variant vec2_v = Vector2(0, 0);
  601. col_v = Color(0.25f, 0.4f, 0.78f);
  602. vec2_v = col_v;
  603. CHECK(vec2_v == Variant(Color(0.25f, 0.4f, 0.78f)));
  604. col_v = Color(0.33f, 0.75f, 0.21f);
  605. vec2_v = col_v;
  606. CHECK(vec2_v.get_type() == Variant::COLOR);
  607. Variant vec2i_v = Vector2i(0, 0);
  608. col_v = Color(0.25f, 0.4f, 0.78f);
  609. vec2i_v = col_v;
  610. CHECK(vec2i_v == Variant(Color(0.25f, 0.4f, 0.78f)));
  611. col_v = Color(0.33f, 0.75f, 0.21f);
  612. vec2i_v = col_v;
  613. CHECK(vec2i_v.get_type() == Variant::COLOR);
  614. Variant vec3_v = Vector3(0, 0, 0);
  615. col_v = Color(0.25f, 0.4f, 0.78f);
  616. vec3_v = col_v;
  617. CHECK(vec3_v == Variant(Color(0.25f, 0.4f, 0.78f)));
  618. col_v = Color(0.33f, 0.75f, 0.21f);
  619. vec3_v = col_v;
  620. CHECK(vec3_v.get_type() == Variant::COLOR);
  621. Variant vec3i_v = Vector3i(0, 0, 0);
  622. col_v = Color(0.25f, 0.4f, 0.78f);
  623. vec3i_v = col_v;
  624. CHECK(vec3i_v == Variant(Color(0.25f, 0.4f, 0.78f)));
  625. col_v = Color(0.33f, 0.75f, 0.21f);
  626. vec3i_v = col_v;
  627. CHECK(vec3i_v.get_type() == Variant::COLOR);
  628. }
  629. TEST_CASE("[Variant] Writer and parser array") {
  630. Array a = build_array(1, String("hello"), build_array(Variant()));
  631. String a_str;
  632. VariantWriter::write_to_string(a, a_str);
  633. CHECK_EQ(a_str, "[1, \"hello\", [null]]");
  634. VariantParser::StreamString ss;
  635. String errs;
  636. int line;
  637. Variant a_parsed;
  638. ss.s = a_str;
  639. VariantParser::parse(&ss, a_parsed, errs, line);
  640. CHECK_MESSAGE(a_parsed == Variant(a), "Should parse back.");
  641. }
  642. TEST_CASE("[Variant] Writer recursive array") {
  643. // There is no way to accurately represent a recursive array,
  644. // the only thing we can do is make sure the writer doesn't blow up
  645. // Self recursive
  646. Array a;
  647. a.push_back(a);
  648. // Writer should it recursion limit while visiting the array
  649. ERR_PRINT_OFF;
  650. String a_str;
  651. VariantWriter::write_to_string(a, a_str);
  652. ERR_PRINT_ON;
  653. // Nested recursive
  654. Array a1;
  655. Array a2;
  656. a1.push_back(a2);
  657. a2.push_back(a1);
  658. // Writer should it recursion limit while visiting the array
  659. ERR_PRINT_OFF;
  660. String a1_str;
  661. VariantWriter::write_to_string(a1, a1_str);
  662. ERR_PRINT_ON;
  663. // Break the recursivity otherwise Dictionary tearndown will leak memory
  664. a.clear();
  665. a1.clear();
  666. a2.clear();
  667. }
  668. TEST_CASE("[Variant] Writer and parser dictionary") {
  669. // d = {{1: 2}: 3, 4: "hello", 5: {null: []}}
  670. Dictionary d = build_dictionary(build_dictionary(1, 2), 3, 4, String("hello"), 5, build_dictionary(Variant(), build_array()));
  671. String d_str;
  672. VariantWriter::write_to_string(d, d_str);
  673. CHECK_EQ(d_str, "{\n4: \"hello\",\n5: {\nnull: []\n},\n{\n1: 2\n}: 3\n}");
  674. VariantParser::StreamString ss;
  675. String errs;
  676. int line;
  677. Variant d_parsed;
  678. ss.s = d_str;
  679. VariantParser::parse(&ss, d_parsed, errs, line);
  680. CHECK_MESSAGE(d_parsed == Variant(d), "Should parse back.");
  681. }
  682. TEST_CASE("[Variant] Writer recursive dictionary") {
  683. // There is no way to accurately represent a recursive dictionary,
  684. // the only thing we can do is make sure the writer doesn't blow up
  685. // Self recursive
  686. Dictionary d;
  687. d[1] = d;
  688. // Writer should it recursion limit while visiting the dictionary
  689. ERR_PRINT_OFF;
  690. String d_str;
  691. VariantWriter::write_to_string(d, d_str);
  692. ERR_PRINT_ON;
  693. // Nested recursive
  694. Dictionary d1;
  695. Dictionary d2;
  696. d1[2] = d2;
  697. d2[1] = d1;
  698. // Writer should it recursion limit while visiting the dictionary
  699. ERR_PRINT_OFF;
  700. String d1_str;
  701. VariantWriter::write_to_string(d1, d1_str);
  702. ERR_PRINT_ON;
  703. // Break the recursivity otherwise Dictionary tearndown will leak memory
  704. d.clear();
  705. d1.clear();
  706. d2.clear();
  707. }
  708. #if 0 // TODO: recursion in dict key is currently buggy
  709. TEST_CASE("[Variant] Writer recursive dictionary on keys") {
  710. // There is no way to accurately represent a recursive dictionary,
  711. // the only thing we can do is make sure the writer doesn't blow up
  712. // Self recursive
  713. Dictionary d;
  714. d[d] = 1;
  715. // Writer should it recursion limit while visiting the dictionary
  716. ERR_PRINT_OFF;
  717. String d_str;
  718. VariantWriter::write_to_string(d, d_str);
  719. ERR_PRINT_ON;
  720. // Nested recursive
  721. Dictionary d1;
  722. Dictionary d2;
  723. d1[d2] = 2;
  724. d2[d1] = 1;
  725. // Writer should it recursion limit while visiting the dictionary
  726. ERR_PRINT_OFF;
  727. String d1_str;
  728. VariantWriter::write_to_string(d1, d1_str);
  729. ERR_PRINT_ON;
  730. // Break the recursivity otherwise Dictionary tearndown will leak memory
  731. d.clear();
  732. d1.clear();
  733. d2.clear();
  734. }
  735. #endif
  736. TEST_CASE("[Variant] Basic comparison") {
  737. CHECK_EQ(Variant(1), Variant(1));
  738. CHECK_FALSE(Variant(1) != Variant(1));
  739. CHECK_NE(Variant(1), Variant(2));
  740. CHECK_EQ(Variant(String("foo")), Variant(String("foo")));
  741. CHECK_NE(Variant(String("foo")), Variant(String("bar")));
  742. // Check "empty" version of different types are not equivalents
  743. CHECK_NE(Variant(0), Variant());
  744. CHECK_NE(Variant(String()), Variant());
  745. CHECK_NE(Variant(Array()), Variant());
  746. CHECK_NE(Variant(Dictionary()), Variant());
  747. }
  748. TEST_CASE("[Variant] Nested array comparison") {
  749. Array a1 = build_array(1, build_array(2, 3));
  750. Array a2 = build_array(1, build_array(2, 3));
  751. Array a_other = build_array(1, build_array(2, 4));
  752. Variant v_a1 = a1;
  753. Variant v_a1_ref2 = a1;
  754. Variant v_a2 = a2;
  755. Variant v_a_other = a_other;
  756. // test both operator== and operator!=
  757. CHECK_EQ(v_a1, v_a1);
  758. CHECK_FALSE(v_a1 != v_a1);
  759. CHECK_EQ(v_a1, v_a1_ref2);
  760. CHECK_FALSE(v_a1 != v_a1_ref2);
  761. CHECK_EQ(v_a1, v_a2);
  762. CHECK_FALSE(v_a1 != v_a2);
  763. CHECK_NE(v_a1, v_a_other);
  764. CHECK_FALSE(v_a1 == v_a_other);
  765. }
  766. TEST_CASE("[Variant] Nested dictionary comparison") {
  767. Dictionary d1 = build_dictionary(build_dictionary(1, 2), build_dictionary(3, 4));
  768. Dictionary d2 = build_dictionary(build_dictionary(1, 2), build_dictionary(3, 4));
  769. Dictionary d_other_key = build_dictionary(build_dictionary(1, 0), build_dictionary(3, 4));
  770. Dictionary d_other_val = build_dictionary(build_dictionary(1, 2), build_dictionary(3, 0));
  771. Variant v_d1 = d1;
  772. Variant v_d1_ref2 = d1;
  773. Variant v_d2 = d2;
  774. Variant v_d_other_key = d_other_key;
  775. Variant v_d_other_val = d_other_val;
  776. // test both operator== and operator!=
  777. CHECK_EQ(v_d1, v_d1);
  778. CHECK_FALSE(v_d1 != v_d1);
  779. CHECK_EQ(v_d1, v_d1_ref2);
  780. CHECK_FALSE(v_d1 != v_d1_ref2);
  781. CHECK_EQ(v_d1, v_d2);
  782. CHECK_FALSE(v_d1 != v_d2);
  783. CHECK_NE(v_d1, v_d_other_key);
  784. CHECK_FALSE(v_d1 == v_d_other_key);
  785. CHECK_NE(v_d1, v_d_other_val);
  786. CHECK_FALSE(v_d1 == v_d_other_val);
  787. }
  788. } // namespace TestVariant
  789. #endif // TEST_VARIANT_H