blob: b6914d22ddbad3b41860d76a1717ab1844bd98e5 [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.
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02004 Copyright (c) 2021, Arm Limited.
Laurence Lundbladed92a6162018-11-01 11:38:35 +07005 All rights reserved.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08006
Laurence Lundblade0dbc9172018-11-01 14:17:21 +07007Redistribution and use in source and binary forms, with or without
8modification, are permitted provided that the following conditions are
9met:
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above
13 copyright notice, this list of conditions and the following
14 disclaimer in the documentation and/or other materials provided
15 with the distribution.
16 * Neither the name of The Linux Foundation nor the names of its
17 contributors, nor the name "Laurence Lundblade" may be used to
18 endorse or promote products derived from this software without
19 specific prior written permission.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080020
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070021THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
22WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
24ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
25BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
31IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundbladeee851742020-01-08 08:37:05 -080032 =============================================================================*/
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053033
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080034#include "UsefulBuf.h"
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080035
36
37/* Basic exercise...
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080038
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080039 Call all the main public functions.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080040
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080041 Binary compare the result to the expected.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080042
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080043 There is nothing adversarial in this test
44 */
Laurence Lundbladeb9702452021-03-08 21:02:57 -080045const char * UOBTest_NonAdversarial(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080046{
47 const char *szReturn = NULL;
48
Laurence Lundblade4fe9f312018-10-22 10:22:39 +053049 UsefulBuf_MAKE_STACK_UB(outbuf,50);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080050
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080051 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080052
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053053 UsefulOutBuf_Init(&UOB, outbuf);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080054
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080055 if(!UsefulOutBuf_AtStart(&UOB)) {
56 szReturn = "Not at start";
57 goto Done;
58 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080059
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080060 // Put 7 bytes at beginning of buf
61 UsefulOutBuf_AppendData(&UOB, "bluster", 7);
62
63 if(UsefulOutBuf_AtStart(&UOB)) {
64 szReturn = "At start";
65 goto Done;
66 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080067
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080068 // add a space to end
69 UsefulOutBuf_AppendByte(&UOB, ' ');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080070
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080071 // Add 5 bytes to the end
72 UsefulBufC UBC = {"hunny", 5};
73 UsefulOutBuf_AppendUsefulBuf(&UOB, UBC);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080074
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080075 // Insert 9 bytes at the beginning, slide the previous stuff right
76 UsefulOutBuf_InsertData(&UOB, "heffalump", 9, 0);
77 UsefulOutBuf_InsertByte(&UOB, ' ', 9);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080078
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080079 // Put 9 bytes in at position 10 -- just after "heffalump "
80 UsefulBufC UBC2 = {"unbounce ", 9};
81 UsefulOutBuf_InsertUsefulBuf(&UOB, UBC2, 10);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080082
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080083
Laurence Lundbladec5fef682020-01-25 11:38:45 -080084 const UsefulBufC Expected = UsefulBuf_FROM_SZ_LITERAL("heffalump unbounce bluster hunny");
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080085
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053086 UsefulBufC U = UsefulOutBuf_OutUBuf(&UOB);
Laurence Lundbladec5fef682020-01-25 11:38:45 -080087 if(UsefulBuf_IsNULLC(U) || UsefulBuf_Compare(Expected, U) || UsefulOutBuf_GetError(&UOB)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080088 szReturn = "OutUBuf";
89 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080090
Laurence Lundblade4fe9f312018-10-22 10:22:39 +053091 UsefulBuf_MAKE_STACK_UB(buf, 50);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053092 UsefulBufC Out = UsefulOutBuf_CopyOut(&UOB, buf);
Laurence Lundbladec5fef682020-01-25 11:38:45 -080093 if(UsefulBuf_IsNULLC(Out) || UsefulBuf_Compare(Expected, Out)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080094 szReturn = "CopyOut";
95 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080096
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080097Done:
98 return szReturn;
99}
100
101
102/*
103 Append test utility.
104 pUOB is the buffer to append too
105 num is the amount to append
106 expected is the expected return code, 0 or 1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800107
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800108 returns 0 if test passed
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800109
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800110 */
111static int AppendTest(UsefulOutBuf *pUOB, size_t num, int expected)
112{
113 //reset
114 UsefulOutBuf_Reset(pUOB);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800115
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800116 // check status first
117 if(UsefulOutBuf_GetError(pUOB))
118 return 1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800119
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800120 // append the bytes
121 UsefulOutBuf_AppendData(pUOB, (const uint8_t *)"bluster", num);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800122
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800123 // check error status after
124 if(UsefulOutBuf_GetError(pUOB) != expected)
125 return 1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800126
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800127 return 0;
128}
129
130
131/*
132 Same as append, but takes a position param too
133 */
134static int InsertTest(UsefulOutBuf *pUOB, size_t num, size_t pos, int expected)
135{
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530136 // reset
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800137 UsefulOutBuf_Reset(pUOB);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800138
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800139 // check
140 if(UsefulOutBuf_GetError(pUOB))
141 return 1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800142
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800143 UsefulOutBuf_InsertData(pUOB, (const uint8_t *)"bluster", num, pos);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800144
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800145 if(UsefulOutBuf_GetError(pUOB) != expected)
146 return 1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800147
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800148 return 0;
149}
150
151
152/*
153 Boundary conditions to test
154 - around 0
155 - around the buffer size
156 - around MAX size_t
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800157
158
Laurence Lundbladeee851742020-01-08 08:37:05 -0800159 Test these for the buffer size and the cursor, the insert amount, the
160 append amount and the insert position
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800161
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800162 */
163
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800164const char *UOBTest_BoundaryConditionsTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800165{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530166 UsefulBuf_MAKE_STACK_UB(outbuf,2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800167
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800168 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800169
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530170 UsefulOutBuf_Init(&UOB, outbuf);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800171
172 // append 0 byte to a 2 byte buffer --> success
173 if(AppendTest(&UOB, 0, 0))
174 return "Append 0 bytes failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800175
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800176 // append 1 byte to a 2 byte buffer --> success
177 if(AppendTest(&UOB, 1, 0))
178 return "Append of 1 byte failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800179
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800180 // append 2 byte to a 2 byte buffer --> success
181 if(AppendTest(&UOB, 2, 0))
182 return "Append to fill buffer failed";
183
184 // append 3 bytes to a 2 byte buffer --> failure
185 if(AppendTest(&UOB, 3, 1))
186 return "Overflow of buffer not caught";
187
188 // append max size_t to a 2 byte buffer --> failure
189 if(AppendTest(&UOB, SIZE_MAX, 1))
190 return "Append of SIZE_MAX error not caught";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800191
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800192 if(InsertTest(&UOB, 1, 0, 0))
193 return "Insert 1 byte at start failed";
194
195 if(InsertTest(&UOB, 2, 0, 0))
196 return "Insert 2 bytes at start failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800197
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800198 if(InsertTest(&UOB, 3, 0, 1))
199 return "Insert overflow not caught";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800200
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800201 if(InsertTest(&UOB, 1, 1, 1))
202 return "Bad insertion point not caught";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800203
204
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530205 UsefulBuf_MAKE_STACK_UB(outBuf2,10);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800206
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530207 UsefulOutBuf_Init(&UOB, outBuf2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800208
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800209 UsefulOutBuf_Reset(&UOB);
210 // put data in the buffer
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800211 UsefulOutBuf_AppendString(&UOB, "abc123");
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800212
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800213 UsefulOutBuf_InsertString(&UOB, "xyz*&^", 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800214
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800215 if(!UsefulOutBuf_GetError(&UOB)) {
216 return "insert with data should have failed";
217 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800218
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800219
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530220 UsefulOutBuf_Init(&UOB, (UsefulBuf){NULL, SIZE_MAX - 5});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800221 UsefulOutBuf_AppendData(&UOB, "123456789", SIZE_MAX -6);
222 if(UsefulOutBuf_GetError(&UOB)) {
223 return "insert in huge should have succeeded";
224 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800225
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530226 UsefulOutBuf_Init(&UOB, (UsefulBuf){NULL, SIZE_MAX - 5});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800227 UsefulOutBuf_AppendData(&UOB, "123456789", SIZE_MAX -5);
228 if(UsefulOutBuf_GetError(&UOB)) {
229 return "insert in huge should have succeeded";
230 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800231
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530232 UsefulOutBuf_Init(&UOB, (UsefulBuf){NULL, SIZE_MAX - 5});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800233 UsefulOutBuf_AppendData(&UOB, "123456789", SIZE_MAX - 4);
234 if(!UsefulOutBuf_GetError(&UOB)) {
235 return "lengths near max size";
236 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800237
Laurence Lundblade30dd0ba2019-05-26 23:37:30 +0300238 UsefulOutBuf_Init(&UOB, (UsefulBuf){NULL, 100});
239 if(!UsefulOutBuf_IsBufferNULL(&UOB)) {
240 return "NULL check failed";
241 }
242
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800243 return NULL;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800244}
245
246
247
248
249
250// Test function to get size and magic number check
251
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800252const char *TestBasicSanity(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800253{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530254 UsefulBuf_MAKE_STACK_UB(outbuf,10);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800255
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800256 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800257
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800258 // First -- make sure that the room left function returns the right amount
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530259 UsefulOutBuf_Init(&UOB, outbuf);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800260
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530261 if(UsefulOutBuf_RoomLeft(&UOB) != 10)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800262 return "room left failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800263
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800264 if(!UsefulOutBuf_WillItFit(&UOB, 9)) {
265 return "it did not fit";
266 }
267
268 if(UsefulOutBuf_WillItFit(&UOB, 11)) {
269 return "it should have not fit";
270 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800271
272
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800273 // Next -- make sure that the magic number checking is working right
274 UOB.magic = 8888; // make magic bogus
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800275
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800276 UsefulOutBuf_AppendData(&UOB, (const uint8_t *)"bluster", 7);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800277
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800278 if(!UsefulOutBuf_GetError(&UOB))
279 return "magic corruption check failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800280
281
282
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800283 // Next make sure that the valid data length check is working right
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530284 UsefulOutBuf_Init(&UOB, outbuf);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800285
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530286 UOB.data_len = UOB.UB.len+1; // make size bogus
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800287
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800288 UsefulOutBuf_AppendData(&UOB, (const uint8_t *)"bluster", 7);
289 if(!UsefulOutBuf_GetError(&UOB))
290 return "valid data check failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800291
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800292 return NULL;
293}
294
295
296
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800297const char *UBMacroConversionsTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800298{
299 char *szFoo = "foo";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800300
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530301 UsefulBufC Foo = UsefulBuf_FromSZ(szFoo);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800302 if(Foo.len != 3 || strncmp(Foo.ptr, szFoo, 3))
303 return "SZToUsefulBufC failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800304
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530305 UsefulBufC Too = UsefulBuf_FROM_SZ_LITERAL("Toooo");
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800306 if(Too.len != 5 || strncmp(Too.ptr, "Toooo", 5))
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530307 return "UsefulBuf_FROM_SZ_LITERAL failed";
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800308
309 uint8_t pB[] = {0x42, 0x6f, 0x6f};
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530310 UsefulBufC Boo = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pB);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800311 if(Boo.len != 3 || strncmp(Boo.ptr, "Boo", 3))
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530312 return "UsefulBuf_FROM_BYTE_ARRAY_LITERAL failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800313
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800314 char *String = "string"; // Intentionally not const
315 UsefulBuf B = (UsefulBuf){(void *)String, strlen(String)};
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530316 UsefulBufC BC = UsefulBuf_Const(B);
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800317 if(BC.len != strlen(String) || BC.ptr != String)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800318 return "UsefulBufConst failed";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800319
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800320 return NULL;
321}
322
323
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800324const char *UBUtilTests(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800325{
326 UsefulBuf UB = NULLUsefulBuf;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800327
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800328 if(!UsefulBuf_IsNULL(UB)){
329 return "IsNull failed";
330 }
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530331
332 if(!UsefulBuf_IsEmpty(UB)){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800333 return "IsEmpty failed";
334 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800335
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530336 if(!UsefulBuf_IsNULLOrEmpty(UB)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800337 return "IsNULLOrEmpty failed";
338 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800339
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800340 const UsefulBufC UBC = UsefulBuf_Const(UB);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800341
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530342 if(!UsefulBuf_IsNULLC(UBC)){
343 return "IsNull const failed";
344 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800345
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530346 if(!UsefulBuf_IsEmptyC(UBC)){
347 return "IsEmptyC failed";
348 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800349
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530350 if(!UsefulBuf_IsNULLOrEmptyC(UBC)){
351 return "IsNULLOrEmptyC failed";
352 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800353
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800354 const UsefulBuf UB2 = UsefulBuf_Unconst(UBC);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530355 if(!UsefulBuf_IsEmpty(UB2)) {
356 return "Back to UB is Empty failed";
357 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800358
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800359 UB.ptr = "x"; // just some valid pointer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800360
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800361 if(UsefulBuf_IsNULL(UB)){
362 return "IsNull failed";
363 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800364
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530365 if(!UsefulBuf_IsEmptyC(UBC)){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800366 return "IsEmpty failed";
367 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800368
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800369 // test the Unconst.
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530370 if(UsefulBuf_Unconst(UBC).ptr != NULL) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800371 return "Unconst failed";
372 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800373
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800374 // Set 100 bytes of '+'; validated a few tests later
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530375 UsefulBuf_MAKE_STACK_UB(Temp, 100);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800376 const UsefulBufC TempC = UsefulBuf_Set(Temp, '+');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800377
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800378 // Try to copy into a buf that is too small and see failure
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530379 UsefulBuf_MAKE_STACK_UB(Temp2, 99);
Laurence Lundblade05ec57b2018-10-21 01:50:03 +0530380 if(!UsefulBuf_IsNULLC(UsefulBuf_Copy(Temp2, TempC))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800381 return "Copy should have failed";
382 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800383
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800384 if(UsefulBuf_IsNULLC(UsefulBuf_CopyPtr(Temp2, "xx", 2))) {
385 return "CopyPtr failed";
386 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800387
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530388 UsefulBufC xxyy = UsefulBuf_CopyOffset(Temp2, 2, UsefulBuf_FROM_SZ_LITERAL("yy"));
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800389 if(UsefulBuf_IsNULLC(xxyy)) {
390 return "CopyOffset Failed";
391 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800392
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530393 if(UsefulBuf_Compare(UsefulBuf_Head(xxyy, 3), UsefulBuf_FROM_SZ_LITERAL("xxy"))) {
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800394 return "head failed";
395 }
396
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530397 if(UsefulBuf_Compare(UsefulBuf_Tail(xxyy, 1), UsefulBuf_FROM_SZ_LITERAL("xyy"))) {
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800398 return "tail failed";
399 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800400
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800401 if(!UsefulBuf_IsNULLC(UsefulBuf_Head(xxyy, 5))) {
402 return "head should have failed";
403 }
404
405 if(!UsefulBuf_IsNULLC(UsefulBuf_Tail(xxyy, 5))) {
406 return "tail should have failed";
407 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -0800408
Laurence Lundblade7412f812019-01-01 18:49:36 -0800409 if(!UsefulBuf_IsNULLC(UsefulBuf_Tail(NULLUsefulBufC, 0))) {
410 return "tail of NULLUsefulBufC is not NULLUsefulBufC";
411 }
Laurence Lundbladedf1c1cf2019-01-17 11:55:05 -0800412
Laurence Lundblade7412f812019-01-01 18:49:36 -0800413 const UsefulBufC TailResult = UsefulBuf_Tail((UsefulBufC){NULL, 100}, 99);
414 if(TailResult.ptr != NULL || TailResult.len != 1) {
415 return "tail of NULL and length incorrect";
416 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800417
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530418 if(!UsefulBuf_IsNULLC(UsefulBuf_CopyOffset(Temp2, 100, UsefulBuf_FROM_SZ_LITERAL("yy")))) {
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800419 return "Copy Offset should have failed";
420 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800421
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800422 // Try to copy into a NULL/empty buf and see failure
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800423 const UsefulBuf UBNull = NULLUsefulBuf;
Laurence Lundblade05ec57b2018-10-21 01:50:03 +0530424 if(!UsefulBuf_IsNULLC(UsefulBuf_Copy(UBNull, TempC))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800425 return "Copy to NULL should have failed";
426 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800427
428
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800429 // Try to set a NULL/empty buf; nothing should happen
Laurence Lundblade05ec57b2018-10-21 01:50:03 +0530430 UsefulBuf_Set(UBNull, '+'); // This will crash on failure
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800431
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800432 // Copy successfully to a buffer
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530433 UsefulBuf_MAKE_STACK_UB(Temp3, 101);
Laurence Lundblade05ec57b2018-10-21 01:50:03 +0530434 if(UsefulBuf_IsNULLC(UsefulBuf_Copy(Temp3, TempC))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800435 return "Copy should not have failed";
436 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800437
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800438 static const uint8_t pExpected[] = {
439 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
440 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
441 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
442 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
443 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
444 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
445 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
446 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
447 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
448 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
449 };
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530450 UsefulBufC Expected = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpected);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800451 // This validates comparison for equality and the UsefulBuf_Set
Laurence Lundblade05ec57b2018-10-21 01:50:03 +0530452 if(UsefulBuf_Compare(Expected, TempC)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800453 return "Set / Copy / Compare failed";
454 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800455
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800456 // Compare two empties and expect success
457 if(UsefulBuf_Compare(NULLUsefulBufC, NULLUsefulBufC)){
458 return "Compare Empties failed";
459 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800460
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800461 // Compare with empty and expect the first to be larger
462 if(UsefulBuf_Compare(Expected, NULLUsefulBufC) <= 0){
463 return "Compare with empty failed";
464 }
465
466
467 static const uint8_t pExpectedBigger[] = {
468 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
469 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
470 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
471 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
472 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
473 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
474 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
475 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
476 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
477 '+', '+', '+', '+', '+', '+', '+', '+', '+', ',',
478 };
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800479 const UsefulBufC ExpectedBigger = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedBigger);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800480
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530481 // Expect -1 when the first arg is smaller
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800482 if(UsefulBuf_Compare(Expected, ExpectedBigger) >= 0){
483 return "Compare with bigger";
484 }
485
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800486
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800487 static const uint8_t pExpectedSmaller[] = {
488 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
489 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
490 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
491 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
492 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
493 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
494 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
495 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
496 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
497 '+', '+', '+', '+', '+', '+', '+', '+', '+', '*',
498 };
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800499 const UsefulBufC ExpectedSmaller = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedSmaller);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530500 // Expect +1 when the first arg is larger
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800501 if(UsefulBuf_Compare(Expected, ExpectedSmaller) <= 0){
502 return "Compare with smaller";
503 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800504
505
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800506 static const uint8_t pExpectedLonger[] = {
507 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
508 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
509 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
510 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
511 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
512 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
513 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
514 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
515 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
516 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+'
517 };
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800518 const UsefulBufC ExpectedLonger = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedLonger);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800519
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530520 // Expect -1 when the first arg is smaller
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800521 if(UsefulBuf_Compare(Expected, ExpectedLonger) >= 0){
522 return "Compare with longer";
523 }
524
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800525
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800526 static const uint8_t pExpectedShorter[] = {
527 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
528 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
529 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
530 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
531 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
532 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
533 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
534 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
535 '+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
536 '+', '+', '+', '+', '+', '+', '+', '+', '+',
537 };
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800538 const UsefulBufC ExpectedShorter = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedShorter);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800539 // Expect +1 with the first arg is larger
540 if(UsefulBuf_Compare(Expected, ExpectedShorter) <= 0){
541 return "Compare with shorter";
542 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800543
544
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530545 if(UsefulBuf_IsNULLC(UsefulBuf_Copy(Temp, NULLUsefulBufC))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800546 return "Copy null/empty failed";
547 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800548
Laurence Lundbladed5e101e2019-03-06 17:23:18 -0800549 if(UsefulBuf_IsValue(ExpectedShorter, '+') != SIZE_MAX) {
550 return "IsValue failed to match all";
551 }
552
553 if(UsefulBuf_IsValue(ExpectedShorter, '-') != 0) {
554 return "IsValue should have failed right away";
555 }
556
557 if(UsefulBuf_IsValue(NULLUsefulBufC, 0x00) != 0) {
558 return "IsValue failed on NULLUsefulBufC";
559 }
560
561 if(UsefulBuf_IsValue((UsefulBufC){(uint8_t[]){0x00}, 1}, 0x00) != SIZE_MAX) {
562 return "IsValue failed finding 0 in one byte of 0";
563 }
564
565 if(UsefulBuf_IsValue((UsefulBufC){(uint8_t[]){0x00}, 1}, 0x01) != 0) {
566 return "IsValue failed not finding 1 in one byte of 0";
567 }
568
569 if(UsefulBuf_IsValue(ExpectedSmaller, '+') != ExpectedSmaller.len -1) {
570 return "IsValue failed to find final *";
571 }
572
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800573 // Look for +++++... in +++++... and find it at the beginning
574 if(0 != UsefulBuf_FindBytes(ExpectedLonger, ExpectedShorter)){
575 return "Failed to find";
576 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800577
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800578 // look for ++* in ....++* and find it at the end
579 static const uint8_t pToFind[] = {'+', '+', '*'};
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800580 const UsefulBufC ToBeFound = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pToFind);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800581
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800582 if(97 != UsefulBuf_FindBytes(ExpectedSmaller, ToBeFound)){
583 return "Failed to find 2";
584 }
585
586 // look for ++* in ....++, and find it near the end
587 if(SIZE_MAX != UsefulBuf_FindBytes(ExpectedBigger, ToBeFound)){
588 return "Failed to not find";
589 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800590
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800591 // Look for the whole buffer in itself and succeed.
592 if(0 != UsefulBuf_FindBytes(ExpectedLonger, ExpectedLonger)){
593 return "Failed to find 3";
594 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800595
Laurence Lundbladecf41c522021-02-20 10:19:07 -0700596
597 const uint8_t pB[] = {0x01, 0x02, 0x03};
598 UsefulBufC Boo = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pB);
599 // Try to map a pointer before
600 if(UsefulBuf_PointerToOffset(Boo, pB-1) != SIZE_MAX) {
601 return "Didn't error on pointer before";
602 }
603
604 // Try to map a pointer after
605 if(UsefulBuf_PointerToOffset(Boo, pB+sizeof(pB)) != SIZE_MAX) {
606 return "Didn't error on pointer after";
607 }
608
609 // Try to map a pointer inside
610 if(UsefulBuf_PointerToOffset(Boo, pB+1) != 1) {
611 return "Incorrect pointer offset";
612 }
613
614 // Try to map a pointer at the start
615 if(UsefulBuf_PointerToOffset(Boo, pB) != 0) {
616 return "Incorrect pointer offset for start";
617 }
618
619 // Try to map a pointer at the end
620 if(UsefulBuf_PointerToOffset(Boo, pB + sizeof(pB)-1) != 2) {
621 return "Incorrect pointer offset for end";
622 }
623
624 // Try to map a pointer on a NULL UB
625 if(UsefulBuf_PointerToOffset(NULLUsefulBufC, pB ) != SIZE_MAX) {
626 return "Incorrect pointer offset for start";
627 }
628
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800629 return NULL;
630}
631
632
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800633const char * UIBTest_IntegerFormat(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800634{
Laurence Lundbladecf41c522021-02-20 10:19:07 -0700635 UsefulOutBuf_MakeOnStack(UOB, 100);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800636
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800637 const uint32_t u32 = 0x0A0B0C0D; // from https://en.wikipedia.org/wiki/Endianness
638 const uint64_t u64 = 1984738472938472;
639 const uint16_t u16 = 40000;
640 const uint8_t u8 = 9;
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +0200641#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800642 const float f = (float)314.15;
643 const double d = 2.1e10;
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +0200644#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800645
646
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530647 UsefulOutBuf_AppendUint32(&UOB, u32); // Also tests UsefulOutBuf_InsertUint64 and UsefulOutBuf_GetEndPosition
648 UsefulOutBuf_AppendUint64(&UOB, u64); // Also tests UsefulOutBuf_InsertUint32
649 UsefulOutBuf_AppendUint16(&UOB, u16); // Also tests UsefulOutBuf_InsertUint16
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800650 UsefulOutBuf_AppendByte(&UOB, u8);
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +0200651#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530652 UsefulOutBuf_AppendFloat(&UOB, f); // Also tests UsefulOutBuf_InsertFloat
653 UsefulOutBuf_AppendDouble(&UOB, d); // Also tests UsefulOutBuf_InsertDouble
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +0200654#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800655
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800656 const UsefulBufC O = UsefulOutBuf_OutUBuf(&UOB);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530657 if(UsefulBuf_IsNULLC(O))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800658 return "Couldn't output integers";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800659
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800660 // from https://en.wikipedia.org/wiki/Endianness
661 const uint8_t pExpectedNetworkOrder[4] = {0x0A, 0x0B, 0x0C, 0x0D};
662 if(memcmp(O.ptr, pExpectedNetworkOrder, 4)) {
663 return "not in network order";
664 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800665
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800666 UsefulInputBuf UIB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800667
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530668 UsefulInputBuf_Init(&UIB, O);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800669
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530670 if(UsefulInputBuf_Tell(&UIB) != 0) {
671 return "UsefulInputBuf_Tell failed";
672 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800673
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800674 if(UsefulInputBuf_GetUint32(&UIB) != u32) {
675 return "u32 out then in failed";
676 }
677 if(UsefulInputBuf_GetUint64(&UIB) != u64) {
678 return "u64 out then in failed";
679 }
680 if(UsefulInputBuf_GetUint16(&UIB) != u16) {
681 return "u16 out then in failed";
682 }
683 if(UsefulInputBuf_GetByte(&UIB) != u8) {
684 return "u8 out then in failed";
685 }
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +0200686#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800687 if(UsefulInputBuf_GetFloat(&UIB) != f) {
688 return "float out then in failed";
689 }
690 if(UsefulInputBuf_GetDouble(&UIB) != d) {
691 return "double out then in failed";
692 }
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +0200693#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800694
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800695 // Reset and go again for a few more tests
696 UsefulInputBuf_Init(&UIB, O);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800697
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800698 const UsefulBufC Four = UsefulInputBuf_GetUsefulBuf(&UIB, 4);
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800699 if(UsefulBuf_IsNULLC(Four)) {
700 return "Four is NULL";
701 }
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530702 if(UsefulBuf_Compare(Four, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExpectedNetworkOrder))) {
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800703 return "Four compare failed";
704 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800705
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +0200706#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800707 if(UsefulInputBuf_BytesUnconsumed(&UIB) != 23){
708 return "Wrong number of unconsumed bytes";
709 }
710
711 if(!UsefulInputBuf_BytesAvailable(&UIB, 23)){
712 return "Wrong number of bytes available I";
713 }
714
715 if(UsefulInputBuf_BytesAvailable(&UIB, 24)){
716 return "Wrong number of bytes available II";
717 }
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +0200718#else /* USEFULBUF_DISABLE_ALL_FLOAT */
719 if(UsefulInputBuf_BytesUnconsumed(&UIB) != 11){
720 return "Wrong number of unconsumed bytes";
721 }
722 if(!UsefulInputBuf_BytesAvailable(&UIB, 11)){
723 return "Wrong number of bytes available I";
724 }
725
726 if(UsefulInputBuf_BytesAvailable(&UIB, 12)){
727 return "Wrong number of bytes available II";
728 }
729#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800730
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800731 UsefulInputBuf_Seek(&UIB, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800732
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800733 if(UsefulInputBuf_GetError(&UIB)) {
734 return "unexpected error after seek";
735 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800736
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800737 const uint8_t *pGetBytes = (const uint8_t *)UsefulInputBuf_GetBytes(&UIB, 4);
738 if(pGetBytes == NULL) {
739 return "GetBytes returns NULL";
740 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800741
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800742 if(memcmp(pGetBytes, pExpectedNetworkOrder, 4)) {
743 return "Got wrong bytes";
744 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800745
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800746 UsefulInputBuf_Seek(&UIB, 28);
747
748 if(!UsefulInputBuf_GetError(&UIB)) {
749 return "expected error after seek";
750 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800751
Laurence Lundbladecf41c522021-02-20 10:19:07 -0700752 if(UsefulInputBuf_PointerToOffset(&UIB, O.ptr) != 0) {
753 return "PointerToOffset not working";
754 }
755
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530756 return NULL;
757}
758
759
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +0200760#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800761const char *UBUTest_CopyUtil(void)
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530762{
763 if(UsefulBufUtil_CopyFloatToUint32(65536.0F) != 0x47800000) {
764 return "CopyFloatToUint32 failed";
765 }
766
767 if(UsefulBufUtil_CopyDoubleToUint64(4e-40F) != 0X37C16C2800000000ULL) {
768 return "CopyDoubleToUint64 failed";
769 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800770
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530771 if(UsefulBufUtil_CopyUint64ToDouble(0X37C16C2800000000ULL) != 4e-40F) {
772 return "CopyUint64ToDouble failed";
773 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800774
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530775 if(UsefulBufUtil_CopyUint32ToFloat(0x47800000) != 65536.0F) {
776 return "CopyUint32ToFloat failed";
777 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800778
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800779 return NULL;
780}
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +0200781#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800782
783
784