Sat, 30 Apr 2022 08:40:40 +0200
fix doc error for cxBufferWrite
530 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
26 | * POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | ||
29 | #include "cx/buffer.h" | |
30 | ||
31 | #include <gtest/gtest.h> | |
32 | #include "util_allocator.h" | |
33 | ||
535
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
34 | TEST(BufferInit, WrapSpace) { |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
35 | CxTestingAllocator alloc; |
530 | 36 | CxBuffer buf; |
535
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
37 | void *space = cxMalloc(&alloc, 16); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
38 | cxBufferInit(&buf, space, 16, &alloc, CX_BUFFER_DEFAULT); |
530 | 39 | EXPECT_EQ(buf.space, space); |
40 | EXPECT_EQ(buf.flags & CX_BUFFER_AUTO_EXTEND, 0); | |
41 | EXPECT_EQ(buf.flags & CX_BUFFER_FREE_CONTENTS, 0); | |
42 | EXPECT_EQ(buf.pos, 0); | |
43 | EXPECT_EQ(buf.size, 0); | |
44 | EXPECT_EQ(buf.capacity, 16); | |
535
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
45 | EXPECT_EQ(buf.allocator, &alloc); |
530 | 46 | cxBufferDestroy(&buf); |
535
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
47 | EXPECT_FALSE(alloc.verify()); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
48 | cxFree(&alloc, space); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
49 | EXPECT_TRUE(alloc.verify()); |
530 | 50 | } |
51 | ||
535
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
52 | TEST(BufferInit, WrapSpaceAutoFree) { |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
53 | CxTestingAllocator alloc; |
530 | 54 | CxBuffer buf; |
535
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
55 | void *space = cxMalloc(&alloc, 16); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
56 | cxBufferInit(&buf, space, 16, &alloc, CX_BUFFER_FREE_CONTENTS); |
530 | 57 | EXPECT_EQ(buf.space, space); |
58 | EXPECT_EQ(buf.flags & CX_BUFFER_AUTO_EXTEND, 0); | |
59 | EXPECT_EQ(buf.flags & CX_BUFFER_FREE_CONTENTS, CX_BUFFER_FREE_CONTENTS); | |
60 | EXPECT_EQ(buf.pos, 0); | |
61 | EXPECT_EQ(buf.size, 0); | |
62 | EXPECT_EQ(buf.capacity, 16); | |
535
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
63 | EXPECT_EQ(buf.allocator, &alloc); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
64 | EXPECT_FALSE(alloc.verify()); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
65 | cxBufferDestroy(&buf); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
66 | EXPECT_TRUE(alloc.verify()); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
67 | } |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
68 | |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
69 | TEST(BufferInit, FreshSpace) { |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
70 | CxTestingAllocator alloc; |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
71 | CxBuffer buf; |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
72 | cxBufferInit(&buf, nullptr, 8, &alloc, CX_BUFFER_DEFAULT); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
73 | EXPECT_NE(buf.space, nullptr); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
74 | EXPECT_EQ(buf.flags & CX_BUFFER_AUTO_EXTEND, 0); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
75 | EXPECT_EQ(buf.flags & CX_BUFFER_FREE_CONTENTS, CX_BUFFER_FREE_CONTENTS); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
76 | EXPECT_EQ(buf.pos, 0); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
77 | EXPECT_EQ(buf.size, 0); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
78 | EXPECT_EQ(buf.capacity, 8); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
79 | EXPECT_EQ(buf.allocator, &alloc); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
80 | EXPECT_FALSE(alloc.verify()); // space is still allocated |
530 | 81 | cxBufferDestroy(&buf); |
535
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
82 | EXPECT_TRUE(alloc.verify()); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
83 | } |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
84 | |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
85 | class BufferShiftFixture : public ::testing::Test { |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
86 | protected: |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
87 | void SetUp() override { |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
88 | ASSERT_TRUE(alloc.verify()); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
89 | cxBufferInit(&buf, nullptr, 16, &alloc, CX_BUFFER_DEFAULT); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
90 | memcpy(buf.space, "test____________", 16); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
91 | buf.capacity = 8; // purposely pretend that the buffer has less capacity s.t. we can test beyond the range |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
92 | buf.pos = 4; |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
93 | buf.size = 4; |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
94 | } |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
95 | |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
96 | void TearDown() override { |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
97 | cxBufferDestroy(&buf); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
98 | EXPECT_TRUE(alloc.verify()); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
99 | } |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
100 | |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
101 | CxTestingAllocator alloc; |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
102 | CxBuffer buf{}; |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
103 | }; |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
104 | |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
105 | class BufferShiftLeft : public BufferShiftFixture { |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
106 | }; |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
107 | |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
108 | TEST_F(BufferShiftLeft, Zero) { |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
109 | ASSERT_EQ(buf.pos, 4); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
110 | ASSERT_EQ(buf.size, 4); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
111 | int ret = cxBufferShiftLeft(&buf, 0); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
112 | EXPECT_EQ(ret, 0); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
113 | EXPECT_EQ(buf.pos, 4); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
114 | EXPECT_EQ(buf.size, 4); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
115 | EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
116 | } |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
117 | |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
118 | TEST_F(BufferShiftLeft, Standard) { |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
119 | ASSERT_EQ(buf.pos, 4); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
120 | ASSERT_EQ(buf.size, 4); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
121 | int ret = cxBufferShiftLeft(&buf, 2); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
122 | EXPECT_EQ(ret, 0); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
123 | EXPECT_EQ(buf.pos, 2); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
124 | EXPECT_EQ(buf.size, 2); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
125 | EXPECT_TRUE(memcmp(buf.space, "stst________", 8) == 0); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
126 | } |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
127 | |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
128 | TEST_F(BufferShiftLeft, Overshift) { |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
129 | ASSERT_LT(buf.pos, 6); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
130 | ASSERT_LT(buf.size, 6); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
131 | int ret = cxBufferShiftLeft(&buf, 6); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
132 | EXPECT_EQ(ret, 0); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
133 | EXPECT_EQ(buf.pos, 0); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
134 | EXPECT_EQ(buf.size, 0); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
135 | EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
136 | } |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
137 | |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
138 | TEST_F(BufferShiftLeft, OvershiftPosOnly) { |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
139 | buf.pos = 2; |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
140 | ASSERT_EQ(buf.size, 4); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
141 | int ret = cxBufferShiftLeft(&buf, 3); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
142 | EXPECT_EQ(ret, 0); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
143 | EXPECT_EQ(buf.pos, 0); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
144 | EXPECT_EQ(buf.size, 1); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
145 | EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
146 | } |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
147 | |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
148 | TEST_F(BufferShiftLeft, OffsetInterface) { |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
149 | buf.pos = 3; |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
150 | ASSERT_EQ(buf.size, 4); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
151 | int ret = cxBufferShift(&buf, -2); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
152 | EXPECT_EQ(ret, 0); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
153 | EXPECT_EQ(buf.pos, 1); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
154 | EXPECT_EQ(buf.size, 2); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
155 | EXPECT_TRUE(memcmp(buf.space, "stst________", 8) == 0); |
530 | 156 | } |
535
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
157 | |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
158 | class BufferShiftRight : public BufferShiftFixture { |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
159 | }; |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
160 | |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
161 | TEST_F(BufferShiftRight, Zero) { |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
162 | ASSERT_EQ(buf.pos, 4); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
163 | ASSERT_EQ(buf.size, 4); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
164 | int ret = cxBufferShiftRight(&buf, 0); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
165 | EXPECT_EQ(ret, 0); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
166 | EXPECT_EQ(buf.pos, 4); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
167 | EXPECT_EQ(buf.size, 4); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
168 | EXPECT_TRUE(memcmp(buf.space, "test________", 8) == 0); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
169 | } |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
170 | |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
171 | TEST_F(BufferShiftRight, Standard) { |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
172 | ASSERT_EQ(buf.pos, 4); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
173 | ASSERT_EQ(buf.size, 4); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
174 | int ret = cxBufferShiftRight(&buf, 3); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
175 | EXPECT_EQ(ret, 0); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
176 | EXPECT_EQ(buf.pos, 7); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
177 | EXPECT_EQ(buf.size, 7); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
178 | EXPECT_TRUE(memcmp(buf.space, "testest_____", 8) == 0); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
179 | } |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
180 | |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
181 | TEST_F(BufferShiftRight, OvershiftDiscard) { |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
182 | ASSERT_EQ(buf.pos, 4); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
183 | ASSERT_EQ(buf.size, 4); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
184 | ASSERT_EQ(buf.capacity, 8); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
185 | int ret = cxBufferShiftRight(&buf, 6); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
186 | EXPECT_EQ(ret, 0); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
187 | EXPECT_EQ(buf.pos, 8); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
188 | EXPECT_EQ(buf.size, 8); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
189 | EXPECT_EQ(buf.capacity, 8); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
190 | EXPECT_TRUE(memcmp(buf.space, "test__te____", 8) == 0); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
191 | } |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
192 | |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
193 | TEST_F(BufferShiftRight, OvershiftExtend) { |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
194 | ASSERT_EQ(buf.pos, 4); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
195 | ASSERT_EQ(buf.size, 4); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
196 | ASSERT_EQ(buf.capacity, 8); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
197 | buf.flags |= CX_BUFFER_AUTO_EXTEND; |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
198 | int ret = cxBufferShiftRight(&buf, 6); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
199 | EXPECT_EQ(ret, 0); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
200 | EXPECT_EQ(buf.pos, 10); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
201 | EXPECT_EQ(buf.size, 10); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
202 | EXPECT_GE(buf.capacity, 10); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
203 | EXPECT_TRUE(memcmp(buf.space, "test__test__", 8) == 0); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
204 | } |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
205 | |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
206 | TEST_F(BufferShiftRight, OffsetInterface) { |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
207 | buf.pos = 3; |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
208 | ASSERT_EQ(buf.size, 4); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
209 | int ret = cxBufferShift(&buf, 2); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
210 | EXPECT_EQ(ret, 0); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
211 | EXPECT_EQ(buf.pos, 5); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
212 | EXPECT_EQ(buf.size, 6); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
213 | EXPECT_TRUE(memcmp(buf.space, "tetest______", 8) == 0); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
214 | } |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
215 | |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
216 | TEST(BufferMinimumCapacity, Sufficient) { |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
217 | CxTestingAllocator alloc; |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
218 | auto space = cxMalloc(&alloc, 8); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
219 | CxBuffer buf; |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
220 | cxBufferInit(&buf, space, 8, &alloc, CX_BUFFER_FREE_CONTENTS); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
221 | memcpy(space, "Testing", 8); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
222 | buf.size = 8; |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
223 | cxBufferMinimumCapacity(&buf, 6); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
224 | EXPECT_EQ(buf.capacity, 8); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
225 | EXPECT_EQ(buf.size, 8); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
226 | EXPECT_TRUE(memcmp(buf.space, "Testing", 8) == 0); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
227 | cxBufferDestroy(&buf); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
228 | EXPECT_TRUE(alloc.verify()); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
229 | } |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
230 | |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
231 | TEST(BufferMinimumCapacity, Extend) { |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
232 | CxTestingAllocator alloc; |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
233 | auto space = cxMalloc(&alloc, 8); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
234 | CxBuffer buf; |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
235 | cxBufferInit(&buf, space, 8, &alloc, CX_BUFFER_FREE_CONTENTS); // NO auto extend! |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
236 | memcpy(space, "Testing", 8); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
237 | buf.size = 8; |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
238 | cxBufferMinimumCapacity(&buf, 16); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
239 | EXPECT_EQ(buf.capacity, 16); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
240 | EXPECT_EQ(buf.size, 8); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
241 | EXPECT_TRUE(memcmp(buf.space, "Testing", 8) == 0); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
242 | cxBufferDestroy(&buf); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
243 | EXPECT_TRUE(alloc.verify()); |
2ff6e9184468
#170 add several more buffer tests
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
244 | } |
536
cb9b9739055e
#170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents:
535
diff
changeset
|
245 | |
cb9b9739055e
#170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents:
535
diff
changeset
|
246 | TEST(BufferClear, Test) { |
cb9b9739055e
#170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents:
535
diff
changeset
|
247 | char space[16]; |
cb9b9739055e
#170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents:
535
diff
changeset
|
248 | strcpy(space, "clear test"); |
cb9b9739055e
#170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents:
535
diff
changeset
|
249 | CxBuffer buf; |
cb9b9739055e
#170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents:
535
diff
changeset
|
250 | cxBufferInit(&buf, space, 16, cxDefaultAllocator, CX_BUFFER_DEFAULT); |
cb9b9739055e
#170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents:
535
diff
changeset
|
251 | ASSERT_EQ(buf.size, 0); |
cb9b9739055e
#170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents:
535
diff
changeset
|
252 | // only clear the used part of the buffer |
cb9b9739055e
#170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents:
535
diff
changeset
|
253 | cxBufferClear(&buf); |
cb9b9739055e
#170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents:
535
diff
changeset
|
254 | EXPECT_EQ(memcmp(space, "clear test", 10), 0); |
cb9b9739055e
#170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents:
535
diff
changeset
|
255 | buf.size = 5; |
cb9b9739055e
#170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents:
535
diff
changeset
|
256 | buf.pos = 3; |
cb9b9739055e
#170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents:
535
diff
changeset
|
257 | cxBufferClear(&buf); |
cb9b9739055e
#170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents:
535
diff
changeset
|
258 | EXPECT_EQ(memcmp(space, "\0\0\0\0\0 test", 10), 0); |
cb9b9739055e
#170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents:
535
diff
changeset
|
259 | EXPECT_EQ(buf.size, 0); |
cb9b9739055e
#170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents:
535
diff
changeset
|
260 | EXPECT_EQ(buf.pos, 0); |
cb9b9739055e
#170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents:
535
diff
changeset
|
261 | } |