add handling word data array structure.
@@ -0,0 +1,538 @@ | ||
1 | +/* | |
2 | + * test_wordarray.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 "wordarray.h" | |
30 | + | |
31 | +#include <btron/btron.h> | |
32 | +#include <bstdio.h> | |
33 | + | |
34 | +LOCAL UNITTEST_RESULT test_wordarray_1() | |
35 | +{ | |
36 | + wordarray_t array; | |
37 | + W i, len, err; | |
38 | + UNITTEST_RESULT result = UNITTEST_RESULT_PASS; | |
39 | + UW data[] = {0x1, 0x2, 0x3}, *p; | |
40 | + W data_len = sizeof(data); | |
41 | + | |
42 | + err = wordarray_initialize(&array); | |
43 | + if (err < 0) { | |
44 | + printf("wordarray_initialize error: %08x\n", err); | |
45 | + return UNITTEST_RESULT_FAIL; | |
46 | + } | |
47 | + | |
48 | + for (i = 0; i < data_len; i++) { | |
49 | + err = wordarray_pushback(&array, data[i]); | |
50 | + if (err < 0) { | |
51 | + printf("wordarray_pushback error: %08x", err); | |
52 | + result = UNITTEST_RESULT_FAIL; | |
53 | + break; | |
54 | + } | |
55 | + } | |
56 | + | |
57 | + p = wordarray_getbuffer(&array); | |
58 | + len = wordarray_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 | + wordarray_finalize(&array); | |
71 | + | |
72 | + return result; | |
73 | +} | |
74 | + | |
75 | +LOCAL UNITTEST_RESULT test_wordarray_2() | |
76 | +{ | |
77 | + wordarray_t array; | |
78 | + W i, len, err; | |
79 | + UNITTEST_RESULT result = UNITTEST_RESULT_PASS; | |
80 | + UW data[] = {0x1, 0x2, 0x3}, *p; | |
81 | + W data_len = sizeof(data); | |
82 | + | |
83 | + err = wordarray_initialize(&array); | |
84 | + if (err < 0) { | |
85 | + printf("wordarray_initialize error: %08x\n", err); | |
86 | + return UNITTEST_RESULT_FAIL; | |
87 | + } | |
88 | + | |
89 | + for (i = 0; i < data_len; i++) { | |
90 | + err = wordarray_pushback(&array, data[i]); | |
91 | + if (err < 0) { | |
92 | + printf("wordarray_pushback error: %08x", err); | |
93 | + result = UNITTEST_RESULT_FAIL; | |
94 | + break; | |
95 | + } | |
96 | + } | |
97 | + err = wordarray_popback(&array); | |
98 | + if (err < 0) { | |
99 | + printf("wordarray_pushback error: %08x", err); | |
100 | + result = UNITTEST_RESULT_FAIL; | |
101 | + } | |
102 | + | |
103 | + p = wordarray_getbuffer(&array); | |
104 | + len = wordarray_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 | + wordarray_finalize(&array); | |
117 | + | |
118 | + return result; | |
119 | +} | |
120 | + | |
121 | +typedef struct { | |
122 | + UW *init; | |
123 | + W init_len; | |
124 | + UW *insert; | |
125 | + W insert_len; | |
126 | + W insert_at; | |
127 | + UW *expected; | |
128 | + W expected_len; | |
129 | +} test_wordarray_cursor_insert; | |
130 | + | |
131 | +LOCAL UNITTEST_RESULT test_wordarray_cursor_insert_common(test_wordarray_cursor_insert *testdata) | |
132 | +{ | |
133 | + wordarray_t array; | |
134 | + W i, len, err; | |
135 | + wordarray_cursor_t cursor; | |
136 | + UW *p; | |
137 | + UNITTEST_RESULT result = UNITTEST_RESULT_PASS; | |
138 | + | |
139 | + err = wordarray_initialize(&array); | |
140 | + if (err < 0) { | |
141 | + printf("wordarray_initialize error: %08x\n", err); | |
142 | + return UNITTEST_RESULT_FAIL; | |
143 | + } | |
144 | + | |
145 | + for (i = 0; i < testdata->init_len; i++) { | |
146 | + err = wordarray_pushback(&array, testdata->init[i]); | |
147 | + if (err < 0) { | |
148 | + printf("wordarray_pushback error: %08x", err); | |
149 | + result = UNITTEST_RESULT_FAIL; | |
150 | + break; | |
151 | + } | |
152 | + } | |
153 | + | |
154 | + wordarray_cursor_initialize(&cursor, &array); | |
155 | + | |
156 | + err = wordarray_cursor_move(&cursor, testdata->insert_at); | |
157 | + if (err < 0) { | |
158 | + printf("wordarray_cursor_move error: %08x", err); | |
159 | + result = UNITTEST_RESULT_FAIL; | |
160 | + } | |
161 | + err = wordarray_cursor_insert(&cursor, testdata->insert, testdata->insert_len); | |
162 | + if (err < 0) { | |
163 | + printf("wordarray_cursor_insert error: %08x", err); | |
164 | + result = UNITTEST_RESULT_FAIL; | |
165 | + } | |
166 | + | |
167 | + wordarray_cursor_finalize(&cursor); | |
168 | + | |
169 | + p = wordarray_getbuffer(&array); | |
170 | + len = wordarray_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 | + wordarray_finalize(&array); | |
185 | + | |
186 | + return result; | |
187 | +} | |
188 | + | |
189 | +LOCAL UNITTEST_RESULT test_wordarray_cursor_insert_1() | |
190 | +{ | |
191 | + UW init[] = {0x1, 0x2, 0x3}; | |
192 | + W init_len = sizeof(init)/sizeof(UW); | |
193 | + UW insert[] = {0xA, 0xB}; | |
194 | + W insert_len = sizeof(insert)/sizeof(UW); | |
195 | + W insert_at = 0; | |
196 | + UW expected[] = {0xA, 0xB, 0x1, 0x2, 0x3}; | |
197 | + W expected_len = sizeof(expected)/sizeof(UW); | |
198 | + test_wordarray_cursor_insert testdata = { | |
199 | + init, init_len, | |
200 | + insert, insert_len, insert_at, | |
201 | + expected, expected_len | |
202 | + }; | |
203 | + return test_wordarray_cursor_insert_common(&testdata); | |
204 | +} | |
205 | + | |
206 | +LOCAL UNITTEST_RESULT test_wordarray_cursor_insert_2() | |
207 | +{ | |
208 | + UW init[] = {0x1, 0x2, 0x3}; | |
209 | + W init_len = sizeof(init)/sizeof(UW); | |
210 | + UW insert[] = {0xA, 0xB}; | |
211 | + W insert_len = sizeof(insert)/sizeof(UW); | |
212 | + W insert_at = 1; | |
213 | + UW expected[] = {0x1, 0xA, 0xB, 0x2, 0x3}; | |
214 | + W expected_len = sizeof(expected)/sizeof(UW); | |
215 | + test_wordarray_cursor_insert testdata = { | |
216 | + init, init_len, | |
217 | + insert, insert_len, insert_at, | |
218 | + expected, expected_len | |
219 | + }; | |
220 | + return test_wordarray_cursor_insert_common(&testdata); | |
221 | +} | |
222 | + | |
223 | +LOCAL UNITTEST_RESULT test_wordarray_cursor_insert_3() | |
224 | +{ | |
225 | + UW init[] = {0x1, 0x2, 0x3}; | |
226 | + W init_len = sizeof(init)/sizeof(UW); | |
227 | + UW insert[] = {0xA, 0xB}; | |
228 | + W insert_len = sizeof(insert)/sizeof(UW); | |
229 | + W insert_at = 2; | |
230 | + UW expected[] = {0x1, 0x2, 0xA, 0xB, 0x3}; | |
231 | + W expected_len = sizeof(expected)/sizeof(UW); | |
232 | + test_wordarray_cursor_insert testdata = { | |
233 | + init, init_len, | |
234 | + insert, insert_len, insert_at, | |
235 | + expected, expected_len | |
236 | + }; | |
237 | + return test_wordarray_cursor_insert_common(&testdata); | |
238 | +} | |
239 | + | |
240 | +typedef struct { | |
241 | + UW *init; | |
242 | + W init_len; | |
243 | + W erase_at; | |
244 | + W erase_len; | |
245 | + UW *expected; | |
246 | + W expected_len; | |
247 | +} test_wordarray_cursor_erase; | |
248 | + | |
249 | +LOCAL UNITTEST_RESULT test_wordarray_cursor_erase_common(test_wordarray_cursor_erase *testdata) | |
250 | +{ | |
251 | + wordarray_t array; | |
252 | + W i, len, err; | |
253 | + wordarray_cursor_t cursor; | |
254 | + UW *p; | |
255 | + UNITTEST_RESULT result = UNITTEST_RESULT_PASS; | |
256 | + | |
257 | + err = wordarray_initialize(&array); | |
258 | + if (err < 0) { | |
259 | + printf("wordarray_initialize error: %08x\n", err); | |
260 | + return UNITTEST_RESULT_FAIL; | |
261 | + } | |
262 | + | |
263 | + for (i = 0; i < testdata->init_len; i++) { | |
264 | + err = wordarray_pushback(&array, testdata->init[i]); | |
265 | + if (err < 0) { | |
266 | + printf("wordarray_pushback error: %08x", err); | |
267 | + result = UNITTEST_RESULT_FAIL; | |
268 | + break; | |
269 | + } | |
270 | + } | |
271 | + | |
272 | + wordarray_cursor_initialize(&cursor, &array); | |
273 | + | |
274 | + err = wordarray_cursor_move(&cursor, testdata->erase_at); | |
275 | + if (err < 0) { | |
276 | + printf("wordarray_cursor_move error: %08x", err); | |
277 | + result = UNITTEST_RESULT_FAIL; | |
278 | + } | |
279 | + err = wordarray_cursor_erase(&cursor, testdata->erase_len); | |
280 | + if (err < 0) { | |
281 | + printf("wordarray_cursor_erase error: %08x", err); | |
282 | + result = UNITTEST_RESULT_FAIL; | |
283 | + } | |
284 | + | |
285 | + wordarray_cursor_finalize(&cursor); | |
286 | + | |
287 | + p = wordarray_getbuffer(&array); | |
288 | + len = wordarray_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 | + wordarray_finalize(&array); | |
303 | + | |
304 | + return result; | |
305 | +} | |
306 | + | |
307 | +LOCAL UNITTEST_RESULT test_wordarray_cursor_erase_1() | |
308 | +{ | |
309 | + UW init[] = {0x1, 0x2, 0x3}; | |
310 | + W init_len = sizeof(init)/sizeof(UW); | |
311 | + W erase_at = 0; | |
312 | + W erase_len = 0; | |
313 | + UW expected[] = {0x1, 0x2, 0x3}; | |
314 | + W expected_len = sizeof(expected)/sizeof(UW); | |
315 | + test_wordarray_cursor_erase testdata = { | |
316 | + init, init_len, | |
317 | + erase_at, erase_len, | |
318 | + expected, expected_len | |
319 | + }; | |
320 | + return test_wordarray_cursor_erase_common(&testdata); | |
321 | +} | |
322 | + | |
323 | +LOCAL UNITTEST_RESULT test_wordarray_cursor_erase_2() | |
324 | +{ | |
325 | + UW init[] = {0x1, 0x2, 0x3}; | |
326 | + W init_len = sizeof(init)/sizeof(UW); | |
327 | + W erase_at = 0; | |
328 | + W erase_len = 1; | |
329 | + UW expected[] = {0x2, 0x3}; | |
330 | + W expected_len = sizeof(expected)/sizeof(UW); | |
331 | + test_wordarray_cursor_erase testdata = { | |
332 | + init, init_len, | |
333 | + erase_at, erase_len, | |
334 | + expected, expected_len | |
335 | + }; | |
336 | + return test_wordarray_cursor_erase_common(&testdata); | |
337 | +} | |
338 | + | |
339 | +LOCAL UNITTEST_RESULT test_wordarray_cursor_erase_3() | |
340 | +{ | |
341 | + UW init[] = {0x1, 0x2, 0x3}; | |
342 | + W init_len = sizeof(init)/sizeof(UW); | |
343 | + W erase_at = 0; | |
344 | + W erase_len = 2; | |
345 | + UW expected[] = {0x3}; | |
346 | + W expected_len = sizeof(expected)/sizeof(UW); | |
347 | + test_wordarray_cursor_erase testdata = { | |
348 | + init, init_len, | |
349 | + erase_at, erase_len, | |
350 | + expected, expected_len | |
351 | + }; | |
352 | + return test_wordarray_cursor_erase_common(&testdata); | |
353 | +} | |
354 | + | |
355 | +LOCAL UNITTEST_RESULT test_wordarray_cursor_erase_4() | |
356 | +{ | |
357 | + UW init[] = {0x1, 0x2, 0x3}; | |
358 | + W init_len = sizeof(init)/sizeof(UW); | |
359 | + W erase_at = 0; | |
360 | + W erase_len = 3; | |
361 | + UW expected[] = {}; | |
362 | + W expected_len = sizeof(expected)/sizeof(UW); | |
363 | + test_wordarray_cursor_erase testdata = { | |
364 | + init, init_len, | |
365 | + erase_at, erase_len, | |
366 | + expected, expected_len | |
367 | + }; | |
368 | + return test_wordarray_cursor_erase_common(&testdata); | |
369 | +} | |
370 | + | |
371 | +LOCAL UNITTEST_RESULT test_wordarray_cursor_erase_5() | |
372 | +{ | |
373 | + UW init[] = {0x1, 0x2, 0x3}; | |
374 | + W init_len = sizeof(init)/sizeof(UW); | |
375 | + W erase_at = 1; | |
376 | + W erase_len = 0; | |
377 | + UW expected[] = {0x1, 0x2, 0x3}; | |
378 | + W expected_len = sizeof(expected)/sizeof(UW); | |
379 | + test_wordarray_cursor_erase testdata = { | |
380 | + init, init_len, | |
381 | + erase_at, erase_len, | |
382 | + expected, expected_len | |
383 | + }; | |
384 | + return test_wordarray_cursor_erase_common(&testdata); | |
385 | +} | |
386 | + | |
387 | +LOCAL UNITTEST_RESULT test_wordarray_cursor_erase_6() | |
388 | +{ | |
389 | + UW init[] = {0x1, 0x2, 0x3}; | |
390 | + W init_len = sizeof(init)/sizeof(UW); | |
391 | + W erase_at = 1; | |
392 | + W erase_len = 1; | |
393 | + UW expected[] = {0x1, 0x3}; | |
394 | + W expected_len = sizeof(expected)/sizeof(UW); | |
395 | + test_wordarray_cursor_erase testdata = { | |
396 | + init, init_len, | |
397 | + erase_at, erase_len, | |
398 | + expected, expected_len | |
399 | + }; | |
400 | + return test_wordarray_cursor_erase_common(&testdata); | |
401 | +} | |
402 | + | |
403 | +LOCAL UNITTEST_RESULT test_wordarray_cursor_erase_7() | |
404 | +{ | |
405 | + UW init[] = {0x1, 0x2, 0x3}; | |
406 | + W init_len = sizeof(init)/sizeof(UW); | |
407 | + W erase_at = 1; | |
408 | + W erase_len = 2; | |
409 | + UW expected[] = {0x1}; | |
410 | + W expected_len = sizeof(expected)/sizeof(UW); | |
411 | + test_wordarray_cursor_erase testdata = { | |
412 | + init, init_len, | |
413 | + erase_at, erase_len, | |
414 | + expected, expected_len | |
415 | + }; | |
416 | + return test_wordarray_cursor_erase_common(&testdata); | |
417 | +} | |
418 | + | |
419 | +LOCAL UNITTEST_RESULT test_wordarray_cursor_erase_8() | |
420 | +{ | |
421 | + UW init[] = {0x1, 0x2, 0x3}; | |
422 | + W init_len = sizeof(init)/sizeof(UW); | |
423 | + W erase_at = 2; | |
424 | + W erase_len = 0; | |
425 | + UW expected[] = {0x1, 0x2, 0x3}; | |
426 | + W expected_len = sizeof(expected)/sizeof(UW); | |
427 | + test_wordarray_cursor_erase testdata = { | |
428 | + init, init_len, | |
429 | + erase_at, erase_len, | |
430 | + expected, expected_len | |
431 | + }; | |
432 | + return test_wordarray_cursor_erase_common(&testdata); | |
433 | +} | |
434 | + | |
435 | +LOCAL UNITTEST_RESULT test_wordarray_cursor_erase_9() | |
436 | +{ | |
437 | + UW init[] = {0x1, 0x2, 0x3}; | |
438 | + W init_len = sizeof(init)/sizeof(UW); | |
439 | + W erase_at = 2; | |
440 | + W erase_len = 1; | |
441 | + UW expected[] = {0x1, 0x2}; | |
442 | + W expected_len = sizeof(expected)/sizeof(UW); | |
443 | + test_wordarray_cursor_erase testdata = { | |
444 | + init, init_len, | |
445 | + erase_at, erase_len, | |
446 | + expected, expected_len | |
447 | + }; | |
448 | + return test_wordarray_cursor_erase_common(&testdata); | |
449 | +} | |
450 | + | |
451 | +typedef struct { | |
452 | + UW *init; | |
453 | + W init_len; | |
454 | + UW *expected; | |
455 | + W expected_len; | |
456 | +} test_wordarray_cursor_getUW; | |
457 | + | |
458 | +LOCAL UNITTEST_RESULT test_wordarray_cursor_getUW_common(test_wordarray_cursor_getUW *testdata) | |
459 | +{ | |
460 | + wordarray_t array; | |
461 | + W i, err; | |
462 | + wordarray_cursor_t cursor; | |
463 | + UW val; | |
464 | + UNITTEST_RESULT result = UNITTEST_RESULT_PASS; | |
465 | + | |
466 | + err = wordarray_initialize(&array); | |
467 | + if (err < 0) { | |
468 | + printf("wordarray_initialize error: %08x\n", err); | |
469 | + return UNITTEST_RESULT_FAIL; | |
470 | + } | |
471 | + | |
472 | + for (i = 0; i < testdata->init_len; i++) { | |
473 | + err = wordarray_pushback(&array, testdata->init[i]); | |
474 | + if (err < 0) { | |
475 | + printf("wordarray_pushback error: %08x", err); | |
476 | + result = UNITTEST_RESULT_FAIL; | |
477 | + break; | |
478 | + } | |
479 | + } | |
480 | + | |
481 | + wordarray_cursor_initialize(&cursor, &array); | |
482 | + | |
483 | + for (i = 0; i < testdata->expected_len; i++) { | |
484 | + err = wordarray_cursor_getUW(&cursor, &val); | |
485 | + if (err < 0) { | |
486 | + printf("wordarray_cursor_getUW 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 = wordarray_cursor_move(&cursor, 1); | |
495 | + if (err < 0) { | |
496 | + printf("wordarray_cursor_move error: %08x", err); | |
497 | + result = UNITTEST_RESULT_FAIL; | |
498 | + } | |
499 | + } | |
500 | + | |
501 | + wordarray_cursor_finalize(&cursor); | |
502 | + | |
503 | + wordarray_finalize(&array); | |
504 | + | |
505 | + return result; | |
506 | +} | |
507 | + | |
508 | +LOCAL UNITTEST_RESULT test_wordarray_cursor_getUW_1() | |
509 | +{ | |
510 | + UW init[] = {0x1, 0x2, 0x3, 0x4, 0x5}; | |
511 | + W init_len = sizeof(init)/sizeof(UW); | |
512 | + UW expected[] = {0x1, 0x2, 0x3, 0x4, 0x5}; | |
513 | + W expected_len = sizeof(expected)/sizeof(UW); | |
514 | + test_wordarray_cursor_getUW testdata = { | |
515 | + init, init_len, | |
516 | + expected, expected_len | |
517 | + }; | |
518 | + return test_wordarray_cursor_getUW_common(&testdata); | |
519 | +} | |
520 | + | |
521 | +EXPORT VOID test_wordarray_main(unittest_driver_t *driver) | |
522 | +{ | |
523 | + UNITTEST_DRIVER_REGIST(driver, test_wordarray_1); | |
524 | + UNITTEST_DRIVER_REGIST(driver, test_wordarray_2); | |
525 | + UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_insert_1); | |
526 | + UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_insert_2); | |
527 | + UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_insert_3); | |
528 | + UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_erase_1); | |
529 | + UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_erase_2); | |
530 | + UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_erase_3); | |
531 | + UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_erase_4); | |
532 | + UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_erase_5); | |
533 | + UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_erase_6); | |
534 | + UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_erase_7); | |
535 | + UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_erase_8); | |
536 | + UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_erase_9); | |
537 | + UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_getUW_1); | |
538 | +} |
@@ -0,0 +1,140 @@ | ||
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 "wordarray.h" | |
28 | + | |
29 | +#include <basic.h> | |
30 | +#include <bstdio.h> | |
31 | + | |
32 | +#include "bytearray.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 UW* wordarray_getbuffer(wordarray_t *wordarray) | |
43 | +{ | |
44 | + return (UW*)bytearray_getbuffer(&wordarray->rawdata); | |
45 | +} | |
46 | + | |
47 | +EXPORT W wordarray_getlength(wordarray_t *wordarray) | |
48 | +{ | |
49 | + return bytearray_getlength(&wordarray->rawdata) / sizeof(UW); | |
50 | +} | |
51 | + | |
52 | +EXPORT W wordarray_pushback(wordarray_t *wordarray, UW val) | |
53 | +{ | |
54 | + W i, j, err; | |
55 | + UB *p; | |
56 | + | |
57 | + /* TODO: more efficient */ | |
58 | + p = (UB*)&val; | |
59 | + for (i = 0; i < sizeof(UW); i++) { | |
60 | + err = bytearray_pushback(&wordarray->rawdata, p[i]); | |
61 | + if (err < 0) { | |
62 | + DP_ER("bytearray_pushback error", err); | |
63 | + goto error; | |
64 | + } | |
65 | + } | |
66 | + | |
67 | + return 0; | |
68 | + | |
69 | +error: | |
70 | + for (j = 0; j < i; j++) { | |
71 | + bytearray_popback(&wordarray->rawdata); | |
72 | + } | |
73 | + return err; | |
74 | +} | |
75 | + | |
76 | +EXPORT W wordarray_popback(wordarray_t *wordarray) | |
77 | +{ | |
78 | + W i, len; | |
79 | + | |
80 | + len = wordarray_getlength(wordarray); | |
81 | + if (len == 0) { | |
82 | + return -1; /* TODO */ | |
83 | + } | |
84 | + | |
85 | + for (i = 0; i < sizeof(UW); i++) { | |
86 | + bytearray_popback(&wordarray->rawdata); | |
87 | + } | |
88 | + | |
89 | + return 0; | |
90 | +} | |
91 | + | |
92 | +EXPORT W wordarray_initialize(wordarray_t *wordarray) | |
93 | +{ | |
94 | + return bytearray_initialize(&wordarray->rawdata); | |
95 | +} | |
96 | + | |
97 | +EXPORT VOID wordarray_finalize(wordarray_t *wordarray) | |
98 | +{ | |
99 | + bytearray_finalize(&wordarray->rawdata); | |
100 | +} | |
101 | + | |
102 | +EXPORT W wordarray_cursor_move(wordarray_cursor_t *cursor, W diff) | |
103 | +{ | |
104 | + return bytearray_cursor_move(&cursor->base, diff*sizeof(UW)); | |
105 | +} | |
106 | + | |
107 | +EXPORT W wordarray_cursor_erase(wordarray_cursor_t *cursor, W len) | |
108 | +{ | |
109 | + return bytearray_cursor_erase(&cursor->base, len*sizeof(UW)); | |
110 | +} | |
111 | + | |
112 | +EXPORT W wordarray_cursor_insert(wordarray_cursor_t *cursor, UW *data, W len) | |
113 | +{ | |
114 | + return bytearray_cursor_insert(&cursor->base, (UB*)data, len*sizeof(UW)); | |
115 | +} | |
116 | + | |
117 | +EXPORT Bool wordarray_cursor_isend(wordarray_cursor_t *cursor) | |
118 | +{ | |
119 | + return bytearray_cursor_isend(&cursor->base); | |
120 | +} | |
121 | + | |
122 | +EXPORT W wordarray_cursor_getW(wordarray_cursor_t *cursor, W *p) | |
123 | +{ | |
124 | + return bytearray_cursor_getW(&cursor->base, p); | |
125 | +} | |
126 | + | |
127 | +EXPORT W wordarray_cursor_getUW(wordarray_cursor_t *cursor, UW *p) | |
128 | +{ | |
129 | + return bytearray_cursor_getUW(&cursor->base, p); | |
130 | +} | |
131 | + | |
132 | +EXPORT VOID wordarray_cursor_initialize(wordarray_cursor_t *cursor, wordarray_t *wordarray) | |
133 | +{ | |
134 | + bytearray_cursor_initialize(&cursor->base, &wordarray->rawdata); | |
135 | +} | |
136 | + | |
137 | +EXPORT VOID wordarray_cursor_finalize(wordarray_cursor_t *cursor) | |
138 | +{ | |
139 | + bytearray_cursor_finalize(&cursor->base); | |
140 | +} |
@@ -0,0 +1,68 @@ | ||
1 | +/* | |
2 | + * wordarray.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: wordarray */ | |
29 | +/* Detail name: */ | |
30 | + | |
31 | +#include <basic.h> | |
32 | + | |
33 | +#include "bytearray.h" | |
34 | + | |
35 | +#ifndef __WORDARRAY_H__ | |
36 | +#define __WORDARRAY_H__ | |
37 | + | |
38 | +/* Functionality name: wordarray */ | |
39 | +/* Detail name: */ | |
40 | +struct wordarray_t_ { | |
41 | + bytearray_t rawdata; | |
42 | +}; | |
43 | +typedef struct wordarray_t_ wordarray_t; | |
44 | + | |
45 | +/* Functionality name: wordarray */ | |
46 | +/* Detail name: cursor */ | |
47 | +struct wordarray_cursor_t_ { | |
48 | + bytearray_cursor_t base; | |
49 | +}; | |
50 | +typedef struct wordarray_cursor_t_ wordarray_cursor_t; | |
51 | + | |
52 | +IMPORT W wordarray_initialize(wordarray_t *wordarray); | |
53 | +IMPORT VOID wordarray_finalize(wordarray_t *wordarray); | |
54 | +IMPORT UW* wordarray_getbuffer(wordarray_t *wordarray); | |
55 | +IMPORT W wordarray_getlength(wordarray_t *wordarray); | |
56 | +IMPORT W wordarray_pushback(wordarray_t *wordarray, UW val); | |
57 | +IMPORT W wordarray_popback(wordarray_t *wordarray); | |
58 | + | |
59 | +IMPORT VOID wordarray_cursor_initialize(wordarray_cursor_t *cursor, wordarray_t *wordarray); | |
60 | +IMPORT VOID wordarray_cursor_finalize(wordarray_cursor_t *cursor); | |
61 | +IMPORT W wordarray_cursor_move(wordarray_cursor_t *cursor, W diff); | |
62 | +IMPORT W wordarray_cursor_erase(wordarray_cursor_t *cursor, W len); | |
63 | +IMPORT W wordarray_cursor_insert(wordarray_cursor_t *cursor, UW *data, W len); | |
64 | +IMPORT Bool wordarray_cursor_isend(wordarray_cursor_t *cursor); | |
65 | +IMPORT W wordarray_cursor_getW(wordarray_cursor_t *cursor, W *p); | |
66 | +IMPORT W wordarray_cursor_getUW(wordarray_cursor_t *cursor, UW *p); | |
67 | + | |
68 | +#endif |