List.cpp 3.6 KB

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