List.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. a.emplaceBack(alloc, 10);
  18. a.emplaceBack(alloc, 11);
  19. U sum = 0;
  20. err = a.iterateForward([&](const Foo& f) -> Error
  21. {
  22. sum += f.x;
  23. return ErrorCode::NONE;
  24. });
  25. ANKI_TEST_EXPECT_EQ(err, ErrorCode::NONE);
  26. ANKI_TEST_EXPECT_EQ(sum, 21);
  27. a.destroy(alloc);
  28. }
  29. // Sort
  30. {
  31. List<I> a;
  32. Error err = ErrorCode::NONE;
  33. a.emplaceBack(alloc, 10);
  34. a.emplaceBack(alloc, 9);
  35. a.emplaceBack(alloc, 11);
  36. a.emplaceBack(alloc, 2);
  37. a.sort();
  38. Array<I, 4> arr = {{2, 9, 10, 11}};
  39. U u = 0;
  40. err = a.iterateForward([&](const I& i) -> Error
  41. {
  42. if(arr[u++] == i)
  43. {
  44. return ErrorCode::NONE;
  45. }
  46. else
  47. {
  48. return ErrorCode::UNKNOWN;
  49. }
  50. });
  51. ANKI_TEST_EXPECT_EQ(err, ErrorCode::NONE);
  52. a.sort([](I& a, I& b) -> Bool
  53. {
  54. return a > b;
  55. });
  56. Array<I, 4> arr2 = {{11, 10, 9, 2}};
  57. u = 0;
  58. err = a.iterateForward([&](const I& i) -> Error
  59. {
  60. if(arr2[u++] == i)
  61. {
  62. return ErrorCode::NONE;
  63. }
  64. else
  65. {
  66. return ErrorCode::UNKNOWN;
  67. }
  68. });
  69. ANKI_TEST_EXPECT_EQ(err, ErrorCode::NONE);
  70. a.destroy(alloc);
  71. }
  72. // Extreme sort
  73. {
  74. const U COUNT = 10000;
  75. List<Foo> a;
  76. std::list<Foo> b;
  77. for(U i = 0; i < COUNT; i++)
  78. {
  79. I randVal = rand();
  80. Foo f(randVal);
  81. a.pushBack(alloc, f);
  82. b.push_back(f);
  83. }
  84. //auto ta = HighRezTimer::getCurrentTime();
  85. b.sort([](const Foo& a, const Foo& b){return a.x < b.x;});
  86. //auto tb = HighRezTimer::getCurrentTime();
  87. a.sort([](const Foo& a, const Foo& b){return a.x < b.x;});
  88. //auto tc = HighRezTimer::getCurrentTime();
  89. //printf("%f %f\n", tb - ta, tc - tb);
  90. auto ait = a.getBegin();
  91. auto bit = b.begin();
  92. auto aend = a.getEnd();
  93. auto bend = b.end();
  94. while(ait != aend && bit != bend)
  95. {
  96. const Foo& afoo = *ait;
  97. const Foo& bfoo = *bit;
  98. ANKI_TEST_EXPECT_EQ(afoo, bfoo);
  99. ++ait;
  100. ++bit;
  101. }
  102. ANKI_TEST_EXPECT_EQ(ait, aend);
  103. ANKI_TEST_EXPECT_EQ(bit, bend);
  104. a.destroy(alloc);
  105. }
  106. // Iterate
  107. {
  108. List<I> a;
  109. Error err = ErrorCode::NONE;
  110. a.emplaceBack(alloc, 10);
  111. a.emplaceBack(alloc, 9);
  112. a.emplaceBack(alloc, 11);
  113. a.emplaceBack(alloc, 2);
  114. Array<I, 4> arr = {{10, 9, 11, 2}};
  115. U count = 0;
  116. // Forward
  117. List<I>::ConstIterator it = a.getBegin();
  118. for(; it != a.getEnd() && !err; ++it)
  119. {
  120. if(*it != arr[count++])
  121. {
  122. err = ErrorCode::UNKNOWN;
  123. }
  124. }
  125. ANKI_TEST_EXPECT_EQ(err, ErrorCode::NONE);
  126. // Backwards
  127. --it;
  128. for(; it != a.getBegin() && !err; --it)
  129. {
  130. if(*it != arr[--count])
  131. {
  132. err = ErrorCode::UNKNOWN;
  133. }
  134. }
  135. ANKI_TEST_EXPECT_EQ(err, ErrorCode::NONE);
  136. a.destroy(alloc);
  137. }
  138. // Erase
  139. {
  140. List<I> a;
  141. a.emplaceBack(alloc, 10);
  142. a.erase(alloc, a.getBegin());
  143. ANKI_TEST_EXPECT_EQ(a.isEmpty(), true);
  144. a.emplaceBack(alloc, 10);
  145. a.emplaceBack(alloc, 20);
  146. a.erase(alloc, a.getBegin() + 1);
  147. ANKI_TEST_EXPECT_EQ(10, *a.getBegin());
  148. a.emplaceFront(alloc, 5);
  149. a.emplaceBack(alloc, 30);
  150. ANKI_TEST_EXPECT_EQ(5, *(a.getBegin()));
  151. ANKI_TEST_EXPECT_EQ(10, *(a.getEnd() - 2));
  152. ANKI_TEST_EXPECT_EQ(30, *(a.getEnd() - 1));
  153. a.erase(alloc, a.getEnd() - 2);
  154. ANKI_TEST_EXPECT_EQ(5, *(a.getBegin()));
  155. ANKI_TEST_EXPECT_EQ(30, *(a.getEnd() - 1));
  156. a.erase(alloc, a.getBegin());
  157. a.erase(alloc, a.getBegin());
  158. }
  159. }