add handling byte data array structure.
@@ -0,0 +1,680 @@ | ||
1 | +/* | |
2 | + * test_bytearray.c | |
3 | + * | |
4 | + * Copyright (c) 2013 project bchan | |
5 | + * | |
6 | + * This software is provided 'as-is', without any express or implied | |
7 | + * warranty. In no event will the authors be held liable for any damages | |
8 | + * arising from the use of this software. | |
9 | + * | |
10 | + * Permission is granted to anyone to use this software for any purpose, | |
11 | + * including commercial applications, and to alter it and redistribute it | |
12 | + * freely, subject to the following restrictions: | |
13 | + * | |
14 | + * 1. The origin of this software must not be misrepresented; you must not | |
15 | + * claim that you wrote the original software. If you use this software | |
16 | + * in a product, an acknowledgment in the product documentation would be | |
17 | + * appreciated but is not required. | |
18 | + * | |
19 | + * 2. Altered source versions must be plainly marked as such, and must not be | |
20 | + * misrepresented as being the original software. | |
21 | + * | |
22 | + * 3. This notice may not be removed or altered from any source | |
23 | + * distribution. | |
24 | + * | |
25 | + */ | |
26 | + | |
27 | +#include "test_coll.h" | |
28 | + | |
29 | +#include "bytearray.h" | |
30 | + | |
31 | +#include <btron/btron.h> | |
32 | +#include <bstdio.h> | |
33 | + | |
34 | +LOCAL UNITTEST_RESULT test_bytearray_1() | |
35 | +{ | |
36 | + bytearray_t array; | |
37 | + W i, len, err; | |
38 | + UNITTEST_RESULT result = UNITTEST_RESULT_PASS; | |
39 | + UB data[] = {0x1, 0x2, 0x3}, *p; | |
40 | + W data_len = sizeof(data); | |
41 | + | |
42 | + err = bytearray_initialize(&array); | |
43 | + if (err < 0) { | |
44 | + printf("bytearray_initialize error: %08x\n", err); | |
45 | + return UNITTEST_RESULT_FAIL; | |
46 | + } | |
47 | + | |
48 | + for (i = 0; i < data_len; i++) { | |
49 | + err = bytearray_pushback(&array, data[i]); | |
50 | + if (err < 0) { | |
51 | + printf("bytearray_pushback error: %08x", err); | |
52 | + result = UNITTEST_RESULT_FAIL; | |
53 | + break; | |
54 | + } | |
55 | + } | |
56 | + | |
57 | + p = bytearray_getbuffer(&array); | |
58 | + len = bytearray_getlength(&array); | |
59 | + | |
60 | + if (len != data_len) { | |
61 | + printf("buffer length fail: expected = %d, result = %d\n", data_len, len); | |
62 | + result = UNITTEST_RESULT_FAIL; | |
63 | + } else { | |
64 | + if (memcmp(data, p, data_len) != 0) { | |
65 | + printf("buffer result fail\n"); | |
66 | + result = UNITTEST_RESULT_FAIL; | |
67 | + } | |
68 | + } | |
69 | + | |
70 | + bytearray_finalize(&array); | |
71 | + | |
72 | + return result; | |
73 | +} | |
74 | + | |
75 | +LOCAL UNITTEST_RESULT test_bytearray_2() | |
76 | +{ | |
77 | + bytearray_t array; | |
78 | + W i, len, err; | |
79 | + UNITTEST_RESULT result = UNITTEST_RESULT_PASS; | |
80 | + UB data[] = {0x1, 0x2, 0x3}, *p; | |
81 | + W data_len = sizeof(data); | |
82 | + | |
83 | + err = bytearray_initialize(&array); | |
84 | + if (err < 0) { | |
85 | + printf("bytearray_initialize error: %08x\n", err); | |
86 | + return UNITTEST_RESULT_FAIL; | |
87 | + } | |
88 | + | |
89 | + for (i = 0; i < data_len; i++) { | |
90 | + err = bytearray_pushback(&array, data[i]); | |
91 | + if (err < 0) { | |
92 | + printf("bytearray_pushback error: %08x", err); | |
93 | + result = UNITTEST_RESULT_FAIL; | |
94 | + break; | |
95 | + } | |
96 | + } | |
97 | + err = bytearray_popback(&array); | |
98 | + if (err < 0) { | |
99 | + printf("bytearray_pushback error: %08x", err); | |
100 | + result = UNITTEST_RESULT_FAIL; | |
101 | + } | |
102 | + | |
103 | + p = bytearray_getbuffer(&array); | |
104 | + len = bytearray_getlength(&array); | |
105 | + | |
106 | + if (len != (data_len-1)) { | |
107 | + printf("buffer length fail: expected = %d, result = %d\n", data_len - 1, len); | |
108 | + result = UNITTEST_RESULT_FAIL; | |
109 | + } else { | |
110 | + if (memcmp(data, p, data_len - 1) != 0) { | |
111 | + printf("buffer result fail\n"); | |
112 | + result = UNITTEST_RESULT_FAIL; | |
113 | + } | |
114 | + } | |
115 | + | |
116 | + bytearray_finalize(&array); | |
117 | + | |
118 | + return result; | |
119 | +} | |
120 | + | |
121 | +typedef struct { | |
122 | + UB *init; | |
123 | + W init_len; | |
124 | + UB *insert; | |
125 | + W insert_len; | |
126 | + W insert_at; | |
127 | + UB *expected; | |
128 | + W expected_len; | |
129 | +} test_bytearray_cursor_insert; | |
130 | + | |
131 | +LOCAL UNITTEST_RESULT test_bytearray_cursor_insert_common(test_bytearray_cursor_insert *testdata) | |
132 | +{ | |
133 | + bytearray_t array; | |
134 | + W i, len, err; | |
135 | + bytearray_cursor_t cursor; | |
136 | + UB *p; | |
137 | + UNITTEST_RESULT result = UNITTEST_RESULT_PASS; | |
138 | + | |
139 | + err = bytearray_initialize(&array); | |
140 | + if (err < 0) { | |
141 | + printf("bytearray_initialize error: %08x\n", err); | |
142 | + return UNITTEST_RESULT_FAIL; | |
143 | + } | |
144 | + | |
145 | + for (i = 0; i < testdata->init_len; i++) { | |
146 | + err = bytearray_pushback(&array, testdata->init[i]); | |
147 | + if (err < 0) { | |
148 | + printf("bytearray_pushback error: %08x", err); | |
149 | + result = UNITTEST_RESULT_FAIL; | |
150 | + break; | |
151 | + } | |
152 | + } | |
153 | + | |
154 | + bytearray_cursor_initialize(&cursor, &array); | |
155 | + | |
156 | + err = bytearray_cursor_move(&cursor, testdata->insert_at); | |
157 | + if (err < 0) { | |
158 | + printf("bytearray_cursor_move error: %08x", err); | |
159 | + result = UNITTEST_RESULT_FAIL; | |
160 | + } | |
161 | + err = bytearray_cursor_insert(&cursor, testdata->insert, testdata->insert_len); | |
162 | + if (err < 0) { | |
163 | + printf("bytearray_cursor_insert error: %08x", err); | |
164 | + result = UNITTEST_RESULT_FAIL; | |
165 | + } | |
166 | + | |
167 | + bytearray_cursor_finalize(&cursor); | |
168 | + | |
169 | + p = bytearray_getbuffer(&array); | |
170 | + len = bytearray_getlength(&array); | |
171 | + if (len != testdata->expected_len) { | |
172 | + printf("buffer length fail: expected = %d, result = %d\n", testdata->expected_len, len); | |
173 | + result = UNITTEST_RESULT_FAIL; | |
174 | + } else { | |
175 | + if (memcmp(testdata->expected, p, testdata->expected_len) != 0) { | |
176 | + printf("buffer result fail\n"); | |
177 | + result = UNITTEST_RESULT_FAIL; | |
178 | + for (i = 0; i < testdata->expected_len; i++) { | |
179 | + printf("%d: %x, %x\n", i, testdata->expected[i], p[i]); | |
180 | + } | |
181 | + } | |
182 | + } | |
183 | + | |
184 | + bytearray_finalize(&array); | |
185 | + | |
186 | + return result; | |
187 | +} | |
188 | + | |
189 | +LOCAL UNITTEST_RESULT test_bytearray_cursor_insert_1() | |
190 | +{ | |
191 | + UB init[] = {0x1, 0x2, 0x3}; | |
192 | + W init_len = sizeof(init); | |
193 | + UB insert[] = {0xA, 0xB}; | |
194 | + W insert_len = sizeof(insert); | |
195 | + W insert_at = 0; | |
196 | + UB expected[] = {0xA, 0xB, 0x1, 0x2, 0x3}; | |
197 | + W expected_len = sizeof(expected); | |
198 | + test_bytearray_cursor_insert testdata = { | |
199 | + init, init_len, | |
200 | + insert, insert_len, insert_at, | |
201 | + expected, expected_len | |
202 | + }; | |
203 | + return test_bytearray_cursor_insert_common(&testdata); | |
204 | +} | |
205 | + | |
206 | +LOCAL UNITTEST_RESULT test_bytearray_cursor_insert_2() | |
207 | +{ | |
208 | + UB init[] = {0x1, 0x2, 0x3}; | |
209 | + W init_len = sizeof(init); | |
210 | + UB insert[] = {0xA, 0xB}; | |
211 | + W insert_len = sizeof(insert); | |
212 | + W insert_at = 1; | |
213 | + UB expected[] = {0x1, 0xA, 0xB, 0x2, 0x3}; | |
214 | + W expected_len = sizeof(expected); | |
215 | + test_bytearray_cursor_insert testdata = { | |
216 | + init, init_len, | |
217 | + insert, insert_len, insert_at, | |
218 | + expected, expected_len | |
219 | + }; | |
220 | + return test_bytearray_cursor_insert_common(&testdata); | |
221 | +} | |
222 | + | |
223 | +LOCAL UNITTEST_RESULT test_bytearray_cursor_insert_3() | |
224 | +{ | |
225 | + UB init[] = {0x1, 0x2, 0x3}; | |
226 | + W init_len = sizeof(init); | |
227 | + UB insert[] = {0xA, 0xB}; | |
228 | + W insert_len = sizeof(insert); | |
229 | + W insert_at = 2; | |
230 | + UB expected[] = {0x1, 0x2, 0xA, 0xB, 0x3}; | |
231 | + W expected_len = sizeof(expected); | |
232 | + test_bytearray_cursor_insert testdata = { | |
233 | + init, init_len, | |
234 | + insert, insert_len, insert_at, | |
235 | + expected, expected_len | |
236 | + }; | |
237 | + return test_bytearray_cursor_insert_common(&testdata); | |
238 | +} | |
239 | + | |
240 | +typedef struct { | |
241 | + UB *init; | |
242 | + W init_len; | |
243 | + W erase_at; | |
244 | + W erase_len; | |
245 | + UB *expected; | |
246 | + W expected_len; | |
247 | +} test_bytearray_cursor_erase; | |
248 | + | |
249 | +LOCAL UNITTEST_RESULT test_bytearray_cursor_erase_common(test_bytearray_cursor_erase *testdata) | |
250 | +{ | |
251 | + bytearray_t array; | |
252 | + W i, len, err; | |
253 | + bytearray_cursor_t cursor; | |
254 | + UB *p; | |
255 | + UNITTEST_RESULT result = UNITTEST_RESULT_PASS; | |
256 | + | |
257 | + err = bytearray_initialize(&array); | |
258 | + if (err < 0) { | |
259 | + printf("bytearray_initialize error: %08x\n", err); | |
260 | + return UNITTEST_RESULT_FAIL; | |
261 | + } | |
262 | + | |
263 | + for (i = 0; i < testdata->init_len; i++) { | |
264 | + err = bytearray_pushback(&array, testdata->init[i]); | |
265 | + if (err < 0) { | |
266 | + printf("bytearray_pushback error: %08x", err); | |
267 | + result = UNITTEST_RESULT_FAIL; | |
268 | + break; | |
269 | + } | |
270 | + } | |
271 | + | |
272 | + bytearray_cursor_initialize(&cursor, &array); | |
273 | + | |
274 | + err = bytearray_cursor_move(&cursor, testdata->erase_at); | |
275 | + if (err < 0) { | |
276 | + printf("bytearray_cursor_move error: %08x", err); | |
277 | + result = UNITTEST_RESULT_FAIL; | |
278 | + } | |
279 | + err = bytearray_cursor_erase(&cursor, testdata->erase_len); | |
280 | + if (err < 0) { | |
281 | + printf("bytearray_cursor_erase error: %08x", err); | |
282 | + result = UNITTEST_RESULT_FAIL; | |
283 | + } | |
284 | + | |
285 | + bytearray_cursor_finalize(&cursor); | |
286 | + | |
287 | + p = bytearray_getbuffer(&array); | |
288 | + len = bytearray_getlength(&array); | |
289 | + if (len != testdata->expected_len) { | |
290 | + printf("buffer length fail: expected = %d, result = %d\n", testdata->expected_len, len); | |
291 | + result = UNITTEST_RESULT_FAIL; | |
292 | + } else { | |
293 | + if (memcmp(testdata->expected, p, testdata->expected_len) != 0) { | |
294 | + printf("buffer result fail\n"); | |
295 | + result = UNITTEST_RESULT_FAIL; | |
296 | + for (i = 0; i < testdata->expected_len; i++) { | |
297 | + printf("%d: %x, %x\n", i, testdata->expected[i], p[i]); | |
298 | + } | |
299 | + } | |
300 | + } | |
301 | + | |
302 | + bytearray_finalize(&array); | |
303 | + | |
304 | + return result; | |
305 | +} | |
306 | + | |
307 | +LOCAL UNITTEST_RESULT test_bytearray_cursor_erase_1() | |
308 | +{ | |
309 | + UB init[] = {0x1, 0x2, 0x3}; | |
310 | + W init_len = sizeof(init); | |
311 | + W erase_at = 0; | |
312 | + W erase_len = 0; | |
313 | + UB expected[] = {0x1, 0x2, 0x3}; | |
314 | + W expected_len = sizeof(expected); | |
315 | + test_bytearray_cursor_erase testdata = { | |
316 | + init, init_len, | |
317 | + erase_at, erase_len, | |
318 | + expected, expected_len | |
319 | + }; | |
320 | + return test_bytearray_cursor_erase_common(&testdata); | |
321 | +} | |
322 | + | |
323 | +LOCAL UNITTEST_RESULT test_bytearray_cursor_erase_2() | |
324 | +{ | |
325 | + UB init[] = {0x1, 0x2, 0x3}; | |
326 | + W init_len = sizeof(init); | |
327 | + W erase_at = 0; | |
328 | + W erase_len = 1; | |
329 | + UB expected[] = {0x2, 0x3}; | |
330 | + W expected_len = sizeof(expected); | |
331 | + test_bytearray_cursor_erase testdata = { | |
332 | + init, init_len, | |
333 | + erase_at, erase_len, | |
334 | + expected, expected_len | |
335 | + }; | |
336 | + return test_bytearray_cursor_erase_common(&testdata); | |
337 | +} | |
338 | + | |
339 | +LOCAL UNITTEST_RESULT test_bytearray_cursor_erase_3() | |
340 | +{ | |
341 | + UB init[] = {0x1, 0x2, 0x3}; | |
342 | + W init_len = sizeof(init); | |
343 | + W erase_at = 0; | |
344 | + W erase_len = 2; | |
345 | + UB expected[] = {0x3}; | |
346 | + W expected_len = sizeof(expected); | |
347 | + test_bytearray_cursor_erase testdata = { | |
348 | + init, init_len, | |
349 | + erase_at, erase_len, | |
350 | + expected, expected_len | |
351 | + }; | |
352 | + return test_bytearray_cursor_erase_common(&testdata); | |
353 | +} | |
354 | + | |
355 | +LOCAL UNITTEST_RESULT test_bytearray_cursor_erase_4() | |
356 | +{ | |
357 | + UB init[] = {0x1, 0x2, 0x3}; | |
358 | + W init_len = sizeof(init); | |
359 | + W erase_at = 0; | |
360 | + W erase_len = 3; | |
361 | + UB expected[] = {}; | |
362 | + W expected_len = sizeof(expected); | |
363 | + test_bytearray_cursor_erase testdata = { | |
364 | + init, init_len, | |
365 | + erase_at, erase_len, | |
366 | + expected, expected_len | |
367 | + }; | |
368 | + return test_bytearray_cursor_erase_common(&testdata); | |
369 | +} | |
370 | + | |
371 | +LOCAL UNITTEST_RESULT test_bytearray_cursor_erase_5() | |
372 | +{ | |
373 | + UB init[] = {0x1, 0x2, 0x3}; | |
374 | + W init_len = sizeof(init); | |
375 | + W erase_at = 1; | |
376 | + W erase_len = 0; | |
377 | + UB expected[] = {0x1, 0x2, 0x3}; | |
378 | + W expected_len = sizeof(expected); | |
379 | + test_bytearray_cursor_erase testdata = { | |
380 | + init, init_len, | |
381 | + erase_at, erase_len, | |
382 | + expected, expected_len | |
383 | + }; | |
384 | + return test_bytearray_cursor_erase_common(&testdata); | |
385 | +} | |
386 | + | |
387 | +LOCAL UNITTEST_RESULT test_bytearray_cursor_erase_6() | |
388 | +{ | |
389 | + UB init[] = {0x1, 0x2, 0x3}; | |
390 | + W init_len = sizeof(init); | |
391 | + W erase_at = 1; | |
392 | + W erase_len = 1; | |
393 | + UB expected[] = {0x1, 0x3}; | |
394 | + W expected_len = sizeof(expected); | |
395 | + test_bytearray_cursor_erase testdata = { | |
396 | + init, init_len, | |
397 | + erase_at, erase_len, | |
398 | + expected, expected_len | |
399 | + }; | |
400 | + return test_bytearray_cursor_erase_common(&testdata); | |
401 | +} | |
402 | + | |
403 | +LOCAL UNITTEST_RESULT test_bytearray_cursor_erase_7() | |
404 | +{ | |
405 | + UB init[] = {0x1, 0x2, 0x3}; | |
406 | + W init_len = sizeof(init); | |
407 | + W erase_at = 1; | |
408 | + W erase_len = 2; | |
409 | + UB expected[] = {0x1}; | |
410 | + W expected_len = sizeof(expected); | |
411 | + test_bytearray_cursor_erase testdata = { | |
412 | + init, init_len, | |
413 | + erase_at, erase_len, | |
414 | + expected, expected_len | |
415 | + }; | |
416 | + return test_bytearray_cursor_erase_common(&testdata); | |
417 | +} | |
418 | + | |
419 | +LOCAL UNITTEST_RESULT test_bytearray_cursor_erase_8() | |
420 | +{ | |
421 | + UB init[] = {0x1, 0x2, 0x3}; | |
422 | + W init_len = sizeof(init); | |
423 | + W erase_at = 2; | |
424 | + W erase_len = 0; | |
425 | + UB expected[] = {0x1, 0x2, 0x3}; | |
426 | + W expected_len = sizeof(expected); | |
427 | + test_bytearray_cursor_erase testdata = { | |
428 | + init, init_len, | |
429 | + erase_at, erase_len, | |
430 | + expected, expected_len | |
431 | + }; | |
432 | + return test_bytearray_cursor_erase_common(&testdata); | |
433 | +} | |
434 | + | |
435 | +LOCAL UNITTEST_RESULT test_bytearray_cursor_erase_9() | |
436 | +{ | |
437 | + UB init[] = {0x1, 0x2, 0x3}; | |
438 | + W init_len = sizeof(init); | |
439 | + W erase_at = 2; | |
440 | + W erase_len = 1; | |
441 | + UB expected[] = {0x1, 0x2}; | |
442 | + W expected_len = sizeof(expected); | |
443 | + test_bytearray_cursor_erase testdata = { | |
444 | + init, init_len, | |
445 | + erase_at, erase_len, | |
446 | + expected, expected_len | |
447 | + }; | |
448 | + return test_bytearray_cursor_erase_common(&testdata); | |
449 | +} | |
450 | + | |
451 | +typedef struct { | |
452 | + UB *init; | |
453 | + W init_len; | |
454 | + UB *expected; | |
455 | + W expected_len; | |
456 | +} test_bytearray_cursor_getUB; | |
457 | + | |
458 | +LOCAL UNITTEST_RESULT test_bytearray_cursor_getUB_common(test_bytearray_cursor_getUB *testdata) | |
459 | +{ | |
460 | + bytearray_t array; | |
461 | + W i, err; | |
462 | + bytearray_cursor_t cursor; | |
463 | + UB val; | |
464 | + UNITTEST_RESULT result = UNITTEST_RESULT_PASS; | |
465 | + | |
466 | + err = bytearray_initialize(&array); | |
467 | + if (err < 0) { | |
468 | + printf("bytearray_initialize error: %08x\n", err); | |
469 | + return UNITTEST_RESULT_FAIL; | |
470 | + } | |
471 | + | |
472 | + for (i = 0; i < testdata->init_len; i++) { | |
473 | + err = bytearray_pushback(&array, testdata->init[i]); | |
474 | + if (err < 0) { | |
475 | + printf("bytearray_pushback error: %08x", err); | |
476 | + result = UNITTEST_RESULT_FAIL; | |
477 | + break; | |
478 | + } | |
479 | + } | |
480 | + | |
481 | + bytearray_cursor_initialize(&cursor, &array); | |
482 | + | |
483 | + for (i = 0; i < testdata->expected_len; i++) { | |
484 | + err = bytearray_cursor_getUB(&cursor, &val); | |
485 | + if (err < 0) { | |
486 | + printf("bytearray_cursor_getUB error: %08x", err); | |
487 | + result = UNITTEST_RESULT_FAIL; | |
488 | + } | |
489 | + if (val != testdata->expected[i]) { | |
490 | + printf("value failed: expected = %08x, result = %02x", val, testdata->expected[i]); | |
491 | + result = UNITTEST_RESULT_FAIL; | |
492 | + } | |
493 | + | |
494 | + err = bytearray_cursor_move(&cursor, 1); | |
495 | + if (err < 0) { | |
496 | + printf("bytearray_cursor_move error: %08x", err); | |
497 | + result = UNITTEST_RESULT_FAIL; | |
498 | + } | |
499 | + } | |
500 | + | |
501 | + bytearray_cursor_finalize(&cursor); | |
502 | + | |
503 | + bytearray_finalize(&array); | |
504 | + | |
505 | + return result; | |
506 | +} | |
507 | + | |
508 | +LOCAL UNITTEST_RESULT test_bytearray_cursor_getUB_1() | |
509 | +{ | |
510 | + UB init[] = {0x1, 0x2, 0x3}; | |
511 | + W init_len = sizeof(init); | |
512 | + UB expected[] = {0x1, 0x2, 0x3}; | |
513 | + W expected_len = sizeof(expected); | |
514 | + test_bytearray_cursor_getUB testdata = { | |
515 | + init, init_len, | |
516 | + expected, expected_len | |
517 | + }; | |
518 | + return test_bytearray_cursor_getUB_common(&testdata); | |
519 | +} | |
520 | + | |
521 | +typedef struct { | |
522 | + UB *init; | |
523 | + W init_len; | |
524 | + UH *expected; | |
525 | + W expected_len; | |
526 | +} test_bytearray_cursor_getUH; | |
527 | + | |
528 | +LOCAL UNITTEST_RESULT test_bytearray_cursor_getUH_common(test_bytearray_cursor_getUH *testdata) | |
529 | +{ | |
530 | + bytearray_t array; | |
531 | + W i, err; | |
532 | + bytearray_cursor_t cursor; | |
533 | + UH val; | |
534 | + UNITTEST_RESULT result = UNITTEST_RESULT_PASS; | |
535 | + | |
536 | + err = bytearray_initialize(&array); | |
537 | + if (err < 0) { | |
538 | + printf("bytearray_initialize error: %08x\n", err); | |
539 | + return UNITTEST_RESULT_FAIL; | |
540 | + } | |
541 | + | |
542 | + for (i = 0; i < testdata->init_len; i++) { | |
543 | + err = bytearray_pushback(&array, testdata->init[i]); | |
544 | + if (err < 0) { | |
545 | + printf("bytearray_pushback error: %08x", err); | |
546 | + result = UNITTEST_RESULT_FAIL; | |
547 | + break; | |
548 | + } | |
549 | + } | |
550 | + | |
551 | + bytearray_cursor_initialize(&cursor, &array); | |
552 | + | |
553 | + for (i = 0; i < testdata->expected_len; i++) { | |
554 | + err = bytearray_cursor_getUH(&cursor, &val); | |
555 | + if (err < 0) { | |
556 | + printf("bytearray_cursor_getUH error: %08x", err); | |
557 | + result = UNITTEST_RESULT_FAIL; | |
558 | + } | |
559 | + if (val != testdata->expected[i]) { | |
560 | + printf("value failed: expected = %08x, result = %02x", val, testdata->expected[i]); | |
561 | + result = UNITTEST_RESULT_FAIL; | |
562 | + } | |
563 | + | |
564 | + err = bytearray_cursor_move(&cursor, 1); | |
565 | + if (err < 0) { | |
566 | + printf("bytearray_cursor_move error: %08x", err); | |
567 | + result = UNITTEST_RESULT_FAIL; | |
568 | + } | |
569 | + } | |
570 | + | |
571 | + bytearray_cursor_finalize(&cursor); | |
572 | + | |
573 | + bytearray_finalize(&array); | |
574 | + | |
575 | + return result; | |
576 | +} | |
577 | + | |
578 | +LOCAL UNITTEST_RESULT test_bytearray_cursor_getUH_1() | |
579 | +{ | |
580 | + UB init[] = {0x1, 0x2, 0x3, 0x4}; | |
581 | + W init_len = sizeof(init); | |
582 | + UH expected[] = {0x0201, 0x0302, 0x0403}; | |
583 | + W expected_len = 3; | |
584 | + test_bytearray_cursor_getUH testdata = { | |
585 | + init, init_len, | |
586 | + expected, expected_len | |
587 | + }; | |
588 | + return test_bytearray_cursor_getUH_common(&testdata); | |
589 | +} | |
590 | + | |
591 | +typedef struct { | |
592 | + UB *init; | |
593 | + W init_len; | |
594 | + UW *expected; | |
595 | + W expected_len; | |
596 | +} test_bytearray_cursor_getUW; | |
597 | + | |
598 | +LOCAL UNITTEST_RESULT test_bytearray_cursor_getUW_common(test_bytearray_cursor_getUW *testdata) | |
599 | +{ | |
600 | + bytearray_t array; | |
601 | + W i, err; | |
602 | + bytearray_cursor_t cursor; | |
603 | + UW val; | |
604 | + UNITTEST_RESULT result = UNITTEST_RESULT_PASS; | |
605 | + | |
606 | + err = bytearray_initialize(&array); | |
607 | + if (err < 0) { | |
608 | + printf("bytearray_initialize error: %08x\n", err); | |
609 | + return UNITTEST_RESULT_FAIL; | |
610 | + } | |
611 | + | |
612 | + for (i = 0; i < testdata->init_len; i++) { | |
613 | + err = bytearray_pushback(&array, testdata->init[i]); | |
614 | + if (err < 0) { | |
615 | + printf("bytearray_pushback error: %08x", err); | |
616 | + result = UNITTEST_RESULT_FAIL; | |
617 | + break; | |
618 | + } | |
619 | + } | |
620 | + | |
621 | + bytearray_cursor_initialize(&cursor, &array); | |
622 | + | |
623 | + for (i = 0; i < testdata->expected_len; i++) { | |
624 | + err = bytearray_cursor_getUW(&cursor, &val); | |
625 | + if (err < 0) { | |
626 | + printf("bytearray_cursor_getUW error: %08x", err); | |
627 | + result = UNITTEST_RESULT_FAIL; | |
628 | + } | |
629 | + if (val != testdata->expected[i]) { | |
630 | + printf("value failed: expected = %08x, result = %02x", val, testdata->expected[i]); | |
631 | + result = UNITTEST_RESULT_FAIL; | |
632 | + } | |
633 | + | |
634 | + err = bytearray_cursor_move(&cursor, 1); | |
635 | + if (err < 0) { | |
636 | + printf("bytearray_cursor_move error: %08x", err); | |
637 | + result = UNITTEST_RESULT_FAIL; | |
638 | + } | |
639 | + } | |
640 | + | |
641 | + bytearray_cursor_finalize(&cursor); | |
642 | + | |
643 | + bytearray_finalize(&array); | |
644 | + | |
645 | + return result; | |
646 | +} | |
647 | + | |
648 | +LOCAL UNITTEST_RESULT test_bytearray_cursor_getUW_1() | |
649 | +{ | |
650 | + UB init[] = {0x1, 0x2, 0x3, 0x4, 0x5}; | |
651 | + W init_len = sizeof(init); | |
652 | + UW expected[] = {0x04030201, 0x05040302}; | |
653 | + W expected_len = 2; | |
654 | + test_bytearray_cursor_getUW testdata = { | |
655 | + init, init_len, | |
656 | + expected, expected_len | |
657 | + }; | |
658 | + return test_bytearray_cursor_getUW_common(&testdata); | |
659 | +} | |
660 | + | |
661 | +EXPORT VOID test_bytearray_main(unittest_driver_t *driver) | |
662 | +{ | |
663 | + UNITTEST_DRIVER_REGIST(driver, test_bytearray_1); | |
664 | + UNITTEST_DRIVER_REGIST(driver, test_bytearray_2); | |
665 | + UNITTEST_DRIVER_REGIST(driver, test_bytearray_cursor_insert_1); | |
666 | + UNITTEST_DRIVER_REGIST(driver, test_bytearray_cursor_insert_2); | |
667 | + UNITTEST_DRIVER_REGIST(driver, test_bytearray_cursor_insert_3); | |
668 | + UNITTEST_DRIVER_REGIST(driver, test_bytearray_cursor_erase_1); | |
669 | + UNITTEST_DRIVER_REGIST(driver, test_bytearray_cursor_erase_2); | |
670 | + UNITTEST_DRIVER_REGIST(driver, test_bytearray_cursor_erase_3); | |
671 | + UNITTEST_DRIVER_REGIST(driver, test_bytearray_cursor_erase_4); | |
672 | + UNITTEST_DRIVER_REGIST(driver, test_bytearray_cursor_erase_5); | |
673 | + UNITTEST_DRIVER_REGIST(driver, test_bytearray_cursor_erase_6); | |
674 | + UNITTEST_DRIVER_REGIST(driver, test_bytearray_cursor_erase_7); | |
675 | + UNITTEST_DRIVER_REGIST(driver, test_bytearray_cursor_erase_8); | |
676 | + UNITTEST_DRIVER_REGIST(driver, test_bytearray_cursor_erase_9); | |
677 | + UNITTEST_DRIVER_REGIST(driver, test_bytearray_cursor_getUB_1); | |
678 | + UNITTEST_DRIVER_REGIST(driver, test_bytearray_cursor_getUH_1); | |
679 | + UNITTEST_DRIVER_REGIST(driver, test_bytearray_cursor_getUW_1); | |
680 | +} |
@@ -0,0 +1,235 @@ | ||
1 | +/* | |
2 | + * bytearray.c | |
3 | + * | |
4 | + * Copyright (c) 2013 project bchan | |
5 | + * | |
6 | + * This software is provided 'as-is', without any express or implied | |
7 | + * warranty. In no event will the authors be held liable for any damages | |
8 | + * arising from the use of this software. | |
9 | + * | |
10 | + * Permission is granted to anyone to use this software for any purpose, | |
11 | + * including commercial applications, and to alter it and redistribute it | |
12 | + * freely, subject to the following restrictions: | |
13 | + * | |
14 | + * 1. The origin of this software must not be misrepresented; you must not | |
15 | + * claim that you wrote the original software. If you use this software | |
16 | + * in a product, an acknowledgment in the product documentation would be | |
17 | + * appreciated but is not required. | |
18 | + * | |
19 | + * 2. Altered source versions must be plainly marked as such, and must not be | |
20 | + * misrepresented as being the original software. | |
21 | + * | |
22 | + * 3. This notice may not be removed or altered from any source | |
23 | + * distribution. | |
24 | + * | |
25 | + */ | |
26 | + | |
27 | +#include "bytearray.h" | |
28 | + | |
29 | +#include <basic.h> | |
30 | +#include <bstdlib.h> | |
31 | +#include <bstdio.h> | |
32 | +#include <bstring.h> | |
33 | + | |
34 | +#ifdef BCHAN_CONFIG_DEBUG | |
35 | +# define DP(arg) printf arg | |
36 | +# define DP_ER(msg, err) printf("%s (%d/%x)\n", msg, err>>16, err) | |
37 | +#else | |
38 | +# define DP(arg) /**/ | |
39 | +# define DP_ER(msg, err) /**/ | |
40 | +#endif | |
41 | + | |
42 | +EXPORT UB* bytearray_getbuffer(bytearray_t *bytearray) | |
43 | +{ | |
44 | + return bytearray->ptr; | |
45 | +} | |
46 | + | |
47 | +EXPORT W bytearray_getlength(bytearray_t *bytearray) | |
48 | +{ | |
49 | + return bytearray->len; | |
50 | +} | |
51 | + | |
52 | +EXPORT W bytearray_pushback(bytearray_t *bytearray, UB val) | |
53 | +{ | |
54 | + UB *p; | |
55 | + | |
56 | + p = realloc(bytearray->ptr, bytearray->len+1); | |
57 | + if (p == NULL) { | |
58 | + return -1; | |
59 | + } | |
60 | + bytearray->ptr = p; | |
61 | + bytearray->ptr[bytearray->len] = val; | |
62 | + bytearray->len++; | |
63 | + return 0; | |
64 | +} | |
65 | + | |
66 | +EXPORT W bytearray_popback(bytearray_t *bytearray) | |
67 | +{ | |
68 | + if (bytearray->len == 0) { | |
69 | + return -1; /* TODO */ | |
70 | + } | |
71 | + bytearray->len--; | |
72 | + return 0; | |
73 | +} | |
74 | + | |
75 | +EXPORT W bytearray_initialize(bytearray_t *bytearray) | |
76 | +{ | |
77 | + bytearray->ptr = NULL; | |
78 | + bytearray->len = 0; | |
79 | + return 0; | |
80 | +} | |
81 | + | |
82 | +EXPORT VOID bytearray_finalize(bytearray_t *bytearray) | |
83 | +{ | |
84 | + if (bytearray->ptr != NULL) { | |
85 | + free(bytearray->ptr); | |
86 | + } | |
87 | +} | |
88 | + | |
89 | +EXPORT W bytearray_cursor_move(bytearray_cursor_t *cursor, W diff) | |
90 | +{ | |
91 | + W next = cursor->index + diff; | |
92 | + | |
93 | + if (next < 0) { | |
94 | + return -1; /* TODO */ | |
95 | + } | |
96 | + if (next > cursor->target->len) { | |
97 | + return -1; /* TODO */ | |
98 | + } | |
99 | + cursor->index = next; | |
100 | + | |
101 | + return 0; | |
102 | +} | |
103 | + | |
104 | +EXPORT W bytearray_cursor_erase(bytearray_cursor_t *cursor, W len) | |
105 | +{ | |
106 | + Bool isend; | |
107 | + UB *dest, *src; | |
108 | + size_t move_len; | |
109 | + | |
110 | + if (len < 0) { | |
111 | + return -1; /* TODO */ | |
112 | + } | |
113 | + | |
114 | + isend = bytearray_cursor_isend(cursor); | |
115 | + if (isend != False) { | |
116 | + return -1; /* TODO */ | |
117 | + } | |
118 | + | |
119 | + if (cursor->index + len > cursor->target->len) { | |
120 | + cursor->target->len = cursor->index; | |
121 | + } else { | |
122 | + dest = cursor->target->ptr + cursor->index; | |
123 | + src = dest + len; | |
124 | + move_len = cursor->target->len - (cursor->index + len); | |
125 | + memmove(dest, src, move_len); | |
126 | + cursor->target->len -= len; | |
127 | + } | |
128 | + | |
129 | + return 0; | |
130 | +} | |
131 | + | |
132 | +EXPORT W bytearray_cursor_insert(bytearray_cursor_t *cursor, UB *data, W len) | |
133 | +{ | |
134 | + UB *p; | |
135 | + UB *dest, *src; | |
136 | + | |
137 | + if (len < 0) { | |
138 | + return -1; /* TODO */ | |
139 | + } | |
140 | + if (len == 0) { | |
141 | + return 0; | |
142 | + } | |
143 | + | |
144 | + p = realloc(cursor->target->ptr, cursor->target->len + len); | |
145 | + if (p == NULL) { | |
146 | + return -1; /* TODO */ | |
147 | + } | |
148 | + cursor->target->ptr = p; | |
149 | + | |
150 | + /* move previous data */ | |
151 | + src = cursor->target->ptr + cursor->index; | |
152 | + dest = src + len; | |
153 | + memmove(dest, src, cursor->target->len - cursor->index); | |
154 | + | |
155 | + /* insert new data */ | |
156 | + dest = src; | |
157 | + src = data; | |
158 | + memcpy(dest, src, len); | |
159 | + | |
160 | + cursor->target->len += len; | |
161 | + | |
162 | + return 0; | |
163 | +} | |
164 | + | |
165 | +EXPORT Bool bytearray_cursor_isend(bytearray_cursor_t *cursor) | |
166 | +{ | |
167 | + if (cursor->index == cursor->target->len) { | |
168 | + return True; | |
169 | + } | |
170 | + return False; | |
171 | +} | |
172 | + | |
173 | +EXPORT W bytearray_cursor_getB(bytearray_cursor_t *cursor, B *p) | |
174 | +{ | |
175 | + if (cursor->index + sizeof(B) > cursor->target->len) { | |
176 | + return -1; /* TODO */ | |
177 | + } | |
178 | + *p = *(cursor->target->ptr + cursor->index); | |
179 | + return 0; | |
180 | +} | |
181 | + | |
182 | +EXPORT W bytearray_cursor_getH(bytearray_cursor_t *cursor, H *p) | |
183 | +{ | |
184 | + if (cursor->index + sizeof(H) > cursor->target->len) { | |
185 | + return -1; /* TODO */ | |
186 | + } | |
187 | + *p = *(H*)(cursor->target->ptr + cursor->index); | |
188 | + return 0; | |
189 | +} | |
190 | + | |
191 | +EXPORT W bytearray_cursor_getW(bytearray_cursor_t *cursor, W *p) | |
192 | +{ | |
193 | + if (cursor->index + sizeof(W) > cursor->target->len) { | |
194 | + return -1; /* TODO */ | |
195 | + } | |
196 | + *p = *(W*)(cursor->target->ptr + cursor->index); | |
197 | + return 0; | |
198 | +} | |
199 | + | |
200 | +EXPORT W bytearray_cursor_getUB(bytearray_cursor_t *cursor, UB *p) | |
201 | +{ | |
202 | + if (cursor->index + sizeof(UB) > cursor->target->len) { | |
203 | + return -1; /* TODO */ | |
204 | + } | |
205 | + *p = *(cursor->target->ptr + cursor->index); | |
206 | + return 0; | |
207 | +} | |
208 | + | |
209 | +EXPORT W bytearray_cursor_getUH(bytearray_cursor_t *cursor, UH *p) | |
210 | +{ | |
211 | + if (cursor->index + sizeof(UH) > cursor->target->len) { | |
212 | + return -1; /* TODO */ | |
213 | + } | |
214 | + *p = *(UH*)(cursor->target->ptr + cursor->index); | |
215 | + return 0; | |
216 | +} | |
217 | + | |
218 | +EXPORT W bytearray_cursor_getUW(bytearray_cursor_t *cursor, UW *p) | |
219 | +{ | |
220 | + if (cursor->index + sizeof(UW) > cursor->target->len) { | |
221 | + return -1; /* TODO */ | |
222 | + } | |
223 | + *p = *(UW*)(cursor->target->ptr + cursor->index); | |
224 | + return 0; | |
225 | +} | |
226 | + | |
227 | +EXPORT VOID bytearray_cursor_initialize(bytearray_cursor_t *cursor, bytearray_t *bytearray) | |
228 | +{ | |
229 | + cursor->target = bytearray; | |
230 | + cursor->index = 0; | |
231 | +} | |
232 | + | |
233 | +EXPORT VOID bytearray_cursor_finalize(bytearray_cursor_t *cursor) | |
234 | +{ | |
235 | +} |
@@ -0,0 +1,74 @@ | ||
1 | +/* | |
2 | + * bytearray.h | |
3 | + * | |
4 | + * Copyright (c) 2013 project bchan | |
5 | + * | |
6 | + * This software is provided 'as-is', without any express or implied | |
7 | + * warranty. In no event will the authors be held liable for any damages | |
8 | + * arising from the use of this software. | |
9 | + * | |
10 | + * Permission is granted to anyone to use this software for any purpose, | |
11 | + * including commercial applications, and to alter it and redistribute it | |
12 | + * freely, subject to the following restrictions: | |
13 | + * | |
14 | + * 1. The origin of this software must not be misrepresented; you must not | |
15 | + * claim that you wrote the original software. If you use this software | |
16 | + * in a product, an acknowledgment in the product documentation would be | |
17 | + * appreciated but is not required. | |
18 | + * | |
19 | + * 2. Altered source versions must be plainly marked as such, and must not be | |
20 | + * misrepresented as being the original software. | |
21 | + * | |
22 | + * 3. This notice may not be removed or altered from any source | |
23 | + * distribution. | |
24 | + * | |
25 | + */ | |
26 | + | |
27 | +/* Vendor name: */ | |
28 | +/* Functionality name: bytearray */ | |
29 | +/* Detail name: */ | |
30 | + | |
31 | +#include <basic.h> | |
32 | + | |
33 | +#ifndef __BYTEARRAY_H__ | |
34 | +#define __BYTEARRAY_H__ | |
35 | + | |
36 | +/* Vendor name: */ | |
37 | +/* Functionality name: bytearray */ | |
38 | +/* Detail name: */ | |
39 | +struct bytearray_t_ { | |
40 | + UB *ptr; | |
41 | + W len; | |
42 | +}; | |
43 | +typedef struct bytearray_t_ bytearray_t; | |
44 | + | |
45 | +/* Vendor name: */ | |
46 | +/* Functionality name: bytearray */ | |
47 | +/* Detail name: cursor */ | |
48 | +struct bytearray_cursor_t_ { | |
49 | + bytearray_t *target; | |
50 | + W index; | |
51 | +}; | |
52 | +typedef struct bytearray_cursor_t_ bytearray_cursor_t; | |
53 | + | |
54 | +IMPORT W bytearray_initialize(bytearray_t *bytearray); | |
55 | +IMPORT VOID bytearray_finalize(bytearray_t *bytearray); | |
56 | +IMPORT UB* bytearray_getbuffer(bytearray_t *bytearray); | |
57 | +IMPORT W bytearray_getlength(bytearray_t *bytearray); | |
58 | +IMPORT W bytearray_pushback(bytearray_t *bytearray, UB val); | |
59 | +IMPORT W bytearray_popback(bytearray_t *bytearray); | |
60 | + | |
61 | +IMPORT VOID bytearray_cursor_initialize(bytearray_cursor_t *cursor, bytearray_t *bytearray); | |
62 | +IMPORT VOID bytearray_cursor_finalize(bytearray_cursor_t *cursor); | |
63 | +IMPORT W bytearray_cursor_move(bytearray_cursor_t *cursor, W diff); | |
64 | +IMPORT W bytearray_cursor_erase(bytearray_cursor_t *cursor, W len); | |
65 | +IMPORT W bytearray_cursor_insert(bytearray_cursor_t *cursor, UB *data, W len); | |
66 | +IMPORT Bool bytearray_cursor_isend(bytearray_cursor_t *cursor); | |
67 | +IMPORT W bytearray_cursor_getB(bytearray_cursor_t *cursor, B *p); | |
68 | +IMPORT W bytearray_cursor_getH(bytearray_cursor_t *cursor, H *p); | |
69 | +IMPORT W bytearray_cursor_getW(bytearray_cursor_t *cursor, W *p); | |
70 | +IMPORT W bytearray_cursor_getUB(bytearray_cursor_t *cursor, UB *p); | |
71 | +IMPORT W bytearray_cursor_getUH(bytearray_cursor_t *cursor, UH *p); | |
72 | +IMPORT W bytearray_cursor_getUW(bytearray_cursor_t *cursor, UW *p); | |
73 | + | |
74 | +#endif |