You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

256 line
6.1 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <lol/main.h>
  14. #include "lol/unit.h"
  15. namespace lol
  16. {
  17. struct TrackedObj
  18. {
  19. static int m_ctor, m_dtor;
  20. TrackedObj() { m_ctor++; }
  21. TrackedObj(TrackedObj const &) { m_ctor++; }
  22. ~TrackedObj() { m_dtor++; }
  23. };
  24. int TrackedObj::m_ctor = 0;
  25. int TrackedObj::m_dtor = 0;
  26. LOLUNIT_FIXTURE(ArrayTest)
  27. {
  28. void SetUp() {}
  29. void TearDown() {}
  30. LOLUNIT_TEST(ArrayPush)
  31. {
  32. array<int> a;
  33. a.Push(0);
  34. a.Push(1);
  35. a.Push(2);
  36. a.Push(3);
  37. LOLUNIT_ASSERT_EQUAL(a[0], 0);
  38. LOLUNIT_ASSERT_EQUAL(a[1], 1);
  39. LOLUNIT_ASSERT_EQUAL(a[2], 2);
  40. LOLUNIT_ASSERT_EQUAL(a[3], 3);
  41. }
  42. LOLUNIT_TEST(ArrayInitializer)
  43. {
  44. array<int> a({ 2, 4, 6 });
  45. LOLUNIT_ASSERT_EQUAL(a[0], 2);
  46. LOLUNIT_ASSERT_EQUAL(a[1], 4);
  47. LOLUNIT_ASSERT_EQUAL(a[2], 6);
  48. array<int> b = { 2, 4, 6 };
  49. LOLUNIT_ASSERT_EQUAL(b[0], 2);
  50. LOLUNIT_ASSERT_EQUAL(b[1], 4);
  51. LOLUNIT_ASSERT_EQUAL(b[2], 6);
  52. array<int, float> c = { { 2, 3.0f },
  53. { 4, 5.0f },
  54. { 6, 7.0f } };
  55. LOLUNIT_ASSERT_EQUAL(c[0].m1, 2);
  56. LOLUNIT_ASSERT_EQUAL(c[0].m2, 3.0f);
  57. LOLUNIT_ASSERT_EQUAL(c[1].m1, 4);
  58. LOLUNIT_ASSERT_EQUAL(c[1].m2, 5.0f);
  59. LOLUNIT_ASSERT_EQUAL(c[2].m1, 6);
  60. LOLUNIT_ASSERT_EQUAL(c[2].m2, 7.0f);
  61. }
  62. LOLUNIT_TEST(ArrayPushWithShift)
  63. {
  64. array<int> a;
  65. a << 0 << 1 << 2 << 3;
  66. LOLUNIT_ASSERT_EQUAL(a[0], 0);
  67. LOLUNIT_ASSERT_EQUAL(a[1], 1);
  68. LOLUNIT_ASSERT_EQUAL(a[2], 2);
  69. LOLUNIT_ASSERT_EQUAL(a[3], 3);
  70. }
  71. LOLUNIT_TEST(ArrayCopy)
  72. {
  73. array<int> a;
  74. a << 0 << 1 << 2 << 3;
  75. array<int> b = a;
  76. LOLUNIT_ASSERT_EQUAL(b[0], 0);
  77. LOLUNIT_ASSERT_EQUAL(b[1], 1);
  78. LOLUNIT_ASSERT_EQUAL(b[2], 2);
  79. LOLUNIT_ASSERT_EQUAL(b[3], 3);
  80. }
  81. LOLUNIT_TEST(ArrayRemove)
  82. {
  83. array<int> a;
  84. a << 0 << 1 << 2 << 3;
  85. a.Remove(1);
  86. LOLUNIT_ASSERT_EQUAL(a.Count(), 3);
  87. LOLUNIT_ASSERT_EQUAL(a[0], 0);
  88. LOLUNIT_ASSERT_EQUAL(a[1], 2);
  89. LOLUNIT_ASSERT_EQUAL(a[2], 3);
  90. array<int> b;
  91. b << 0 << 1 << 2 << 3;
  92. b.Remove(-2);
  93. LOLUNIT_ASSERT_EQUAL(b.Count(), 3);
  94. LOLUNIT_ASSERT_EQUAL(b[0], 0);
  95. LOLUNIT_ASSERT_EQUAL(b[1], 1);
  96. LOLUNIT_ASSERT_EQUAL(b[2], 3);
  97. }
  98. LOLUNIT_TEST(ArrayRemoveSwap)
  99. {
  100. array<int> a;
  101. a << 0 << 1 << 2 << 3;
  102. a.RemoveSwap(1);
  103. LOLUNIT_ASSERT_EQUAL(a.Count(), 3);
  104. LOLUNIT_ASSERT_EQUAL(a[0], 0);
  105. LOLUNIT_ASSERT_EQUAL(a[1], 3);
  106. LOLUNIT_ASSERT_EQUAL(a[2], 2);
  107. array<int> b;
  108. b << 0 << 1 << 2 << 3;
  109. b.Remove(1, 2);
  110. LOLUNIT_ASSERT_EQUAL(b.Count(), 2);
  111. LOLUNIT_ASSERT_EQUAL(b[0], 0);
  112. LOLUNIT_ASSERT_EQUAL(b[1], 3);
  113. }
  114. LOLUNIT_TEST(EightElements)
  115. {
  116. array<int, long, float, double, unsigned, char, bool, void *> a;
  117. a.Push(1, 2, 3.f, 4.0, 5, 'a', true, 0);
  118. LOLUNIT_ASSERT_EQUAL(a[0].m1, 1);
  119. LOLUNIT_ASSERT_EQUAL(a[0].m2, 2);
  120. LOLUNIT_ASSERT_EQUAL(a[0].m3, 3.f);
  121. LOLUNIT_ASSERT_EQUAL(a[0].m4, 4.0);
  122. LOLUNIT_ASSERT_EQUAL(a[0].m5, 5);
  123. LOLUNIT_ASSERT_EQUAL(a[0].m6, 'a');
  124. LOLUNIT_ASSERT_EQUAL(a[0].m7, true);
  125. LOLUNIT_ASSERT_EQUAL(a[0].m8, 0);
  126. }
  127. LOLUNIT_TEST(ArraySwap)
  128. {
  129. array<int, int> a;
  130. a.Push(10, 20);
  131. a.Push(30, 40);
  132. a.Swap(0, 1);
  133. LOLUNIT_ASSERT_EQUAL(30, a[0].m1);
  134. LOLUNIT_ASSERT_EQUAL(40, a[0].m2);
  135. LOLUNIT_ASSERT_EQUAL(10, a[1].m1);
  136. LOLUNIT_ASSERT_EQUAL(20, a[1].m2);
  137. }
  138. LOLUNIT_TEST(ArrayInsert)
  139. {
  140. array<int> a;
  141. a << 1 << 2;
  142. a.Insert(5, 0);
  143. LOLUNIT_ASSERT_EQUAL(5, a[0]);
  144. LOLUNIT_ASSERT_EQUAL(1, a[1]);
  145. LOLUNIT_ASSERT_EQUAL(2, a[2]);
  146. a.Insert(6, 3);
  147. LOLUNIT_ASSERT_EQUAL(5, a[0]);
  148. LOLUNIT_ASSERT_EQUAL(1, a[1]);
  149. LOLUNIT_ASSERT_EQUAL(2, a[2]);
  150. LOLUNIT_ASSERT_EQUAL(6, a[3]);
  151. a.Insert(7, 2);
  152. LOLUNIT_ASSERT_EQUAL(5, a[0]);
  153. LOLUNIT_ASSERT_EQUAL(1, a[1]);
  154. LOLUNIT_ASSERT_EQUAL(7, a[2]);
  155. LOLUNIT_ASSERT_EQUAL(2, a[3]);
  156. LOLUNIT_ASSERT_EQUAL(6, a[4]);
  157. }
  158. LOLUNIT_TEST(ArrayConcat)
  159. {
  160. array<int> a, b;
  161. a << 0 << 1;
  162. b << 2 << 3;
  163. array<int> c = a + b;
  164. LOLUNIT_ASSERT_EQUAL(c[0], 0);
  165. LOLUNIT_ASSERT_EQUAL(c[1], 1);
  166. LOLUNIT_ASSERT_EQUAL(c[2], 2);
  167. LOLUNIT_ASSERT_EQUAL(c[3], 3);
  168. }
  169. LOLUNIT_TEST(ArrayAppend)
  170. {
  171. array<int> a, b;
  172. a << 0 << 1;
  173. b << 2 << 3;
  174. a += b;
  175. LOLUNIT_ASSERT_EQUAL(a[0], 0);
  176. LOLUNIT_ASSERT_EQUAL(a[1], 1);
  177. LOLUNIT_ASSERT_EQUAL(a[2], 2);
  178. LOLUNIT_ASSERT_EQUAL(a[3], 3);
  179. b += b;
  180. LOLUNIT_ASSERT_EQUAL(b[0], 2);
  181. LOLUNIT_ASSERT_EQUAL(b[1], 3);
  182. LOLUNIT_ASSERT_EQUAL(b[2], 2);
  183. LOLUNIT_ASSERT_EQUAL(b[3], 3);
  184. }
  185. LOLUNIT_TEST(ElementCtorDtor)
  186. {
  187. /* Ensure array elements get created and destroyed the proper
  188. * number of times. */
  189. TrackedObj::m_ctor = 0;
  190. TrackedObj::m_dtor = 0;
  191. {
  192. array<TrackedObj> a;
  193. a.Push(TrackedObj());
  194. }
  195. LOLUNIT_ASSERT_EQUAL(TrackedObj::m_ctor, TrackedObj::m_dtor);
  196. TrackedObj::m_ctor = 0;
  197. TrackedObj::m_dtor = 0;
  198. {
  199. array<TrackedObj> a;
  200. a.Resize(2);
  201. a.Resize(4);
  202. a.Resize(1);
  203. }
  204. LOLUNIT_ASSERT_EQUAL(TrackedObj::m_ctor, TrackedObj::m_dtor);
  205. }
  206. };
  207. } /* namespace lol */