Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

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