RingBufferTests.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #include "../Tests.hpp"
  2. #include "core/data/RingBuffer.hpp"
  3. template class Core::RingBuffer<int, 5>;
  4. struct Tester final {
  5. static int ids;
  6. static int sum;
  7. int id;
  8. Tester() : id(++ids) {
  9. sum += id;
  10. }
  11. Tester(const Tester&) : id(++ids) {
  12. sum += id;
  13. }
  14. Tester(Tester&&) : id(++ids) {
  15. sum += id;
  16. }
  17. Tester& operator=(const Tester&) {
  18. return *this;
  19. }
  20. Tester& operator=(Tester&&) {
  21. return *this;
  22. }
  23. ~Tester() {
  24. sum -= id;
  25. }
  26. void toString(Core::BufferString& s) const {
  27. s.append(id);
  28. }
  29. };
  30. int Tester::ids = 0;
  31. int Tester::sum = 0;
  32. static void resetTester() {
  33. Tester::ids = 0;
  34. Tester::sum = 0;
  35. }
  36. static void testReadAndWrite() {
  37. Core::RingBuffer<int, 5> buffer;
  38. CORE_TEST_FALSE(buffer.canRemove());
  39. CORE_TEST_EQUAL(0, buffer.getLength());
  40. buffer.add(4);
  41. CORE_TEST_EQUAL(1, buffer.getLength());
  42. CORE_TEST_TRUE(buffer.canRemove());
  43. CORE_TEST_EQUAL(4, buffer[0]);
  44. buffer.remove();
  45. CORE_TEST_FALSE(buffer.canRemove());
  46. CORE_TEST_EQUAL(0, buffer.getLength());
  47. }
  48. static void testOverflow() {
  49. Core::RingBuffer<int, 3> buffer;
  50. buffer.add(1).add(2).add(3).add(4).add(5);
  51. CORE_TEST_EQUAL(3, buffer.getLength());
  52. CORE_TEST_EQUAL(1, buffer[0]);
  53. buffer.remove();
  54. CORE_TEST_EQUAL(2, buffer.getLength());
  55. CORE_TEST_EQUAL(2, buffer[0]);
  56. buffer.remove();
  57. CORE_TEST_EQUAL(1, buffer.getLength());
  58. CORE_TEST_EQUAL(3, buffer[0]);
  59. buffer.remove();
  60. CORE_TEST_FALSE(buffer.canRemove());
  61. CORE_TEST_EQUAL(0, buffer.getLength());
  62. }
  63. static void testRefill() {
  64. Core::RingBuffer<int, 3> buffer;
  65. buffer.add(1).add(2).add(3).add(4);
  66. CORE_TEST_EQUAL(3, buffer.getLength());
  67. CORE_TEST_TRUE(buffer.canRemove());
  68. CORE_TEST_EQUAL(1, buffer[0]);
  69. buffer.remove();
  70. CORE_TEST_EQUAL(2, buffer[0]);
  71. buffer.remove();
  72. CORE_TEST_EQUAL(3, buffer[0]);
  73. buffer.remove();
  74. CORE_TEST_EQUAL(0, buffer.getLength());
  75. CORE_TEST_FALSE(buffer.canRemove());
  76. buffer.add(5).add(6);
  77. CORE_TEST_EQUAL(2, buffer.getLength());
  78. CORE_TEST_TRUE(buffer.canRemove());
  79. CORE_TEST_EQUAL(5, buffer[0]);
  80. buffer.remove();
  81. CORE_TEST_EQUAL(6, buffer[0]);
  82. buffer.remove();
  83. CORE_TEST_FALSE(buffer.canRemove());
  84. CORE_TEST_EQUAL(0, buffer.getLength());
  85. }
  86. static void testClear() {
  87. Core::RingBuffer<int, 3> buffer;
  88. buffer.add(1).add(2);
  89. CORE_TEST_EQUAL(2, buffer.getLength());
  90. buffer.clear();
  91. CORE_TEST_FALSE(buffer.canRemove());
  92. CORE_TEST_EQUAL(0, buffer.getLength());
  93. }
  94. static void testConstructDestruct() {
  95. resetTester();
  96. Core::RingBuffer<Tester, 3> buffer;
  97. buffer.add();
  98. CORE_TEST_EQUAL(1, Tester::sum);
  99. buffer.add();
  100. CORE_TEST_EQUAL(3, Tester::sum);
  101. buffer.add();
  102. CORE_TEST_EQUAL(6, Tester::sum);
  103. buffer.remove();
  104. CORE_TEST_EQUAL(5, Tester::sum);
  105. buffer.remove();
  106. CORE_TEST_EQUAL(3, Tester::sum);
  107. buffer.remove();
  108. CORE_TEST_EQUAL(0, Tester::sum);
  109. }
  110. static void testCopyDestruct() {
  111. resetTester();
  112. {
  113. Core::RingBuffer<Tester, 3> buffer;
  114. buffer.add();
  115. CORE_TEST_EQUAL(1, Tester::sum);
  116. buffer.add();
  117. CORE_TEST_EQUAL(3, Tester::sum);
  118. buffer.add();
  119. CORE_TEST_EQUAL(6, Tester::sum);
  120. {
  121. Core::RingBuffer<Tester, 3> copy = buffer;
  122. CORE_TEST_EQUAL(6 + 4 + 5 + 6, Tester::sum);
  123. }
  124. CORE_TEST_EQUAL(6, Tester::sum);
  125. }
  126. CORE_TEST_EQUAL(0, Tester::sum);
  127. }
  128. static void testCopyAssignmentDestruct() {
  129. resetTester();
  130. {
  131. Core::RingBuffer<Tester, 3> buffer;
  132. buffer.add();
  133. CORE_TEST_EQUAL(1, Tester::sum);
  134. buffer.add();
  135. CORE_TEST_EQUAL(3, Tester::sum);
  136. buffer.add();
  137. CORE_TEST_EQUAL(6, Tester::sum);
  138. {
  139. Core::RingBuffer<Tester, 3> copy;
  140. copy = buffer;
  141. CORE_TEST_EQUAL(6 + 4 + 5 + 6, Tester::sum);
  142. }
  143. CORE_TEST_EQUAL(6, Tester::sum);
  144. }
  145. CORE_TEST_EQUAL(0, Tester::sum);
  146. }
  147. static void testMoveDestruct() {
  148. resetTester();
  149. {
  150. Core::RingBuffer<Tester, 3> buffer;
  151. buffer.add();
  152. CORE_TEST_EQUAL(1, Tester::sum);
  153. buffer.add();
  154. CORE_TEST_EQUAL(3, Tester::sum);
  155. buffer.add();
  156. CORE_TEST_EQUAL(6, Tester::sum);
  157. {
  158. Core::RingBuffer<Tester, 3> move = Core::move(buffer);
  159. CORE_TEST_EQUAL(4 + 5 + 6, Tester::sum);
  160. CORE_TEST_EQUAL(0, buffer.getLength());
  161. }
  162. CORE_TEST_EQUAL(0, Tester::sum);
  163. }
  164. CORE_TEST_EQUAL(0, Tester::sum);
  165. }
  166. static void testMoveAssignmentDestruct() {
  167. resetTester();
  168. {
  169. Core::RingBuffer<Tester, 3> buffer;
  170. buffer.add();
  171. CORE_TEST_EQUAL(1, Tester::sum);
  172. buffer.add();
  173. CORE_TEST_EQUAL(3, Tester::sum);
  174. buffer.add();
  175. CORE_TEST_EQUAL(6, Tester::sum);
  176. {
  177. Core::RingBuffer<Tester, 3> move;
  178. move = Core::move(buffer);
  179. CORE_TEST_EQUAL(4 + 5 + 6, Tester::sum);
  180. CORE_TEST_EQUAL(0, buffer.getLength());
  181. }
  182. CORE_TEST_EQUAL(0, Tester::sum);
  183. }
  184. CORE_TEST_EQUAL(0, Tester::sum);
  185. }
  186. static void testOverall() {
  187. resetTester();
  188. Core::RingBuffer<Tester, 3> buffer;
  189. buffer.add().add().add();
  190. CORE_TEST_STRING("[1, 2, 3]", buffer);
  191. CORE_TEST_EQUAL(3, buffer.getLength());
  192. CORE_TEST_EQUAL(6, Tester::sum);
  193. buffer.remove();
  194. CORE_TEST_STRING("[2, 3]", buffer);
  195. CORE_TEST_EQUAL(2, buffer.getLength());
  196. CORE_TEST_EQUAL(5, Tester::sum);
  197. buffer.add();
  198. CORE_TEST_STRING("[2, 3, 4]", buffer);
  199. CORE_TEST_EQUAL(3, buffer.getLength());
  200. CORE_TEST_EQUAL(9, Tester::sum);
  201. buffer.remove();
  202. CORE_TEST_STRING("[3, 4]", buffer);
  203. CORE_TEST_EQUAL(2, buffer.getLength());
  204. CORE_TEST_EQUAL(7, Tester::sum);
  205. buffer.add();
  206. CORE_TEST_STRING("[3, 4, 5]", buffer);
  207. CORE_TEST_EQUAL(3, buffer.getLength());
  208. CORE_TEST_EQUAL(12, Tester::sum);
  209. buffer.remove();
  210. CORE_TEST_STRING("[4, 5]", buffer);
  211. CORE_TEST_EQUAL(2, buffer.getLength());
  212. CORE_TEST_EQUAL(9, Tester::sum);
  213. buffer.clear();
  214. CORE_TEST_STRING("[]", buffer);
  215. CORE_TEST_EQUAL(0, buffer.getLength());
  216. CORE_TEST_EQUAL(0, Tester::sum);
  217. }
  218. void Core::testRingBuffer() {
  219. testReadAndWrite();
  220. testOverflow();
  221. testRefill();
  222. testClear();
  223. testConstructDestruct();
  224. testCopyDestruct();
  225. testCopyAssignmentDestruct();
  226. testMoveDestruct();
  227. testMoveAssignmentDestruct();
  228. testOverall();
  229. }