Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
ByteBufferTests.cpp
Go to the documentation of this file.
2
4
5namespace Divide{
6
7template<typename T>
8bool compareVectors(const vector<T>& a, const vector<T>& b) {
9 if (a.size() == b.size()) {
10 for (size_t i = 0; i < a.size(); ++i) {
11 if (a[i] != b[i]) {
12 return false;
13 }
14 }
15 return true;
16 }
17
18 return false;
19}
20
21template<typename T, size_t N>
22bool compareArrays(const std::array<T, N>& a, const std::array<T, N>& b) noexcept {
23 for (size_t i = 0; i < N; ++i) {
24 if (a[i] != b[i]) {
25 return false;
26 }
27 }
28
29 return true;
30}
31
32TEST_CASE("ByteBuffer RW Bool", "[byte_buffer]")
33{
34 constexpr bool input = false;
35
36 ByteBuffer test;
37 test << input;
38
39 bool output = true;
40 test >> output;
41 CHECK_EQUAL(output, input);
42}
43
44TEST_CASE( "ByteBuffer RW POD", "[byte_buffer]" )
45{
46 constexpr U8 inputU8 = 2;
47 constexpr U16 inputU16 = 4;
48 constexpr U32 inputU32 = 6;
49 constexpr I8 inputI8 = -2;
50 constexpr I16 inputI16 = 40;
51 constexpr I32 inputI32 = -6;
52 constexpr F32 inputF32 = 3.45632f;
53 constexpr D64 inputD64 = 1.14159;
54
55 ByteBuffer test;
56 test << inputU8;
57 test << inputU16;
58 test << inputU32;
59 test << inputI8;
60 test << inputI16;
61 test << inputI32;
62 test << inputF32;
63 test << inputD64;
64
65 U8 outputU8 = 0;
66 U16 outputU16 = 0;
67 U32 outputU32 = 0;
68 I8 outputI8 = 0;
69 I16 outputI16 = 0;
70 I32 outputI32 = 0;
71 F32 outputF32 = 0.0f;
72 D64 outputD64 = 0.0;
73
74 test >> outputU8;
75 test >> outputU16;
76 test >> outputU32;
77 test >> outputI8;
78 test >> outputI16;
79 test >> outputI32;
80 test >> outputF32;
81 test >> outputD64;
82
83 CHECK_EQUAL(outputU8, inputU8);
84 CHECK_EQUAL(outputU16, inputU16);
85 CHECK_EQUAL(outputU32, inputU32);
86 CHECK_EQUAL(outputI8, inputI8);
87 CHECK_EQUAL(outputI16, inputI16);
88 CHECK_EQUAL(outputI32, inputI32);
89 CHECK_TRUE(COMPARE(outputF32, inputF32));
90 CHECK_TRUE(COMPARE(outputD64, inputD64));
91}
92
93TEST_CASE( "ByteBuffer Simple Marker", "[byte_buffer]" )
94{
95 constexpr U16 testMarker[3]{ 444u, 555u, 777u };
96
97 constexpr U8 input = 122u;
98
99 ByteBuffer test;
100 test << string{ "StringTest Whatever" };
101 test << U32{ 123456u };
102 test.addMarker(testMarker);
103 test << input;
104
105 test.readSkipToMarker(testMarker);
106
107 U8 output = 0u;
108 test >> output;
109 CHECK_EQUAL(input, output);
110}
111
112TEST_CASE( "ByteBuffer No Marker", "[byte_buffer]" )
113{
114 SECTION( "EVEN" )
115 {
116 constexpr U16 testMarker[3]{ 444u, 555u, 777u };
117
118 ByteBuffer test;
119 test << string{ "StringTest Whatever" };
120 test << U32{ 123456u }; //Multiple of our marker size
121
122 test.readSkipToMarker(testMarker);
123
124 // Should just skip to the end
125 CHECK_TRUE(test.bufferEmpty());
126 }
127
128 SECTION( "ODD" )
129 {
130 constexpr U16 testMarker[3]{ 444u, 555u, 777u };
131
132 ByteBuffer test;
133 test << string{ "StringTest Whatever" };
134 test << U32{ 123456u }; //Multiple of our marker size
135 test << U8{ 122u }; //Extra byte to check proper skipping
136
137 test.readSkipToMarker( testMarker );
138
139 // Should just skip to the end, but we use 16bit markers and we have 5 x 8bit values in the buffer
140 // So we will fall short by a single byte. Hopefully, we skip it automatically
141 CHECK_TRUE( test.bufferEmpty() );
142 }
143}
144
145TEST_CASE( "ByteBuffer Wrong Marker", "[byte_buffer]" )
146{
147 U16 testMarker[3]{ 444u, 555u, 777u };
148
149 ByteBuffer test;
150 test << string{ "StringTest Whatever" };
151 test << U32{ 123456u };
152 test.addMarker(testMarker);
153 test << U8{122u};
154
155 testMarker[2] -= 1;
156 test.readSkipToMarker(testMarker);
157
158 // Should just skip to the end
159 CHECK_TRUE(test.bufferEmpty());
160}
161
162
163TEST_CASE( "ByteBuffer RW String", "[byte_buffer]" )
164{
165 const string input = "StringTest Whatever";
166
167 ByteBuffer test;
168 test << input;
169
170 string output = "Output";
171
172 test >> output;
173
174 CHECK_EQUAL(input, output);
175}
176
177
178TEST_CASE( "ByteBuffer RW Vector<Int>", "[byte_buffer]" )
179{
180
181 SECTION("Int")
182 {
183 const vector<I32> input = { -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 };
184
185 ByteBuffer test;
186 test << input;
187
188 vector<I32> output;
189
190 test >> output;
191
192 CHECK_TRUE(compareVectors(input, output));
193 }
194 SECTION("String")
195 {
196 const vector<string> input = { "-5", "-4", "-3", "-2", "-1", "0", "1", "2", "3", "4", "5" };
197
198 ByteBuffer test;
199 test << input;
200
201 vector<string> output;
202
203 test >> output;
204
205 CHECK_TRUE( compareVectors( input, output ) );
206 }
207}
208
209TEST_CASE( "ByteBuffer RW Array", "[byte_buffer]" )
210{
211 SECTION("Int")
212 {
213 const std::array<I32, 11> input = { -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 };
214
215 ByteBuffer test;
216 test << input;
217
218 std::array<I32, 11> output{};
219
220 test >> output;
221
222 CHECK_TRUE(compareArrays(input, output));
223 }
224 SECTION("String")
225 {
226 const std::array<string, 11> input = { "-5", "-4", "-3", "-2", "-1", "0", "1", "2", "3", "4", "5" };
227
228 ByteBuffer test;
229 test << input;
230
231 std::array<string, 11> output;
232
233 test >> output;
234
235 CHECK_TRUE(compareArrays(input, output));
236 }
237}
238
239
240TEST_CASE( "ByteBuffer RW Mixed Data", "[byte_buffer]" )
241{
242 constexpr bool inputBool = false;
243 const vector<I32> inputVectorInt = { -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 };
244 const vector<string> inputVectorStr = { "-5", "-4", "-3", "-2", "-1", "0", "1", "2", "3", "4", "5" };
245 constexpr std::array<I32, 11> inputArrayInt = {{ -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 }};
246 const std::array<string, 11> inputArrayStr = {{ "-5", "-4", "-3", "-2", "-1", "0", "1", "2", "3", "4", "5" }};
247 constexpr U8 inputU8 = 2;
248 constexpr U16 inputU16 = 4;
249 constexpr U32 inputU32 = 6;
250 constexpr I8 inputI8 = -2;
251 constexpr I16 inputI16 = 40;
252 constexpr I32 inputI32 = -6;
253 constexpr F32 inputF32 = 3.45632f;
254 const string inputStr = "StringTest Whatever";
255 constexpr D64 inputD64 = 1.14159;
256
257 bool outputBool = true;
258 vector<I32> outputVectorInt;
259 vector<string> outputVectorStr;
260 std::array<I32, 11> outputArrayInt{};
261 std::array<string, 11> outputArrayStr;
262 U8 outputU8 = 0;
263 U16 outputU16 = 0;
264 U32 outputU32 = 0;
265 I8 outputI8 = 0;
266 I16 outputI16 = 0;
267 I32 outputI32 = 0;
268 F32 outputF32 = 0.0f;
269 string outputStr = "Output";
270 D64 outputD64 = 0.0;
271
272 ByteBuffer test;
273 test << inputBool;
274 test << inputVectorInt;
275 test << inputVectorStr;
276 test << inputArrayInt;
277 test << inputArrayStr;
278 test << inputU8;
279 test << inputU16;
280 test << inputU32;
281 test << inputI8;
282 test << inputI16;
283 test << inputI32;
284 test << inputF32;
285 test << inputStr;
286 test << inputD64;
287
288
289 test >> outputBool;
290 test >> outputVectorInt;
291 test >> outputVectorStr;
292 test >> outputArrayInt;
293 test >> outputArrayStr;
294 test >> outputU8;
295 test >> outputU16;
296 test >> outputU32;
297 test >> outputI8;
298 test >> outputI16;
299 test >> outputI32;
300 test >> outputF32;
301 test >> outputStr;
302 test >> outputD64;
303
304 CHECK_EQUAL(inputBool, outputBool);
305 CHECK_TRUE(compareVectors(inputVectorInt, outputVectorInt));
306 CHECK_TRUE(compareVectors(inputVectorStr, outputVectorStr));
307 CHECK_TRUE(compareArrays(inputArrayInt, outputArrayInt));
308 CHECK_TRUE(compareArrays(inputArrayStr, outputArrayStr));
309 CHECK_EQUAL(outputU8, inputU8);
310 CHECK_EQUAL(outputU16, inputU16);
311 CHECK_EQUAL(outputU32, inputU32);
312 CHECK_EQUAL(outputI8, inputI8);
313 CHECK_EQUAL(outputI16, inputI16);
314 CHECK_EQUAL(outputI32, inputI32);
315 CHECK_TRUE(COMPARE(outputF32, inputF32));
316 CHECK_EQUAL(inputStr, outputStr);
317 CHECK_TRUE(COMPARE(outputD64, inputD64));
318
319}
320
321}//namespace Divide
void readSkipToMarker(const T(&pattern)[N])
Skip (consume) everything in the buffer until we find the specified list of items....
Definition: ByteBuffer.inl:65
void addMarker(const T(&pattern)[N])
Add a specific list of items in order to mark a special point in the buffer. Can be used for skipping...
Definition: ByteBuffer.inl:56
bool bufferEmpty() const noexcept
Returns true if the read position and the write position are identical.
Definition: ByteBuffer.inl:353
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
bool compareArrays(const std::array< T, N > &a, const std::array< T, N > &b) noexcept
int32_t I32
uint8_t U8
int16_t I16
eastl::vector< Type > vector
Definition: Vector.h:42
uint16_t U16
double D64
bool compareVectors(const vector< T > &a, const vector< T > &b)
bool COMPARE(T X, U Y) noexcept
TEST_CASE("ByteBuffer RW Bool", "[byte_buffer]")
uint32_t U32
#define CHECK_EQUAL(LHS, RHS)
#define CHECK_TRUE(...)