blob: d8ccbac5fcaf694dfef378a49ea2e1802fa54360 [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 Lundbladeee851742020-01-08 08:37:05 -08003 Copyright (c) 2018-2020, 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 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 Lundbladeee851742020-01-08 08:37:05 -080082 // Make it a null terminated string (because all the appends and
83 // inserts above not strcpy !)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080084 UsefulOutBuf_AppendByte(&UOB, '\0');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080085
86
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053087 UsefulBufC U = UsefulOutBuf_OutUBuf(&UOB);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080088
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080089 const char *expected = "heffalump unbounce bluster hunny";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080090
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053091 if(UsefulBuf_IsNULLC(U) || U.len-1 != strlen(expected) || strcmp(expected, U.ptr) || UsefulOutBuf_GetError(&UOB)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080092 szReturn = "OutUBuf";
93 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080094
Laurence Lundblade4fe9f312018-10-22 10:22:39 +053095 UsefulBuf_MAKE_STACK_UB(buf, 50);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053096 UsefulBufC Out = UsefulOutBuf_CopyOut(&UOB, buf);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080097
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053098 if(UsefulBuf_IsNULLC(Out) || Out.len-1 != strlen(expected) || strcmp(expected, Out.ptr)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080099 szReturn = "CopyOut";
100 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800101
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800102Done:
103 return szReturn;
104}
105
106
107/*
108 Append test utility.
109 pUOB is the buffer to append too
110 num is the amount to append
111 expected is the expected return code, 0 or 1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800112
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800113 returns 0 if test passed
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800114
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800115 */
116static int AppendTest(UsefulOutBuf *pUOB, size_t num, int expected)
117{
118 //reset
119 UsefulOutBuf_Reset(pUOB);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800120
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800121 // check status first
122 if(UsefulOutBuf_GetError(pUOB))
123 return 1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800124
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800125 // append the bytes
126 UsefulOutBuf_AppendData(pUOB, (const uint8_t *)"bluster", num);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800127
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800128 // check error status after
129 if(UsefulOutBuf_GetError(pUOB) != expected)
130 return 1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800131
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800132 return 0;
133}
134
135
136/*
137 Same as append, but takes a position param too
138 */
139static int InsertTest(UsefulOutBuf *pUOB, size_t num, size_t pos, int expected)
140{
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530141 // reset
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800142 UsefulOutBuf_Reset(pUOB);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800143
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800144 // check
145 if(UsefulOutBuf_GetError(pUOB))
146 return 1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800147
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800148 UsefulOutBuf_InsertData(pUOB, (const uint8_t *)"bluster", num, pos);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800149
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800150 if(UsefulOutBuf_GetError(pUOB) != expected)
151 return 1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800152
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800153 return 0;
154}
155
156
157/*
158 Boundary conditions to test
159 - around 0
160 - around the buffer size
161 - around MAX size_t
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800162
163
Laurence Lundbladeee851742020-01-08 08:37:05 -0800164 Test these for the buffer size and the cursor, the insert amount, the
165 append amount and the insert position
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800166
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800167 */
168
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800169const char *UOBTest_BoundaryConditionsTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800170{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530171 UsefulBuf_MAKE_STACK_UB(outbuf,2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800172
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800173 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800174
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530175 UsefulOutBuf_Init(&UOB, outbuf);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800176
177 // append 0 byte to a 2 byte buffer --> success
178 if(AppendTest(&UOB, 0, 0))
179 return "Append 0 bytes failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800180
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800181 // append 1 byte to a 2 byte buffer --> success
182 if(AppendTest(&UOB, 1, 0))
183 return "Append of 1 byte failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800184
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800185 // append 2 byte to a 2 byte buffer --> success
186 if(AppendTest(&UOB, 2, 0))
187 return "Append to fill buffer failed";
188
189 // append 3 bytes to a 2 byte buffer --> failure
190 if(AppendTest(&UOB, 3, 1))
191 return "Overflow of buffer not caught";
192
193 // append max size_t to a 2 byte buffer --> failure
194 if(AppendTest(&UOB, SIZE_MAX, 1))
195 return "Append of SIZE_MAX error not caught";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800196
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800197 if(InsertTest(&UOB, 1, 0, 0))
198 return "Insert 1 byte at start failed";
199
200 if(InsertTest(&UOB, 2, 0, 0))
201 return "Insert 2 bytes at start failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800202
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800203 if(InsertTest(&UOB, 3, 0, 1))
204 return "Insert overflow not caught";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800205
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800206 if(InsertTest(&UOB, 1, 1, 1))
207 return "Bad insertion point not caught";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800208
209
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530210 UsefulBuf_MAKE_STACK_UB(outBuf2,10);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800211
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530212 UsefulOutBuf_Init(&UOB, outBuf2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800213
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800214 UsefulOutBuf_Reset(&UOB);
215 // put data in the buffer
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800216 UsefulOutBuf_AppendString(&UOB, "abc123");
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800217
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800218 UsefulOutBuf_InsertString(&UOB, "xyz*&^", 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800219
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800220 if(!UsefulOutBuf_GetError(&UOB)) {
221 return "insert with data should have failed";
222 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800223
Laurence Lundblade2ded3d92018-10-09 21:36:11 +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 -6);
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 -5);
233 if(UsefulOutBuf_GetError(&UOB)) {
234 return "insert in huge should have succeeded";
235 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800236
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530237 UsefulOutBuf_Init(&UOB, (UsefulBuf){NULL, SIZE_MAX - 5});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800238 UsefulOutBuf_AppendData(&UOB, "123456789", SIZE_MAX - 4);
239 if(!UsefulOutBuf_GetError(&UOB)) {
240 return "lengths near max size";
241 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800242
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +0300243 UsefulOutBuf_Init(&UOB, (UsefulBuf){NULL, 100});
244 if(!UsefulOutBuf_IsBufferNULL(&UOB)) {
245 return "NULL check failed";
246 }
247
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800248 return NULL;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800249}
250
251
252
253
254
255// Test function to get size and magic number check
256
257const char *TestBasicSanity()
258{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530259 UsefulBuf_MAKE_STACK_UB(outbuf,10);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800260
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800261 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800262
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800263 // First -- make sure that the room left function returns the right amount
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530264 UsefulOutBuf_Init(&UOB, outbuf);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800265
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530266 if(UsefulOutBuf_RoomLeft(&UOB) != 10)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800267 return "room left failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800268
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800269 if(!UsefulOutBuf_WillItFit(&UOB, 9)) {
270 return "it did not fit";
271 }
272
273 if(UsefulOutBuf_WillItFit(&UOB, 11)) {
274 return "it should have not fit";
275 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800276
277
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800278 // Next -- make sure that the magic number checking is working right
279 UOB.magic = 8888; // make magic bogus
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800280
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800281 UsefulOutBuf_AppendData(&UOB, (const uint8_t *)"bluster", 7);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800282
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800283 if(!UsefulOutBuf_GetError(&UOB))
284 return "magic corruption check failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800285
286
287
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800288 // Next make sure that the valid data length check is working right
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530289 UsefulOutBuf_Init(&UOB, outbuf);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800290
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530291 UOB.data_len = UOB.UB.len+1; // make size bogus
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800292
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800293 UsefulOutBuf_AppendData(&UOB, (const uint8_t *)"bluster", 7);
294 if(!UsefulOutBuf_GetError(&UOB))
295 return "valid data check failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800296
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800297 return NULL;
298}
299
300
301
302const char *UBMacroConversionsTest()
303{
304 char *szFoo = "foo";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800305
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530306 UsefulBufC Foo = UsefulBuf_FromSZ(szFoo);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800307 if(Foo.len != 3 || strncmp(Foo.ptr, szFoo, 3))
308 return "SZToUsefulBufC failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800309
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530310 UsefulBufC Too = UsefulBuf_FROM_SZ_LITERAL("Toooo");
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800311 if(Too.len != 5 || strncmp(Too.ptr, "Toooo", 5))
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530312 return "UsefulBuf_FROM_SZ_LITERAL failed";
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800313
314 uint8_t pB[] = {0x42, 0x6f, 0x6f};
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530315 UsefulBufC Boo = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pB);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800316 if(Boo.len != 3 || strncmp(Boo.ptr, "Boo", 3))
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530317 return "UsefulBuf_FROM_BYTE_ARRAY_LITERAL failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800318
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800319 char *sz = "not const"; // some data for the test
320 UsefulBuf B = (UsefulBuf){sz, sizeof(sz)};
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530321 UsefulBufC BC = UsefulBuf_Const(B);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800322 if(BC.len != sizeof(sz) || BC.ptr != sz)
323 return "UsefulBufConst failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800324
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800325 return NULL;
326}
327
328
329const char *UBUtilTests()
330{
331 UsefulBuf UB = NULLUsefulBuf;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800332
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800333 if(!UsefulBuf_IsNULL(UB)){
334 return "IsNull failed";
335 }
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530336
337 if(!UsefulBuf_IsEmpty(UB)){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800338 return "IsEmpty failed";
339 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800340
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530341 if(!UsefulBuf_IsNULLOrEmpty(UB)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800342 return "IsNULLOrEmpty failed";
343 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800344
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800345 const UsefulBufC UBC = UsefulBuf_Const(UB);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800346
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530347 if(!UsefulBuf_IsNULLC(UBC)){
348 return "IsNull const failed";
349 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800350
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530351 if(!UsefulBuf_IsEmptyC(UBC)){
352 return "IsEmptyC failed";
353 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800354
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530355 if(!UsefulBuf_IsNULLOrEmptyC(UBC)){
356 return "IsNULLOrEmptyC failed";
357 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800358
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800359 const UsefulBuf UB2 = UsefulBuf_Unconst(UBC);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530360 if(!UsefulBuf_IsEmpty(UB2)) {
361 return "Back to UB is Empty failed";
362 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800363
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800364 UB.ptr = "x"; // just some valid pointer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800365
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800366 if(UsefulBuf_IsNULL(UB)){
367 return "IsNull failed";
368 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800369
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530370 if(!UsefulBuf_IsEmptyC(UBC)){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800371 return "IsEmpty failed";
372 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800373
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800374 // test the Unconst.
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530375 if(UsefulBuf_Unconst(UBC).ptr != NULL) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800376 return "Unconst failed";
377 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800378
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800379 // Set 100 bytes of '+'; validated a few tests later
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530380 UsefulBuf_MAKE_STACK_UB(Temp, 100);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800381 const UsefulBufC TempC = UsefulBuf_Set(Temp, '+');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800382
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800383 // Try to copy into a buf that is too small and see failure
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530384 UsefulBuf_MAKE_STACK_UB(Temp2, 99);
Laurence Lundblade05ec57b2018-10-21 01:50:03 +0530385 if(!UsefulBuf_IsNULLC(UsefulBuf_Copy(Temp2, TempC))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800386 return "Copy should have failed";
387 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800388
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800389 if(UsefulBuf_IsNULLC(UsefulBuf_CopyPtr(Temp2, "xx", 2))) {
390 return "CopyPtr failed";
391 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800392
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530393 UsefulBufC xxyy = UsefulBuf_CopyOffset(Temp2, 2, UsefulBuf_FROM_SZ_LITERAL("yy"));
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800394 if(UsefulBuf_IsNULLC(xxyy)) {
395 return "CopyOffset Failed";
396 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800397
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530398 if(UsefulBuf_Compare(UsefulBuf_Head(xxyy, 3), UsefulBuf_FROM_SZ_LITERAL("xxy"))) {
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800399 return "head failed";
400 }
401
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530402 if(UsefulBuf_Compare(UsefulBuf_Tail(xxyy, 1), UsefulBuf_FROM_SZ_LITERAL("xyy"))) {
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800403 return "tail failed";
404 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800405
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800406 if(!UsefulBuf_IsNULLC(UsefulBuf_Head(xxyy, 5))) {
407 return "head should have failed";
408 }
409
410 if(!UsefulBuf_IsNULLC(UsefulBuf_Tail(xxyy, 5))) {
411 return "tail should have failed";
412 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -0800413
Laurence Lundblade7412f812019-01-01 18:49:36 -0800414 if(!UsefulBuf_IsNULLC(UsefulBuf_Tail(NULLUsefulBufC, 0))) {
415 return "tail of NULLUsefulBufC is not NULLUsefulBufC";
416 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -0800417
Laurence Lundblade7412f812019-01-01 18:49:36 -0800418 const UsefulBufC TailResult = UsefulBuf_Tail((UsefulBufC){NULL, 100}, 99);
419 if(TailResult.ptr != NULL || TailResult.len != 1) {
420 return "tail of NULL and length incorrect";
421 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800422
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530423 if(!UsefulBuf_IsNULLC(UsefulBuf_CopyOffset(Temp2, 100, UsefulBuf_FROM_SZ_LITERAL("yy")))) {
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800424 return "Copy Offset should have failed";
425 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800426
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800427 // Try to copy into a NULL/empty buf and see failure
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800428 const UsefulBuf UBNull = NULLUsefulBuf;
Laurence Lundblade05ec57b2018-10-21 01:50:03 +0530429 if(!UsefulBuf_IsNULLC(UsefulBuf_Copy(UBNull, TempC))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800430 return "Copy to NULL should have failed";
431 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800432
433
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800434 // Try to set a NULL/empty buf; nothing should happen
Laurence Lundblade05ec57b2018-10-21 01:50:03 +0530435 UsefulBuf_Set(UBNull, '+'); // This will crash on failure
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800436
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800437 // Copy successfully to a buffer
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530438 UsefulBuf_MAKE_STACK_UB(Temp3, 101);
Laurence Lundblade05ec57b2018-10-21 01:50:03 +0530439 if(UsefulBuf_IsNULLC(UsefulBuf_Copy(Temp3, TempC))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800440 return "Copy should not have failed";
441 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800442
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800443 static const uint8_t pExpected[] = {
444 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
445 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
446 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
447 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
448 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
449 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
450 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
451 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
452 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
453 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
454 };
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530455 UsefulBufC Expected = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpected);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800456 // This validates comparison for equality and the UsefulBuf_Set
Laurence Lundblade05ec57b2018-10-21 01:50:03 +0530457 if(UsefulBuf_Compare(Expected, TempC)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800458 return "Set / Copy / Compare failed";
459 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800460
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800461 // Compare two empties and expect success
462 if(UsefulBuf_Compare(NULLUsefulBufC, NULLUsefulBufC)){
463 return "Compare Empties failed";
464 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800465
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800466 // Compare with empty and expect the first to be larger
467 if(UsefulBuf_Compare(Expected, NULLUsefulBufC) <= 0){
468 return "Compare with empty failed";
469 }
470
471
472 static const uint8_t pExpectedBigger[] = {
473 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
474 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
475 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
476 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
477 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
478 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
479 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
480 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
481 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
482 '+', '+', '+', '+', '+', '+', '+', '+', '+', ',',
483 };
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800484 const UsefulBufC ExpectedBigger = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedBigger);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800485
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530486 // Expect -1 when the first arg is smaller
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800487 if(UsefulBuf_Compare(Expected, ExpectedBigger) >= 0){
488 return "Compare with bigger";
489 }
490
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800491
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800492 static const uint8_t pExpectedSmaller[] = {
493 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
494 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
495 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
496 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
497 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
498 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
499 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
500 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
501 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
502 '+', '+', '+', '+', '+', '+', '+', '+', '+', '*',
503 };
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800504 const UsefulBufC ExpectedSmaller = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedSmaller);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530505 // Expect +1 when the first arg is larger
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800506 if(UsefulBuf_Compare(Expected, ExpectedSmaller) <= 0){
507 return "Compare with smaller";
508 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800509
510
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800511 static const uint8_t pExpectedLonger[] = {
512 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
513 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
514 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
515 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
516 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
517 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
518 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
519 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
520 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
521 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+'
522 };
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800523 const UsefulBufC ExpectedLonger = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedLonger);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800524
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530525 // Expect -1 when the first arg is smaller
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800526 if(UsefulBuf_Compare(Expected, ExpectedLonger) >= 0){
527 return "Compare with longer";
528 }
529
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800530
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800531 static const uint8_t pExpectedShorter[] = {
532 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
533 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
534 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
535 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
536 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
537 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
538 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
539 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
540 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
541 '+', '+', '+', '+', '+', '+', '+', '+', '+',
542 };
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800543 const UsefulBufC ExpectedShorter = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedShorter);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800544 // Expect +1 with the first arg is larger
545 if(UsefulBuf_Compare(Expected, ExpectedShorter) <= 0){
546 return "Compare with shorter";
547 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800548
549
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530550 if(UsefulBuf_IsNULLC(UsefulBuf_Copy(Temp, NULLUsefulBufC))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800551 return "Copy null/empty failed";
552 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800553
Laurence Lundbladed5e101e2019-03-06 17:23:18 -0800554 if(UsefulBuf_IsValue(ExpectedShorter, '+') != SIZE_MAX) {
555 return "IsValue failed to match all";
556 }
557
558 if(UsefulBuf_IsValue(ExpectedShorter, '-') != 0) {
559 return "IsValue should have failed right away";
560 }
561
562 if(UsefulBuf_IsValue(NULLUsefulBufC, 0x00) != 0) {
563 return "IsValue failed on NULLUsefulBufC";
564 }
565
566 if(UsefulBuf_IsValue((UsefulBufC){(uint8_t[]){0x00}, 1}, 0x00) != SIZE_MAX) {
567 return "IsValue failed finding 0 in one byte of 0";
568 }
569
570 if(UsefulBuf_IsValue((UsefulBufC){(uint8_t[]){0x00}, 1}, 0x01) != 0) {
571 return "IsValue failed not finding 1 in one byte of 0";
572 }
573
574 if(UsefulBuf_IsValue(ExpectedSmaller, '+') != ExpectedSmaller.len -1) {
575 return "IsValue failed to find final *";
576 }
577
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800578 // Look for +++++... in +++++... and find it at the beginning
579 if(0 != UsefulBuf_FindBytes(ExpectedLonger, ExpectedShorter)){
580 return "Failed to find";
581 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800582
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800583 // look for ++* in ....++* and find it at the end
584 static const uint8_t pToFind[] = {'+', '+', '*'};
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800585 const UsefulBufC ToBeFound = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pToFind);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800586
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800587 if(97 != UsefulBuf_FindBytes(ExpectedSmaller, ToBeFound)){
588 return "Failed to find 2";
589 }
590
591 // look for ++* in ....++, and find it near the end
592 if(SIZE_MAX != UsefulBuf_FindBytes(ExpectedBigger, ToBeFound)){
593 return "Failed to not find";
594 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800595
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800596 // Look for the whole buffer in itself and succeed.
597 if(0 != UsefulBuf_FindBytes(ExpectedLonger, ExpectedLonger)){
598 return "Failed to find 3";
599 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800600
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800601 return NULL;
602}
603
604
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800605const char * UIBTest_IntegerFormat()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800606{
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530607 UsefulOutBuf_MakeOnStack(UOB,100);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800608
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800609 const uint32_t u32 = 0x0A0B0C0D; // from https://en.wikipedia.org/wiki/Endianness
610 const uint64_t u64 = 1984738472938472;
611 const uint16_t u16 = 40000;
612 const uint8_t u8 = 9;
613 const float f = (float)314.15;
614 const double d = 2.1e10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800615
616
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530617 UsefulOutBuf_AppendUint32(&UOB, u32); // Also tests UsefulOutBuf_InsertUint64 and UsefulOutBuf_GetEndPosition
618 UsefulOutBuf_AppendUint64(&UOB, u64); // Also tests UsefulOutBuf_InsertUint32
619 UsefulOutBuf_AppendUint16(&UOB, u16); // Also tests UsefulOutBuf_InsertUint16
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800620 UsefulOutBuf_AppendByte(&UOB, u8);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530621 UsefulOutBuf_AppendFloat(&UOB, f); // Also tests UsefulOutBuf_InsertFloat
622 UsefulOutBuf_AppendDouble(&UOB, d); // Also tests UsefulOutBuf_InsertDouble
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800623
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800624 const UsefulBufC O = UsefulOutBuf_OutUBuf(&UOB);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530625 if(UsefulBuf_IsNULLC(O))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800626 return "Couldn't output integers";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800627
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800628 // from https://en.wikipedia.org/wiki/Endianness
629 const uint8_t pExpectedNetworkOrder[4] = {0x0A, 0x0B, 0x0C, 0x0D};
630 if(memcmp(O.ptr, pExpectedNetworkOrder, 4)) {
631 return "not in network order";
632 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800633
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800634 UsefulInputBuf UIB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800635
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530636 UsefulInputBuf_Init(&UIB, O);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800637
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530638 if(UsefulInputBuf_Tell(&UIB) != 0) {
639 return "UsefulInputBuf_Tell failed";
640 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800641
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800642 if(UsefulInputBuf_GetUint32(&UIB) != u32) {
643 return "u32 out then in failed";
644 }
645 if(UsefulInputBuf_GetUint64(&UIB) != u64) {
646 return "u64 out then in failed";
647 }
648 if(UsefulInputBuf_GetUint16(&UIB) != u16) {
649 return "u16 out then in failed";
650 }
651 if(UsefulInputBuf_GetByte(&UIB) != u8) {
652 return "u8 out then in failed";
653 }
654 if(UsefulInputBuf_GetFloat(&UIB) != f) {
655 return "float out then in failed";
656 }
657 if(UsefulInputBuf_GetDouble(&UIB) != d) {
658 return "double out then in failed";
659 }
660
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800661 // Reset and go again for a few more tests
662 UsefulInputBuf_Init(&UIB, O);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800663
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800664 const UsefulBufC Four = UsefulInputBuf_GetUsefulBuf(&UIB, 4);
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800665 if(UsefulBuf_IsNULLC(Four)) {
666 return "Four is NULL";
667 }
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530668 if(UsefulBuf_Compare(Four, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedNetworkOrder))) {
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800669 return "Four compare failed";
670 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800671
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800672 if(UsefulInputBuf_BytesUnconsumed(&UIB) != 23){
673 return "Wrong number of unconsumed bytes";
674 }
675
676 if(!UsefulInputBuf_BytesAvailable(&UIB, 23)){
677 return "Wrong number of bytes available I";
678 }
679
680 if(UsefulInputBuf_BytesAvailable(&UIB, 24)){
681 return "Wrong number of bytes available II";
682 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800683
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800684 UsefulInputBuf_Seek(&UIB, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800685
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800686 if(UsefulInputBuf_GetError(&UIB)) {
687 return "unexpected error after seek";
688 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800689
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800690 const uint8_t *pGetBytes = (const uint8_t *)UsefulInputBuf_GetBytes(&UIB, 4);
691 if(pGetBytes == NULL) {
692 return "GetBytes returns NULL";
693 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800694
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800695 if(memcmp(pGetBytes, pExpectedNetworkOrder, 4)) {
696 return "Got wrong bytes";
697 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800698
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800699 UsefulInputBuf_Seek(&UIB, 28);
700
701 if(!UsefulInputBuf_GetError(&UIB)) {
702 return "expected error after seek";
703 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800704
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530705 return NULL;
706}
707
708
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800709const char *UBUTest_CopyUtil()
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530710{
711 if(UsefulBufUtil_CopyFloatToUint32(65536.0F) != 0x47800000) {
712 return "CopyFloatToUint32 failed";
713 }
714
715 if(UsefulBufUtil_CopyDoubleToUint64(4e-40F) != 0X37C16C2800000000ULL) {
716 return "CopyDoubleToUint64 failed";
717 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800718
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530719 if(UsefulBufUtil_CopyUint64ToDouble(0X37C16C2800000000ULL) != 4e-40F) {
720 return "CopyUint64ToDouble failed";
721 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800722
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530723 if(UsefulBufUtil_CopyUint32ToFloat(0x47800000) != 65536.0F) {
724 return "CopyUint32ToFloat failed";
725 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800726
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800727 return NULL;
728}
729
730
731