Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

210 rindas
4.8 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(ArrayConcat)
  123. {
  124. Array<int> a, b;
  125. a << 0 << 1;
  126. b << 2 << 3;
  127. Array<int> c = a + b;
  128. LOLUNIT_ASSERT_EQUAL(c[0], 0);
  129. LOLUNIT_ASSERT_EQUAL(c[1], 1);
  130. LOLUNIT_ASSERT_EQUAL(c[2], 2);
  131. LOLUNIT_ASSERT_EQUAL(c[3], 3);
  132. }
  133. LOLUNIT_TEST(ArrayAppend)
  134. {
  135. Array<int> a, b;
  136. a << 0 << 1;
  137. b << 2 << 3;
  138. a += b;
  139. LOLUNIT_ASSERT_EQUAL(a[0], 0);
  140. LOLUNIT_ASSERT_EQUAL(a[1], 1);
  141. LOLUNIT_ASSERT_EQUAL(a[2], 2);
  142. LOLUNIT_ASSERT_EQUAL(a[3], 3);
  143. b += b;
  144. LOLUNIT_ASSERT_EQUAL(b[0], 2);
  145. LOLUNIT_ASSERT_EQUAL(b[1], 3);
  146. LOLUNIT_ASSERT_EQUAL(b[2], 2);
  147. LOLUNIT_ASSERT_EQUAL(b[3], 3);
  148. }
  149. LOLUNIT_TEST(ElementCtorDtor)
  150. {
  151. /* Ensure array elements get created and destroyed the proper
  152. * number of times. */
  153. TrackedObj::m_ctor = 0;
  154. TrackedObj::m_dtor = 0;
  155. {
  156. Array<TrackedObj> a;
  157. a.Push(TrackedObj());
  158. }
  159. LOLUNIT_ASSERT_EQUAL(TrackedObj::m_ctor, TrackedObj::m_dtor);
  160. TrackedObj::m_ctor = 0;
  161. TrackedObj::m_dtor = 0;
  162. {
  163. Array<TrackedObj> a;
  164. a.Resize(2);
  165. a.Resize(4);
  166. a.Resize(1);
  167. }
  168. LOLUNIT_ASSERT_EQUAL(TrackedObj::m_ctor, TrackedObj::m_dtor);
  169. }
  170. };
  171. } /* namespace lol */