RingBufferTests.cpp 7.6 KB

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