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

array.cpp 3.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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(ArrayConcat)
  84. {
  85. Array<int> a, b;
  86. a << 0 << 1;
  87. b << 2 << 3;
  88. Array<int> c = a + b;
  89. LOLUNIT_ASSERT_EQUAL(c[0], 0);
  90. LOLUNIT_ASSERT_EQUAL(c[1], 1);
  91. LOLUNIT_ASSERT_EQUAL(c[2], 2);
  92. LOLUNIT_ASSERT_EQUAL(c[3], 3);
  93. }
  94. LOLUNIT_TEST(ArrayAppend)
  95. {
  96. Array<int> a, b;
  97. a << 0 << 1;
  98. b << 2 << 3;
  99. a += b;
  100. LOLUNIT_ASSERT_EQUAL(a[0], 0);
  101. LOLUNIT_ASSERT_EQUAL(a[1], 1);
  102. LOLUNIT_ASSERT_EQUAL(a[2], 2);
  103. LOLUNIT_ASSERT_EQUAL(a[3], 3);
  104. b += b;
  105. LOLUNIT_ASSERT_EQUAL(b[0], 2);
  106. LOLUNIT_ASSERT_EQUAL(b[1], 3);
  107. LOLUNIT_ASSERT_EQUAL(b[2], 2);
  108. LOLUNIT_ASSERT_EQUAL(b[3], 3);
  109. }
  110. LOLUNIT_TEST(ElementCtorDtor)
  111. {
  112. /* Ensure array elements get created and destroyed the proper
  113. * number of times. */
  114. TrackedObj::m_ctor = 0;
  115. TrackedObj::m_dtor = 0;
  116. {
  117. Array<TrackedObj> a;
  118. a.Push(TrackedObj());
  119. }
  120. LOLUNIT_ASSERT_EQUAL(TrackedObj::m_ctor, TrackedObj::m_dtor);
  121. TrackedObj::m_ctor = 0;
  122. TrackedObj::m_dtor = 0;
  123. {
  124. Array<TrackedObj> a;
  125. a.Resize(2);
  126. a.Resize(4);
  127. a.Resize(1);
  128. }
  129. LOLUNIT_ASSERT_EQUAL(TrackedObj::m_ctor, TrackedObj::m_dtor);
  130. }
  131. };
  132. } /* namespace lol */