blob: f53693a1b8863be80406fef903135719ff5aad10 [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 Lundblade6d926af2019-01-01 19:01:43 -08003 Copyright (c) 2018-2019, 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 Lundbladedc6e28e2018-10-11 19:19:27 +053031 ==============================================================================*/
32
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 Lundblade7566b9f2018-10-12 09:13:32 +080044const char * UOBTest_NonAdversarial()
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 Lundblade2ded3d92018-10-09 21:36:11 +080082 // Make it a null terminated string (because all the appends and inserts above not strcpy !)
83 UsefulOutBuf_AppendByte(&UOB, '\0');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080084
85
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053086 UsefulBufC U = UsefulOutBuf_OutUBuf(&UOB);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080087
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080088 const char *expected = "heffalump unbounce bluster hunny";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080089
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053090 if(UsefulBuf_IsNULLC(U) || U.len-1 != strlen(expected) || strcmp(expected, U.ptr) || UsefulOutBuf_GetError(&UOB)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080091 szReturn = "OutUBuf";
92 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080093
Laurence Lundblade4fe9f312018-10-22 10:22:39 +053094 UsefulBuf_MAKE_STACK_UB(buf, 50);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053095 UsefulBufC Out = UsefulOutBuf_CopyOut(&UOB, buf);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080096
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053097 if(UsefulBuf_IsNULLC(Out) || Out.len-1 != strlen(expected) || strcmp(expected, Out.ptr)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080098 szReturn = "CopyOut";
99 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800100
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800101Done:
102 return szReturn;
103}
104
105
106/*
107 Append test utility.
108 pUOB is the buffer to append too
109 num is the amount to append
110 expected is the expected return code, 0 or 1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800111
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800112 returns 0 if test passed
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800113
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800114 */
115static int AppendTest(UsefulOutBuf *pUOB, size_t num, int expected)
116{
117 //reset
118 UsefulOutBuf_Reset(pUOB);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800119
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800120 // check status first
121 if(UsefulOutBuf_GetError(pUOB))
122 return 1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800123
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800124 // append the bytes
125 UsefulOutBuf_AppendData(pUOB, (const uint8_t *)"bluster", num);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800126
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800127 // check error status after
128 if(UsefulOutBuf_GetError(pUOB) != expected)
129 return 1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800130
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800131 return 0;
132}
133
134
135/*
136 Same as append, but takes a position param too
137 */
138static int InsertTest(UsefulOutBuf *pUOB, size_t num, size_t pos, int expected)
139{
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530140 // reset
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800141 UsefulOutBuf_Reset(pUOB);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800142
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800143 // check
144 if(UsefulOutBuf_GetError(pUOB))
145 return 1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800146
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800147 UsefulOutBuf_InsertData(pUOB, (const uint8_t *)"bluster", num, pos);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800148
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800149 if(UsefulOutBuf_GetError(pUOB) != expected)
150 return 1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800151
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800152 return 0;
153}
154
155
156/*
157 Boundary conditions to test
158 - around 0
159 - around the buffer size
160 - around MAX size_t
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800161
162
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800163 Test these for the buffer size and the cursor, the insert amount, the append amount and the insert position
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800164
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800165 */
166
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800167const char *UOBTest_BoundaryConditionsTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800168{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530169 UsefulBuf_MAKE_STACK_UB(outbuf,2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800170
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800171 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800172
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530173 UsefulOutBuf_Init(&UOB, outbuf);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800174
175 // append 0 byte to a 2 byte buffer --> success
176 if(AppendTest(&UOB, 0, 0))
177 return "Append 0 bytes failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800178
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800179 // append 1 byte to a 2 byte buffer --> success
180 if(AppendTest(&UOB, 1, 0))
181 return "Append of 1 byte failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800182
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800183 // append 2 byte to a 2 byte buffer --> success
184 if(AppendTest(&UOB, 2, 0))
185 return "Append to fill buffer failed";
186
187 // append 3 bytes to a 2 byte buffer --> failure
188 if(AppendTest(&UOB, 3, 1))
189 return "Overflow of buffer not caught";
190
191 // append max size_t to a 2 byte buffer --> failure
192 if(AppendTest(&UOB, SIZE_MAX, 1))
193 return "Append of SIZE_MAX error not caught";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800194
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800195 if(InsertTest(&UOB, 1, 0, 0))
196 return "Insert 1 byte at start failed";
197
198 if(InsertTest(&UOB, 2, 0, 0))
199 return "Insert 2 bytes at start failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800200
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800201 if(InsertTest(&UOB, 3, 0, 1))
202 return "Insert overflow not caught";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800203
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800204 if(InsertTest(&UOB, 1, 1, 1))
205 return "Bad insertion point not caught";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800206
207
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530208 UsefulBuf_MAKE_STACK_UB(outBuf2,10);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800209
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530210 UsefulOutBuf_Init(&UOB, outBuf2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800211
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800212 UsefulOutBuf_Reset(&UOB);
213 // put data in the buffer
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800214 UsefulOutBuf_AppendString(&UOB, "abc123");
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800215
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800216 UsefulOutBuf_InsertString(&UOB, "xyz*&^", 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800217
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800218 if(!UsefulOutBuf_GetError(&UOB)) {
219 return "insert with data should have failed";
220 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800221
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800222
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530223 UsefulOutBuf_Init(&UOB, (UsefulBuf){NULL, SIZE_MAX - 5});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800224 UsefulOutBuf_AppendData(&UOB, "123456789", SIZE_MAX -6);
225 if(UsefulOutBuf_GetError(&UOB)) {
226 return "insert in huge should have succeeded";
227 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800228
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530229 UsefulOutBuf_Init(&UOB, (UsefulBuf){NULL, SIZE_MAX - 5});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800230 UsefulOutBuf_AppendData(&UOB, "123456789", SIZE_MAX -5);
231 if(UsefulOutBuf_GetError(&UOB)) {
232 return "insert in huge should have succeeded";
233 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800234
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530235 UsefulOutBuf_Init(&UOB, (UsefulBuf){NULL, SIZE_MAX - 5});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800236 UsefulOutBuf_AppendData(&UOB, "123456789", SIZE_MAX - 4);
237 if(!UsefulOutBuf_GetError(&UOB)) {
238 return "lengths near max size";
239 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800240
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +0300241 UsefulOutBuf_Init(&UOB, (UsefulBuf){NULL, 100});
242 if(!UsefulOutBuf_IsBufferNULL(&UOB)) {
243 return "NULL check failed";
244 }
245
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800246 return NULL;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800247}
248
249
250
251
252
253// Test function to get size and magic number check
254
255const char *TestBasicSanity()
256{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530257 UsefulBuf_MAKE_STACK_UB(outbuf,10);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800258
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800259 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800260
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800261 // First -- make sure that the room left function returns the right amount
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530262 UsefulOutBuf_Init(&UOB, outbuf);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800263
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530264 if(UsefulOutBuf_RoomLeft(&UOB) != 10)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800265 return "room left failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800266
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800267 if(!UsefulOutBuf_WillItFit(&UOB, 9)) {
268 return "it did not fit";
269 }
270
271 if(UsefulOutBuf_WillItFit(&UOB, 11)) {
272 return "it should have not fit";
273 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800274
275
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800276 // Next -- make sure that the magic number checking is working right
277 UOB.magic = 8888; // make magic bogus
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800278
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800279 UsefulOutBuf_AppendData(&UOB, (const uint8_t *)"bluster", 7);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800280
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800281 if(!UsefulOutBuf_GetError(&UOB))
282 return "magic corruption check failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800283
284
285
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800286 // Next make sure that the valid data length check is working right
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530287 UsefulOutBuf_Init(&UOB, outbuf);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800288
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530289 UOB.data_len = UOB.UB.len+1; // make size bogus
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800290
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800291 UsefulOutBuf_AppendData(&UOB, (const uint8_t *)"bluster", 7);
292 if(!UsefulOutBuf_GetError(&UOB))
293 return "valid data check failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800294
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800295 return NULL;
296}
297
298
299
300const char *UBMacroConversionsTest()
301{
302 char *szFoo = "foo";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800303
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530304 UsefulBufC Foo = UsefulBuf_FromSZ(szFoo);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800305 if(Foo.len != 3 || strncmp(Foo.ptr, szFoo, 3))
306 return "SZToUsefulBufC failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800307
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530308 UsefulBufC Too = UsefulBuf_FROM_SZ_LITERAL("Toooo");
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800309 if(Too.len != 5 || strncmp(Too.ptr, "Toooo", 5))
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530310 return "UsefulBuf_FROM_SZ_LITERAL failed";
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800311
312 uint8_t pB[] = {0x42, 0x6f, 0x6f};
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530313 UsefulBufC Boo = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pB);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800314 if(Boo.len != 3 || strncmp(Boo.ptr, "Boo", 3))
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530315 return "UsefulBuf_FROM_BYTE_ARRAY_LITERAL failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800316
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800317 char *sz = "not const"; // some data for the test
318 UsefulBuf B = (UsefulBuf){sz, sizeof(sz)};
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530319 UsefulBufC BC = UsefulBuf_Const(B);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800320 if(BC.len != sizeof(sz) || BC.ptr != sz)
321 return "UsefulBufConst failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800322
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800323 return NULL;
324}
325
326
327const char *UBUtilTests()
328{
329 UsefulBuf UB = NULLUsefulBuf;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800330
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800331 if(!UsefulBuf_IsNULL(UB)){
332 return "IsNull failed";
333 }
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530334
335 if(!UsefulBuf_IsEmpty(UB)){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800336 return "IsEmpty failed";
337 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800338
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530339 if(!UsefulBuf_IsNULLOrEmpty(UB)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800340 return "IsNULLOrEmpty failed";
341 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800342
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800343 const UsefulBufC UBC = UsefulBuf_Const(UB);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800344
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530345 if(!UsefulBuf_IsNULLC(UBC)){
346 return "IsNull const failed";
347 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800348
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530349 if(!UsefulBuf_IsEmptyC(UBC)){
350 return "IsEmptyC failed";
351 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800352
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530353 if(!UsefulBuf_IsNULLOrEmptyC(UBC)){
354 return "IsNULLOrEmptyC failed";
355 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800356
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800357 const UsefulBuf UB2 = UsefulBuf_Unconst(UBC);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530358 if(!UsefulBuf_IsEmpty(UB2)) {
359 return "Back to UB is Empty failed";
360 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800361
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800362 UB.ptr = "x"; // just some valid pointer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800363
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800364 if(UsefulBuf_IsNULL(UB)){
365 return "IsNull failed";
366 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800367
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530368 if(!UsefulBuf_IsEmptyC(UBC)){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800369 return "IsEmpty failed";
370 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800371
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800372 // test the Unconst.
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530373 if(UsefulBuf_Unconst(UBC).ptr != NULL) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800374 return "Unconst failed";
375 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800376
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800377 // Set 100 bytes of '+'; validated a few tests later
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530378 UsefulBuf_MAKE_STACK_UB(Temp, 100);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800379 const UsefulBufC TempC = UsefulBuf_Set(Temp, '+');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800380
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800381 // Try to copy into a buf that is too small and see failure
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530382 UsefulBuf_MAKE_STACK_UB(Temp2, 99);
Laurence Lundblade05ec57b2018-10-21 01:50:03 +0530383 if(!UsefulBuf_IsNULLC(UsefulBuf_Copy(Temp2, TempC))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800384 return "Copy should have failed";
385 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800386
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800387 if(UsefulBuf_IsNULLC(UsefulBuf_CopyPtr(Temp2, "xx", 2))) {
388 return "CopyPtr failed";
389 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800390
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530391 UsefulBufC xxyy = UsefulBuf_CopyOffset(Temp2, 2, UsefulBuf_FROM_SZ_LITERAL("yy"));
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800392 if(UsefulBuf_IsNULLC(xxyy)) {
393 return "CopyOffset Failed";
394 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800395
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530396 if(UsefulBuf_Compare(UsefulBuf_Head(xxyy, 3), UsefulBuf_FROM_SZ_LITERAL("xxy"))) {
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800397 return "head failed";
398 }
399
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530400 if(UsefulBuf_Compare(UsefulBuf_Tail(xxyy, 1), UsefulBuf_FROM_SZ_LITERAL("xyy"))) {
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800401 return "tail failed";
402 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800403
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800404 if(!UsefulBuf_IsNULLC(UsefulBuf_Head(xxyy, 5))) {
405 return "head should have failed";
406 }
407
408 if(!UsefulBuf_IsNULLC(UsefulBuf_Tail(xxyy, 5))) {
409 return "tail should have failed";
410 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -0800411
Laurence Lundblade7412f812019-01-01 18:49:36 -0800412 if(!UsefulBuf_IsNULLC(UsefulBuf_Tail(NULLUsefulBufC, 0))) {
413 return "tail of NULLUsefulBufC is not NULLUsefulBufC";
414 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -0800415
Laurence Lundblade7412f812019-01-01 18:49:36 -0800416 const UsefulBufC TailResult = UsefulBuf_Tail((UsefulBufC){NULL, 100}, 99);
417 if(TailResult.ptr != NULL || TailResult.len != 1) {
418 return "tail of NULL and length incorrect";
419 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800420
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530421 if(!UsefulBuf_IsNULLC(UsefulBuf_CopyOffset(Temp2, 100, UsefulBuf_FROM_SZ_LITERAL("yy")))) {
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800422 return "Copy Offset should have failed";
423 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800424
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800425 // Try to copy into a NULL/empty buf and see failure
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800426 const UsefulBuf UBNull = NULLUsefulBuf;
Laurence Lundblade05ec57b2018-10-21 01:50:03 +0530427 if(!UsefulBuf_IsNULLC(UsefulBuf_Copy(UBNull, TempC))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800428 return "Copy to NULL should have failed";
429 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800430
431
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800432 // Try to set a NULL/empty buf; nothing should happen
Laurence Lundblade05ec57b2018-10-21 01:50:03 +0530433 UsefulBuf_Set(UBNull, '+'); // This will crash on failure
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800434
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800435 // Copy successfully to a buffer
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530436 UsefulBuf_MAKE_STACK_UB(Temp3, 101);
Laurence Lundblade05ec57b2018-10-21 01:50:03 +0530437 if(UsefulBuf_IsNULLC(UsefulBuf_Copy(Temp3, TempC))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800438 return "Copy should not have failed";
439 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800440
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800441 static const uint8_t pExpected[] = {
442 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
443 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
444 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
445 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
446 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
447 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
448 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
449 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
450 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
451 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
452 };
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530453 UsefulBufC Expected = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpected);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800454 // This validates comparison for equality and the UsefulBuf_Set
Laurence Lundblade05ec57b2018-10-21 01:50:03 +0530455 if(UsefulBuf_Compare(Expected, TempC)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800456 return "Set / Copy / Compare failed";
457 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800458
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800459 // Compare two empties and expect success
460 if(UsefulBuf_Compare(NULLUsefulBufC, NULLUsefulBufC)){
461 return "Compare Empties failed";
462 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800463
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800464 // Compare with empty and expect the first to be larger
465 if(UsefulBuf_Compare(Expected, NULLUsefulBufC) <= 0){
466 return "Compare with empty failed";
467 }
468
469
470 static const uint8_t pExpectedBigger[] = {
471 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
472 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
473 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
474 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
475 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
476 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
477 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
478 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
479 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
480 '+', '+', '+', '+', '+', '+', '+', '+', '+', ',',
481 };
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800482 const UsefulBufC ExpectedBigger = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedBigger);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800483
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530484 // Expect -1 when the first arg is smaller
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800485 if(UsefulBuf_Compare(Expected, ExpectedBigger) >= 0){
486 return "Compare with bigger";
487 }
488
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800489
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800490 static const uint8_t pExpectedSmaller[] = {
491 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
492 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
493 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
494 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
495 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
496 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
497 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
498 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
499 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
500 '+', '+', '+', '+', '+', '+', '+', '+', '+', '*',
501 };
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800502 const UsefulBufC ExpectedSmaller = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedSmaller);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530503 // Expect +1 when the first arg is larger
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800504 if(UsefulBuf_Compare(Expected, ExpectedSmaller) <= 0){
505 return "Compare with smaller";
506 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800507
508
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800509 static const uint8_t pExpectedLonger[] = {
510 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
511 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
512 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
513 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
514 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
515 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
516 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
517 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
518 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
519 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+'
520 };
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800521 const UsefulBufC ExpectedLonger = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedLonger);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800522
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530523 // Expect -1 when the first arg is smaller
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800524 if(UsefulBuf_Compare(Expected, ExpectedLonger) >= 0){
525 return "Compare with longer";
526 }
527
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800528
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800529 static const uint8_t pExpectedShorter[] = {
530 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
531 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
532 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
533 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
534 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
535 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
536 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
537 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
538 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
539 '+', '+', '+', '+', '+', '+', '+', '+', '+',
540 };
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800541 const UsefulBufC ExpectedShorter = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedShorter);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800542 // Expect +1 with the first arg is larger
543 if(UsefulBuf_Compare(Expected, ExpectedShorter) <= 0){
544 return "Compare with shorter";
545 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800546
547
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530548 if(UsefulBuf_IsNULLC(UsefulBuf_Copy(Temp, NULLUsefulBufC))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800549 return "Copy null/empty failed";
550 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800551
Laurence Lundbladed5e101e2019-03-06 17:23:18 -0800552 if(UsefulBuf_IsValue(ExpectedShorter, '+') != SIZE_MAX) {
553 return "IsValue failed to match all";
554 }
555
556 if(UsefulBuf_IsValue(ExpectedShorter, '-') != 0) {
557 return "IsValue should have failed right away";
558 }
559
560 if(UsefulBuf_IsValue(NULLUsefulBufC, 0x00) != 0) {
561 return "IsValue failed on NULLUsefulBufC";
562 }
563
564 if(UsefulBuf_IsValue((UsefulBufC){(uint8_t[]){0x00}, 1}, 0x00) != SIZE_MAX) {
565 return "IsValue failed finding 0 in one byte of 0";
566 }
567
568 if(UsefulBuf_IsValue((UsefulBufC){(uint8_t[]){0x00}, 1}, 0x01) != 0) {
569 return "IsValue failed not finding 1 in one byte of 0";
570 }
571
572 if(UsefulBuf_IsValue(ExpectedSmaller, '+') != ExpectedSmaller.len -1) {
573 return "IsValue failed to find final *";
574 }
575
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800576 // Look for +++++... in +++++... and find it at the beginning
577 if(0 != UsefulBuf_FindBytes(ExpectedLonger, ExpectedShorter)){
578 return "Failed to find";
579 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800580
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800581 // look for ++* in ....++* and find it at the end
582 static const uint8_t pToFind[] = {'+', '+', '*'};
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800583 const UsefulBufC ToBeFound = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pToFind);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800584
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800585 if(97 != UsefulBuf_FindBytes(ExpectedSmaller, ToBeFound)){
586 return "Failed to find 2";
587 }
588
589 // look for ++* in ....++, and find it near the end
590 if(SIZE_MAX != UsefulBuf_FindBytes(ExpectedBigger, ToBeFound)){
591 return "Failed to not find";
592 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800593
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800594 // Look for the whole buffer in itself and succeed.
595 if(0 != UsefulBuf_FindBytes(ExpectedLonger, ExpectedLonger)){
596 return "Failed to find 3";
597 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800598
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800599 return NULL;
600}
601
602
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800603const char * UIBTest_IntegerFormat()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800604{
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530605 UsefulOutBuf_MakeOnStack(UOB,100);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800606
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800607 const uint32_t u32 = 0x0A0B0C0D; // from https://en.wikipedia.org/wiki/Endianness
608 const uint64_t u64 = 1984738472938472;
609 const uint16_t u16 = 40000;
610 const uint8_t u8 = 9;
611 const float f = (float)314.15;
612 const double d = 2.1e10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800613
614
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530615 UsefulOutBuf_AppendUint32(&UOB, u32); // Also tests UsefulOutBuf_InsertUint64 and UsefulOutBuf_GetEndPosition
616 UsefulOutBuf_AppendUint64(&UOB, u64); // Also tests UsefulOutBuf_InsertUint32
617 UsefulOutBuf_AppendUint16(&UOB, u16); // Also tests UsefulOutBuf_InsertUint16
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800618 UsefulOutBuf_AppendByte(&UOB, u8);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530619 UsefulOutBuf_AppendFloat(&UOB, f); // Also tests UsefulOutBuf_InsertFloat
620 UsefulOutBuf_AppendDouble(&UOB, d); // Also tests UsefulOutBuf_InsertDouble
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800621
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800622 const UsefulBufC O = UsefulOutBuf_OutUBuf(&UOB);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530623 if(UsefulBuf_IsNULLC(O))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800624 return "Couldn't output integers";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800625
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800626 // from https://en.wikipedia.org/wiki/Endianness
627 const uint8_t pExpectedNetworkOrder[4] = {0x0A, 0x0B, 0x0C, 0x0D};
628 if(memcmp(O.ptr, pExpectedNetworkOrder, 4)) {
629 return "not in network order";
630 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800631
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800632 UsefulInputBuf UIB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800633
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530634 UsefulInputBuf_Init(&UIB, O);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800635
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530636 if(UsefulInputBuf_Tell(&UIB) != 0) {
637 return "UsefulInputBuf_Tell failed";
638 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800639
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800640 if(UsefulInputBuf_GetUint32(&UIB) != u32) {
641 return "u32 out then in failed";
642 }
643 if(UsefulInputBuf_GetUint64(&UIB) != u64) {
644 return "u64 out then in failed";
645 }
646 if(UsefulInputBuf_GetUint16(&UIB) != u16) {
647 return "u16 out then in failed";
648 }
649 if(UsefulInputBuf_GetByte(&UIB) != u8) {
650 return "u8 out then in failed";
651 }
652 if(UsefulInputBuf_GetFloat(&UIB) != f) {
653 return "float out then in failed";
654 }
655 if(UsefulInputBuf_GetDouble(&UIB) != d) {
656 return "double out then in failed";
657 }
658
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800659 // Reset and go again for a few more tests
660 UsefulInputBuf_Init(&UIB, O);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800661
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800662 const UsefulBufC Four = UsefulInputBuf_GetUsefulBuf(&UIB, 4);
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800663 if(UsefulBuf_IsNULLC(Four)) {
664 return "Four is NULL";
665 }
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530666 if(UsefulBuf_Compare(Four, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedNetworkOrder))) {
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800667 return "Four compare failed";
668 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800669
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800670 if(UsefulInputBuf_BytesUnconsumed(&UIB) != 23){
671 return "Wrong number of unconsumed bytes";
672 }
673
674 if(!UsefulInputBuf_BytesAvailable(&UIB, 23)){
675 return "Wrong number of bytes available I";
676 }
677
678 if(UsefulInputBuf_BytesAvailable(&UIB, 24)){
679 return "Wrong number of bytes available II";
680 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800681
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800682 UsefulInputBuf_Seek(&UIB, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800683
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800684 if(UsefulInputBuf_GetError(&UIB)) {
685 return "unexpected error after seek";
686 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800687
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800688 const uint8_t *pGetBytes = (const uint8_t *)UsefulInputBuf_GetBytes(&UIB, 4);
689 if(pGetBytes == NULL) {
690 return "GetBytes returns NULL";
691 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800692
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800693 if(memcmp(pGetBytes, pExpectedNetworkOrder, 4)) {
694 return "Got wrong bytes";
695 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800696
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800697 UsefulInputBuf_Seek(&UIB, 28);
698
699 if(!UsefulInputBuf_GetError(&UIB)) {
700 return "expected error after seek";
701 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800702
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530703 return NULL;
704}
705
706
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800707const char *UBUTest_CopyUtil()
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530708{
709 if(UsefulBufUtil_CopyFloatToUint32(65536.0F) != 0x47800000) {
710 return "CopyFloatToUint32 failed";
711 }
712
713 if(UsefulBufUtil_CopyDoubleToUint64(4e-40F) != 0X37C16C2800000000ULL) {
714 return "CopyDoubleToUint64 failed";
715 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800716
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530717 if(UsefulBufUtil_CopyUint64ToDouble(0X37C16C2800000000ULL) != 4e-40F) {
718 return "CopyUint64ToDouble failed";
719 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800720
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530721 if(UsefulBufUtil_CopyUint32ToFloat(0x47800000) != 65536.0F) {
722 return "CopyUint32ToFloat failed";
723 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800724
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800725 return NULL;
726}
727
728
729