List.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include "tests/framework/Framework.h"
  6. #include "tests/util/Foo.h"
  7. #include "anki/util/List.h"
  8. #include "anki/util/HighRezTimer.h"
  9. #include <list>
  10. ANKI_TEST(Util, List)
  11. {
  12. HeapAllocator<U8> alloc(allocAligned, nullptr);
  13. // Simple
  14. {
  15. List<Foo> a;
  16. Error err = ErrorCode::NONE;
  17. err = a.emplaceBack(alloc, 10);
  18. ANKI_TEST_EXPECT_EQ(err, ErrorCode::NONE);
  19. err = a.emplaceBack(alloc, 11);
  20. ANKI_TEST_EXPECT_EQ(err, ErrorCode::NONE);
  21. U sum = 0;
  22. err = a.iterateForward([&](const Foo& f) -> Error
  23. {
  24. sum += f.x;
  25. return ErrorCode::NONE;
  26. });
  27. ANKI_TEST_EXPECT_EQ(err, ErrorCode::NONE);
  28. ANKI_TEST_EXPECT_EQ(sum, 21);
  29. a.destroy(alloc);
  30. }
  31. // Sort
  32. {
  33. List<I> a;
  34. Error err = ErrorCode::NONE;
  35. err = a.emplaceBack(alloc, 10);
  36. ANKI_TEST_EXPECT_EQ(err, ErrorCode::NONE);
  37. err = a.emplaceBack(alloc, 9);
  38. ANKI_TEST_EXPECT_EQ(err, ErrorCode::NONE);
  39. err = a.emplaceBack(alloc, 11);
  40. ANKI_TEST_EXPECT_EQ(err, ErrorCode::NONE);
  41. err = a.emplaceBack(alloc, 2);
  42. ANKI_TEST_EXPECT_EQ(err, ErrorCode::NONE);
  43. a.sort();
  44. Array<I, 4> arr = {{2, 9, 10, 11}};
  45. U u = 0;
  46. err = a.iterateForward([&](const I& i) -> Error
  47. {
  48. if(arr[u++] == i)
  49. {
  50. return ErrorCode::NONE;
  51. }
  52. else
  53. {
  54. return ErrorCode::UNKNOWN;
  55. }
  56. });
  57. ANKI_TEST_EXPECT_EQ(err, ErrorCode::NONE);
  58. a.sort([](I& a, I& b) -> Bool
  59. {
  60. return a > b;
  61. });
  62. Array<I, 4> arr2 = {{11, 10, 9, 2}};
  63. u = 0;
  64. err = a.iterateForward([&](const I& i) -> Error
  65. {
  66. if(arr2[u++] == i)
  67. {
  68. return ErrorCode::NONE;
  69. }
  70. else
  71. {
  72. return ErrorCode::UNKNOWN;
  73. }
  74. });
  75. ANKI_TEST_EXPECT_EQ(err, ErrorCode::NONE);
  76. a.destroy(alloc);
  77. }
  78. // Extreme sort
  79. {
  80. const U COUNT = 10000;
  81. List<Foo> a;
  82. std::list<Foo> b;
  83. for(U i = 0; i < COUNT; i++)
  84. {
  85. I randVal = rand();
  86. Foo f(randVal);
  87. ANKI_TEST_EXPECT_NO_ERR(a.pushBack(alloc, f));
  88. b.push_back(f);
  89. }
  90. //auto ta = HighRezTimer::getCurrentTime();
  91. b.sort([](const Foo& a, const Foo& b){return a.x < b.x;});
  92. //auto tb = HighRezTimer::getCurrentTime();
  93. a.sort([](const Foo& a, const Foo& b){return a.x < b.x;});
  94. //auto tc = HighRezTimer::getCurrentTime();
  95. //printf("%f %f\n", tb - ta, tc - tb);
  96. auto ait = a.getBegin();
  97. auto bit = b.begin();
  98. auto aend = a.getEnd();
  99. auto bend = b.end();
  100. while(ait != aend && bit != bend)
  101. {
  102. const Foo& afoo = *ait;
  103. const Foo& bfoo = *bit;
  104. ANKI_TEST_EXPECT_EQ(afoo, bfoo);
  105. ++ait;
  106. ++bit;
  107. }
  108. ANKI_TEST_EXPECT_EQ(ait, aend);
  109. ANKI_TEST_EXPECT_EQ(bit, bend);
  110. a.destroy(alloc);
  111. }
  112. // Iterate
  113. {
  114. List<I> a;
  115. Error err = ErrorCode::NONE;
  116. err = a.emplaceBack(alloc, 10);
  117. ANKI_TEST_EXPECT_EQ(err, ErrorCode::NONE);
  118. err = a.emplaceBack(alloc, 9);
  119. ANKI_TEST_EXPECT_EQ(err, ErrorCode::NONE);
  120. err = a.emplaceBack(alloc, 11);
  121. ANKI_TEST_EXPECT_EQ(err, ErrorCode::NONE);
  122. err = a.emplaceBack(alloc, 2);
  123. ANKI_TEST_EXPECT_EQ(err, ErrorCode::NONE);
  124. Array<I, 4> arr = {{10, 9, 11, 2}};
  125. U count = 0;
  126. // Forward
  127. List<I>::ConstIterator it = a.getBegin();
  128. for(; it != a.getEnd() && !err; ++it)
  129. {
  130. if(*it != arr[count++])
  131. {
  132. err = ErrorCode::UNKNOWN;
  133. }
  134. }
  135. ANKI_TEST_EXPECT_EQ(err, ErrorCode::NONE);
  136. // Backwards
  137. --it;
  138. for(; it != a.getBegin() && !err; --it)
  139. {
  140. if(*it != arr[--count])
  141. {
  142. err = ErrorCode::UNKNOWN;
  143. }
  144. }
  145. ANKI_TEST_EXPECT_EQ(err, ErrorCode::NONE);
  146. a.destroy(alloc);
  147. }
  148. // Erase
  149. {
  150. List<I> a;
  151. ANKI_TEST_EXPECT_EQ(a.emplaceBack(alloc, 10), ErrorCode::NONE);
  152. a.erase(alloc, a.getBegin());
  153. ANKI_TEST_EXPECT_EQ(a.isEmpty(), true);
  154. ANKI_TEST_EXPECT_EQ(a.emplaceBack(alloc, 10), ErrorCode::NONE);
  155. ANKI_TEST_EXPECT_EQ(a.emplaceBack(alloc, 20), ErrorCode::NONE);
  156. a.erase(alloc, a.getBegin() + 1);
  157. ANKI_TEST_EXPECT_EQ(10, *a.getBegin());
  158. ANKI_TEST_EXPECT_EQ(a.emplaceFront(alloc, 5), ErrorCode::NONE);
  159. ANKI_TEST_EXPECT_EQ(a.emplaceBack(alloc, 30), ErrorCode::NONE);
  160. ANKI_TEST_EXPECT_EQ(5, *(a.getBegin()));
  161. ANKI_TEST_EXPECT_EQ(10, *(a.getEnd() - 2));
  162. ANKI_TEST_EXPECT_EQ(30, *(a.getEnd() - 1));
  163. a.erase(alloc, a.getEnd() - 2);
  164. ANKI_TEST_EXPECT_EQ(5, *(a.getBegin()));
  165. ANKI_TEST_EXPECT_EQ(30, *(a.getEnd() - 1));
  166. a.erase(alloc, a.getBegin());
  167. a.erase(alloc, a.getBegin());
  168. }
  169. }