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.
 
 
 

176 regels
4.0 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. 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(ArrayPushWithShift)
  43. {
  44. Array<int> a;
  45. a << 0 << 1 << 2 << 3;
  46. LOLUNIT_ASSERT_EQUAL(a[0], 0);
  47. LOLUNIT_ASSERT_EQUAL(a[1], 1);
  48. LOLUNIT_ASSERT_EQUAL(a[2], 2);
  49. LOLUNIT_ASSERT_EQUAL(a[3], 3);
  50. }
  51. LOLUNIT_TEST(ArrayCopy)
  52. {
  53. Array<int> a;
  54. a << 0 << 1 << 2 << 3;
  55. Array<int> b = a;
  56. LOLUNIT_ASSERT_EQUAL(b[0], 0);
  57. LOLUNIT_ASSERT_EQUAL(b[1], 1);
  58. LOLUNIT_ASSERT_EQUAL(b[2], 2);
  59. LOLUNIT_ASSERT_EQUAL(b[3], 3);
  60. }
  61. LOLUNIT_TEST(ArrayRemove)
  62. {
  63. Array<int> a;
  64. a << 0 << 1 << 2 << 3;
  65. a.Remove(1);
  66. LOLUNIT_ASSERT_EQUAL(a[0], 0);
  67. LOLUNIT_ASSERT_EQUAL(a[1], 2);
  68. LOLUNIT_ASSERT_EQUAL(a[2], 3);
  69. }
  70. LOLUNIT_TEST(EightElements)
  71. {
  72. Array<int, long, float, double, unsigned, char, bool, void *> a;
  73. a.Push(1, 2, 3.f, 4.0, 5, 'a', true, 0);
  74. LOLUNIT_ASSERT_EQUAL(a[0].m1, 1);
  75. LOLUNIT_ASSERT_EQUAL(a[0].m2, 2);
  76. LOLUNIT_ASSERT_EQUAL(a[0].m3, 3.f);
  77. LOLUNIT_ASSERT_EQUAL(a[0].m4, 4.0);
  78. LOLUNIT_ASSERT_EQUAL(a[0].m5, 5);
  79. LOLUNIT_ASSERT_EQUAL(a[0].m6, 'a');
  80. LOLUNIT_ASSERT_EQUAL(a[0].m7, true);
  81. LOLUNIT_ASSERT_EQUAL(a[0].m8, 0);
  82. }
  83. LOLUNIT_TEST(ArraySwap)
  84. {
  85. Array<int, int> a;
  86. a.Push(10, 20);
  87. a.Push(30, 40);
  88. a.Swap(0, 1);
  89. LOLUNIT_ASSERT_EQUAL(30, a[0].m1);
  90. LOLUNIT_ASSERT_EQUAL(40, a[0].m2);
  91. LOLUNIT_ASSERT_EQUAL(10, a[1].m1);
  92. LOLUNIT_ASSERT_EQUAL(20, a[1].m2);
  93. }
  94. LOLUNIT_TEST(ArrayConcat)
  95. {
  96. Array<int> a, b;
  97. a << 0 << 1;
  98. b << 2 << 3;
  99. Array<int> c = a + b;
  100. LOLUNIT_ASSERT_EQUAL(c[0], 0);
  101. LOLUNIT_ASSERT_EQUAL(c[1], 1);
  102. LOLUNIT_ASSERT_EQUAL(c[2], 2);
  103. LOLUNIT_ASSERT_EQUAL(c[3], 3);
  104. }
  105. LOLUNIT_TEST(ArrayAppend)
  106. {
  107. Array<int> a, b;
  108. a << 0 << 1;
  109. b << 2 << 3;
  110. a += b;
  111. LOLUNIT_ASSERT_EQUAL(a[0], 0);
  112. LOLUNIT_ASSERT_EQUAL(a[1], 1);
  113. LOLUNIT_ASSERT_EQUAL(a[2], 2);
  114. LOLUNIT_ASSERT_EQUAL(a[3], 3);
  115. b += b;
  116. LOLUNIT_ASSERT_EQUAL(b[0], 2);
  117. LOLUNIT_ASSERT_EQUAL(b[1], 3);
  118. LOLUNIT_ASSERT_EQUAL(b[2], 2);
  119. LOLUNIT_ASSERT_EQUAL(b[3], 3);
  120. }
  121. LOLUNIT_TEST(ElementCtorDtor)
  122. {
  123. /* Ensure array elements get created and destroyed the proper
  124. * number of times. */
  125. TrackedObj::m_ctor = 0;
  126. TrackedObj::m_dtor = 0;
  127. {
  128. Array<TrackedObj> a;
  129. a.Push(TrackedObj());
  130. }
  131. LOLUNIT_ASSERT_EQUAL(TrackedObj::m_ctor, TrackedObj::m_dtor);
  132. TrackedObj::m_ctor = 0;
  133. TrackedObj::m_dtor = 0;
  134. {
  135. Array<TrackedObj> a;
  136. a.Resize(2);
  137. a.Resize(4);
  138. a.Resize(1);
  139. }
  140. LOLUNIT_ASSERT_EQUAL(TrackedObj::m_ctor, TrackedObj::m_dtor);
  141. }
  142. };
  143. } /* namespace lol */