test_variant.h 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. /**************************************************************************/
  2. /* test_variant.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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] Identity comparison") {
  749. // Value types are compared by value
  750. Variant aabb = AABB();
  751. CHECK(aabb.identity_compare(aabb));
  752. CHECK(aabb.identity_compare(AABB()));
  753. CHECK_FALSE(aabb.identity_compare(AABB(Vector3(1, 2, 3), Vector3(1, 2, 3))));
  754. Variant basis = Basis();
  755. CHECK(basis.identity_compare(basis));
  756. CHECK(basis.identity_compare(Basis()));
  757. CHECK_FALSE(basis.identity_compare(Basis(Quaternion(Vector3(1, 2, 3).normalized(), 45))));
  758. Variant bool_var = true;
  759. CHECK(bool_var.identity_compare(bool_var));
  760. CHECK(bool_var.identity_compare(true));
  761. CHECK_FALSE(bool_var.identity_compare(false));
  762. Variant callable = Callable();
  763. CHECK(callable.identity_compare(callable));
  764. CHECK(callable.identity_compare(Callable()));
  765. CHECK_FALSE(callable.identity_compare(Callable(ObjectID(), StringName("lambda"))));
  766. Variant color = Color();
  767. CHECK(color.identity_compare(color));
  768. CHECK(color.identity_compare(Color()));
  769. CHECK_FALSE(color.identity_compare(Color(255, 0, 255)));
  770. Variant float_var = 1.0;
  771. CHECK(float_var.identity_compare(float_var));
  772. CHECK(float_var.identity_compare(1.0));
  773. CHECK_FALSE(float_var.identity_compare(2.0));
  774. Variant int_var = 1;
  775. CHECK(int_var.identity_compare(int_var));
  776. CHECK(int_var.identity_compare(1));
  777. CHECK_FALSE(int_var.identity_compare(2));
  778. Variant nil = Variant();
  779. CHECK(nil.identity_compare(nil));
  780. CHECK(nil.identity_compare(Variant()));
  781. CHECK_FALSE(nil.identity_compare(true));
  782. Variant node_path = NodePath("godot");
  783. CHECK(node_path.identity_compare(node_path));
  784. CHECK(node_path.identity_compare(NodePath("godot")));
  785. CHECK_FALSE(node_path.identity_compare(NodePath("waiting")));
  786. Variant plane = Plane();
  787. CHECK(plane.identity_compare(plane));
  788. CHECK(plane.identity_compare(Plane()));
  789. CHECK_FALSE(plane.identity_compare(Plane(Vector3(1, 2, 3), 42)));
  790. Variant projection = Projection();
  791. CHECK(projection.identity_compare(projection));
  792. CHECK(projection.identity_compare(Projection()));
  793. CHECK_FALSE(projection.identity_compare(Projection(Transform3D(Basis(Vector3(1, 2, 3).normalized(), 45), Vector3(1, 2, 3)))));
  794. Variant quaternion = Quaternion();
  795. CHECK(quaternion.identity_compare(quaternion));
  796. CHECK(quaternion.identity_compare(Quaternion()));
  797. CHECK_FALSE(quaternion.identity_compare(Quaternion(Vector3(1, 2, 3).normalized(), 45)));
  798. Variant rect2 = Rect2();
  799. CHECK(rect2.identity_compare(rect2));
  800. CHECK(rect2.identity_compare(Rect2()));
  801. CHECK_FALSE(rect2.identity_compare(Rect2(Point2(Vector2(1, 2)), Size2(Vector2(1, 2)))));
  802. Variant rect2i = Rect2i();
  803. CHECK(rect2i.identity_compare(rect2i));
  804. CHECK(rect2i.identity_compare(Rect2i()));
  805. CHECK_FALSE(rect2i.identity_compare(Rect2i(Point2i(Vector2i(1, 2)), Size2i(Vector2i(1, 2)))));
  806. Variant rid = RID();
  807. CHECK(rid.identity_compare(rid));
  808. CHECK(rid.identity_compare(RID()));
  809. CHECK_FALSE(rid.identity_compare(RID::from_uint64(123)));
  810. Variant signal = Signal();
  811. CHECK(signal.identity_compare(signal));
  812. CHECK(signal.identity_compare(Signal()));
  813. CHECK_FALSE(signal.identity_compare(Signal(ObjectID(), StringName("lambda"))));
  814. Variant str = "godot";
  815. CHECK(str.identity_compare(str));
  816. CHECK(str.identity_compare("godot"));
  817. CHECK_FALSE(str.identity_compare("waiting"));
  818. Variant str_name = StringName("godot");
  819. CHECK(str_name.identity_compare(str_name));
  820. CHECK(str_name.identity_compare(StringName("godot")));
  821. CHECK_FALSE(str_name.identity_compare(StringName("waiting")));
  822. Variant transform2d = Transform2D();
  823. CHECK(transform2d.identity_compare(transform2d));
  824. CHECK(transform2d.identity_compare(Transform2D()));
  825. CHECK_FALSE(transform2d.identity_compare(Transform2D(45, Vector2(1, 2))));
  826. Variant transform3d = Transform3D();
  827. CHECK(transform3d.identity_compare(transform3d));
  828. CHECK(transform3d.identity_compare(Transform3D()));
  829. CHECK_FALSE(transform3d.identity_compare(Transform3D(Basis(Quaternion(Vector3(1, 2, 3).normalized(), 45)), Vector3(1, 2, 3))));
  830. Variant vect2 = Vector2();
  831. CHECK(vect2.identity_compare(vect2));
  832. CHECK(vect2.identity_compare(Vector2()));
  833. CHECK_FALSE(vect2.identity_compare(Vector2(1, 2)));
  834. Variant vect2i = Vector2i();
  835. CHECK(vect2i.identity_compare(vect2i));
  836. CHECK(vect2i.identity_compare(Vector2i()));
  837. CHECK_FALSE(vect2i.identity_compare(Vector2i(1, 2)));
  838. Variant vect3 = Vector3();
  839. CHECK(vect3.identity_compare(vect3));
  840. CHECK(vect3.identity_compare(Vector3()));
  841. CHECK_FALSE(vect3.identity_compare(Vector3(1, 2, 3)));
  842. Variant vect3i = Vector3i();
  843. CHECK(vect3i.identity_compare(vect3i));
  844. CHECK(vect3i.identity_compare(Vector3i()));
  845. CHECK_FALSE(vect3i.identity_compare(Vector3i(1, 2, 3)));
  846. Variant vect4 = Vector4();
  847. CHECK(vect4.identity_compare(vect4));
  848. CHECK(vect4.identity_compare(Vector4()));
  849. CHECK_FALSE(vect4.identity_compare(Vector4(1, 2, 3, 4)));
  850. Variant vect4i = Vector4i();
  851. CHECK(vect4i.identity_compare(vect4i));
  852. CHECK(vect4i.identity_compare(Vector4i()));
  853. CHECK_FALSE(vect4i.identity_compare(Vector4i(1, 2, 3, 4)));
  854. // Reference types are compared by reference
  855. Variant array = Array();
  856. CHECK(array.identity_compare(array));
  857. CHECK_FALSE(array.identity_compare(Array()));
  858. Variant dictionary = Dictionary();
  859. CHECK(dictionary.identity_compare(dictionary));
  860. CHECK_FALSE(dictionary.identity_compare(Dictionary()));
  861. Variant packed_byte_array = PackedByteArray();
  862. CHECK(packed_byte_array.identity_compare(packed_byte_array));
  863. CHECK_FALSE(packed_byte_array.identity_compare(PackedByteArray()));
  864. Variant packed_color_array = PackedColorArray();
  865. CHECK(packed_color_array.identity_compare(packed_color_array));
  866. CHECK_FALSE(packed_color_array.identity_compare(PackedColorArray()));
  867. Variant packed_float32_array = PackedFloat32Array();
  868. CHECK(packed_float32_array.identity_compare(packed_float32_array));
  869. CHECK_FALSE(packed_float32_array.identity_compare(PackedFloat32Array()));
  870. Variant packed_float64_array = PackedFloat64Array();
  871. CHECK(packed_float64_array.identity_compare(packed_float64_array));
  872. CHECK_FALSE(packed_float64_array.identity_compare(PackedFloat64Array()));
  873. Variant packed_int32_array = PackedInt32Array();
  874. CHECK(packed_int32_array.identity_compare(packed_int32_array));
  875. CHECK_FALSE(packed_int32_array.identity_compare(PackedInt32Array()));
  876. Variant packed_int64_array = PackedInt64Array();
  877. CHECK(packed_int64_array.identity_compare(packed_int64_array));
  878. CHECK_FALSE(packed_int64_array.identity_compare(PackedInt64Array()));
  879. Variant packed_string_array = PackedStringArray();
  880. CHECK(packed_string_array.identity_compare(packed_string_array));
  881. CHECK_FALSE(packed_string_array.identity_compare(PackedStringArray()));
  882. Variant packed_vector2_array = PackedVector2Array();
  883. CHECK(packed_vector2_array.identity_compare(packed_vector2_array));
  884. CHECK_FALSE(packed_vector2_array.identity_compare(PackedVector2Array()));
  885. Variant packed_vector3_array = PackedVector3Array();
  886. CHECK(packed_vector3_array.identity_compare(packed_vector3_array));
  887. CHECK_FALSE(packed_vector3_array.identity_compare(PackedVector3Array()));
  888. Object obj_one = Object();
  889. Variant obj_one_var = &obj_one;
  890. Object obj_two = Object();
  891. Variant obj_two_var = &obj_two;
  892. CHECK(obj_one_var.identity_compare(obj_one_var));
  893. CHECK_FALSE(obj_one_var.identity_compare(obj_two_var));
  894. Variant obj_null_one_var = Variant((Object *)nullptr);
  895. Variant obj_null_two_var = Variant((Object *)nullptr);
  896. CHECK(obj_null_one_var.identity_compare(obj_null_one_var));
  897. CHECK(obj_null_one_var.identity_compare(obj_null_two_var));
  898. Object *freed_one = new Object();
  899. Variant freed_one_var = freed_one;
  900. delete freed_one;
  901. Object *freed_two = new Object();
  902. Variant freed_two_var = freed_two;
  903. delete freed_two;
  904. CHECK_FALSE(freed_one_var.identity_compare(freed_two_var));
  905. }
  906. TEST_CASE("[Variant] Nested array comparison") {
  907. Array a1 = build_array(1, build_array(2, 3));
  908. Array a2 = build_array(1, build_array(2, 3));
  909. Array a_other = build_array(1, build_array(2, 4));
  910. Variant v_a1 = a1;
  911. Variant v_a1_ref2 = a1;
  912. Variant v_a2 = a2;
  913. Variant v_a_other = a_other;
  914. // test both operator== and operator!=
  915. CHECK_EQ(v_a1, v_a1);
  916. CHECK_FALSE(v_a1 != v_a1);
  917. CHECK_EQ(v_a1, v_a1_ref2);
  918. CHECK_FALSE(v_a1 != v_a1_ref2);
  919. CHECK_EQ(v_a1, v_a2);
  920. CHECK_FALSE(v_a1 != v_a2);
  921. CHECK_NE(v_a1, v_a_other);
  922. CHECK_FALSE(v_a1 == v_a_other);
  923. }
  924. TEST_CASE("[Variant] Nested dictionary comparison") {
  925. Dictionary d1 = build_dictionary(build_dictionary(1, 2), build_dictionary(3, 4));
  926. Dictionary d2 = build_dictionary(build_dictionary(1, 2), build_dictionary(3, 4));
  927. Dictionary d_other_key = build_dictionary(build_dictionary(1, 0), build_dictionary(3, 4));
  928. Dictionary d_other_val = build_dictionary(build_dictionary(1, 2), build_dictionary(3, 0));
  929. Variant v_d1 = d1;
  930. Variant v_d1_ref2 = d1;
  931. Variant v_d2 = d2;
  932. Variant v_d_other_key = d_other_key;
  933. Variant v_d_other_val = d_other_val;
  934. // test both operator== and operator!=
  935. CHECK_EQ(v_d1, v_d1);
  936. CHECK_FALSE(v_d1 != v_d1);
  937. CHECK_EQ(v_d1, v_d1_ref2);
  938. CHECK_FALSE(v_d1 != v_d1_ref2);
  939. CHECK_EQ(v_d1, v_d2);
  940. CHECK_FALSE(v_d1 != v_d2);
  941. CHECK_NE(v_d1, v_d_other_key);
  942. CHECK_FALSE(v_d1 == v_d_other_key);
  943. CHECK_NE(v_d1, v_d_other_val);
  944. CHECK_FALSE(v_d1 == v_d_other_val);
  945. }
  946. struct ArgumentData {
  947. Variant::Type type;
  948. String name;
  949. bool has_defval = false;
  950. Variant defval;
  951. int position;
  952. };
  953. struct MethodData {
  954. StringName name;
  955. Variant::Type return_type;
  956. List<ArgumentData> arguments;
  957. bool is_virtual = false;
  958. bool is_vararg = false;
  959. };
  960. TEST_CASE("[Variant] Utility functions") {
  961. List<MethodData> functions;
  962. List<StringName> function_names;
  963. Variant::get_utility_function_list(&function_names);
  964. function_names.sort_custom<StringName::AlphCompare>();
  965. for (const StringName &E : function_names) {
  966. MethodData md;
  967. md.name = E;
  968. // Utility function's return type.
  969. if (Variant::has_utility_function_return_value(E)) {
  970. md.return_type = Variant::get_utility_function_return_type(E);
  971. }
  972. // Utility function's arguments.
  973. if (Variant::is_utility_function_vararg(E)) {
  974. md.is_vararg = true;
  975. } else {
  976. for (int i = 0; i < Variant::get_utility_function_argument_count(E); i++) {
  977. ArgumentData arg;
  978. arg.type = Variant::get_utility_function_argument_type(E, i);
  979. arg.name = Variant::get_utility_function_argument_name(E, i);
  980. arg.position = i;
  981. md.arguments.push_back(arg);
  982. }
  983. }
  984. functions.push_back(md);
  985. }
  986. SUBCASE("[Variant] Validate utility functions") {
  987. for (const MethodData &E : functions) {
  988. for (const ArgumentData &F : E.arguments) {
  989. const ArgumentData &arg = F;
  990. TEST_COND((arg.name.is_empty() || arg.name.begins_with("_unnamed_arg")),
  991. vformat("Unnamed argument in position %d of function '%s'.", arg.position, E.name));
  992. }
  993. }
  994. }
  995. }
  996. } // namespace TestVariant
  997. #endif // TEST_VARIANT_H