File: test_simple_circular_buffer.cc

package info (click to toggle)
jellyfish1 1.1.11-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,860 kB
  • sloc: cpp: 20,786; sh: 11,394; makefile: 186
file content (71 lines) | stat: -rw-r--r-- 2,240 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <gtest/gtest.h>
#include <jellyfish/simple_circular_buffer.hpp>

namespace {
  // template<typename T>
  // class CircularBufferTest : public ::testing::Test { };

  // typedef ::testing::Types<circular_buffer::fixed<int, 16>,
  //                          circular_buffer::dyn<int> > CircularBufferTypes;
  // TYPED_TEST_CASE(CircularBufferTest, CircularBufferTypes);
  static const int capa = 16;

  class test_simple_circular_buffer : 
    public jellyfish::simple_circular_buffer::pre_alloc<int, capa> {
  public:
    typedef jellyfish::simple_circular_buffer::pre_alloc<int, capa> super;
    explicit test_simple_circular_buffer(int* data) : super(data) { }

    // int next_index(int i) const { return super::next_index(i); }
    // int prev_index(int i) const { return super::prev_index(i); }
    // int front_ptr() const { return front_; }
    // int back_ptr() const { return back_; }
  };

  TEST(SimpleCircularBufferTest, FillEmpty) {
    int ary[capa];
    test_simple_circular_buffer buffer(ary);
    EXPECT_EQ(capa, buffer.capacity());

    EXPECT_TRUE(buffer.empty());
    for(int i = 0; i < buffer.capacity(); ++i) {
      SCOPED_TRACE(::testing::Message() << "i=" << i);
      EXPECT_FALSE(buffer.full());
      EXPECT_EQ(i, buffer.size());
      buffer.push_back(i);
      EXPECT_EQ(i, buffer.back());
      EXPECT_FALSE(buffer.empty());
    }
    EXPECT_TRUE(buffer.full());

    for(int i = 0; i < buffer.capacity(); ++i) {
      SCOPED_TRACE(::testing::Message() << "i=" << i);
      EXPECT_FALSE(buffer.empty());
      EXPECT_EQ(i, buffer.front());
      EXPECT_EQ(buffer.capacity() - i, buffer.size());
      buffer.pop_front();
      EXPECT_FALSE(buffer.full());
    }
    EXPECT_TRUE(buffer.empty());
    EXPECT_EQ(0, buffer.size());
  }

  TEST(SimpleCircularBufferTest, Usefull) {
    int ary[capa];
    test_simple_circular_buffer buffer(ary);
    EXPECT_EQ(capa, buffer.capacity());

    int i = 0;
    for( ; i < buffer.capacity(); ++i) {
      EXPECT_TRUE(buffer.push_back());
      buffer.back() = i;
    }

    for( ; i < 10 * buffer.capacity(); ++i) {
      EXPECT_EQ(i - capa, buffer.front());
      buffer.pop_front();
      EXPECT_TRUE(buffer.push_back());
      buffer.back() = i;
    }
  }
}