1 #include <gtest/gtest.h>
2 
3 extern "C" {
4 #include "ringbuffer.h"
5 #include "osi.h"
6 }
7 
TEST(RingbufferTest,test_new_simple)8 TEST(RingbufferTest, test_new_simple) {
9   ringbuffer_t *rb = ringbuffer_init(4096);
10   ASSERT_TRUE(rb != NULL);
11   EXPECT_EQ(4096, ringbuffer_available(rb));
12   EXPECT_EQ(0, ringbuffer_size(rb));
13   ringbuffer_free(rb);
14 }
15 
TEST(RingbufferTest,test_insert_basic)16 TEST(RingbufferTest, test_insert_basic) {
17   ringbuffer_t *rb = ringbuffer_init(16);
18 
19   uint8_t buffer[10] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A};
20   ringbuffer_insert(rb, buffer, 10);
21   EXPECT_EQ(10, ringbuffer_size(rb));
22   EXPECT_EQ(6, ringbuffer_available(rb));
23 
24   uint8_t peek[10] = {0};
25   size_t peeked = ringbuffer_peek(rb, peek, 10);
26   EXPECT_EQ(10, ringbuffer_size(rb)); // Ensure size doesn't change
27   EXPECT_EQ(6, ringbuffer_available(rb));
28   EXPECT_EQ(10, peeked);
29   ASSERT_TRUE(0 == memcmp(buffer, peek, peeked));
30 
31   ringbuffer_free(rb);
32 }
33 
TEST(RingbufferTest,test_insert_full)34 TEST(RingbufferTest, test_insert_full) {
35   ringbuffer_t *rb = ringbuffer_init(5);
36 
37   uint8_t aa[] = {0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA};
38   uint8_t bb[] = {0xBB, 0xBB, 0xBB, 0xBB, 0xBB};
39   uint8_t peek[5] = {0};
40 
41   size_t added = ringbuffer_insert(rb, aa, 7);
42   EXPECT_EQ(5, added);
43   EXPECT_EQ(0, ringbuffer_available(rb));
44   EXPECT_EQ(5, ringbuffer_size(rb));
45 
46   added = ringbuffer_insert(rb, bb, 5);
47   EXPECT_EQ(0, added);
48   EXPECT_EQ(0, ringbuffer_available(rb));
49   EXPECT_EQ(5, ringbuffer_size(rb));
50 
51   size_t peeked = ringbuffer_peek(rb, peek, 5);
52   EXPECT_EQ(5, peeked);
53   EXPECT_EQ(0, ringbuffer_available(rb));
54   EXPECT_EQ(5, ringbuffer_size(rb));
55 
56   ASSERT_TRUE(0 == memcmp(aa, peek, peeked));
57 
58   ringbuffer_free(rb);
59 }
60 
TEST(RingbufferTest,test_multi_insert_delete)61 TEST(RingbufferTest, test_multi_insert_delete) {
62   ringbuffer_t *rb = ringbuffer_init(16);
63   EXPECT_EQ(16, ringbuffer_available(rb));
64   EXPECT_EQ(0, ringbuffer_size(rb));
65 
66   // Insert some bytes
67 
68   uint8_t aa[] = {0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA};
69   size_t added = ringbuffer_insert(rb, aa, sizeof(aa));
70   EXPECT_EQ(8, added);
71   EXPECT_EQ(8, ringbuffer_available(rb));
72   EXPECT_EQ(8, ringbuffer_size(rb));
73 
74   uint8_t bb[] = {0xBB, 0xBB, 0xBB, 0xBB, 0xBB};
75   ringbuffer_insert(rb, bb, sizeof(bb));
76   EXPECT_EQ(3, ringbuffer_available(rb));
77   EXPECT_EQ(13, ringbuffer_size(rb));
78 
79   uint8_t content[] = {0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB};
80   uint8_t peek[16] = {0};
81   size_t peeked = ringbuffer_peek(rb, peek, 16);
82   EXPECT_EQ(13, peeked);
83   ASSERT_TRUE(0 == memcmp(content, peek, peeked));
84 
85   // Delete some bytes
86 
87   ringbuffer_delete(rb, sizeof(aa));
88   EXPECT_EQ(11, ringbuffer_available(rb));
89   EXPECT_EQ(5, ringbuffer_size(rb));
90 
91   // Add some more to wrap buffer
92 
93   uint8_t cc[] = {0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC};
94   ringbuffer_insert(rb, cc, sizeof(cc));
95   EXPECT_EQ(2, ringbuffer_available(rb));
96   EXPECT_EQ(14, ringbuffer_size(rb));
97 
98   uint8_t content2[] = {0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xCC, 0xCC};
99   peeked = ringbuffer_peek(rb, peek, 7);
100   EXPECT_EQ(7, peeked);
101   ASSERT_TRUE(0 == memcmp(content2, peek, peeked));
102 
103   // Pop buffer
104 
105   memset(peek, 0, 16);
106   size_t popped = ringbuffer_pop(rb, peek, 7);
107   EXPECT_EQ(7, popped);
108   EXPECT_EQ(9, ringbuffer_available(rb));
109   ASSERT_TRUE(0 == memcmp(content2, peek, peeked));
110 
111   // Add more again to check head motion
112 
113   uint8_t dd[] = { 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD };
114   added = ringbuffer_insert(rb, dd, sizeof(dd));
115   EXPECT_EQ(8, added);
116   EXPECT_EQ(1, ringbuffer_available(rb));
117 
118   // Delete everything
119 
120   ringbuffer_delete(rb, 16);
121   EXPECT_EQ(16, ringbuffer_available(rb));
122   EXPECT_EQ(0, ringbuffer_size(rb));
123 
124   // Add small token
125 
126   uint8_t ae[] = { 0xAE, 0xAE, 0xAE };
127   added = ringbuffer_insert(rb, ae, sizeof(ae));
128   EXPECT_EQ(13, ringbuffer_available(rb));
129 
130   // Get everything
131 
132   popped = ringbuffer_pop(rb, peek, 16);
133   EXPECT_EQ(added, popped);
134   EXPECT_EQ(16, ringbuffer_available(rb));
135   EXPECT_EQ(0, ringbuffer_size(rb));
136   ASSERT_TRUE(0 == memcmp(ae, peek, popped));
137 
138   ringbuffer_free(rb);
139 }
140