ListTest.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. 
  2. #include "../testTools.h"
  3. #include "../../DFPSR/collection/List.h"
  4. static void targetByReference(List<int32_t> &target, int32_t value) {
  5. target.push(value);
  6. }
  7. struct Unique {
  8. String name;
  9. // Constructible
  10. Unique(const ReadableString &name) : name(name) {}
  11. // Movable
  12. Unique(Unique &&original) = default;
  13. Unique& operator=(Unique &&original) = default;
  14. // Destructible
  15. ~Unique() = default;
  16. // Not clonable
  17. Unique(const Unique& original) = delete;
  18. Unique& operator=(const Unique& original) = delete;
  19. };
  20. static_assert(std::is_move_constructible<Unique>::value, "The Unique type should be move constructible!");
  21. static_assert(!std::is_copy_constructible<Unique>::value, "The Unique type should not be copy constructible!");
  22. static_assert(!std::is_default_constructible<Unique>::value, "The Unique type should not be default constructible!");
  23. struct Tree {
  24. String name;
  25. List<Tree> children;
  26. Tree() {}
  27. Tree(const ReadableString &name)
  28. : name(name) {}
  29. Tree(const ReadableString &name, List<Tree> children)
  30. : name(name), children(children) {}
  31. // Copy/move construct/assign should be generated automatically by the compiler, because only constructors have been specified manually.
  32. };
  33. static_assert(std::is_move_constructible<Tree>::value, "The Tree type should be move constructible!");
  34. static_assert(std::is_copy_constructible<Tree>::value, "The Tree type should be copy constructible!");
  35. static_assert(std::is_default_constructible<Tree>::value, "The Tree type should be default constructible!");
  36. START_TEST(List)
  37. {
  38. // Populate
  39. List<int32_t> integers;
  40. ASSERT_EQUAL(integers.length(), 0);
  41. targetByReference(integers, 5);
  42. ASSERT_EQUAL(integers.length(), 1);
  43. ASSERT_EQUAL(integers[0], 5);
  44. targetByReference(integers, 86);
  45. ASSERT_EQUAL(integers.length(), 2);
  46. ASSERT_EQUAL(integers[0], 5);
  47. ASSERT_EQUAL(integers[1], 86);
  48. std::function<void(int32_t value)> method = [&integers](int32_t value) {
  49. integers.push(value);
  50. };
  51. method(24);
  52. ASSERT_EQUAL(integers.length(), 3);
  53. ASSERT_EQUAL(integers[0], 5);
  54. ASSERT_EQUAL(integers[1], 86);
  55. ASSERT_EQUAL(integers[2], 24);
  56. integers.pushConstruct(123);
  57. ASSERT_EQUAL(integers.length(), 4);
  58. ASSERT_EQUAL(integers[0], 5);
  59. ASSERT_EQUAL(integers[1], 86);
  60. ASSERT_EQUAL(integers[2], 24);
  61. ASSERT_EQUAL(integers[3], 123);
  62. // Copy
  63. List<int32_t> copied = List<int32_t>(integers);
  64. ASSERT_EQUAL(integers.length(), 4);
  65. ASSERT_EQUAL(integers[0], 5);
  66. ASSERT_EQUAL(integers[1], 86);
  67. ASSERT_EQUAL(integers[2], 24);
  68. ASSERT_EQUAL(integers[3], 123);
  69. ASSERT_EQUAL(copied.length(), 4);
  70. ASSERT_EQUAL(copied[0], 5);
  71. ASSERT_EQUAL(copied[1], 86);
  72. ASSERT_EQUAL(copied[2], 24);
  73. ASSERT_EQUAL(copied[3], 123);
  74. // Assign
  75. List<int32_t> assigned = integers;
  76. ASSERT_EQUAL(integers.length(), 4);
  77. ASSERT_EQUAL(integers[0], 5);
  78. ASSERT_EQUAL(integers[1], 86);
  79. ASSERT_EQUAL(integers[2], 24);
  80. ASSERT_EQUAL(integers[3], 123);
  81. ASSERT_EQUAL(assigned.length(), 4);
  82. ASSERT_EQUAL(assigned[0], 5);
  83. ASSERT_EQUAL(assigned[1], 86);
  84. ASSERT_EQUAL(assigned[2], 24);
  85. ASSERT_EQUAL(assigned[3], 123);
  86. // Move
  87. List<int32_t> moved = std::move(integers);
  88. ASSERT_EQUAL(integers.length(), 0);
  89. ASSERT_EQUAL(moved.length(), 4);
  90. ASSERT_EQUAL(moved[0], 5);
  91. ASSERT_EQUAL(moved[1], 86);
  92. ASSERT_EQUAL(moved[2], 24);
  93. ASSERT_EQUAL(moved[3], 123);
  94. }
  95. { // Fixed size elements
  96. List<int> myIntegers;
  97. ASSERT_EQUAL(myIntegers.length(), 0);
  98. for (int i = 0; i < 1000; i++) {
  99. myIntegers.push(i * 2 + 1); // 1, 3, 5, 7, 9, 11, 13...
  100. }
  101. ASSERT_EQUAL(myIntegers.length(), 1000);
  102. int integerErrorCount = 0;
  103. for (int i = 0; i < 1000; i++) {
  104. if (myIntegers[i] != i * 2 + 1) { // 1, 3, 5, 7, 9, 11, 13...
  105. integerErrorCount++;
  106. }
  107. }
  108. ASSERT_EQUAL(integerErrorCount, 0);
  109. }
  110. { // Complex elements
  111. List<String> myStrings;
  112. ASSERT_EQUAL(myStrings.length(), 0);
  113. myStrings.pushConstruct(U"is");
  114. myStrings.push(U"this");
  115. myStrings.push(U"a");
  116. myStrings.push(U"list");
  117. ASSERT_EQUAL(myStrings.length(), 4);
  118. ASSERT_EQUAL(myStrings[0], U"is");
  119. ASSERT_EQUAL(myStrings[1], U"this");
  120. ASSERT_EQUAL(myStrings[2], U"a");
  121. ASSERT_EQUAL(myStrings[3], U"list");
  122. ASSERT_EQUAL(myStrings.first(), U"is");
  123. ASSERT_EQUAL(myStrings.last(), U"list");
  124. ASSERT_EQUAL(myStrings, List<String>(U"is", U"this", U"a", U"list"));
  125. myStrings.swap(0, 1);
  126. ASSERT_EQUAL(myStrings.length(), 4);
  127. ASSERT_EQUAL(myStrings[0], U"this");
  128. ASSERT_EQUAL(myStrings[1], U"is");
  129. ASSERT_EQUAL(myStrings[2], U"a");
  130. ASSERT_EQUAL(myStrings[3], U"list");
  131. ASSERT_EQUAL(myStrings, List<String>(U"this", U"is", U"a", U"list"));
  132. List<String> myOtherStrings = myStrings;
  133. myStrings.remove(1);
  134. ASSERT_EQUAL(myStrings.length(), 3);
  135. ASSERT_EQUAL(myStrings[0], U"this");
  136. ASSERT_EQUAL(myStrings[1], U"a");
  137. ASSERT_EQUAL(myStrings[2], U"list");
  138. ASSERT_EQUAL(myStrings, List<String>(U"this", U"a", U"list"));
  139. myStrings.remove(0);
  140. ASSERT_EQUAL(myStrings.length(), 2);
  141. ASSERT_EQUAL(myStrings[0], U"a");
  142. ASSERT_EQUAL(myStrings[1], U"list");
  143. ASSERT_EQUAL(myStrings, List<String>(U"a", U"list"));
  144. myStrings.pop();
  145. ASSERT_EQUAL(myStrings.length(), 1);
  146. ASSERT_EQUAL(myStrings[0], U"a");
  147. ASSERT_EQUAL(myStrings, List<String>(U"a"));
  148. myStrings.clear();
  149. ASSERT_EQUAL(myStrings.length(), 0);
  150. ASSERT_EQUAL(myOtherStrings.length(), 4);
  151. ASSERT_EQUAL(myOtherStrings[0], U"this");
  152. ASSERT_EQUAL(myOtherStrings[1], U"is");
  153. ASSERT_EQUAL(myOtherStrings[2], U"a");
  154. ASSERT_EQUAL(myOtherStrings[3], U"list");
  155. ASSERT_EQUAL(myOtherStrings, List<String>(U"this", U"is", U"a", U"list"));
  156. myOtherStrings.clear();
  157. ASSERT_EQUAL(myOtherStrings.length(), 0);
  158. }
  159. {
  160. // Non-copyable types ensure that the constructed object is not accidentally copied into another location.
  161. List<Unique> objects = List<Unique>(Unique(U"One"), Unique(U"Two"));
  162. ASSERT_EQUAL(objects.length(), 2);
  163. ASSERT_EQUAL(objects[0].name, U"One");
  164. ASSERT_EQUAL(objects[1].name, U"Two");
  165. // The push method can not be called with on non-copyable element types, because push must copy the given element.
  166. //objects.push(Unique(U"Three"));
  167. // The pushConstruct method can be used instead to construct the element inside of the list.
  168. objects.pushConstruct(U"Three");
  169. ASSERT_EQUAL(objects.length(), 3);
  170. ASSERT_EQUAL(objects[0].name, U"One");
  171. ASSERT_EQUAL(objects[1].name, U"Two");
  172. ASSERT_EQUAL(objects[2].name, U"Three");
  173. objects.swap(0, 1);
  174. ASSERT_EQUAL(objects.length(), 3);
  175. ASSERT_EQUAL(objects[0].name, U"Two");
  176. ASSERT_EQUAL(objects[1].name, U"One");
  177. ASSERT_EQUAL(objects[2].name, U"Three");
  178. // Move the whole list.
  179. List<Unique> objects2 = std::move(objects);
  180. ASSERT_EQUAL(objects.length(), 0);
  181. ASSERT_EQUAL(objects2.length(), 3);
  182. ASSERT_EQUAL(objects2[0].name, U"Two");
  183. ASSERT_EQUAL(objects2[1].name, U"One");
  184. ASSERT_EQUAL(objects2[2].name, U"Three");
  185. }
  186. {
  187. // Default movable and copyable types should clone the content recursively when the list is copied.
  188. Tree treeOne = Tree(U"A", List<Tree>(Tree(U"B", List<Tree>(Tree(U"D"), Tree(U"E"))), Tree(U"C")));
  189. ASSERT_EQUAL(treeOne.name, U"A");
  190. ASSERT_EQUAL(treeOne.children.length(), 2);
  191. ASSERT_EQUAL(treeOne.children[0].name, U"B");
  192. ASSERT_EQUAL(treeOne.children[0].children.length(), 2);
  193. ASSERT_EQUAL(treeOne.children[0].children[0].name, U"D");
  194. ASSERT_EQUAL(treeOne.children[0].children[0].children.length(), 0);
  195. ASSERT_EQUAL(treeOne.children[0].children[1].name, U"E");
  196. ASSERT_EQUAL(treeOne.children[0].children[1].children.length(), 0);
  197. ASSERT_EQUAL(treeOne.children[1].name, U"C");
  198. ASSERT_EQUAL(treeOne.children[1].children.length(), 0);
  199. // Copy the tree using copy assignment. (Default construction can be optimized away.)
  200. Tree treeTwo = treeOne;
  201. // Modify an element in treeTwo to have the content copied to the next trees.
  202. treeTwo.children[0].name = U"BBBB";
  203. // Copy the tree using copy construction. (Default construction can be optimized away.)
  204. Tree treeThree = Tree(treeTwo);
  205. // Copy the tree using default generated copy construction. (Direct construction without any assignment.)
  206. Tree treeFour(treeThree);
  207. // Modify each version of the tree
  208. treeOne.name = U"A1";
  209. treeTwo.name = U"A2";
  210. treeThree.name = U"A3";
  211. treeFour.name = U"A4";
  212. ASSERT_EQUAL(treeOne.name , U"A1");
  213. ASSERT_EQUAL(treeOne.children.length(), 2);
  214. ASSERT_EQUAL(treeTwo.name , U"A2");
  215. ASSERT_EQUAL(treeTwo.children.length(), 2);
  216. ASSERT_EQUAL(treeThree.name, U"A3");
  217. ASSERT_EQUAL(treeThree.children.length(), 2);
  218. ASSERT_EQUAL(treeFour.name , U"A4");
  219. ASSERT_EQUAL(treeFour.children.length(), 2);
  220. ASSERT_EQUAL(treeFour.children[0].name, U"BBBB");
  221. ASSERT_EQUAL(treeFour.children[0].children.length(), 2);
  222. ASSERT_EQUAL(treeFour.children[0].children[0].name, U"D");
  223. ASSERT_EQUAL(treeFour.children[0].children[0].children.length(), 0);
  224. ASSERT_EQUAL(treeFour.children[0].children[1].name, U"E");
  225. ASSERT_EQUAL(treeFour.children[0].children[1].children.length(), 0);
  226. ASSERT_EQUAL(treeFour.children[1].name, U"C");
  227. ASSERT_EQUAL(treeFour.children[1].children.length(), 0);
  228. // Move the first tree to a new location.
  229. Tree newTree = std::move(treeOne);
  230. ASSERT_EQUAL(treeOne.children.length(), 0);
  231. ASSERT_EQUAL(newTree.name, U"A1");
  232. ASSERT_EQUAL(newTree.children.length(), 2);
  233. ASSERT_EQUAL(newTree.children[0].name, U"B");
  234. ASSERT_EQUAL(newTree.children[0].children.length(), 2);
  235. ASSERT_EQUAL(newTree.children[0].children[0].name, U"D");
  236. ASSERT_EQUAL(newTree.children[0].children[0].children.length(), 0);
  237. ASSERT_EQUAL(newTree.children[0].children[1].name, U"E");
  238. ASSERT_EQUAL(newTree.children[0].children[1].children.length(), 0);
  239. ASSERT_EQUAL(newTree.children[1].name, U"C");
  240. ASSERT_EQUAL(newTree.children[1].children.length(), 0);
  241. }
  242. {
  243. // Construct and push.
  244. Tree tree = Tree(U"A");
  245. tree.children.push(Tree(U"B", List<Tree>(Tree(U"D"), Tree(U"E"))));
  246. tree.children.push(Tree(U"C"));
  247. ASSERT_EQUAL(tree.name, U"A");
  248. ASSERT_EQUAL(tree.children.length(), 2);
  249. ASSERT_EQUAL(tree.children[0].name, U"B");
  250. ASSERT_EQUAL(tree.children[0].children.length(), 2);
  251. ASSERT_EQUAL(tree.children[0].children[0].name, U"D");
  252. ASSERT_EQUAL(tree.children[0].children[0].children.length(), 0);
  253. ASSERT_EQUAL(tree.children[0].children[1].name, U"E");
  254. ASSERT_EQUAL(tree.children[0].children[1].children.length(), 0);
  255. ASSERT_EQUAL(tree.children[1].name, U"C");
  256. ASSERT_EQUAL(tree.children[1].children.length(), 0);
  257. }
  258. {
  259. // Push-construct.
  260. Tree tree = Tree(U"A");
  261. tree.children.pushConstruct(U"B", List<Tree>(Tree(U"D"), Tree(U"E")));
  262. tree.children.pushConstruct(U"C");
  263. ASSERT_EQUAL(tree.name, U"A");
  264. ASSERT_EQUAL(tree.children.length(), 2);
  265. ASSERT_EQUAL(tree.children[0].name, U"B");
  266. ASSERT_EQUAL(tree.children[0].children.length(), 2);
  267. ASSERT_EQUAL(tree.children[0].children[0].name, U"D");
  268. ASSERT_EQUAL(tree.children[0].children[0].children.length(), 0);
  269. ASSERT_EQUAL(tree.children[0].children[1].name, U"E");
  270. ASSERT_EQUAL(tree.children[0].children[1].children.length(), 0);
  271. ASSERT_EQUAL(tree.children[1].name, U"C");
  272. ASSERT_EQUAL(tree.children[1].children.length(), 0);
  273. }
  274. END_TEST