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.
 
 
 

243 lines
5.6 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 "core.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. /* HACK: we disable these tests because they fail with the
  31. * Xcode iPhone compiler. */
  32. #if !defined __clang__ || !defined __arm__
  33. LOLUNIT_TEST(ArrayPush)
  34. {
  35. Array<int> a;
  36. a.Push(0);
  37. a.Push(1);
  38. a.Push(2);
  39. a.Push(3);
  40. LOLUNIT_ASSERT_EQUAL(a[0], 0);
  41. LOLUNIT_ASSERT_EQUAL(a[1], 1);
  42. LOLUNIT_ASSERT_EQUAL(a[2], 2);
  43. LOLUNIT_ASSERT_EQUAL(a[3], 3);
  44. }
  45. LOLUNIT_TEST(ArrayInitializer)
  46. {
  47. Array<int> a = { 2, 4, 6 };
  48. LOLUNIT_ASSERT_EQUAL(a[0], 2);
  49. LOLUNIT_ASSERT_EQUAL(a[1], 4);
  50. LOLUNIT_ASSERT_EQUAL(a[2], 6);
  51. }
  52. LOLUNIT_TEST(ArrayPushWithShift)
  53. {
  54. Array<int> a;
  55. a << 0 << 1 << 2 << 3;
  56. LOLUNIT_ASSERT_EQUAL(a[0], 0);
  57. LOLUNIT_ASSERT_EQUAL(a[1], 1);
  58. LOLUNIT_ASSERT_EQUAL(a[2], 2);
  59. LOLUNIT_ASSERT_EQUAL(a[3], 3);
  60. }
  61. LOLUNIT_TEST(ArrayCopy)
  62. {
  63. Array<int> a;
  64. a << 0 << 1 << 2 << 3;
  65. Array<int> b = a;
  66. LOLUNIT_ASSERT_EQUAL(b[0], 0);
  67. LOLUNIT_ASSERT_EQUAL(b[1], 1);
  68. LOLUNIT_ASSERT_EQUAL(b[2], 2);
  69. LOLUNIT_ASSERT_EQUAL(b[3], 3);
  70. }
  71. LOLUNIT_TEST(ArrayRemove)
  72. {
  73. Array<int> a;
  74. a << 0 << 1 << 2 << 3;
  75. a.Remove(1);
  76. LOLUNIT_ASSERT_EQUAL(a.Count(), 3);
  77. LOLUNIT_ASSERT_EQUAL(a[0], 0);
  78. LOLUNIT_ASSERT_EQUAL(a[1], 2);
  79. LOLUNIT_ASSERT_EQUAL(a[2], 3);
  80. Array<int> b;
  81. b << 0 << 1 << 2 << 3;
  82. b.Remove(-2);
  83. LOLUNIT_ASSERT_EQUAL(b.Count(), 3);
  84. LOLUNIT_ASSERT_EQUAL(b[0], 0);
  85. LOLUNIT_ASSERT_EQUAL(b[1], 1);
  86. LOLUNIT_ASSERT_EQUAL(b[2], 3);
  87. }
  88. LOLUNIT_TEST(ArrayRemoveSwap)
  89. {
  90. Array<int> a;
  91. a << 0 << 1 << 2 << 3;
  92. a.RemoveSwap(1);
  93. LOLUNIT_ASSERT_EQUAL(a.Count(), 3);
  94. LOLUNIT_ASSERT_EQUAL(a[0], 0);
  95. LOLUNIT_ASSERT_EQUAL(a[1], 3);
  96. LOLUNIT_ASSERT_EQUAL(a[2], 2);
  97. Array<int> b;
  98. b << 0 << 1 << 2 << 3;
  99. b.Remove(1, 2);
  100. LOLUNIT_ASSERT_EQUAL(b.Count(), 2);
  101. LOLUNIT_ASSERT_EQUAL(b[0], 0);
  102. LOLUNIT_ASSERT_EQUAL(b[1], 3);
  103. }
  104. #endif
  105. LOLUNIT_TEST(EightElements)
  106. {
  107. Array<int, long, float, double, unsigned, char, bool, void *> a;
  108. a.Push(1, 2, 3.f, 4.0, 5, 'a', true, 0);
  109. LOLUNIT_ASSERT_EQUAL(a[0].m1, 1);
  110. LOLUNIT_ASSERT_EQUAL(a[0].m2, 2);
  111. LOLUNIT_ASSERT_EQUAL(a[0].m3, 3.f);
  112. LOLUNIT_ASSERT_EQUAL(a[0].m4, 4.0);
  113. LOLUNIT_ASSERT_EQUAL(a[0].m5, 5);
  114. LOLUNIT_ASSERT_EQUAL(a[0].m6, 'a');
  115. LOLUNIT_ASSERT_EQUAL(a[0].m7, true);
  116. LOLUNIT_ASSERT_EQUAL(a[0].m8, 0);
  117. }
  118. LOLUNIT_TEST(ArraySwap)
  119. {
  120. Array<int, int> a;
  121. a.Push(10, 20);
  122. a.Push(30, 40);
  123. a.Swap(0, 1);
  124. LOLUNIT_ASSERT_EQUAL(30, a[0].m1);
  125. LOLUNIT_ASSERT_EQUAL(40, a[0].m2);
  126. LOLUNIT_ASSERT_EQUAL(10, a[1].m1);
  127. LOLUNIT_ASSERT_EQUAL(20, a[1].m2);
  128. }
  129. LOLUNIT_TEST(ArrayInsert)
  130. {
  131. Array<int> a;
  132. a << 1 << 2;
  133. a.Insert(5, 0);
  134. LOLUNIT_ASSERT_EQUAL(5, a[0]);
  135. LOLUNIT_ASSERT_EQUAL(1, a[1]);
  136. LOLUNIT_ASSERT_EQUAL(2, a[2]);
  137. a.Insert(6, 3);
  138. LOLUNIT_ASSERT_EQUAL(5, a[0]);
  139. LOLUNIT_ASSERT_EQUAL(1, a[1]);
  140. LOLUNIT_ASSERT_EQUAL(2, a[2]);
  141. LOLUNIT_ASSERT_EQUAL(6, a[3]);
  142. a.Insert(7, 2);
  143. LOLUNIT_ASSERT_EQUAL(5, a[0]);
  144. LOLUNIT_ASSERT_EQUAL(1, a[1]);
  145. LOLUNIT_ASSERT_EQUAL(7, a[2]);
  146. LOLUNIT_ASSERT_EQUAL(2, a[3]);
  147. LOLUNIT_ASSERT_EQUAL(6, a[4]);
  148. }
  149. LOLUNIT_TEST(ArrayConcat)
  150. {
  151. Array<int> a, b;
  152. a << 0 << 1;
  153. b << 2 << 3;
  154. Array<int> c = a + b;
  155. LOLUNIT_ASSERT_EQUAL(c[0], 0);
  156. LOLUNIT_ASSERT_EQUAL(c[1], 1);
  157. LOLUNIT_ASSERT_EQUAL(c[2], 2);
  158. LOLUNIT_ASSERT_EQUAL(c[3], 3);
  159. }
  160. LOLUNIT_TEST(ArrayAppend)
  161. {
  162. Array<int> a, b;
  163. a << 0 << 1;
  164. b << 2 << 3;
  165. a += b;
  166. LOLUNIT_ASSERT_EQUAL(a[0], 0);
  167. LOLUNIT_ASSERT_EQUAL(a[1], 1);
  168. LOLUNIT_ASSERT_EQUAL(a[2], 2);
  169. LOLUNIT_ASSERT_EQUAL(a[3], 3);
  170. b += b;
  171. LOLUNIT_ASSERT_EQUAL(b[0], 2);
  172. LOLUNIT_ASSERT_EQUAL(b[1], 3);
  173. LOLUNIT_ASSERT_EQUAL(b[2], 2);
  174. LOLUNIT_ASSERT_EQUAL(b[3], 3);
  175. }
  176. LOLUNIT_TEST(ElementCtorDtor)
  177. {
  178. /* Ensure array elements get created and destroyed the proper
  179. * number of times. */
  180. TrackedObj::m_ctor = 0;
  181. TrackedObj::m_dtor = 0;
  182. {
  183. Array<TrackedObj> a;
  184. a.Push(TrackedObj());
  185. }
  186. LOLUNIT_ASSERT_EQUAL(TrackedObj::m_ctor, TrackedObj::m_dtor);
  187. TrackedObj::m_ctor = 0;
  188. TrackedObj::m_dtor = 0;
  189. {
  190. Array<TrackedObj> a;
  191. a.Resize(2);
  192. a.Resize(4);
  193. a.Resize(1);
  194. }
  195. LOLUNIT_ASSERT_EQUAL(TrackedObj::m_ctor, TrackedObj::m_dtor);
  196. }
  197. };
  198. } /* namespace lol */