blob: 264fbca9cf0a3d1ab8ed383dae6918294c0b2d6e [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
Laurence Lundbladed92a6162018-11-01 11:38:35 +07002 Copyright (c) 2016-2018, The Linux Foundation.
Laurence Lundbladecf41c522021-02-20 10:19:07 -07003 Copyright (c) 2018-2021, Laurence Lundblade.
Laurence Lundbladed92a6162018-11-01 11:38:35 +07004 All rights reserved.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005
Laurence Lundblade0dbc9172018-11-01 14:17:21 +07006Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are
8met:
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above
12 copyright notice, this list of conditions and the following
13 disclaimer in the documentation and/or other materials provided
14 with the distribution.
15 * Neither the name of The Linux Foundation nor the names of its
16 contributors, nor the name "Laurence Lundblade" may be used to
17 endorse or promote products derived from this software without
18 specific prior written permission.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080019
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070020THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
21WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
23ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
24BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundbladeee851742020-01-08 08:37:05 -080031 =============================================================================*/
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053032
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080033#include "UsefulBuf.h"
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080034
35
36/* Basic exercise...
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080037
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080038 Call all the main public functions.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080039
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080040 Binary compare the result to the expected.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080041
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080042 There is nothing adversarial in this test
43 */
Laurence Lundbladeb9702452021-03-08 21:02:57 -080044const char * UOBTest_NonAdversarial(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080045{
46 const char *szReturn = NULL;
47
Laurence Lundblade4fe9f312018-10-22 10:22:39 +053048 UsefulBuf_MAKE_STACK_UB(outbuf,50);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080049
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080050 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080051
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053052 UsefulOutBuf_Init(&UOB, outbuf);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080053
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080054 if(!UsefulOutBuf_AtStart(&UOB)) {
55 szReturn = "Not at start";
56 goto Done;
57 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080058
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080059 // Put 7 bytes at beginning of buf
60 UsefulOutBuf_AppendData(&UOB, "bluster", 7);
61
62 if(UsefulOutBuf_AtStart(&UOB)) {
63 szReturn = "At start";
64 goto Done;
65 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080066
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080067 // add a space to end
68 UsefulOutBuf_AppendByte(&UOB, ' ');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080069
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080070 // Add 5 bytes to the end
71 UsefulBufC UBC = {"hunny", 5};
72 UsefulOutBuf_AppendUsefulBuf(&UOB, UBC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080073
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080074 // Insert 9 bytes at the beginning, slide the previous stuff right
75 UsefulOutBuf_InsertData(&UOB, "heffalump", 9, 0);
76 UsefulOutBuf_InsertByte(&UOB, ' ', 9);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080077
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080078 // Put 9 bytes in at position 10 -- just after "heffalump "
79 UsefulBufC UBC2 = {"unbounce ", 9};
80 UsefulOutBuf_InsertUsefulBuf(&UOB, UBC2, 10);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080081
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080082
Laurence Lundbladec5fef682020-01-25 11:38:45 -080083 const UsefulBufC Expected = UsefulBuf_FROM_SZ_LITERAL("heffalump unbounce bluster hunny");
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080084
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053085 UsefulBufC U = UsefulOutBuf_OutUBuf(&UOB);
Laurence Lundbladec5fef682020-01-25 11:38:45 -080086 if(UsefulBuf_IsNULLC(U) || UsefulBuf_Compare(Expected, U) || UsefulOutBuf_GetError(&UOB)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080087 szReturn = "OutUBuf";
88 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080089
Laurence Lundblade4fe9f312018-10-22 10:22:39 +053090 UsefulBuf_MAKE_STACK_UB(buf, 50);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053091 UsefulBufC Out = UsefulOutBuf_CopyOut(&UOB, buf);
Laurence Lundbladec5fef682020-01-25 11:38:45 -080092 if(UsefulBuf_IsNULLC(Out) || UsefulBuf_Compare(Expected, Out)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080093 szReturn = "CopyOut";
94 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080095
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080096Done:
97 return szReturn;
98}
99
100
101/*
102 Append test utility.
103 pUOB is the buffer to append too
104 num is the amount to append
105 expected is the expected return code, 0 or 1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800106
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800107 returns 0 if test passed
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800108
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800109 */
110static int AppendTest(UsefulOutBuf *pUOB, size_t num, int expected)
111{
112 //reset
113 UsefulOutBuf_Reset(pUOB);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800114
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800115 // check status first
116 if(UsefulOutBuf_GetError(pUOB))
117 return 1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800118
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800119 // append the bytes
120 UsefulOutBuf_AppendData(pUOB, (const uint8_t *)"bluster", num);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800121
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800122 // check error status after
123 if(UsefulOutBuf_GetError(pUOB) != expected)
124 return 1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800125
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800126 return 0;
127}
128
129
130/*
131 Same as append, but takes a position param too
132 */
133static int InsertTest(UsefulOutBuf *pUOB, size_t num, size_t pos, int expected)
134{
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530135 // reset
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800136 UsefulOutBuf_Reset(pUOB);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800137
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800138 // check
139 if(UsefulOutBuf_GetError(pUOB))
140 return 1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800141
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800142 UsefulOutBuf_InsertData(pUOB, (const uint8_t *)"bluster", num, pos);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800143
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800144 if(UsefulOutBuf_GetError(pUOB) != expected)
145 return 1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800146
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800147 return 0;
148}
149
150
151/*
152 Boundary conditions to test
153 - around 0
154 - around the buffer size
155 - around MAX size_t
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800156
157
Laurence Lundbladeee851742020-01-08 08:37:05 -0800158 Test these for the buffer size and the cursor, the insert amount, the
159 append amount and the insert position
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800160
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800161 */
162
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800163const char *UOBTest_BoundaryConditionsTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800164{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530165 UsefulBuf_MAKE_STACK_UB(outbuf,2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800166
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800167 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800168
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530169 UsefulOutBuf_Init(&UOB, outbuf);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800170
171 // append 0 byte to a 2 byte buffer --> success
172 if(AppendTest(&UOB, 0, 0))
173 return "Append 0 bytes failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800174
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800175 // append 1 byte to a 2 byte buffer --> success
176 if(AppendTest(&UOB, 1, 0))
177 return "Append of 1 byte failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800178
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800179 // append 2 byte to a 2 byte buffer --> success
180 if(AppendTest(&UOB, 2, 0))
181 return "Append to fill buffer failed";
182
183 // append 3 bytes to a 2 byte buffer --> failure
184 if(AppendTest(&UOB, 3, 1))
185 return "Overflow of buffer not caught";
186
187 // append max size_t to a 2 byte buffer --> failure
188 if(AppendTest(&UOB, SIZE_MAX, 1))
189 return "Append of SIZE_MAX error not caught";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800190
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800191 if(InsertTest(&UOB, 1, 0, 0))
192 return "Insert 1 byte at start failed";
193
194 if(InsertTest(&UOB, 2, 0, 0))
195 return "Insert 2 bytes at start failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800196
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800197 if(InsertTest(&UOB, 3, 0, 1))
198 return "Insert overflow not caught";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800199
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800200 if(InsertTest(&UOB, 1, 1, 1))
201 return "Bad insertion point not caught";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800202
203
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530204 UsefulBuf_MAKE_STACK_UB(outBuf2,10);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800205
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530206 UsefulOutBuf_Init(&UOB, outBuf2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800207
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800208 UsefulOutBuf_Reset(&UOB);
209 // put data in the buffer
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800210 UsefulOutBuf_AppendString(&UOB, "abc123");
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800211
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800212 UsefulOutBuf_InsertString(&UOB, "xyz*&^", 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800213
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800214 if(!UsefulOutBuf_GetError(&UOB)) {
215 return "insert with data should have failed";
216 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800217
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800218
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530219 UsefulOutBuf_Init(&UOB, (UsefulBuf){NULL, SIZE_MAX - 5});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800220 UsefulOutBuf_AppendData(&UOB, "123456789", SIZE_MAX -6);
221 if(UsefulOutBuf_GetError(&UOB)) {
222 return "insert in huge should have succeeded";
223 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800224
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530225 UsefulOutBuf_Init(&UOB, (UsefulBuf){NULL, SIZE_MAX - 5});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800226 UsefulOutBuf_AppendData(&UOB, "123456789", SIZE_MAX -5);
227 if(UsefulOutBuf_GetError(&UOB)) {
228 return "insert in huge should have succeeded";
229 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800230
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530231 UsefulOutBuf_Init(&UOB, (UsefulBuf){NULL, SIZE_MAX - 5});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800232 UsefulOutBuf_AppendData(&UOB, "123456789", SIZE_MAX - 4);
233 if(!UsefulOutBuf_GetError(&UOB)) {
234 return "lengths near max size";
235 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800236
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +0300237 UsefulOutBuf_Init(&UOB, (UsefulBuf){NULL, 100});
238 if(!UsefulOutBuf_IsBufferNULL(&UOB)) {
239 return "NULL check failed";
240 }
241
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800242 return NULL;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800243}
244
245
246
247
248
249// Test function to get size and magic number check
250
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800251const char *TestBasicSanity(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800252{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530253 UsefulBuf_MAKE_STACK_UB(outbuf,10);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800254
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800255 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800256
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800257 // First -- make sure that the room left function returns the right amount
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530258 UsefulOutBuf_Init(&UOB, outbuf);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800259
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530260 if(UsefulOutBuf_RoomLeft(&UOB) != 10)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800261 return "room left failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800262
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800263 if(!UsefulOutBuf_WillItFit(&UOB, 9)) {
264 return "it did not fit";
265 }
266
267 if(UsefulOutBuf_WillItFit(&UOB, 11)) {
268 return "it should have not fit";
269 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800270
271
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800272 // Next -- make sure that the magic number checking is working right
273 UOB.magic = 8888; // make magic bogus
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800274
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800275 UsefulOutBuf_AppendData(&UOB, (const uint8_t *)"bluster", 7);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800276
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800277 if(!UsefulOutBuf_GetError(&UOB))
278 return "magic corruption check failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800279
280
281
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800282 // Next make sure that the valid data length check is working right
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530283 UsefulOutBuf_Init(&UOB, outbuf);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800284
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530285 UOB.data_len = UOB.UB.len+1; // make size bogus
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800286
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800287 UsefulOutBuf_AppendData(&UOB, (const uint8_t *)"bluster", 7);
288 if(!UsefulOutBuf_GetError(&UOB))
289 return "valid data check failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800290
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800291 return NULL;
292}
293
294
295
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800296const char *UBMacroConversionsTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800297{
298 char *szFoo = "foo";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800299
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530300 UsefulBufC Foo = UsefulBuf_FromSZ(szFoo);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800301 if(Foo.len != 3 || strncmp(Foo.ptr, szFoo, 3))
302 return "SZToUsefulBufC failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800303
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530304 UsefulBufC Too = UsefulBuf_FROM_SZ_LITERAL("Toooo");
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800305 if(Too.len != 5 || strncmp(Too.ptr, "Toooo", 5))
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530306 return "UsefulBuf_FROM_SZ_LITERAL failed";
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800307
308 uint8_t pB[] = {0x42, 0x6f, 0x6f};
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530309 UsefulBufC Boo = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pB);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800310 if(Boo.len != 3 || strncmp(Boo.ptr, "Boo", 3))
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530311 return "UsefulBuf_FROM_BYTE_ARRAY_LITERAL failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800312
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800313 char *String = "string"; // Intentionally not const
314 UsefulBuf B = (UsefulBuf){(void *)String, strlen(String)};
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530315 UsefulBufC BC = UsefulBuf_Const(B);
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800316 if(BC.len != strlen(String) || BC.ptr != String)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800317 return "UsefulBufConst failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800318
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800319 return NULL;
320}
321
322
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800323const char *UBUtilTests(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800324{
325 UsefulBuf UB = NULLUsefulBuf;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800326
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800327 if(!UsefulBuf_IsNULL(UB)){
328 return "IsNull failed";
329 }
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530330
331 if(!UsefulBuf_IsEmpty(UB)){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800332 return "IsEmpty failed";
333 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800334
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530335 if(!UsefulBuf_IsNULLOrEmpty(UB)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800336 return "IsNULLOrEmpty failed";
337 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800338
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800339 const UsefulBufC UBC = UsefulBuf_Const(UB);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800340
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530341 if(!UsefulBuf_IsNULLC(UBC)){
342 return "IsNull const failed";
343 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800344
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530345 if(!UsefulBuf_IsEmptyC(UBC)){
346 return "IsEmptyC failed";
347 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800348
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530349 if(!UsefulBuf_IsNULLOrEmptyC(UBC)){
350 return "IsNULLOrEmptyC failed";
351 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800352
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800353 const UsefulBuf UB2 = UsefulBuf_Unconst(UBC);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530354 if(!UsefulBuf_IsEmpty(UB2)) {
355 return "Back to UB is Empty failed";
356 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800357
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800358 UB.ptr = "x"; // just some valid pointer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800359
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800360 if(UsefulBuf_IsNULL(UB)){
361 return "IsNull failed";
362 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800363
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530364 if(!UsefulBuf_IsEmptyC(UBC)){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800365 return "IsEmpty failed";
366 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800367
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800368 // test the Unconst.
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530369 if(UsefulBuf_Unconst(UBC).ptr != NULL) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800370 return "Unconst failed";
371 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800372
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800373 // Set 100 bytes of '+'; validated a few tests later
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530374 UsefulBuf_MAKE_STACK_UB(Temp, 100);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800375 const UsefulBufC TempC = UsefulBuf_Set(Temp, '+');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800376
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800377 // Try to copy into a buf that is too small and see failure
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530378 UsefulBuf_MAKE_STACK_UB(Temp2, 99);
Laurence Lundblade05ec57b2018-10-21 01:50:03 +0530379 if(!UsefulBuf_IsNULLC(UsefulBuf_Copy(Temp2, TempC))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800380 return "Copy should have failed";
381 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800382
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800383 if(UsefulBuf_IsNULLC(UsefulBuf_CopyPtr(Temp2, "xx", 2))) {
384 return "CopyPtr failed";
385 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800386
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530387 UsefulBufC xxyy = UsefulBuf_CopyOffset(Temp2, 2, UsefulBuf_FROM_SZ_LITERAL("yy"));
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800388 if(UsefulBuf_IsNULLC(xxyy)) {
389 return "CopyOffset Failed";
390 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800391
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530392 if(UsefulBuf_Compare(UsefulBuf_Head(xxyy, 3), UsefulBuf_FROM_SZ_LITERAL("xxy"))) {
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800393 return "head failed";
394 }
395
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530396 if(UsefulBuf_Compare(UsefulBuf_Tail(xxyy, 1), UsefulBuf_FROM_SZ_LITERAL("xyy"))) {
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800397 return "tail failed";
398 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800399
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800400 if(!UsefulBuf_IsNULLC(UsefulBuf_Head(xxyy, 5))) {
401 return "head should have failed";
402 }
403
404 if(!UsefulBuf_IsNULLC(UsefulBuf_Tail(xxyy, 5))) {
405 return "tail should have failed";
406 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -0800407
Laurence Lundblade7412f812019-01-01 18:49:36 -0800408 if(!UsefulBuf_IsNULLC(UsefulBuf_Tail(NULLUsefulBufC, 0))) {
409 return "tail of NULLUsefulBufC is not NULLUsefulBufC";
410 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -0800411
Laurence Lundblade7412f812019-01-01 18:49:36 -0800412 const UsefulBufC TailResult = UsefulBuf_Tail((UsefulBufC){NULL, 100}, 99);
413 if(TailResult.ptr != NULL || TailResult.len != 1) {
414 return "tail of NULL and length incorrect";
415 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800416
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530417 if(!UsefulBuf_IsNULLC(UsefulBuf_CopyOffset(Temp2, 100, UsefulBuf_FROM_SZ_LITERAL("yy")))) {
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800418 return "Copy Offset should have failed";
419 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800420
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800421 // Try to copy into a NULL/empty buf and see failure
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800422 const UsefulBuf UBNull = NULLUsefulBuf;
Laurence Lundblade05ec57b2018-10-21 01:50:03 +0530423 if(!UsefulBuf_IsNULLC(UsefulBuf_Copy(UBNull, TempC))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800424 return "Copy to NULL should have failed";
425 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800426
427
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800428 // Try to set a NULL/empty buf; nothing should happen
Laurence Lundblade05ec57b2018-10-21 01:50:03 +0530429 UsefulBuf_Set(UBNull, '+'); // This will crash on failure
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800430
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800431 // Copy successfully to a buffer
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530432 UsefulBuf_MAKE_STACK_UB(Temp3, 101);
Laurence Lundblade05ec57b2018-10-21 01:50:03 +0530433 if(UsefulBuf_IsNULLC(UsefulBuf_Copy(Temp3, TempC))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800434 return "Copy should not have failed";
435 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800436
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800437 static const uint8_t pExpected[] = {
438 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
439 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
440 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
441 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
442 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
443 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
444 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
445 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
446 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
447 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
448 };
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530449 UsefulBufC Expected = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpected);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800450 // This validates comparison for equality and the UsefulBuf_Set
Laurence Lundblade05ec57b2018-10-21 01:50:03 +0530451 if(UsefulBuf_Compare(Expected, TempC)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800452 return "Set / Copy / Compare failed";
453 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800454
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800455 // Compare two empties and expect success
456 if(UsefulBuf_Compare(NULLUsefulBufC, NULLUsefulBufC)){
457 return "Compare Empties failed";
458 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800459
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800460 // Compare with empty and expect the first to be larger
461 if(UsefulBuf_Compare(Expected, NULLUsefulBufC) <= 0){
462 return "Compare with empty failed";
463 }
464
465
466 static const uint8_t pExpectedBigger[] = {
467 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
468 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
469 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
470 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
471 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
472 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
473 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
474 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
475 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
476 '+', '+', '+', '+', '+', '+', '+', '+', '+', ',',
477 };
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800478 const UsefulBufC ExpectedBigger = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedBigger);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800479
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530480 // Expect -1 when the first arg is smaller
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800481 if(UsefulBuf_Compare(Expected, ExpectedBigger) >= 0){
482 return "Compare with bigger";
483 }
484
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800485
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800486 static const uint8_t pExpectedSmaller[] = {
487 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
488 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
489 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
490 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
491 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
492 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
493 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
494 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
495 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
496 '+', '+', '+', '+', '+', '+', '+', '+', '+', '*',
497 };
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800498 const UsefulBufC ExpectedSmaller = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedSmaller);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530499 // Expect +1 when the first arg is larger
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800500 if(UsefulBuf_Compare(Expected, ExpectedSmaller) <= 0){
501 return "Compare with smaller";
502 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800503
504
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800505 static const uint8_t pExpectedLonger[] = {
506 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
507 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
508 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
509 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
510 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
511 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
512 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
513 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
514 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
515 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+'
516 };
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800517 const UsefulBufC ExpectedLonger = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedLonger);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800518
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530519 // Expect -1 when the first arg is smaller
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800520 if(UsefulBuf_Compare(Expected, ExpectedLonger) >= 0){
521 return "Compare with longer";
522 }
523
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800524
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800525 static const uint8_t pExpectedShorter[] = {
526 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
527 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
528 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
529 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
530 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
531 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
532 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
533 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
534 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
535 '+', '+', '+', '+', '+', '+', '+', '+', '+',
536 };
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800537 const UsefulBufC ExpectedShorter = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedShorter);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800538 // Expect +1 with the first arg is larger
539 if(UsefulBuf_Compare(Expected, ExpectedShorter) <= 0){
540 return "Compare with shorter";
541 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800542
543
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530544 if(UsefulBuf_IsNULLC(UsefulBuf_Copy(Temp, NULLUsefulBufC))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800545 return "Copy null/empty failed";
546 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800547
Laurence Lundbladed5e101e2019-03-06 17:23:18 -0800548 if(UsefulBuf_IsValue(ExpectedShorter, '+') != SIZE_MAX) {
549 return "IsValue failed to match all";
550 }
551
552 if(UsefulBuf_IsValue(ExpectedShorter, '-') != 0) {
553 return "IsValue should have failed right away";
554 }
555
556 if(UsefulBuf_IsValue(NULLUsefulBufC, 0x00) != 0) {
557 return "IsValue failed on NULLUsefulBufC";
558 }
559
560 if(UsefulBuf_IsValue((UsefulBufC){(uint8_t[]){0x00}, 1}, 0x00) != SIZE_MAX) {
561 return "IsValue failed finding 0 in one byte of 0";
562 }
563
564 if(UsefulBuf_IsValue((UsefulBufC){(uint8_t[]){0x00}, 1}, 0x01) != 0) {
565 return "IsValue failed not finding 1 in one byte of 0";
566 }
567
568 if(UsefulBuf_IsValue(ExpectedSmaller, '+') != ExpectedSmaller.len -1) {
569 return "IsValue failed to find final *";
570 }
571
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800572 // Look for +++++... in +++++... and find it at the beginning
573 if(0 != UsefulBuf_FindBytes(ExpectedLonger, ExpectedShorter)){
574 return "Failed to find";
575 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800576
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800577 // look for ++* in ....++* and find it at the end
578 static const uint8_t pToFind[] = {'+', '+', '*'};
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800579 const UsefulBufC ToBeFound = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pToFind);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800580
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800581 if(97 != UsefulBuf_FindBytes(ExpectedSmaller, ToBeFound)){
582 return "Failed to find 2";
583 }
584
585 // look for ++* in ....++, and find it near the end
586 if(SIZE_MAX != UsefulBuf_FindBytes(ExpectedBigger, ToBeFound)){
587 return "Failed to not find";
588 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800589
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800590 // Look for the whole buffer in itself and succeed.
591 if(0 != UsefulBuf_FindBytes(ExpectedLonger, ExpectedLonger)){
592 return "Failed to find 3";
593 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800594
Laurence Lundbladecf41c522021-02-20 10:19:07 -0700595
596 const uint8_t pB[] = {0x01, 0x02, 0x03};
597 UsefulBufC Boo = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pB);
598 // Try to map a pointer before
599 if(UsefulBuf_PointerToOffset(Boo, pB-1) != SIZE_MAX) {
600 return "Didn't error on pointer before";
601 }
602
603 // Try to map a pointer after
604 if(UsefulBuf_PointerToOffset(Boo, pB+sizeof(pB)) != SIZE_MAX) {
605 return "Didn't error on pointer after";
606 }
607
608 // Try to map a pointer inside
609 if(UsefulBuf_PointerToOffset(Boo, pB+1) != 1) {
610 return "Incorrect pointer offset";
611 }
612
613 // Try to map a pointer at the start
614 if(UsefulBuf_PointerToOffset(Boo, pB) != 0) {
615 return "Incorrect pointer offset for start";
616 }
617
618 // Try to map a pointer at the end
619 if(UsefulBuf_PointerToOffset(Boo, pB + sizeof(pB)-1) != 2) {
620 return "Incorrect pointer offset for end";
621 }
622
623 // Try to map a pointer on a NULL UB
624 if(UsefulBuf_PointerToOffset(NULLUsefulBufC, pB ) != SIZE_MAX) {
625 return "Incorrect pointer offset for start";
626 }
627
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800628 return NULL;
629}
630
631
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800632const char * UIBTest_IntegerFormat(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800633{
Laurence Lundbladecf41c522021-02-20 10:19:07 -0700634 UsefulOutBuf_MakeOnStack(UOB, 100);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800635
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800636 const uint32_t u32 = 0x0A0B0C0D; // from https://en.wikipedia.org/wiki/Endianness
637 const uint64_t u64 = 1984738472938472;
638 const uint16_t u16 = 40000;
639 const uint8_t u8 = 9;
640 const float f = (float)314.15;
641 const double d = 2.1e10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800642
643
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530644 UsefulOutBuf_AppendUint32(&UOB, u32); // Also tests UsefulOutBuf_InsertUint64 and UsefulOutBuf_GetEndPosition
645 UsefulOutBuf_AppendUint64(&UOB, u64); // Also tests UsefulOutBuf_InsertUint32
646 UsefulOutBuf_AppendUint16(&UOB, u16); // Also tests UsefulOutBuf_InsertUint16
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800647 UsefulOutBuf_AppendByte(&UOB, u8);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530648 UsefulOutBuf_AppendFloat(&UOB, f); // Also tests UsefulOutBuf_InsertFloat
649 UsefulOutBuf_AppendDouble(&UOB, d); // Also tests UsefulOutBuf_InsertDouble
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800650
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800651 const UsefulBufC O = UsefulOutBuf_OutUBuf(&UOB);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530652 if(UsefulBuf_IsNULLC(O))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800653 return "Couldn't output integers";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800654
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800655 // from https://en.wikipedia.org/wiki/Endianness
656 const uint8_t pExpectedNetworkOrder[4] = {0x0A, 0x0B, 0x0C, 0x0D};
657 if(memcmp(O.ptr, pExpectedNetworkOrder, 4)) {
658 return "not in network order";
659 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800660
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800661 UsefulInputBuf UIB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800662
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530663 UsefulInputBuf_Init(&UIB, O);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800664
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530665 if(UsefulInputBuf_Tell(&UIB) != 0) {
666 return "UsefulInputBuf_Tell failed";
667 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800668
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800669 if(UsefulInputBuf_GetUint32(&UIB) != u32) {
670 return "u32 out then in failed";
671 }
672 if(UsefulInputBuf_GetUint64(&UIB) != u64) {
673 return "u64 out then in failed";
674 }
675 if(UsefulInputBuf_GetUint16(&UIB) != u16) {
676 return "u16 out then in failed";
677 }
678 if(UsefulInputBuf_GetByte(&UIB) != u8) {
679 return "u8 out then in failed";
680 }
681 if(UsefulInputBuf_GetFloat(&UIB) != f) {
682 return "float out then in failed";
683 }
684 if(UsefulInputBuf_GetDouble(&UIB) != d) {
685 return "double out then in failed";
686 }
687
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800688 // Reset and go again for a few more tests
689 UsefulInputBuf_Init(&UIB, O);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800690
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800691 const UsefulBufC Four = UsefulInputBuf_GetUsefulBuf(&UIB, 4);
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800692 if(UsefulBuf_IsNULLC(Four)) {
693 return "Four is NULL";
694 }
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530695 if(UsefulBuf_Compare(Four, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedNetworkOrder))) {
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800696 return "Four compare failed";
697 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800698
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800699 if(UsefulInputBuf_BytesUnconsumed(&UIB) != 23){
700 return "Wrong number of unconsumed bytes";
701 }
702
703 if(!UsefulInputBuf_BytesAvailable(&UIB, 23)){
704 return "Wrong number of bytes available I";
705 }
706
707 if(UsefulInputBuf_BytesAvailable(&UIB, 24)){
708 return "Wrong number of bytes available II";
709 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800710
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800711 UsefulInputBuf_Seek(&UIB, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800712
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800713 if(UsefulInputBuf_GetError(&UIB)) {
714 return "unexpected error after seek";
715 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800716
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800717 const uint8_t *pGetBytes = (const uint8_t *)UsefulInputBuf_GetBytes(&UIB, 4);
718 if(pGetBytes == NULL) {
719 return "GetBytes returns NULL";
720 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800721
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800722 if(memcmp(pGetBytes, pExpectedNetworkOrder, 4)) {
723 return "Got wrong bytes";
724 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800725
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800726 UsefulInputBuf_Seek(&UIB, 28);
727
728 if(!UsefulInputBuf_GetError(&UIB)) {
729 return "expected error after seek";
730 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800731
Laurence Lundbladecf41c522021-02-20 10:19:07 -0700732 if(UsefulInputBuf_PointerToOffset(&UIB, O.ptr) != 0) {
733 return "PointerToOffset not working";
734 }
735
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530736 return NULL;
737}
738
739
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800740const char *UBUTest_CopyUtil(void)
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530741{
742 if(UsefulBufUtil_CopyFloatToUint32(65536.0F) != 0x47800000) {
743 return "CopyFloatToUint32 failed";
744 }
745
746 if(UsefulBufUtil_CopyDoubleToUint64(4e-40F) != 0X37C16C2800000000ULL) {
747 return "CopyDoubleToUint64 failed";
748 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800749
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530750 if(UsefulBufUtil_CopyUint64ToDouble(0X37C16C2800000000ULL) != 4e-40F) {
751 return "CopyUint64ToDouble failed";
752 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800753
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530754 if(UsefulBufUtil_CopyUint32ToFloat(0x47800000) != 65536.0F) {
755 return "CopyUint32ToFloat failed";
756 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800757
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800758 return NULL;
759}
760
761
762