testRingBuffer.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include <cstdio>
  2. #include "gtest/gtest.h"
  3. #include "rb/RingBuffer.h"
  4. using TestRingBuffer = cc::RingBufferT<int, 5>;
  5. // The fixture for testing class Project1. From google test primer.
  6. class RBTest : public ::testing::Test
  7. {
  8. protected:
  9. TestRingBuffer rb;
  10. };
  11. TEST_F(RBTest, LockFree)
  12. {
  13. EXPECT_TRUE(rb.isLockFree());
  14. }
  15. TEST_F(RBTest, RingBufferEmpty)
  16. {
  17. int x;
  18. TestRingBuffer::VecT v;
  19. ASSERT_TRUE(rb.wasEmpty());
  20. ASSERT_FALSE(rb.pop(x));
  21. ASSERT_FALSE(rb.consume(v));
  22. }
  23. TEST_F(RBTest, AddOneItem)
  24. {
  25. ASSERT_TRUE(rb.push(1));
  26. ASSERT_FALSE(rb.wasEmpty());
  27. }
  28. TEST_F(RBTest, ReadOneItem)
  29. {
  30. int x = 0;
  31. ASSERT_TRUE(rb.push(1));
  32. ASSERT_TRUE(rb.pop(x));
  33. ASSERT_EQ(x, 1);
  34. ASSERT_TRUE(rb.wasEmpty());
  35. }
  36. TEST_F(RBTest, ConsumeOneItem)
  37. {
  38. TestRingBuffer::VecT v;
  39. ASSERT_EQ(v.size(), 0);
  40. ASSERT_TRUE(rb.push(1));
  41. ASSERT_TRUE(rb.consume(v));
  42. ASSERT_EQ(v.size(), 1);
  43. ASSERT_EQ(v[0], 1);
  44. ASSERT_TRUE(rb.wasEmpty());
  45. }
  46. TEST_F(RBTest, ConsumeSomeItems)
  47. {
  48. TestRingBuffer::VecT v;
  49. ASSERT_EQ(v.size(), 0);
  50. ASSERT_TRUE(rb.push(1));
  51. ASSERT_TRUE(rb.push(2));
  52. ASSERT_TRUE(rb.push(3));
  53. ASSERT_TRUE(rb.push(4));
  54. ASSERT_TRUE(rb.consume(v));
  55. ASSERT_EQ(v.size(), 4);
  56. ASSERT_EQ(v[0], 1);
  57. ASSERT_EQ(v[1], 2);
  58. ASSERT_EQ(v[2], 3);
  59. ASSERT_EQ(v[3], 4);
  60. ASSERT_TRUE(rb.wasEmpty());
  61. }
  62. TEST_F(RBTest, FillBuffer)
  63. {
  64. TestRingBuffer::VecT v;
  65. ASSERT_EQ(v.size(), 0);
  66. ASSERT_TRUE(rb.push(1));
  67. ASSERT_TRUE(rb.push(2));
  68. ASSERT_TRUE(rb.push(3));
  69. ASSERT_TRUE(rb.push(4));
  70. ASSERT_TRUE(rb.push(5));
  71. ASSERT_TRUE(rb.wasFull());
  72. rb.consume(v);
  73. ASSERT_EQ(v.size(), 5);
  74. ASSERT_EQ(v[0], 1);
  75. ASSERT_EQ(v[1], 2);
  76. ASSERT_EQ(v[2], 3);
  77. ASSERT_EQ(v[3], 4);
  78. ASSERT_EQ(v[4], 5);
  79. ASSERT_TRUE(rb.wasEmpty());
  80. }
  81. TEST_F(RBTest, OverflowBuffer)
  82. {
  83. TestRingBuffer::VecT v;
  84. size_t overflow_count;
  85. ASSERT_EQ(v.size(), 0);
  86. ASSERT_TRUE(rb.push(1));
  87. ASSERT_TRUE(rb.push(2));
  88. ASSERT_TRUE(rb.push(3));
  89. ASSERT_TRUE(rb.push(4));
  90. ASSERT_TRUE(rb.push(5));
  91. ASSERT_FALSE(rb.push(6));
  92. ASSERT_TRUE(rb.wasFull());
  93. ASSERT_EQ(rb.getOverflowCount(), 1);
  94. ASSERT_TRUE(rb.consume(v));
  95. ASSERT_EQ(rb.getOverflowCount(), 0);
  96. ASSERT_EQ(v.size(), 5);
  97. ASSERT_EQ(v[0], 1);
  98. ASSERT_EQ(v[1], 2);
  99. ASSERT_EQ(v[2], 3);
  100. ASSERT_EQ(v[3], 4);
  101. ASSERT_EQ(v[4], 5);
  102. ASSERT_TRUE(rb.wasEmpty());
  103. ASSERT_TRUE(rb.push(1));
  104. ASSERT_TRUE(rb.push(2));
  105. ASSERT_TRUE(rb.push(3));
  106. ASSERT_TRUE(rb.push(4));
  107. ASSERT_TRUE(rb.push(5));
  108. ASSERT_FALSE(rb.push(6));
  109. ASSERT_EQ(rb.getOverflowCount(), 1);
  110. ASSERT_TRUE(rb.consume(v, overflow_count));
  111. ASSERT_EQ(rb.getOverflowCount(), 0);
  112. ASSERT_EQ(overflow_count, 1);
  113. }