123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- #include <cstdio>
- #include "gtest/gtest.h"
- #include "rb/RingBuffer.h"
- using TestRingBuffer = cc::RingBufferT<int, 5>;
- // The fixture for testing class Project1. From google test primer.
- class RBTest : public ::testing::Test
- {
- protected:
- TestRingBuffer rb;
- };
- TEST_F(RBTest, LockFree)
- {
- EXPECT_TRUE(rb.isLockFree());
- }
- TEST_F(RBTest, RingBufferEmpty)
- {
- int x;
- TestRingBuffer::VecT v;
-
- ASSERT_TRUE(rb.wasEmpty());
- ASSERT_FALSE(rb.pop(x));
- ASSERT_FALSE(rb.consume(v));
- }
- TEST_F(RBTest, AddOneItem)
- {
- ASSERT_TRUE(rb.push(1));
- ASSERT_FALSE(rb.wasEmpty());
- }
- TEST_F(RBTest, ReadOneItem)
- {
- int x = 0;
-
- ASSERT_TRUE(rb.push(1));
- ASSERT_TRUE(rb.pop(x));
-
- ASSERT_EQ(x, 1);
- ASSERT_TRUE(rb.wasEmpty());
- }
- TEST_F(RBTest, ConsumeOneItem)
- {
- TestRingBuffer::VecT v;
- ASSERT_EQ(v.size(), 0);
- ASSERT_TRUE(rb.push(1));
- ASSERT_TRUE(rb.consume(v));
- ASSERT_EQ(v.size(), 1);
- ASSERT_EQ(v[0], 1);
-
- ASSERT_TRUE(rb.wasEmpty());
- }
- TEST_F(RBTest, ConsumeSomeItems)
- {
- TestRingBuffer::VecT v;
- ASSERT_EQ(v.size(), 0);
- ASSERT_TRUE(rb.push(1));
- ASSERT_TRUE(rb.push(2));
- ASSERT_TRUE(rb.push(3));
- ASSERT_TRUE(rb.push(4));
- ASSERT_TRUE(rb.consume(v));
- ASSERT_EQ(v.size(), 4);
- ASSERT_EQ(v[0], 1);
- ASSERT_EQ(v[1], 2);
- ASSERT_EQ(v[2], 3);
- ASSERT_EQ(v[3], 4);
-
- ASSERT_TRUE(rb.wasEmpty());
- }
- TEST_F(RBTest, FillBuffer)
- {
- TestRingBuffer::VecT v;
- ASSERT_EQ(v.size(), 0);
- ASSERT_TRUE(rb.push(1));
- ASSERT_TRUE(rb.push(2));
- ASSERT_TRUE(rb.push(3));
- ASSERT_TRUE(rb.push(4));
- ASSERT_TRUE(rb.push(5));
- ASSERT_TRUE(rb.wasFull());
- rb.consume(v);
- ASSERT_EQ(v.size(), 5);
- ASSERT_EQ(v[0], 1);
- ASSERT_EQ(v[1], 2);
- ASSERT_EQ(v[2], 3);
- ASSERT_EQ(v[3], 4);
- ASSERT_EQ(v[4], 5);
-
- ASSERT_TRUE(rb.wasEmpty());
- }
- TEST_F(RBTest, OverflowBuffer)
- {
- TestRingBuffer::VecT v;
- size_t overflow_count;
- ASSERT_EQ(v.size(), 0);
- ASSERT_TRUE(rb.push(1));
- ASSERT_TRUE(rb.push(2));
- ASSERT_TRUE(rb.push(3));
- ASSERT_TRUE(rb.push(4));
- ASSERT_TRUE(rb.push(5));
- ASSERT_FALSE(rb.push(6));
- ASSERT_TRUE(rb.wasFull());
- ASSERT_EQ(rb.getOverflowCount(), 1);
- ASSERT_TRUE(rb.consume(v));
- ASSERT_EQ(rb.getOverflowCount(), 0);
- ASSERT_EQ(v.size(), 5);
- ASSERT_EQ(v[0], 1);
- ASSERT_EQ(v[1], 2);
- ASSERT_EQ(v[2], 3);
- ASSERT_EQ(v[3], 4);
- ASSERT_EQ(v[4], 5);
-
- ASSERT_TRUE(rb.wasEmpty());
- ASSERT_TRUE(rb.push(1));
- ASSERT_TRUE(rb.push(2));
- ASSERT_TRUE(rb.push(3));
- ASSERT_TRUE(rb.push(4));
- ASSERT_TRUE(rb.push(5));
- ASSERT_FALSE(rb.push(6));
- ASSERT_EQ(rb.getOverflowCount(), 1);
- ASSERT_TRUE(rb.consume(v, overflow_count));
- ASSERT_EQ(rb.getOverflowCount(), 0);
- ASSERT_EQ(overflow_count, 1);
- }
|