選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

array.cpp 4.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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[0], 0);
  70. LOLUNIT_ASSERT_EQUAL(a[1], 2);
  71. LOLUNIT_ASSERT_EQUAL(a[2], 3);
  72. }
  73. #endif
  74. LOLUNIT_TEST(EightElements)
  75. {
  76. Array<int, long, float, double, unsigned, char, bool, void *> a;
  77. a.Push(1, 2, 3.f, 4.0, 5, 'a', true, 0);
  78. LOLUNIT_ASSERT_EQUAL(a[0].m1, 1);
  79. LOLUNIT_ASSERT_EQUAL(a[0].m2, 2);
  80. LOLUNIT_ASSERT_EQUAL(a[0].m3, 3.f);
  81. LOLUNIT_ASSERT_EQUAL(a[0].m4, 4.0);
  82. LOLUNIT_ASSERT_EQUAL(a[0].m5, 5);
  83. LOLUNIT_ASSERT_EQUAL(a[0].m6, 'a');
  84. LOLUNIT_ASSERT_EQUAL(a[0].m7, true);
  85. LOLUNIT_ASSERT_EQUAL(a[0].m8, 0);
  86. }
  87. LOLUNIT_TEST(ArraySwap)
  88. {
  89. Array<int, int> a;
  90. a.Push(10, 20);
  91. a.Push(30, 40);
  92. a.Swap(0, 1);
  93. LOLUNIT_ASSERT_EQUAL(30, a[0].m1);
  94. LOLUNIT_ASSERT_EQUAL(40, a[0].m2);
  95. LOLUNIT_ASSERT_EQUAL(10, a[1].m1);
  96. LOLUNIT_ASSERT_EQUAL(20, a[1].m2);
  97. }
  98. LOLUNIT_TEST(ArrayConcat)
  99. {
  100. Array<int> a, b;
  101. a << 0 << 1;
  102. b << 2 << 3;
  103. Array<int> c = a + b;
  104. LOLUNIT_ASSERT_EQUAL(c[0], 0);
  105. LOLUNIT_ASSERT_EQUAL(c[1], 1);
  106. LOLUNIT_ASSERT_EQUAL(c[2], 2);
  107. LOLUNIT_ASSERT_EQUAL(c[3], 3);
  108. }
  109. LOLUNIT_TEST(ArrayAppend)
  110. {
  111. Array<int> a, b;
  112. a << 0 << 1;
  113. b << 2 << 3;
  114. a += b;
  115. LOLUNIT_ASSERT_EQUAL(a[0], 0);
  116. LOLUNIT_ASSERT_EQUAL(a[1], 1);
  117. LOLUNIT_ASSERT_EQUAL(a[2], 2);
  118. LOLUNIT_ASSERT_EQUAL(a[3], 3);
  119. b += b;
  120. LOLUNIT_ASSERT_EQUAL(b[0], 2);
  121. LOLUNIT_ASSERT_EQUAL(b[1], 3);
  122. LOLUNIT_ASSERT_EQUAL(b[2], 2);
  123. LOLUNIT_ASSERT_EQUAL(b[3], 3);
  124. }
  125. LOLUNIT_TEST(ElementCtorDtor)
  126. {
  127. /* Ensure array elements get created and destroyed the proper
  128. * number of times. */
  129. TrackedObj::m_ctor = 0;
  130. TrackedObj::m_dtor = 0;
  131. {
  132. Array<TrackedObj> a;
  133. a.Push(TrackedObj());
  134. }
  135. LOLUNIT_ASSERT_EQUAL(TrackedObj::m_ctor, TrackedObj::m_dtor);
  136. TrackedObj::m_ctor = 0;
  137. TrackedObj::m_dtor = 0;
  138. {
  139. Array<TrackedObj> a;
  140. a.Resize(2);
  141. a.Resize(4);
  142. a.Resize(1);
  143. }
  144. LOLUNIT_ASSERT_EQUAL(TrackedObj::m_ctor, TrackedObj::m_dtor);
  145. }
  146. };
  147. } /* namespace lol */