List.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  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 = Error::NONE;
  17. a.emplaceBack(alloc, 10);
  18. a.emplaceBack(alloc, 11);
  19. U sum = 0;
  20. err = a.iterateForward([&](const Foo& f) -> Error {
  21. sum += f.x;
  22. return Error::NONE;
  23. });
  24. ANKI_TEST_EXPECT_EQ(err, Error::NONE);
  25. ANKI_TEST_EXPECT_EQ(sum, 21);
  26. a.destroy(alloc);
  27. }
  28. // Sort
  29. {
  30. List<I> a;
  31. Error err = Error::NONE;
  32. a.emplaceBack(alloc, 10);
  33. a.emplaceBack(alloc, 9);
  34. a.emplaceBack(alloc, 11);
  35. a.emplaceBack(alloc, 2);
  36. a.sort();
  37. Array<I, 4> arr = {{2, 9, 10, 11}};
  38. U u = 0;
  39. err = a.iterateForward([&](const I& i) -> Error {
  40. if(arr[u++] == i)
  41. {
  42. return Error::NONE;
  43. }
  44. else
  45. {
  46. return Error::UNKNOWN;
  47. }
  48. });
  49. ANKI_TEST_EXPECT_EQ(err, Error::NONE);
  50. a.sort([](I& a, I& b) -> Bool {
  51. return a > b;
  52. });
  53. Array<I, 4> arr2 = {{11, 10, 9, 2}};
  54. u = 0;
  55. err = a.iterateForward([&](const I& i) -> Error {
  56. if(arr2[u++] == i)
  57. {
  58. return Error::NONE;
  59. }
  60. else
  61. {
  62. return Error::UNKNOWN;
  63. }
  64. });
  65. ANKI_TEST_EXPECT_EQ(err, Error::NONE);
  66. a.destroy(alloc);
  67. }
  68. // Extreme sort
  69. {
  70. const U COUNT = 10000;
  71. List<Foo> a;
  72. std::list<Foo> b;
  73. for(U i = 0; i < COUNT; i++)
  74. {
  75. I32 randVal = rand();
  76. Foo f(randVal);
  77. a.pushBack(alloc, f);
  78. b.push_back(f);
  79. }
  80. // auto ta = HighRezTimer::getCurrentTime();
  81. b.sort([](const Foo& a, const Foo& b) {
  82. return a.x < b.x;
  83. });
  84. // auto tb = HighRezTimer::getCurrentTime();
  85. a.sort([](const Foo& a, const Foo& b) {
  86. return a.x < b.x;
  87. });
  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 = Error::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 = Error::UNKNOWN;
  123. }
  124. }
  125. ANKI_TEST_EXPECT_EQ(err, Error::NONE);
  126. // Backwards
  127. --it;
  128. for(; it != a.getBegin() && !err; --it)
  129. {
  130. if(*it != arr[--count])
  131. {
  132. err = Error::UNKNOWN;
  133. }
  134. }
  135. ANKI_TEST_EXPECT_EQ(err, Error::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. }