1 #include <gtest/gtest.h>
2
3 #include "osi/include/osi.h"
4 #include "osi/include/ringbuffer.h"
5
TEST(RingbufferTest,test_new_simple)6 TEST(RingbufferTest, test_new_simple) {
7 ringbuffer_t* rb = ringbuffer_init(4096);
8 ASSERT_TRUE(rb != NULL);
9 EXPECT_EQ((size_t)4096, ringbuffer_available(rb));
10 EXPECT_EQ((size_t)0, ringbuffer_size(rb));
11 ringbuffer_free(rb);
12 }
13
TEST(RingbufferTest,test_insert_basic)14 TEST(RingbufferTest, test_insert_basic) {
15 ringbuffer_t* rb = ringbuffer_init(16);
16
17 uint8_t buffer[10] = {0x01, 0x02, 0x03, 0x04, 0x05,
18 0x06, 0x07, 0x08, 0x09, 0x0A};
19 ringbuffer_insert(rb, buffer, 10);
20 EXPECT_EQ((size_t)10, ringbuffer_size(rb));
21 EXPECT_EQ((size_t)6, ringbuffer_available(rb));
22
23 uint8_t peek[10] = {0};
24 size_t peeked = ringbuffer_peek(rb, 0, peek, 10);
25 EXPECT_EQ((size_t)10, ringbuffer_size(rb)); // Ensure size doesn't change
26 EXPECT_EQ((size_t)6, ringbuffer_available(rb));
27 EXPECT_EQ((size_t)10, peeked);
28 ASSERT_TRUE(0 == memcmp(buffer, peek, peeked));
29
30 ringbuffer_free(rb);
31 }
32
TEST(RingbufferTest,test_insert_full)33 TEST(RingbufferTest, test_insert_full) {
34 ringbuffer_t* rb = ringbuffer_init(5);
35
36 uint8_t aa[] = {0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA};
37 uint8_t bb[] = {0xBB, 0xBB, 0xBB, 0xBB, 0xBB};
38 uint8_t peek[5] = {0};
39
40 size_t added = ringbuffer_insert(rb, aa, 7);
41 EXPECT_EQ((size_t)5, added);
42 EXPECT_EQ((size_t)0, ringbuffer_available(rb));
43 EXPECT_EQ((size_t)5, ringbuffer_size(rb));
44
45 added = ringbuffer_insert(rb, bb, 5);
46 EXPECT_EQ((size_t)0, added);
47 EXPECT_EQ((size_t)0, ringbuffer_available(rb));
48 EXPECT_EQ((size_t)5, ringbuffer_size(rb));
49
50 size_t peeked = ringbuffer_peek(rb, 0, peek, 5);
51 EXPECT_EQ((size_t)5, peeked);
52 EXPECT_EQ((size_t)0, ringbuffer_available(rb));
53 EXPECT_EQ((size_t)5, ringbuffer_size(rb));
54
55 ASSERT_TRUE(0 == memcmp(aa, peek, peeked));
56
57 ringbuffer_free(rb);
58 }
59
TEST(RingbufferTest,test_multi_insert_delete)60 TEST(RingbufferTest, test_multi_insert_delete) {
61 ringbuffer_t* rb = ringbuffer_init(16);
62 EXPECT_EQ((size_t)16, ringbuffer_available(rb));
63 EXPECT_EQ((size_t)0, ringbuffer_size(rb));
64
65 // Insert some bytes
66
67 uint8_t aa[] = {0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA};
68 size_t added = ringbuffer_insert(rb, aa, sizeof(aa));
69 EXPECT_EQ((size_t)8, added);
70 EXPECT_EQ((size_t)8, ringbuffer_available(rb));
71 EXPECT_EQ((size_t)8, ringbuffer_size(rb));
72
73 uint8_t bb[] = {0xBB, 0xBB, 0xBB, 0xBB, 0xBB};
74 ringbuffer_insert(rb, bb, sizeof(bb));
75 EXPECT_EQ((size_t)3, ringbuffer_available(rb));
76 EXPECT_EQ((size_t)13, ringbuffer_size(rb));
77
78 uint8_t content[] = {0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
79 0xAA, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB};
80 uint8_t peek[16] = {0};
81 size_t peeked = ringbuffer_peek(rb, 0, peek, 16);
82 EXPECT_EQ((size_t)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((size_t)11, ringbuffer_available(rb));
89 EXPECT_EQ((size_t)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((size_t)2, ringbuffer_available(rb));
96 EXPECT_EQ((size_t)14, ringbuffer_size(rb));
97
98 uint8_t content2[] = {0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xCC, 0xCC};
99 peeked = ringbuffer_peek(rb, 0, peek, 7);
100 EXPECT_EQ((size_t)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((size_t)7, popped);
108 EXPECT_EQ((size_t)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((size_t)8, added);
116 EXPECT_EQ((size_t)1, ringbuffer_available(rb));
117
118 // Delete everything
119
120 ringbuffer_delete(rb, 16);
121 EXPECT_EQ((size_t)16, ringbuffer_available(rb));
122 EXPECT_EQ((size_t)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((size_t)13, ringbuffer_available(rb));
129
130 // Get everything
131
132 popped = ringbuffer_pop(rb, peek, 16);
133 EXPECT_EQ(added, popped);
134 EXPECT_EQ((size_t)16, ringbuffer_available(rb));
135 EXPECT_EQ((size_t)0, ringbuffer_size(rb));
136 ASSERT_TRUE(0 == memcmp(ae, peek, popped));
137
138 ringbuffer_free(rb);
139 }
140