blob: 05d04a21b1efff3e09318d44f3f53c486fb82979 [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
2Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
3
4Redistribution and use in source and binary forms, with or without
5modification, are permitted provided that the following conditions are
6met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above
10 copyright notice, this list of conditions and the following
11 disclaimer in the documentation and/or other materials provided
12 with the distribution.
13 * Neither the name of The Linux Foundation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
16
17THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28==============================================================================*/
29
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053030/*==============================================================================
31 Modifications beyond the version released on CAF are under the MIT license:
32
33 Copyright 2018 Laurence Lundblade
34
35 Permission is hereby granted, free of charge, to any person obtaining
36 a copy of this software and associated documentation files (the
37 "Software"), to deal in the Software without restriction, including
38 without limitation the rights to use, copy, modify, merge, publish,
39 distribute, sublicense, and/or sell copies of the Software, and to
40 permit persons to whom the Software is furnished to do so, subject to
41 the following conditions:
42
43 The above copyright notice and this permission notice shall be included
44 in all copies or substantial portions of the Software.
45
46 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
47 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
48 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
49 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
50 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
51 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
52 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
53 SOFTWARE.
54 ==============================================================================*/
55
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080056#include "UsefulBuf.h"
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080057
58
59/* Basic exercise...
60
61 Call all the main public functions.
62
63 Binary compare the result to the expected.
64
65 There is nothing adversarial in this test
66 */
67const char * NonAdversarialUOBTest()
68{
69 const char *szReturn = NULL;
70
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053071 UsefulBuf_MakeStackUB(outbuf,50);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080072
73 UsefulOutBuf UOB;
74
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053075 UsefulOutBuf_Init(&UOB, outbuf);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080076
77 if(!UsefulOutBuf_AtStart(&UOB)) {
78 szReturn = "Not at start";
79 goto Done;
80 }
81
82 // Put 7 bytes at beginning of buf
83 UsefulOutBuf_AppendData(&UOB, "bluster", 7);
84
85 if(UsefulOutBuf_AtStart(&UOB)) {
86 szReturn = "At start";
87 goto Done;
88 }
89
90 // add a space to end
91 UsefulOutBuf_AppendByte(&UOB, ' ');
92
93 // Add 5 bytes to the end
94 UsefulBufC UBC = {"hunny", 5};
95 UsefulOutBuf_AppendUsefulBuf(&UOB, UBC);
96
97 // Insert 9 bytes at the beginning, slide the previous stuff right
98 UsefulOutBuf_InsertData(&UOB, "heffalump", 9, 0);
99 UsefulOutBuf_InsertByte(&UOB, ' ', 9);
100
101 // Put 9 bytes in at position 10 -- just after "heffalump "
102 UsefulBufC UBC2 = {"unbounce ", 9};
103 UsefulOutBuf_InsertUsefulBuf(&UOB, UBC2, 10);
104
105 // Make it a null terminated string (because all the appends and inserts above not strcpy !)
106 UsefulOutBuf_AppendByte(&UOB, '\0');
107
108
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530109 UsefulBufC U = UsefulOutBuf_OutUBuf(&UOB);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800110
111 const char *expected = "heffalump unbounce bluster hunny";
112
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530113 if(UsefulBuf_IsNULLC(U) || U.len-1 != strlen(expected) || strcmp(expected, U.ptr) || UsefulOutBuf_GetError(&UOB)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800114 szReturn = "OutUBuf";
115 }
116
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530117 UsefulBuf_MakeStackUB(buf, 50);
118 UsefulBufC Out = UsefulOutBuf_CopyOut(&UOB, buf);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800119
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530120 if(UsefulBuf_IsNULLC(Out) || Out.len-1 != strlen(expected) || strcmp(expected, Out.ptr)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800121 szReturn = "CopyOut";
122 }
123
124Done:
125 return szReturn;
126}
127
128
129/*
130 Append test utility.
131 pUOB is the buffer to append too
132 num is the amount to append
133 expected is the expected return code, 0 or 1
134
135 returns 0 if test passed
136
137 */
138static int AppendTest(UsefulOutBuf *pUOB, size_t num, int expected)
139{
140 //reset
141 UsefulOutBuf_Reset(pUOB);
142
143 // check status first
144 if(UsefulOutBuf_GetError(pUOB))
145 return 1;
146
147 // append the bytes
148 UsefulOutBuf_AppendData(pUOB, (const uint8_t *)"bluster", num);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530149
150 // TODO: test UsefulOutBuf_AppendString
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800151
152 // check error status after
153 if(UsefulOutBuf_GetError(pUOB) != expected)
154 return 1;
155
156 return 0;
157}
158
159
160/*
161 Same as append, but takes a position param too
162 */
163static int InsertTest(UsefulOutBuf *pUOB, size_t num, size_t pos, int expected)
164{
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530165 // reset
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800166 UsefulOutBuf_Reset(pUOB);
167
168 // check
169 if(UsefulOutBuf_GetError(pUOB))
170 return 1;
171
172 UsefulOutBuf_InsertData(pUOB, (const uint8_t *)"bluster", num, pos);
173
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530174 // TODO: test UsefulOutBuf_InsertString
175
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800176 if(UsefulOutBuf_GetError(pUOB) != expected)
177 return 1;
178
179 return 0;
180}
181
182
183/*
184 Boundary conditions to test
185 - around 0
186 - around the buffer size
187 - around MAX size_t
188
189
190 Test these for the buffer size and the cursor, the insert amount, the append amount and the insert position
191
192 */
193
194const char *BoundaryConditionsTest()
195{
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530196 UsefulBuf_MakeStackUB(outbuf,2);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800197
198 UsefulOutBuf UOB;
199
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530200 UsefulOutBuf_Init(&UOB, outbuf);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800201
202 // append 0 byte to a 2 byte buffer --> success
203 if(AppendTest(&UOB, 0, 0))
204 return "Append 0 bytes failed";
205
206 // append 1 byte to a 2 byte buffer --> success
207 if(AppendTest(&UOB, 1, 0))
208 return "Append of 1 byte failed";
209
210 // append 2 byte to a 2 byte buffer --> success
211 if(AppendTest(&UOB, 2, 0))
212 return "Append to fill buffer failed";
213
214 // append 3 bytes to a 2 byte buffer --> failure
215 if(AppendTest(&UOB, 3, 1))
216 return "Overflow of buffer not caught";
217
218 // append max size_t to a 2 byte buffer --> failure
219 if(AppendTest(&UOB, SIZE_MAX, 1))
220 return "Append of SIZE_MAX error not caught";
221
222 if(InsertTest(&UOB, 1, 0, 0))
223 return "Insert 1 byte at start failed";
224
225 if(InsertTest(&UOB, 2, 0, 0))
226 return "Insert 2 bytes at start failed";
227
228 if(InsertTest(&UOB, 3, 0, 1))
229 return "Insert overflow not caught";
230
231 if(InsertTest(&UOB, 1, 1, 1))
232 return "Bad insertion point not caught";
233
234
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530235 UsefulBuf_MakeStackUB(outBuf2,10);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800236
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530237 UsefulOutBuf_Init(&UOB, outBuf2);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800238
239 UsefulOutBuf_Reset(&UOB);
240 // put data in the buffer
241 UsefulOutBuf_AppendData(&UOB, "abc123", 6);
242
243 UsefulOutBuf_InsertData(&UOB, "xyz*&^", 6, 0);
244
245 if(!UsefulOutBuf_GetError(&UOB)) {
246 return "insert with data should have failed";
247 }
248
249
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530250 UsefulOutBuf_Init(&UOB, (UsefulBuf){NULL, SIZE_MAX - 5});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800251 UsefulOutBuf_AppendData(&UOB, "123456789", SIZE_MAX -6);
252 if(UsefulOutBuf_GetError(&UOB)) {
253 return "insert in huge should have succeeded";
254 }
255
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530256 UsefulOutBuf_Init(&UOB, (UsefulBuf){NULL, SIZE_MAX - 5});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800257 UsefulOutBuf_AppendData(&UOB, "123456789", SIZE_MAX -5);
258 if(UsefulOutBuf_GetError(&UOB)) {
259 return "insert in huge should have succeeded";
260 }
261
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530262 UsefulOutBuf_Init(&UOB, (UsefulBuf){NULL, SIZE_MAX - 5});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800263 UsefulOutBuf_AppendData(&UOB, "123456789", SIZE_MAX - 4);
264 if(!UsefulOutBuf_GetError(&UOB)) {
265 return "lengths near max size";
266 }
267
268 return NULL;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800269}
270
271
272
273
274
275// Test function to get size and magic number check
276
277const char *TestBasicSanity()
278{
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530279 UsefulBuf_MakeStackUB(outbuf,10);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800280
281 UsefulOutBuf UOB;
282
283 // First -- make sure that the room left function returns the right amount
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530284 UsefulOutBuf_Init(&UOB, outbuf);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800285
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530286 if(UsefulOutBuf_RoomLeft(&UOB) != 10)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800287 return "room left failed";
288
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530289 // TODO: test UsefulOutBuf_WillItFit
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800290
291 // Next -- make sure that the magic number checking is working right
292 UOB.magic = 8888; // make magic bogus
293
294 UsefulOutBuf_AppendData(&UOB, (const uint8_t *)"bluster", 7);
295
296 if(!UsefulOutBuf_GetError(&UOB))
297 return "magic corruption check failed";
298
299
300
301 // Next make sure that the valid data length check is working right
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530302 UsefulOutBuf_Init(&UOB, outbuf);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800303
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530304 UOB.data_len = UOB.UB.len+1; // make size bogus
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800305
306 UsefulOutBuf_AppendData(&UOB, (const uint8_t *)"bluster", 7);
307 if(!UsefulOutBuf_GetError(&UOB))
308 return "valid data check failed";
309
310 return NULL;
311}
312
313
314
315const char *UBMacroConversionsTest()
316{
317 char *szFoo = "foo";
318
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530319 UsefulBufC Foo = UsefulBuf_FromSZ(szFoo);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800320 if(Foo.len != 3 || strncmp(Foo.ptr, szFoo, 3))
321 return "SZToUsefulBufC failed";
322
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530323 UsefulBufC Too = UsefulBuf_FromSZLiteral("Toooo");
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800324 if(Too.len != 5 || strncmp(Too.ptr, "Toooo", 5))
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530325 return "UsefulBuf_FromSZLiteral failed";
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800326
327 uint8_t pB[] = {0x42, 0x6f, 0x6f};
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530328 UsefulBufC Boo = UsefulBuf_FromByteArrayLiteral(pB);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800329 if(Boo.len != 3 || strncmp(Boo.ptr, "Boo", 3))
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530330 return "UsefulBuf_FromByteArrayLiteral failed";
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800331
332 char *sz = "not const"; // some data for the test
333 UsefulBuf B = (UsefulBuf){sz, sizeof(sz)};
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530334 UsefulBufC BC = UsefulBuf_Const(B);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800335 if(BC.len != sizeof(sz) || BC.ptr != sz)
336 return "UsefulBufConst failed";
337
338 return NULL;
339}
340
341
342const char *UBUtilTests()
343{
344 UsefulBuf UB = NULLUsefulBuf;
345
346 if(!UsefulBuf_IsNULL(UB)){
347 return "IsNull failed";
348 }
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530349
350 if(!UsefulBuf_IsEmpty(UB)){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800351 return "IsEmpty failed";
352 }
353
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530354 if(!UsefulBuf_IsNULLOrEmpty(UB)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800355 return "IsNULLOrEmpty failed";
356 }
357
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530358 UsefulBufC UBC = UsefulBuf_Const(UB);
359
360 if(!UsefulBuf_IsNULLC(UBC)){
361 return "IsNull const failed";
362 }
363
364 if(!UsefulBuf_IsEmptyC(UBC)){
365 return "IsEmptyC failed";
366 }
367
368 if(!UsefulBuf_IsNULLOrEmptyC(UBC)){
369 return "IsNULLOrEmptyC failed";
370 }
371
372 UsefulBuf UB2 = UsefulBuf_Unconst(UBC);
373 if(!UsefulBuf_IsEmpty(UB2)) {
374 return "Back to UB is Empty failed";
375 }
376
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800377 UB.ptr = "x"; // just some valid pointer
378
379 if(UsefulBuf_IsNULL(UB)){
380 return "IsNull failed";
381 }
382
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530383 if(!UsefulBuf_IsEmptyC(UBC)){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800384 return "IsEmpty failed";
385 }
386
387 // test the Unconst.
388 if(UsefulBufC_Unconst(UBC).ptr != NULL) {
389 return "Unconst failed";
390 }
391
392 // Set 100 bytes of '+'; validated a few tests later
393 MakeUsefulBufOnStack(Temp, 100);
394 UsefulBuf_Set(&Temp, '+');
395
396 // Try to copy into a buf that is too small and see failure
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530397 UsefulBuf_MakeStackUB(Temp2, 99);
398 if(!UsefulBuf_IsNULLC(UsefulBuf_Copy(Temp2, UsefulBuf_Const(Temp)))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800399 return "Copy should have failed";
400 }
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530401
402 // TODO: complete this test
403 // UsefulBuf_CopyOffset();
404 // TODO: complete this test
405 // UsefulBuf_CopyPtr();
406 // TODO: complete this test
407 // UsefulBuf_Head();
408 // TODO: complete this test
409 // UsefulBuf_CopyPtr();
410
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800411 // Try to copy into a NULL/empty buf and see failure
412 UsefulBuf UBNull = NULLUsefulBuf;
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530413 if(!UsefulBuf_IsNULLC(UsefulBuf_Copy(UBNull, UsefulBuf_Const(Temp)))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800414 return "Copy to NULL should have failed";
415 }
416
417 // Try to set a NULL/empty buf; nothing should happen
418 UsefulBuf_Set(&UBNull, '+'); // This will crash on failure
419
420 // Copy successfully to a buffer
421 MakeUsefulBufOnStack(Temp3, 101);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530422 if(UsefulBuf_IsNULLC(UsefulBuf_Copy(Temp3, UsefulBuf_Const(Temp)))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800423 return "Copy should not have failed";
424 }
425
426 static const uint8_t pExpected[] = {
427 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
428 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
429 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
430 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
431 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
432 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
433 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
434 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
435 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
436 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
437 };
438 UsefulBufC Expected = ByteArrayLiteralToUsefulBufC(pExpected);
439 // This validates comparison for equality and the UsefulBuf_Set
440 if(UsefulBuf_Compare(Expected, UsefulBuf_Const(Temp))) {
441 return "Set / Copy / Compare failed";
442 }
443
444 // Compare two empties and expect success
445 if(UsefulBuf_Compare(NULLUsefulBufC, NULLUsefulBufC)){
446 return "Compare Empties failed";
447 }
448
449 // Compare with empty and expect the first to be larger
450 if(UsefulBuf_Compare(Expected, NULLUsefulBufC) <= 0){
451 return "Compare with empty failed";
452 }
453
454
455 static const uint8_t pExpectedBigger[] = {
456 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
457 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
458 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
459 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
460 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
461 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
462 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
463 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
464 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
465 '+', '+', '+', '+', '+', '+', '+', '+', '+', ',',
466 };
467 UsefulBufC ExpectedBigger = ByteArrayLiteralToUsefulBufC(pExpectedBigger);
468
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530469 // Expect -1 when the first arg is smaller
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800470 if(UsefulBuf_Compare(Expected, ExpectedBigger) >= 0){
471 return "Compare with bigger";
472 }
473
474
475 static const uint8_t pExpectedSmaller[] = {
476 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
477 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
478 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
479 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
480 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
481 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
482 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
483 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
484 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
485 '+', '+', '+', '+', '+', '+', '+', '+', '+', '*',
486 };
487 UsefulBufC ExpectedSmaller = ByteArrayLiteralToUsefulBufC(pExpectedSmaller);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530488 // Expect +1 when the first arg is larger
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800489 if(UsefulBuf_Compare(Expected, ExpectedSmaller) <= 0){
490 return "Compare with smaller";
491 }
492
493
494 static const uint8_t pExpectedLonger[] = {
495 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
496 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
497 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
498 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
499 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
500 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
501 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
502 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
503 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
504 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+'
505 };
506 UsefulBufC ExpectedLonger = ByteArrayLiteralToUsefulBufC(pExpectedLonger);
507
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530508 // Expect -1 when the first arg is smaller
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800509 if(UsefulBuf_Compare(Expected, ExpectedLonger) >= 0){
510 return "Compare with longer";
511 }
512
513
514 static const uint8_t pExpectedShorter[] = {
515 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
516 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
517 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
518 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
519 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
520 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
521 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
522 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
523 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
524 '+', '+', '+', '+', '+', '+', '+', '+', '+',
525 };
526 UsefulBufC ExpectedShorter = ByteArrayLiteralToUsefulBufC(pExpectedShorter);
527 // Expect +1 with the first arg is larger
528 if(UsefulBuf_Compare(Expected, ExpectedShorter) <= 0){
529 return "Compare with shorter";
530 }
531
532
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530533 if(UsefulBuf_IsNULLC(UsefulBuf_Copy(Temp, NULLUsefulBufC))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800534 return "Copy null/empty failed";
535 }
536
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800537 // Look for +++++... in +++++... and find it at the beginning
538 if(0 != UsefulBuf_FindBytes(ExpectedLonger, ExpectedShorter)){
539 return "Failed to find";
540 }
541
542 // look for ++* in ....++* and find it at the end
543 static const uint8_t pToFind[] = {'+', '+', '*'};
544 UsefulBufC ToBeFound = ByteArrayLiteralToUsefulBufC(pToFind);
545
546 if(97 != UsefulBuf_FindBytes(ExpectedSmaller, ToBeFound)){
547 return "Failed to find 2";
548 }
549
550 // look for ++* in ....++, and find it near the end
551 if(SIZE_MAX != UsefulBuf_FindBytes(ExpectedBigger, ToBeFound)){
552 return "Failed to not find";
553 }
554
555 // Look for the whole buffer in itself and succeed.
556 if(0 != UsefulBuf_FindBytes(ExpectedLonger, ExpectedLonger)){
557 return "Failed to find 3";
558 }
559
560 return NULL;
561}
562
563
564const char * UBIntegerFormatTests()
565{
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530566 UsefulOutBuf_MakeOnStack(UOB,100);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800567
568 const uint32_t u32 = 0x0A0B0C0D; // from https://en.wikipedia.org/wiki/Endianness
569 const uint64_t u64 = 1984738472938472;
570 const uint16_t u16 = 40000;
571 const uint8_t u8 = 9;
572 const float f = (float)314.15;
573 const double d = 2.1e10;
574
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800575
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530576 UsefulOutBuf_AppendUint32(&UOB, u32); // Also tests UsefulOutBuf_InsertUint64 and UsefulOutBuf_GetEndPosition
577 UsefulOutBuf_AppendUint64(&UOB, u64); // Also tests UsefulOutBuf_InsertUint32
578 UsefulOutBuf_AppendUint16(&UOB, u16); // Also tests UsefulOutBuf_InsertUint16
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800579 UsefulOutBuf_AppendByte(&UOB, u8);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530580 UsefulOutBuf_AppendFloat(&UOB, f); // Also tests UsefulOutBuf_InsertFloat
581 UsefulOutBuf_AppendDouble(&UOB, d); // Also tests UsefulOutBuf_InsertDouble
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800582
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530583 UsefulBufC O = UsefulOutBuf_OutUBuf(&UOB);
584 if(UsefulBuf_IsNULLC(O))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800585 return "Couldn't output integers";
586
587 // from https://en.wikipedia.org/wiki/Endianness
588 const uint8_t pExpectedNetworkOrder[4] = {0x0A, 0x0B, 0x0C, 0x0D};
589 if(memcmp(O.ptr, pExpectedNetworkOrder, 4)) {
590 return "not in network order";
591 }
592
593 UsefulInputBuf UIB;
594
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530595 UsefulInputBuf_Init(&UIB, O);
596
597 if(UsefulInputBuf_Tell(&UIB) != 0) {
598 return "UsefulInputBuf_Tell failed";
599 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800600
601 if(UsefulInputBuf_GetUint32(&UIB) != u32) {
602 return "u32 out then in failed";
603 }
604 if(UsefulInputBuf_GetUint64(&UIB) != u64) {
605 return "u64 out then in failed";
606 }
607 if(UsefulInputBuf_GetUint16(&UIB) != u16) {
608 return "u16 out then in failed";
609 }
610 if(UsefulInputBuf_GetByte(&UIB) != u8) {
611 return "u8 out then in failed";
612 }
613 if(UsefulInputBuf_GetFloat(&UIB) != f) {
614 return "float out then in failed";
615 }
616 if(UsefulInputBuf_GetDouble(&UIB) != d) {
617 return "double out then in failed";
618 }
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530619
620 // TODO: test UsefulInputBuf_Seek
621 // TODO: test UsefulInputBuf_BytesUnconsumed
622 // TODO: test UsefulInputBuf_BytesAvailable
623 // TODO: test UsefulInputBuf_GetBytes
624 // TODO: test UsefulInputBuf_GetUsefulBuf
625 // TODO: test UsefulInputBuf_GetError
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800626
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530627 return NULL;
628}
629
630
631const char *UBCopyUtilTest()
632{
633 if(UsefulBufUtil_CopyFloatToUint32(65536.0F) != 0x47800000) {
634 return "CopyFloatToUint32 failed";
635 }
636
637 if(UsefulBufUtil_CopyDoubleToUint64(4e-40F) != 0X37C16C2800000000ULL) {
638 return "CopyDoubleToUint64 failed";
639 }
640
641 if(UsefulBufUtil_CopyUint64ToDouble(0X37C16C2800000000ULL) != 4e-40F) {
642 return "CopyUint64ToDouble failed";
643 }
644
645 if(UsefulBufUtil_CopyUint32ToFloat(0x47800000) != 65536.0F) {
646 return "CopyUint32ToFloat failed";
647 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800648
649 return NULL;
650}
651
652
653