test_vector.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /**************************************************************************/
  2. /* test_vector.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_VECTOR_H
  31. #define TEST_VECTOR_H
  32. #include "core/templates/vector.h"
  33. #include "tests/test_macros.h"
  34. namespace TestVector {
  35. TEST_CASE("[Vector] List initialization") {
  36. Vector<int> vector{ 0, 1, 2, 3, 4 };
  37. CHECK(vector.size() == 5);
  38. CHECK(vector[0] == 0);
  39. CHECK(vector[1] == 1);
  40. CHECK(vector[2] == 2);
  41. CHECK(vector[3] == 3);
  42. CHECK(vector[4] == 4);
  43. }
  44. TEST_CASE("[Vector] Push back and append") {
  45. Vector<int> vector;
  46. vector.push_back(0);
  47. vector.push_back(1);
  48. vector.push_back(2);
  49. vector.push_back(3);
  50. // Alias for `push_back`.
  51. vector.append(4);
  52. CHECK(vector[0] == 0);
  53. CHECK(vector[1] == 1);
  54. CHECK(vector[2] == 2);
  55. CHECK(vector[3] == 3);
  56. CHECK(vector[4] == 4);
  57. }
  58. TEST_CASE("[Vector] Append array") {
  59. Vector<int> vector;
  60. vector.push_back(1);
  61. vector.push_back(2);
  62. Vector<int> vector_other;
  63. vector_other.push_back(128);
  64. vector_other.push_back(129);
  65. vector.append_array(vector_other);
  66. CHECK(vector.size() == 4);
  67. CHECK(vector[0] == 1);
  68. CHECK(vector[1] == 2);
  69. CHECK(vector[2] == 128);
  70. CHECK(vector[3] == 129);
  71. }
  72. TEST_CASE("[Vector] Insert") {
  73. Vector<int> vector;
  74. vector.insert(0, 2);
  75. vector.insert(0, 8);
  76. vector.insert(2, 5);
  77. vector.insert(1, 5);
  78. vector.insert(0, -2);
  79. CHECK(vector.size() == 5);
  80. CHECK(vector[0] == -2);
  81. CHECK(vector[1] == 8);
  82. CHECK(vector[2] == 5);
  83. CHECK(vector[3] == 2);
  84. CHECK(vector[4] == 5);
  85. }
  86. TEST_CASE("[Vector] Ordered insert") {
  87. Vector<int> vector;
  88. vector.ordered_insert(2);
  89. vector.ordered_insert(8);
  90. vector.ordered_insert(5);
  91. vector.ordered_insert(5);
  92. vector.ordered_insert(-2);
  93. CHECK(vector.size() == 5);
  94. CHECK(vector[0] == -2);
  95. CHECK(vector[1] == 2);
  96. CHECK(vector[2] == 5);
  97. CHECK(vector[3] == 5);
  98. CHECK(vector[4] == 8);
  99. }
  100. TEST_CASE("[Vector] Insert + Ordered insert") {
  101. Vector<int> vector;
  102. vector.ordered_insert(2);
  103. vector.ordered_insert(8);
  104. vector.insert(0, 5);
  105. vector.ordered_insert(5);
  106. vector.insert(1, -2);
  107. CHECK(vector.size() == 5);
  108. CHECK(vector[0] == 5);
  109. CHECK(vector[1] == -2);
  110. CHECK(vector[2] == 2);
  111. CHECK(vector[3] == 5);
  112. CHECK(vector[4] == 8);
  113. }
  114. TEST_CASE("[Vector] Fill large array and modify it") {
  115. Vector<int> vector;
  116. vector.resize(1'000'000);
  117. vector.fill(0x60d07);
  118. vector.write[200] = 0;
  119. CHECK(vector.size() == 1'000'000);
  120. CHECK(vector[0] == 0x60d07);
  121. CHECK(vector[200] == 0);
  122. CHECK(vector[499'999] == 0x60d07);
  123. CHECK(vector[999'999] == 0x60d07);
  124. vector.remove_at(200);
  125. CHECK(vector[200] == 0x60d07);
  126. vector.clear();
  127. CHECK(vector.size() == 0);
  128. }
  129. TEST_CASE("[Vector] Copy creation") {
  130. Vector<int> vector;
  131. vector.push_back(0);
  132. vector.push_back(1);
  133. vector.push_back(2);
  134. vector.push_back(3);
  135. vector.push_back(4);
  136. Vector<int> vector_other = Vector<int>(vector);
  137. vector_other.remove_at(0);
  138. CHECK(vector_other[0] == 1);
  139. CHECK(vector_other[1] == 2);
  140. CHECK(vector_other[2] == 3);
  141. CHECK(vector_other[3] == 4);
  142. // Make sure the original vector isn't modified.
  143. CHECK(vector[0] == 0);
  144. CHECK(vector[1] == 1);
  145. CHECK(vector[2] == 2);
  146. CHECK(vector[3] == 3);
  147. CHECK(vector[4] == 4);
  148. }
  149. TEST_CASE("[Vector] Duplicate") {
  150. Vector<int> vector;
  151. vector.push_back(0);
  152. vector.push_back(1);
  153. vector.push_back(2);
  154. vector.push_back(3);
  155. vector.push_back(4);
  156. Vector<int> vector_other = vector.duplicate();
  157. vector_other.remove_at(0);
  158. CHECK(vector_other[0] == 1);
  159. CHECK(vector_other[1] == 2);
  160. CHECK(vector_other[2] == 3);
  161. CHECK(vector_other[3] == 4);
  162. // Make sure the original vector isn't modified.
  163. CHECK(vector[0] == 0);
  164. CHECK(vector[1] == 1);
  165. CHECK(vector[2] == 2);
  166. CHECK(vector[3] == 3);
  167. CHECK(vector[4] == 4);
  168. }
  169. TEST_CASE("[Vector] Get, set") {
  170. Vector<int> vector;
  171. vector.push_back(0);
  172. vector.push_back(1);
  173. vector.push_back(2);
  174. vector.push_back(3);
  175. vector.push_back(4);
  176. CHECK(vector.get(0) == 0);
  177. CHECK(vector.get(1) == 1);
  178. vector.set(2, 256);
  179. CHECK(vector.get(2) == 256);
  180. CHECK(vector.get(3) == 3);
  181. ERR_PRINT_OFF;
  182. // Invalid (but should not crash): setting out of bounds.
  183. vector.set(6, 500);
  184. ERR_PRINT_ON;
  185. CHECK(vector.get(4) == 4);
  186. }
  187. TEST_CASE("[Vector] To byte array") {
  188. Vector<int> vector;
  189. vector.push_back(0);
  190. vector.push_back(-1);
  191. vector.push_back(2008);
  192. vector.push_back(999999999);
  193. Vector<uint8_t> byte_array = vector.to_byte_array();
  194. CHECK(byte_array.size() == 16);
  195. // vector[0]
  196. CHECK(byte_array[0] == 0);
  197. CHECK(byte_array[1] == 0);
  198. CHECK(byte_array[2] == 0);
  199. CHECK(byte_array[3] == 0);
  200. // vector[1]
  201. CHECK(byte_array[4] == 255);
  202. CHECK(byte_array[5] == 255);
  203. CHECK(byte_array[6] == 255);
  204. CHECK(byte_array[7] == 255);
  205. // vector[2]
  206. CHECK(byte_array[8] == 216);
  207. CHECK(byte_array[9] == 7);
  208. CHECK(byte_array[10] == 0);
  209. CHECK(byte_array[11] == 0);
  210. // vector[3]
  211. CHECK(byte_array[12] == 255);
  212. CHECK(byte_array[13] == 201);
  213. CHECK(byte_array[14] == 154);
  214. CHECK(byte_array[15] == 59);
  215. }
  216. TEST_CASE("[Vector] Slice") {
  217. Vector<int> vector;
  218. vector.push_back(0);
  219. vector.push_back(1);
  220. vector.push_back(2);
  221. vector.push_back(3);
  222. vector.push_back(4);
  223. Vector<int> slice0 = vector.slice(0, 0);
  224. CHECK(slice0.size() == 0);
  225. Vector<int> slice1 = vector.slice(1, 3);
  226. CHECK(slice1.size() == 2);
  227. CHECK(slice1[0] == 1);
  228. CHECK(slice1[1] == 2);
  229. Vector<int> slice2 = vector.slice(1, -1);
  230. CHECK(slice2.size() == 3);
  231. CHECK(slice2[0] == 1);
  232. CHECK(slice2[1] == 2);
  233. CHECK(slice2[2] == 3);
  234. Vector<int> slice3 = vector.slice(3);
  235. CHECK(slice3.size() == 2);
  236. CHECK(slice3[0] == 3);
  237. CHECK(slice3[1] == 4);
  238. Vector<int> slice4 = vector.slice(2, -2);
  239. CHECK(slice4.size() == 1);
  240. CHECK(slice4[0] == 2);
  241. Vector<int> slice5 = vector.slice(-2);
  242. CHECK(slice5.size() == 2);
  243. CHECK(slice5[0] == 3);
  244. CHECK(slice5[1] == 4);
  245. Vector<int> slice6 = vector.slice(2, 42);
  246. CHECK(slice6.size() == 3);
  247. CHECK(slice6[0] == 2);
  248. CHECK(slice6[1] == 3);
  249. CHECK(slice6[2] == 4);
  250. ERR_PRINT_OFF;
  251. Vector<int> slice7 = vector.slice(5, 1);
  252. CHECK(slice7.size() == 0); // Expected to fail.
  253. ERR_PRINT_ON;
  254. }
  255. TEST_CASE("[Vector] Find, has") {
  256. Vector<int> vector;
  257. vector.push_back(3);
  258. vector.push_back(1);
  259. vector.push_back(4);
  260. vector.push_back(0);
  261. vector.push_back(2);
  262. CHECK(vector[0] == 3);
  263. CHECK(vector[1] == 1);
  264. CHECK(vector[2] == 4);
  265. CHECK(vector[3] == 0);
  266. CHECK(vector[4] == 2);
  267. CHECK(vector.find(0) == 3);
  268. CHECK(vector.find(1) == 1);
  269. CHECK(vector.find(2) == 4);
  270. CHECK(vector.find(3) == 0);
  271. CHECK(vector.find(4) == 2);
  272. CHECK(vector.find(-1) == -1);
  273. CHECK(vector.find(5) == -1);
  274. CHECK(vector.has(0));
  275. CHECK(vector.has(1));
  276. CHECK(vector.has(2));
  277. CHECK(vector.has(3));
  278. CHECK(vector.has(4));
  279. CHECK(!vector.has(-1));
  280. CHECK(!vector.has(5));
  281. }
  282. TEST_CASE("[Vector] Remove at") {
  283. Vector<int> vector;
  284. vector.push_back(0);
  285. vector.push_back(1);
  286. vector.push_back(2);
  287. vector.push_back(3);
  288. vector.push_back(4);
  289. vector.remove_at(0);
  290. CHECK(vector[0] == 1);
  291. CHECK(vector[1] == 2);
  292. CHECK(vector[2] == 3);
  293. CHECK(vector[3] == 4);
  294. vector.remove_at(2);
  295. CHECK(vector[0] == 1);
  296. CHECK(vector[1] == 2);
  297. CHECK(vector[2] == 4);
  298. vector.remove_at(1);
  299. CHECK(vector[0] == 1);
  300. CHECK(vector[1] == 4);
  301. vector.remove_at(0);
  302. CHECK(vector[0] == 4);
  303. }
  304. TEST_CASE("[Vector] Remove at and find") {
  305. Vector<int> vector;
  306. vector.push_back(0);
  307. vector.push_back(1);
  308. vector.push_back(2);
  309. vector.push_back(3);
  310. vector.push_back(4);
  311. CHECK(vector.size() == 5);
  312. vector.remove_at(0);
  313. CHECK(vector.size() == 4);
  314. CHECK(vector.find(0) == -1);
  315. CHECK(vector.find(1) != -1);
  316. CHECK(vector.find(2) != -1);
  317. CHECK(vector.find(3) != -1);
  318. CHECK(vector.find(4) != -1);
  319. vector.remove_at(vector.find(3));
  320. CHECK(vector.size() == 3);
  321. CHECK(vector.find(3) == -1);
  322. CHECK(vector.find(1) != -1);
  323. CHECK(vector.find(2) != -1);
  324. CHECK(vector.find(4) != -1);
  325. vector.remove_at(vector.find(2));
  326. CHECK(vector.size() == 2);
  327. CHECK(vector.find(2) == -1);
  328. CHECK(vector.find(1) != -1);
  329. CHECK(vector.find(4) != -1);
  330. vector.remove_at(vector.find(4));
  331. CHECK(vector.size() == 1);
  332. CHECK(vector.find(4) == -1);
  333. CHECK(vector.find(1) != -1);
  334. vector.remove_at(0);
  335. CHECK(vector.is_empty());
  336. CHECK(vector.size() == 0);
  337. }
  338. TEST_CASE("[Vector] Erase") {
  339. Vector<int> vector;
  340. vector.push_back(1);
  341. vector.push_back(3);
  342. vector.push_back(0);
  343. vector.push_back(2);
  344. vector.push_back(4);
  345. CHECK(vector.find(2) == 3);
  346. vector.erase(2);
  347. CHECK(vector.find(2) == -1);
  348. CHECK(vector.size() == 4);
  349. }
  350. TEST_CASE("[Vector] Size, resize, reserve") {
  351. Vector<int> vector;
  352. CHECK(vector.is_empty());
  353. CHECK(vector.size() == 0);
  354. vector.resize(10);
  355. CHECK(vector.size() == 10);
  356. vector.resize(5);
  357. CHECK(vector.size() == 5);
  358. vector.remove_at(0);
  359. vector.remove_at(0);
  360. vector.remove_at(0);
  361. CHECK(vector.size() == 2);
  362. vector.clear();
  363. CHECK(vector.size() == 0);
  364. CHECK(vector.is_empty());
  365. vector.push_back(0);
  366. vector.push_back(0);
  367. vector.push_back(0);
  368. CHECK(vector.size() == 3);
  369. vector.push_back(0);
  370. CHECK(vector.size() == 4);
  371. }
  372. TEST_CASE("[Vector] Sort") {
  373. Vector<int> vector;
  374. vector.push_back(2);
  375. vector.push_back(8);
  376. vector.push_back(-4);
  377. vector.push_back(5);
  378. vector.sort();
  379. CHECK(vector.size() == 4);
  380. CHECK(vector[0] == -4);
  381. CHECK(vector[1] == 2);
  382. CHECK(vector[2] == 5);
  383. CHECK(vector[3] == 8);
  384. }
  385. TEST_CASE("[Vector] Sort custom") {
  386. Vector<String> vector;
  387. vector.push_back("world");
  388. vector.push_back("World");
  389. vector.push_back("Hello");
  390. vector.push_back("10Hello");
  391. vector.push_back("12Hello");
  392. vector.push_back("01Hello");
  393. vector.push_back("1Hello");
  394. vector.push_back(".Hello");
  395. vector.sort_custom<NaturalNoCaseComparator>();
  396. CHECK(vector.size() == 8);
  397. CHECK(vector[0] == ".Hello");
  398. CHECK(vector[1] == "01Hello");
  399. CHECK(vector[2] == "1Hello");
  400. CHECK(vector[3] == "10Hello");
  401. CHECK(vector[4] == "12Hello");
  402. CHECK(vector[5] == "Hello");
  403. CHECK(vector[6] == "world");
  404. CHECK(vector[7] == "World");
  405. }
  406. TEST_CASE("[Vector] Search") {
  407. Vector<int> vector;
  408. vector.push_back(1);
  409. vector.push_back(2);
  410. vector.push_back(3);
  411. vector.push_back(5);
  412. vector.push_back(8);
  413. CHECK(vector.bsearch(2, true) == 1);
  414. CHECK(vector.bsearch(2, false) == 2);
  415. CHECK(vector.bsearch(5, true) == 3);
  416. CHECK(vector.bsearch(5, false) == 4);
  417. }
  418. TEST_CASE("[Vector] Operators") {
  419. Vector<int> vector;
  420. vector.push_back(2);
  421. vector.push_back(8);
  422. vector.push_back(-4);
  423. vector.push_back(5);
  424. Vector<int> vector_other;
  425. vector_other.push_back(2);
  426. vector_other.push_back(8);
  427. vector_other.push_back(-4);
  428. vector_other.push_back(5);
  429. CHECK(vector == vector_other);
  430. vector_other.push_back(10);
  431. CHECK(vector != vector_other);
  432. }
  433. } // namespace TestVector
  434. #endif // TEST_VECTOR_H