123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- #include "tests/RingBufferTests.h"
- #include "data/RingBuffer.h"
- #include "test/Test.h"
- struct Tester final {
- static int ids;
- static int sum;
- int id;
- Tester() : id(++ids) {
- sum += id;
- }
- Tester(const Tester&) : id(++ids) {
- sum += id;
- }
- Tester(Tester&&) : id(++ids) {
- sum += id;
- }
- Tester& operator=(const Tester&) {
- return *this;
- }
- Tester& operator=(Tester&&) {
- return *this;
- }
- ~Tester() {
- sum -= id;
- }
- template<typename String>
- check_return Core::Error toString(String& s) const {
- return s.append(id);
- }
- };
- int Tester::ids = 0;
- int Tester::sum = 0;
- static void resetTester() {
- Tester::ids = 0;
- Tester::sum = 0;
- }
- static void testReadAndWrite() {
- Core::RingBuffer<int, 5> buffer;
- CORE_TEST_FALSE(buffer.canRemove());
- CORE_TEST_EQUAL(0, buffer.getLength());
- CORE_TEST_ERROR(buffer.add(4));
- CORE_TEST_EQUAL(1, buffer.getLength());
- CORE_TEST_TRUE(buffer.canRemove());
- CORE_TEST_EQUAL(4, buffer[0]);
- CORE_TEST_ERROR(buffer.remove());
- CORE_TEST_FALSE(buffer.canRemove());
- CORE_TEST_EQUAL(0, buffer.getLength());
- }
- static void testOverflow() {
- Core::RingBuffer<int, 3> buffer;
- CORE_TEST_ERROR(buffer.add(1));
- CORE_TEST_ERROR(buffer.add(2));
- CORE_TEST_ERROR(buffer.add(3));
- CORE_TEST_EQUAL(Core::Error::CAPACITY_REACHED, buffer.add(4));
- CORE_TEST_EQUAL(Core::Error::CAPACITY_REACHED, buffer.add(5));
- CORE_TEST_EQUAL(3, buffer.getLength());
- CORE_TEST_EQUAL(1, buffer[0]);
- CORE_TEST_ERROR(buffer.remove());
- CORE_TEST_EQUAL(2, buffer.getLength());
- CORE_TEST_EQUAL(2, buffer[0]);
- CORE_TEST_ERROR(buffer.remove());
- CORE_TEST_EQUAL(1, buffer.getLength());
- CORE_TEST_EQUAL(3, buffer[0]);
- CORE_TEST_ERROR(buffer.remove());
- CORE_TEST_FALSE(buffer.canRemove());
- CORE_TEST_EQUAL(0, buffer.getLength());
- }
- static void testRefill() {
- Core::RingBuffer<int, 3> buffer;
- CORE_TEST_ERROR(buffer.add(1));
- CORE_TEST_ERROR(buffer.add(2));
- CORE_TEST_ERROR(buffer.add(3));
- CORE_TEST_EQUAL(Core::Error::CAPACITY_REACHED, buffer.add(4));
- CORE_TEST_EQUAL(3, buffer.getLength());
- CORE_TEST_TRUE(buffer.canRemove());
- CORE_TEST_EQUAL(1, buffer[0]);
- CORE_TEST_ERROR(buffer.remove());
- CORE_TEST_EQUAL(2, buffer[0]);
- CORE_TEST_ERROR(buffer.remove());
- CORE_TEST_EQUAL(3, buffer[0]);
- CORE_TEST_ERROR(buffer.remove());
- CORE_TEST_EQUAL(0, buffer.getLength());
- CORE_TEST_FALSE(buffer.canRemove());
- CORE_TEST_ERROR(buffer.add(5));
- CORE_TEST_ERROR(buffer.add(6));
- CORE_TEST_EQUAL(2, buffer.getLength());
- CORE_TEST_TRUE(buffer.canRemove());
- CORE_TEST_EQUAL(5, buffer[0]);
- CORE_TEST_ERROR(buffer.remove());
- CORE_TEST_EQUAL(6, buffer[0]);
- CORE_TEST_ERROR(buffer.remove());
- CORE_TEST_FALSE(buffer.canRemove());
- CORE_TEST_EQUAL(0, buffer.getLength());
- }
- static void testClear() {
- Core::RingBuffer<int, 3> buffer;
- CORE_TEST_ERROR(buffer.add(1));
- CORE_TEST_ERROR(buffer.add(2));
- CORE_TEST_EQUAL(2, buffer.getLength());
- buffer.clear();
- CORE_TEST_FALSE(buffer.canRemove());
- CORE_TEST_EQUAL(0, buffer.getLength());
- }
- static void testConstructDestruct() {
- resetTester();
- Core::RingBuffer<Tester, 3> buffer;
- CORE_TEST_ERROR(buffer.add());
- CORE_TEST_EQUAL(1, Tester::sum);
- CORE_TEST_ERROR(buffer.add());
- CORE_TEST_EQUAL(3, Tester::sum);
- CORE_TEST_ERROR(buffer.add());
- CORE_TEST_EQUAL(6, Tester::sum);
- CORE_TEST_ERROR(buffer.remove());
- CORE_TEST_EQUAL(5, Tester::sum);
- CORE_TEST_ERROR(buffer.remove());
- CORE_TEST_EQUAL(3, Tester::sum);
- CORE_TEST_ERROR(buffer.remove());
- CORE_TEST_EQUAL(0, Tester::sum);
- }
- static void testCopyDestruct() {
- resetTester();
- {
- Core::RingBuffer<Tester, 3> buffer;
- CORE_TEST_ERROR(buffer.add());
- CORE_TEST_EQUAL(1, Tester::sum);
- CORE_TEST_ERROR(buffer.add());
- CORE_TEST_EQUAL(3, Tester::sum);
- CORE_TEST_ERROR(buffer.add());
- CORE_TEST_EQUAL(6, Tester::sum);
- {
- Core::RingBuffer<Tester, 3> copy = buffer;
- CORE_TEST_EQUAL(6 + 4 + 5 + 6, Tester::sum);
- }
- CORE_TEST_EQUAL(6, Tester::sum);
- }
- CORE_TEST_EQUAL(0, Tester::sum);
- }
- static void testCopyAssignmentDestruct() {
- resetTester();
- {
- Core::RingBuffer<Tester, 3> buffer;
- CORE_TEST_ERROR(buffer.add());
- CORE_TEST_EQUAL(1, Tester::sum);
- CORE_TEST_ERROR(buffer.add());
- CORE_TEST_EQUAL(3, Tester::sum);
- CORE_TEST_ERROR(buffer.add());
- CORE_TEST_EQUAL(6, Tester::sum);
- {
- Core::RingBuffer<Tester, 3> copy;
- copy = buffer;
- CORE_TEST_EQUAL(6 + 4 + 5 + 6, Tester::sum);
- }
- CORE_TEST_EQUAL(6, Tester::sum);
- }
- CORE_TEST_EQUAL(0, Tester::sum);
- }
- static void testMoveDestruct() {
- resetTester();
- {
- Core::RingBuffer<Tester, 3> buffer;
- CORE_TEST_ERROR(buffer.add());
- CORE_TEST_EQUAL(1, Tester::sum);
- CORE_TEST_ERROR(buffer.add());
- CORE_TEST_EQUAL(3, Tester::sum);
- CORE_TEST_ERROR(buffer.add());
- CORE_TEST_EQUAL(6, Tester::sum);
- {
- Core::RingBuffer<Tester, 3> move = Core::move(buffer);
- CORE_TEST_EQUAL(4 + 5 + 6, Tester::sum);
- CORE_TEST_EQUAL(0, buffer.getLength());
- }
- CORE_TEST_EQUAL(0, Tester::sum);
- }
- CORE_TEST_EQUAL(0, Tester::sum);
- }
- static void testMoveAssignmentDestruct() {
- resetTester();
- {
- Core::RingBuffer<Tester, 3> buffer;
- CORE_TEST_ERROR(buffer.add());
- CORE_TEST_EQUAL(1, Tester::sum);
- CORE_TEST_ERROR(buffer.add());
- CORE_TEST_EQUAL(3, Tester::sum);
- CORE_TEST_ERROR(buffer.add());
- CORE_TEST_EQUAL(6, Tester::sum);
- {
- Core::RingBuffer<Tester, 3> move;
- move = Core::move(buffer);
- CORE_TEST_EQUAL(4 + 5 + 6, Tester::sum);
- CORE_TEST_EQUAL(0, buffer.getLength());
- }
- CORE_TEST_EQUAL(0, Tester::sum);
- }
- CORE_TEST_EQUAL(0, Tester::sum);
- }
- static void testOverall() {
- resetTester();
- Core::RingBuffer<Tester, 3> buffer;
- CORE_TEST_ERROR(buffer.add());
- CORE_TEST_ERROR(buffer.add());
- CORE_TEST_ERROR(buffer.add());
- CORE_TEST_STRING("[1, 2, 3]", buffer);
- CORE_TEST_EQUAL(3, buffer.getLength());
- CORE_TEST_EQUAL(6, Tester::sum);
- CORE_TEST_ERROR(buffer.remove());
- CORE_TEST_STRING("[2, 3]", buffer);
- CORE_TEST_EQUAL(2, buffer.getLength());
- CORE_TEST_EQUAL(5, Tester::sum);
- CORE_TEST_ERROR(buffer.add());
- CORE_TEST_STRING("[2, 3, 4]", buffer);
- CORE_TEST_EQUAL(3, buffer.getLength());
- CORE_TEST_EQUAL(9, Tester::sum);
- CORE_TEST_ERROR(buffer.remove());
- CORE_TEST_STRING("[3, 4]", buffer);
- CORE_TEST_EQUAL(2, buffer.getLength());
- CORE_TEST_EQUAL(7, Tester::sum);
- CORE_TEST_ERROR(buffer.add());
- CORE_TEST_STRING("[3, 4, 5]", buffer);
- CORE_TEST_EQUAL(3, buffer.getLength());
- CORE_TEST_EQUAL(12, Tester::sum);
- CORE_TEST_ERROR(buffer.remove());
- CORE_TEST_STRING("[4, 5]", buffer);
- CORE_TEST_EQUAL(2, buffer.getLength());
- CORE_TEST_EQUAL(9, Tester::sum);
- buffer.clear();
- CORE_TEST_STRING("[]", buffer);
- CORE_TEST_EQUAL(0, buffer.getLength());
- CORE_TEST_EQUAL(0, Tester::sum);
- }
- void Core::RingBufferTests::test() {
- testReadAndWrite();
- testOverflow();
- testRefill();
- testClear();
- testConstructDestruct();
- testCopyDestruct();
- testCopyAssignmentDestruct();
- testMoveDestruct();
- testMoveAssignmentDestruct();
- testOverall();
- }
|