test/test_buffer.cpp

changeset 535
2ff6e9184468
parent 530
e866516cac17
child 536
cb9b9739055e
equal deleted inserted replaced
534:0e893f84a7dd 535:2ff6e9184468
29 #include "cx/buffer.h" 29 #include "cx/buffer.h"
30 30
31 #include <gtest/gtest.h> 31 #include <gtest/gtest.h>
32 #include "util_allocator.h" 32 #include "util_allocator.h"
33 33
34 class Buffer : public ::testing::Test { 34 TEST(BufferInit, WrapSpace) {
35 protected: 35 CxTestingAllocator alloc;
36 void TearDown() override { 36 CxBuffer buf;
37 EXPECT_TRUE(testingAllocator.verify()); 37 void *space = cxMalloc(&alloc, 16);
38 } 38 cxBufferInit(&buf, space, 16, &alloc, CX_BUFFER_DEFAULT);
39
40 CxTestingAllocator testingAllocator;
41 };
42
43 TEST_F(Buffer, WrapSpace) {
44 CxBuffer buf;
45 void *space = cxMalloc(&testingAllocator, 16);
46 cxBufferInit(&buf, space, 16, &testingAllocator, CX_BUFFER_DEFAULT);
47 EXPECT_EQ(buf.space, space); 39 EXPECT_EQ(buf.space, space);
48 EXPECT_EQ(buf.flags & CX_BUFFER_AUTO_EXTEND, 0); 40 EXPECT_EQ(buf.flags & CX_BUFFER_AUTO_EXTEND, 0);
49 EXPECT_EQ(buf.flags & CX_BUFFER_FREE_CONTENTS, 0); 41 EXPECT_EQ(buf.flags & CX_BUFFER_FREE_CONTENTS, 0);
50 EXPECT_EQ(buf.pos, 0); 42 EXPECT_EQ(buf.pos, 0);
51 EXPECT_EQ(buf.size, 0); 43 EXPECT_EQ(buf.size, 0);
52 EXPECT_EQ(buf.capacity, 16); 44 EXPECT_EQ(buf.capacity, 16);
53 EXPECT_EQ(buf.allocator, &testingAllocator); 45 EXPECT_EQ(buf.allocator, &alloc);
54 cxBufferDestroy(&buf); 46 cxBufferDestroy(&buf);
55 EXPECT_FALSE(testingAllocator.verify()); 47 EXPECT_FALSE(alloc.verify());
56 cxFree(&testingAllocator, space); 48 cxFree(&alloc, space);
57 EXPECT_TRUE(testingAllocator.verify()); 49 EXPECT_TRUE(alloc.verify());
58 } 50 }
59 51
60 TEST_F(Buffer, WrapSpaceAutoFree) { 52 TEST(BufferInit, WrapSpaceAutoFree) {
61 CxBuffer buf; 53 CxTestingAllocator alloc;
62 void *space = cxMalloc(&testingAllocator, 16); 54 CxBuffer buf;
63 cxBufferInit(&buf, space, 16, &testingAllocator, CX_BUFFER_FREE_CONTENTS); 55 void *space = cxMalloc(&alloc, 16);
56 cxBufferInit(&buf, space, 16, &alloc, CX_BUFFER_FREE_CONTENTS);
64 EXPECT_EQ(buf.space, space); 57 EXPECT_EQ(buf.space, space);
65 EXPECT_EQ(buf.flags & CX_BUFFER_AUTO_EXTEND, 0); 58 EXPECT_EQ(buf.flags & CX_BUFFER_AUTO_EXTEND, 0);
66 EXPECT_EQ(buf.flags & CX_BUFFER_FREE_CONTENTS, CX_BUFFER_FREE_CONTENTS); 59 EXPECT_EQ(buf.flags & CX_BUFFER_FREE_CONTENTS, CX_BUFFER_FREE_CONTENTS);
67 EXPECT_EQ(buf.pos, 0); 60 EXPECT_EQ(buf.pos, 0);
68 EXPECT_EQ(buf.size, 0); 61 EXPECT_EQ(buf.size, 0);
69 EXPECT_EQ(buf.capacity, 16); 62 EXPECT_EQ(buf.capacity, 16);
70 EXPECT_EQ(buf.allocator, &testingAllocator); 63 EXPECT_EQ(buf.allocator, &alloc);
71 EXPECT_FALSE(testingAllocator.verify()); 64 EXPECT_FALSE(alloc.verify());
72 cxBufferDestroy(&buf); 65 cxBufferDestroy(&buf);
73 EXPECT_TRUE(testingAllocator.verify()); 66 EXPECT_TRUE(alloc.verify());
74 } 67 }
68
69 TEST(BufferInit, FreshSpace) {
70 CxTestingAllocator alloc;
71 CxBuffer buf;
72 cxBufferInit(&buf, nullptr, 8, &alloc, CX_BUFFER_DEFAULT);
73 EXPECT_NE(buf.space, nullptr);
74 EXPECT_EQ(buf.flags & CX_BUFFER_AUTO_EXTEND, 0);
75 EXPECT_EQ(buf.flags & CX_BUFFER_FREE_CONTENTS, CX_BUFFER_FREE_CONTENTS);
76 EXPECT_EQ(buf.pos, 0);
77 EXPECT_EQ(buf.size, 0);
78 EXPECT_EQ(buf.capacity, 8);
79 EXPECT_EQ(buf.allocator, &alloc);
80 EXPECT_FALSE(alloc.verify()); // space is still allocated
81 cxBufferDestroy(&buf);
82 EXPECT_TRUE(alloc.verify());
83 }
84
85 class BufferShiftFixture : public ::testing::Test {
86 protected:
87 void SetUp() override {
88 ASSERT_TRUE(alloc.verify());
89 cxBufferInit(&buf, nullptr, 16, &alloc, CX_BUFFER_DEFAULT);
90 memcpy(buf.space, "test____________", 16);
91 buf.capacity = 8; // purposely pretend that the buffer has less capacity s.t. we can test beyond the range
92 buf.pos = 4;
93 buf.size = 4;
94 }
95
96 void TearDown() override {
97 cxBufferDestroy(&buf);
98 EXPECT_TRUE(alloc.verify());
99 }
100
101 CxTestingAllocator alloc;
102 CxBuffer buf{};
103 };
104
105 class BufferShiftLeft : public BufferShiftFixture {
106 };
107
108 TEST_F(BufferShiftLeft, Zero) {
109 ASSERT_EQ(buf.pos, 4);
110 ASSERT_EQ(buf.size, 4);
111 int ret = cxBufferShiftLeft(&buf, 0);
112 EXPECT_EQ(ret, 0);
113 EXPECT_EQ(buf.pos, 4);
114 EXPECT_EQ(buf.size, 4);
115 EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0);
116 }
117
118 TEST_F(BufferShiftLeft, Standard) {
119 ASSERT_EQ(buf.pos, 4);
120 ASSERT_EQ(buf.size, 4);
121 int ret = cxBufferShiftLeft(&buf, 2);
122 EXPECT_EQ(ret, 0);
123 EXPECT_EQ(buf.pos, 2);
124 EXPECT_EQ(buf.size, 2);
125 EXPECT_TRUE(memcmp(buf.space, "stst________", 8) == 0);
126 }
127
128 TEST_F(BufferShiftLeft, Overshift) {
129 ASSERT_LT(buf.pos, 6);
130 ASSERT_LT(buf.size, 6);
131 int ret = cxBufferShiftLeft(&buf, 6);
132 EXPECT_EQ(ret, 0);
133 EXPECT_EQ(buf.pos, 0);
134 EXPECT_EQ(buf.size, 0);
135 EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0);
136 }
137
138 TEST_F(BufferShiftLeft, OvershiftPosOnly) {
139 buf.pos = 2;
140 ASSERT_EQ(buf.size, 4);
141 int ret = cxBufferShiftLeft(&buf, 3);
142 EXPECT_EQ(ret, 0);
143 EXPECT_EQ(buf.pos, 0);
144 EXPECT_EQ(buf.size, 1);
145 EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0);
146 }
147
148 TEST_F(BufferShiftLeft, OffsetInterface) {
149 buf.pos = 3;
150 ASSERT_EQ(buf.size, 4);
151 int ret = cxBufferShift(&buf, -2);
152 EXPECT_EQ(ret, 0);
153 EXPECT_EQ(buf.pos, 1);
154 EXPECT_EQ(buf.size, 2);
155 EXPECT_TRUE(memcmp(buf.space, "stst________", 8) == 0);
156 }
157
158 class BufferShiftRight : public BufferShiftFixture {
159 };
160
161 TEST_F(BufferShiftRight, Zero) {
162 ASSERT_EQ(buf.pos, 4);
163 ASSERT_EQ(buf.size, 4);
164 int ret = cxBufferShiftRight(&buf, 0);
165 EXPECT_EQ(ret, 0);
166 EXPECT_EQ(buf.pos, 4);
167 EXPECT_EQ(buf.size, 4);
168 EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0);
169 }
170
171 TEST_F(BufferShiftRight, Standard) {
172 ASSERT_EQ(buf.pos, 4);
173 ASSERT_EQ(buf.size, 4);
174 int ret = cxBufferShiftRight(&buf, 3);
175 EXPECT_EQ(ret, 0);
176 EXPECT_EQ(buf.pos, 7);
177 EXPECT_EQ(buf.size, 7);
178 EXPECT_TRUE(memcmp(buf.space, "testest_____", 8) == 0);
179 }
180
181 TEST_F(BufferShiftRight, OvershiftDiscard) {
182 ASSERT_EQ(buf.pos, 4);
183 ASSERT_EQ(buf.size, 4);
184 ASSERT_EQ(buf.capacity, 8);
185 int ret = cxBufferShiftRight(&buf, 6);
186 EXPECT_EQ(ret, 0);
187 EXPECT_EQ(buf.pos, 8);
188 EXPECT_EQ(buf.size, 8);
189 EXPECT_EQ(buf.capacity, 8);
190 EXPECT_TRUE(memcmp(buf.space, "test__te____", 8) == 0);
191 }
192
193 TEST_F(BufferShiftRight, OvershiftExtend) {
194 ASSERT_EQ(buf.pos, 4);
195 ASSERT_EQ(buf.size, 4);
196 ASSERT_EQ(buf.capacity, 8);
197 buf.flags |= CX_BUFFER_AUTO_EXTEND;
198 int ret = cxBufferShiftRight(&buf, 6);
199 EXPECT_EQ(ret, 0);
200 EXPECT_EQ(buf.pos, 10);
201 EXPECT_EQ(buf.size, 10);
202 EXPECT_GE(buf.capacity, 10);
203 EXPECT_TRUE(memcmp(buf.space, "test__test__", 8) == 0);
204 }
205
206 TEST_F(BufferShiftRight, OffsetInterface) {
207 buf.pos = 3;
208 ASSERT_EQ(buf.size, 4);
209 int ret = cxBufferShift(&buf, 2);
210 EXPECT_EQ(ret, 0);
211 EXPECT_EQ(buf.pos, 5);
212 EXPECT_EQ(buf.size, 6);
213 EXPECT_TRUE(memcmp(buf.space, "tetest______", 8) == 0);
214 }
215
216 TEST(BufferMinimumCapacity, Sufficient) {
217 CxTestingAllocator alloc;
218 auto space = cxMalloc(&alloc, 8);
219 CxBuffer buf;
220 cxBufferInit(&buf, space, 8, &alloc, CX_BUFFER_FREE_CONTENTS);
221 memcpy(space, "Testing", 8);
222 buf.size = 8;
223 cxBufferMinimumCapacity(&buf, 6);
224 EXPECT_EQ(buf.capacity, 8);
225 EXPECT_EQ(buf.size, 8);
226 EXPECT_TRUE(memcmp(buf.space, "Testing", 8) == 0);
227 cxBufferDestroy(&buf);
228 EXPECT_TRUE(alloc.verify());
229 }
230
231 TEST(BufferMinimumCapacity, Extend) {
232 CxTestingAllocator alloc;
233 auto space = cxMalloc(&alloc, 8);
234 CxBuffer buf;
235 cxBufferInit(&buf, space, 8, &alloc, CX_BUFFER_FREE_CONTENTS); // NO auto extend!
236 memcpy(space, "Testing", 8);
237 buf.size = 8;
238 cxBufferMinimumCapacity(&buf, 16);
239 EXPECT_EQ(buf.capacity, 16);
240 EXPECT_EQ(buf.size, 8);
241 EXPECT_TRUE(memcmp(buf.space, "Testing", 8) == 0);
242 cxBufferDestroy(&buf);
243 EXPECT_TRUE(alloc.verify());
244 }

mercurial