4 * Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <rte_string_fns.h>
41 #include <cmdline_cirbuf.h>
43 #include "test_cmdline.h"
45 /* different length strings */
46 #define CIRBUF_STR_HEAD " HEAD"
47 #define CIRBUF_STR_TAIL "TAIL"
49 /* miscelaneous tests - they make bullseye happy */
51 test_cirbuf_string_misc(void)
54 char buf[CMDLINE_TEST_BUFSIZE];
55 char tmp[CMDLINE_TEST_BUFSIZE];
57 /* initialize buffers */
58 memset(buf, 0, sizeof(buf));
59 memset(tmp, 0, sizeof(tmp));
62 * initialize circular buffer
64 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
65 printf("Error: failed to initialize circular buffer!\n");
70 * add strings to head and tail, but read only tail
71 * this results in read operation that does not transcend
72 * from buffer end to buffer beginning (in other words,
73 * strlen <= cb->maxlen - cb->end)
76 /* add string to head */
77 if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
78 != (sizeof(CIRBUF_STR_HEAD))) {
79 printf("Error: failed to add string to head!\n");
82 /* add string to tail */
83 if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
84 != (sizeof(CIRBUF_STR_TAIL))) {
85 printf("Error: failed to add string to head!\n");
88 /* read string from tail */
89 if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_TAIL))
90 != (sizeof(CIRBUF_STR_TAIL))) {
91 printf("Error: failed to get string from tail!\n");
95 if (strncmp(tmp, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) != 0) {
96 printf("Error: tail strings do not match!\n");
100 memset(tmp, 0, sizeof(tmp));
101 memset(buf, 0, sizeof(buf));
106 * add a string to buffer when start/end is at end of buffer
110 * reinitialize circular buffer with start at the end of cirbuf
112 if (cirbuf_init(&cb, buf, CMDLINE_TEST_BUFSIZE - 2, sizeof(buf)) < 0) {
113 printf("Error: failed to reinitialize circular buffer!\n");
118 /* add string to tail */
119 if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
120 != (sizeof(CIRBUF_STR_TAIL))) {
121 printf("Error: failed to add string to tail!\n");
124 /* read string from tail */
125 if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_TAIL))
126 != (sizeof(CIRBUF_STR_TAIL))) {
127 printf("Error: failed to get string from tail!\n");
131 if (strncmp(tmp, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) != 0) {
132 printf("Error: tail strings do not match!\n");
135 /* clear tmp buffer */
136 memset(tmp, 0, sizeof(tmp));
139 /* add string to head */
140 if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
141 != (sizeof(CIRBUF_STR_HEAD))) {
142 printf("Error: failed to add string to head!\n");
145 /* read string from tail */
146 if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_HEAD))
147 != (sizeof(CIRBUF_STR_HEAD))) {
148 printf("Error: failed to get string from head!\n");
152 if (strncmp(tmp, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD)) != 0) {
153 printf("Error: headstrings do not match!\n");
160 /* test adding and deleting strings */
162 test_cirbuf_string_add_del(void)
165 char buf[CMDLINE_TEST_BUFSIZE];
166 char tmp[CMDLINE_TEST_BUFSIZE];
168 /* initialize buffers */
169 memset(buf, 0, sizeof(buf));
170 memset(tmp, 0, sizeof(tmp));
173 * initialize circular buffer
175 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
176 printf("Error: failed to initialize circular buffer!\n");
180 /* add string to head */
181 if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
182 != (sizeof(CIRBUF_STR_HEAD))) {
183 printf("Error: failed to add string to head!\n");
186 /* read string from head */
187 if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_HEAD))
188 != (sizeof(CIRBUF_STR_HEAD))) {
189 printf("Error: failed to get string from head!\n");
193 if (strncmp(tmp, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD)) != 0) {
194 printf("Error: head strings do not match!\n");
197 /* clear tmp buffer */
198 memset(tmp, 0, sizeof(tmp));
199 /* read string from tail */
200 if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_HEAD))
201 != (sizeof(CIRBUF_STR_HEAD))) {
202 printf("Error: failed to get string from head!\n");
206 if (strncmp(tmp, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD)) != 0) {
207 printf("Error: head strings do not match!\n");
210 /* delete string from head*/
211 if (cirbuf_del_buf_head(&cb, sizeof(CIRBUF_STR_HEAD)) < 0) {
212 printf("Error: failed to delete string from head!\n");
215 /* verify string was deleted */
216 if (cirbuf_del_head_safe(&cb) == 0) {
217 printf("Error: buffer should have been empty!\n");
220 /* clear tmp buffer */
221 memset(tmp, 0, sizeof(tmp));
226 * reinitialize circular buffer
228 memset(buf, 0, sizeof(buf));
229 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
230 printf("Error: failed to reinitialize circular buffer!\n");
234 /* add string to tail */
235 if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
236 != (sizeof(CIRBUF_STR_TAIL))) {
237 printf("Error: failed to add string to tail!\n");
240 /* get string from tail */
241 if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_TAIL))
242 != (sizeof(CIRBUF_STR_TAIL))) {
243 printf("Error: failed to get string from tail!\n");
247 if (strncmp(tmp, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) != 0) {
248 printf("Error: tail strings do not match!\n");
251 /* clear tmp buffer */
252 memset(tmp, 0, sizeof(tmp));
253 /* get string from head */
254 if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_TAIL))
255 != (sizeof(CIRBUF_STR_TAIL))) {
256 printf("Error: failed to get string from tail!\n");
260 if (strncmp(tmp, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) != 0) {
261 printf("Error: tail strings do not match!\n");
264 /* delete string from tail */
265 if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_TAIL)) < 0) {
266 printf("Error: failed to delete string from tail!\n");
269 /* verify string was deleted */
270 if (cirbuf_del_tail_safe(&cb) == 0) {
271 printf("Error: buffer should have been empty!\n");
278 /* test adding from head and deleting from tail, and vice versa */
280 test_cirbuf_string_add_del_reverse(void)
283 char buf[CMDLINE_TEST_BUFSIZE];
284 char tmp[CMDLINE_TEST_BUFSIZE];
286 /* initialize buffers */
287 memset(buf, 0, sizeof(buf));
288 memset(tmp, 0, sizeof(tmp));
291 * initialize circular buffer
293 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
294 printf("Error: failed to initialize circular buffer!\n");
298 /* add string to head */
299 if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
300 != (sizeof(CIRBUF_STR_HEAD))) {
301 printf("Error: failed to add string to head!\n");
304 /* delete string from tail */
305 if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_HEAD)) < 0) {
306 printf("Error: failed to delete string from tail!\n");
309 /* verify string was deleted */
310 if (cirbuf_del_tail_safe(&cb) == 0) {
311 printf("Error: buffer should have been empty!\n");
314 /* clear tmp buffer */
315 memset(tmp, 0, sizeof(tmp));
318 * reinitialize circular buffer
320 memset(buf, 0, sizeof(buf));
321 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
322 printf("Error: failed to reinitialize circular buffer!\n");
326 /* add string to tail */
327 if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
328 != (sizeof(CIRBUF_STR_TAIL))) {
329 printf("Error: failed to add string to tail!\n");
332 /* delete string from head */
333 if (cirbuf_del_buf_head(&cb, sizeof(CIRBUF_STR_TAIL)) < 0) {
334 printf("Error: failed to delete string from head!\n");
337 /* verify string was deleted */
338 if (cirbuf_del_head_safe(&cb) == 0) {
339 printf("Error: buffer should have been empty!\n");
346 /* try to write more than available */
348 test_cirbuf_string_add_boundaries(void)
351 char buf[CMDLINE_TEST_BUFSIZE];
354 /* initialize buffers */
355 memset(buf, 0, sizeof(buf));
358 * initialize circular buffer
360 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
361 printf("Error: failed to initialize circular buffer!\n");
365 /* fill the buffer from tail */
366 for (i = 0; i < CMDLINE_TEST_BUFSIZE - sizeof(CIRBUF_STR_TAIL) + 1; i++)
367 cirbuf_add_tail_safe(&cb, 't');
369 /* try adding a string to tail */
370 if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
372 printf("Error: buffer should have been full!\n");
375 /* try adding a string to head */
376 if (cirbuf_add_buf_head(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
378 printf("Error: buffer should have been full!\n");
383 * reinitialize circular buffer
385 memset(buf, 0, sizeof(buf));
386 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
387 printf("Error: failed to reinitialize circular buffer!\n");
391 /* fill the buffer from head */
392 for (i = 0; i < CMDLINE_TEST_BUFSIZE - sizeof(CIRBUF_STR_HEAD) + 1; i++)
393 cirbuf_add_head_safe(&cb, 'h');
395 /* try adding a string to head */
396 if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
398 printf("Error: buffer should have been full!\n");
401 /* try adding a string to tail */
402 if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
404 printf("Error: buffer should have been full!\n");
411 /* try to read/delete more than written */
413 test_cirbuf_string_get_del_boundaries(void)
416 char buf[CMDLINE_TEST_BUFSIZE];
417 char tmp[CMDLINE_TEST_BUFSIZE];
419 /* initialize buffers */
420 memset(buf, 0, sizeof(buf));
421 memset(tmp, 0, sizeof(tmp));
424 * initialize circular buffer
426 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
427 printf("Error: failed to initialize circular buffer!\n");
432 /* add string to head */
433 if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
434 != (sizeof(CIRBUF_STR_HEAD))) {
435 printf("Error: failed to add string to head!\n");
438 /* read more than written (head) */
439 if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_HEAD) + 1)
440 != sizeof(CIRBUF_STR_HEAD)) {
441 printf("Error: unexpected result when reading too much data!\n");
444 /* read more than written (tail) */
445 if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_HEAD) + 1)
446 != sizeof(CIRBUF_STR_HEAD)) {
447 printf("Error: unexpected result when reading too much data!\n");
450 /* delete more than written (head) */
451 if (cirbuf_del_buf_head(&cb, sizeof(CIRBUF_STR_HEAD) + 1) == 0) {
452 printf("Error: unexpected result when deleting too much data!\n");
455 /* delete more than written (tail) */
456 if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_HEAD) + 1) == 0) {
457 printf("Error: unexpected result when deleting too much data!\n");
462 * reinitialize circular buffer
464 memset(buf, 0, sizeof(buf));
465 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
466 printf("Error: failed to reinitialize circular buffer!\n");
470 /* add string to tail */
471 if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
472 != (sizeof(CIRBUF_STR_TAIL))) {
473 printf("Error: failed to add string to tail!\n");
476 /* read more than written (tail) */
477 if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_TAIL) + 1)
478 != sizeof(CIRBUF_STR_TAIL)) {
479 printf("Error: unexpected result when reading too much data!\n");
482 /* read more than written (head) */
483 if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_TAIL) + 1)
484 != sizeof(CIRBUF_STR_TAIL)) {
485 printf("Error: unexpected result when reading too much data!\n");
488 /* delete more than written (tail) */
489 if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_TAIL) + 1) == 0) {
490 printf("Error: unexpected result when deleting too much data!\n");
493 /* delete more than written (head) */
494 if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_TAIL) + 1) == 0) {
495 printf("Error: unexpected result when deleting too much data!\n");
502 /* try to read/delete less than written */
504 test_cirbuf_string_get_del_partial(void)
507 char buf[CMDLINE_TEST_BUFSIZE];
508 char tmp[CMDLINE_TEST_BUFSIZE];
509 char tmp2[CMDLINE_TEST_BUFSIZE];
511 /* initialize buffers */
512 memset(buf, 0, sizeof(buf));
513 memset(tmp, 0, sizeof(tmp));
514 memset(tmp2, 0, sizeof(tmp));
516 rte_snprintf(tmp2, sizeof(tmp2), "%s", CIRBUF_STR_HEAD);
519 * initialize circular buffer
521 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
522 printf("Error: failed to initialize circular buffer!\n");
526 /* add string to head */
527 if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
528 != (sizeof(CIRBUF_STR_HEAD))) {
529 printf("Error: failed to add string to head!\n");
532 /* read less than written (head) */
533 if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_HEAD) - 1)
534 != sizeof(CIRBUF_STR_HEAD) - 1) {
535 printf("Error: unexpected result when reading from head!\n");
539 if (strncmp(tmp, tmp2, sizeof(CIRBUF_STR_HEAD) - 1) != 0) {
540 printf("Error: strings mismatch!\n");
543 memset(tmp, 0, sizeof(tmp));
544 /* read less than written (tail) */
545 if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_HEAD) - 1)
546 != sizeof(CIRBUF_STR_HEAD) - 1) {
547 printf("Error: unexpected result when reading from tail!\n");
551 if (strncmp(tmp, &tmp2[1], sizeof(CIRBUF_STR_HEAD) - 1) != 0) {
552 printf("Error: strings mismatch!\n");
557 * verify correct deletion
561 memset(tmp, 0, sizeof(tmp));
563 /* delete less than written (head) */
564 if (cirbuf_del_buf_head(&cb, 1) != 0) {
565 printf("Error: delete from head failed!\n");
569 if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_HEAD) - 1)
570 != sizeof(CIRBUF_STR_HEAD) - 1) {
571 printf("Error: unexpected result when reading from head!\n");
574 /* since we deleted from head, first char should be deleted */
575 if (strncmp(tmp, &tmp2[1], sizeof(CIRBUF_STR_HEAD) - 1) != 0) {
576 printf("Error: strings mismatch!\n");
580 memset(tmp, 0, sizeof(tmp));
582 /* delete less than written (tail) */
583 if (cirbuf_del_buf_tail(&cb, 1) != 0) {
584 printf("Error: delete from tail failed!\n");
588 if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_HEAD) - 2)
589 != sizeof(CIRBUF_STR_HEAD) - 2) {
590 printf("Error: unexpected result when reading from head!\n");
593 /* since we deleted from tail, last char should be deleted */
594 if (strncmp(tmp, &tmp2[1], sizeof(CIRBUF_STR_HEAD) - 2) != 0) {
595 printf("Error: strings mismatch!\n");
602 /* test cmdline_cirbuf char add/del functions */
604 test_cirbuf_char_add_del(void)
607 char buf[CMDLINE_TEST_BUFSIZE];
608 char tmp[CMDLINE_TEST_BUFSIZE];
611 memset(buf, 0, sizeof(buf));
612 memset(tmp, 0, sizeof(tmp));
615 * initialize circular buffer
617 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
618 printf("Error: failed to initialize circular buffer!\n");
623 * try to delete something from cirbuf. since it's empty,
626 if (cirbuf_del_head_safe(&cb) == 0) {
627 printf("Error: deleting from empty cirbuf head succeeded!\n");
630 if (cirbuf_del_tail_safe(&cb) == 0) {
631 printf("Error: deleting from empty cirbuf tail succeeded!\n");
636 * add, verify and delete. these should pass.
638 if (cirbuf_add_head_safe(&cb,'h') < 0) {
639 printf("Error: adding to cirbuf head failed!\n");
642 if (cirbuf_get_head(&cb) != 'h') {
643 printf("Error: wrong head content!\n");
646 if (cirbuf_del_head_safe(&cb) < 0) {
647 printf("Error: deleting from cirbuf head failed!\n");
650 if (cirbuf_add_tail_safe(&cb,'t') < 0) {
651 printf("Error: adding to cirbuf tail failed!\n");
654 if (cirbuf_get_tail(&cb) != 't') {
655 printf("Error: wrong tail content!\n");
658 if (cirbuf_del_tail_safe(&cb) < 0) {
659 printf("Error: deleting from cirbuf tail failed!\n");
662 /* do the same for unsafe versions. those are void. */
663 cirbuf_add_head(&cb,'h');
664 if (cirbuf_get_head(&cb) != 'h') {
665 printf("Error: wrong head content!\n");
668 cirbuf_del_head(&cb);
670 /* test if char has been deleted. we can't call cirbuf_get_head
671 * because it's unsafe, but we can call cirbuf_get_buf_head.
673 if (cirbuf_get_buf_head(&cb, tmp, 1) > 0) {
674 printf("Error: buffer should have been empty!\n");
678 cirbuf_add_tail(&cb,'t');
679 if (cirbuf_get_tail(&cb) != 't') {
680 printf("Error: wrong tail content!\n");
683 cirbuf_del_tail(&cb);
685 /* test if char has been deleted. we can't call cirbuf_get_tail
686 * because it's unsafe, but we can call cirbuf_get_buf_tail.
688 if (cirbuf_get_buf_tail(&cb, tmp, 1) > 0) {
689 printf("Error: buffer should have been empty!\n");
696 /* test filling up buffer with chars */
698 test_cirbuf_char_fill(void)
701 char buf[CMDLINE_TEST_BUFSIZE];
705 memset(buf, 0, sizeof(buf));
708 * initialize circular buffer
710 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
711 printf("Error: failed to initialize circular buffer!\n");
716 * fill the buffer from head or tail, verify contents, test boundaries
717 * and clear the buffer
720 /* fill the buffer from tail */
721 for (i = 0; i < CMDLINE_TEST_BUFSIZE; i++)
722 cirbuf_add_tail_safe(&cb, 't');
723 /* verify that contents of the buffer are what they are supposed to be */
724 for (i = 0; i < sizeof(buf); i++) {
726 printf("Error: wrong content in buffer!\n");
730 /* try to add to a full buffer from tail */
731 if (cirbuf_add_tail_safe(&cb, 't') == 0) {
732 printf("Error: buffer should have been full!\n");
735 /* try to add to a full buffer from head */
736 if (cirbuf_add_head_safe(&cb, 'h') == 0) {
737 printf("Error: buffer should have been full!\n");
740 /* delete buffer from tail */
741 for(i = 0; i < CMDLINE_TEST_BUFSIZE; i++)
742 cirbuf_del_tail_safe(&cb);
743 /* try to delete from an empty buffer */
744 if (cirbuf_del_tail_safe(&cb) >= 0) {
745 printf("Error: buffer should have been empty!\n");
749 /* fill the buffer from head */
750 for (i = 0; i < CMDLINE_TEST_BUFSIZE; i++)
751 cirbuf_add_head_safe(&cb, 'h');
752 /* verify that contents of the buffer are what they are supposed to be */
753 for (i = 0; i < sizeof(buf); i++) {
755 printf("Error: wrong content in buffer!\n");
759 /* try to add to a full buffer from head */
760 if (cirbuf_add_head_safe(&cb,'h') >= 0) {
761 printf("Error: buffer should have been full!\n");
764 /* try to add to a full buffer from tail */
765 if (cirbuf_add_tail_safe(&cb, 't') == 0) {
766 printf("Error: buffer should have been full!\n");
769 /* delete buffer from head */
770 for(i = 0; i < CMDLINE_TEST_BUFSIZE; i++)
771 cirbuf_del_head_safe(&cb);
772 /* try to delete from an empty buffer */
773 if (cirbuf_del_head_safe(&cb) >= 0) {
774 printf("Error: buffer should have been empty!\n");
779 * fill the buffer from both head and tail, with alternating characters,
780 * verify contents and clear the buffer
783 /* fill half of buffer from tail */
784 for (i = 0; i < CMDLINE_TEST_BUFSIZE / 2; i++)
785 cirbuf_add_tail_safe(&cb, (char) (i % 2 ? 't' : 'T'));
786 /* fill other half of the buffer from head */
787 for (i = 0; i < CMDLINE_TEST_BUFSIZE / 2; i++)
788 cirbuf_add_head_safe(&cb, (char) (i % 2 ? 'H' : 'h')); /* added in reverse */
790 /* verify that contents of the buffer are what they are supposed to be */
791 for (i = 0; i < sizeof(buf) / 2; i++) {
792 if (buf[i] != (char) (i % 2 ? 't' : 'T')) {
793 printf("Error: wrong content in buffer at %u!\n", i);
797 for (i = sizeof(buf) / 2; i < sizeof(buf); i++) {
798 if (buf[i] != (char) (i % 2 ? 'h' : 'H')) {
799 printf("Error: wrong content in buffer %u!\n", i);
807 /* test left alignment */
809 test_cirbuf_align_left(void)
811 #define HALF_OFFSET CMDLINE_TEST_BUFSIZE / 2
812 #define SMALL_OFFSET HALF_OFFSET / 2
813 /* resulting buffer lengths for each of the test cases */
814 #define LEN1 HALF_OFFSET - SMALL_OFFSET - 1
815 #define LEN2 HALF_OFFSET + SMALL_OFFSET + 2
816 #define LEN3 HALF_OFFSET - SMALL_OFFSET
817 #define LEN4 HALF_OFFSET + SMALL_OFFSET - 1
820 char buf[CMDLINE_TEST_BUFSIZE];
821 char tmp[CMDLINE_TEST_BUFSIZE];
825 * align left when start < end and start in left half
829 * initialize circular buffer
831 memset(buf, 0, sizeof(buf));
832 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
833 printf("Error: failed to initialize circular buffer!\n");
837 /* push end into left half */
838 for (i = 0; i < HALF_OFFSET - 1; i++)
839 cirbuf_add_tail_safe(&cb, 't');
841 /* push start into left half < end */
842 for (i = 0; i < SMALL_OFFSET; i++)
843 cirbuf_del_head_safe(&cb);
846 if (cirbuf_align_left(&cb) < 0) {
847 printf("Error: alignment failed!\n");
852 if (cb.start != 0 || cb.len != LEN1 || cb.end != cb.len - 1) {
853 printf("Error: buffer alignment is wrong!\n");
858 * align left when start > end and start in left half
862 * reinitialize circular buffer
864 memset(buf, 0, sizeof(buf));
865 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
866 printf("Error: failed to reinitialize circular buffer!\n");
870 /* push start into left half */
871 for (i = 0; i < HALF_OFFSET + 2; i++)
872 cirbuf_add_head_safe(&cb, 'h');
874 /* push end into left half > start */
875 for (i = 0; i < SMALL_OFFSET; i++)
876 cirbuf_add_tail_safe(&cb, 't');
879 if (cirbuf_align_left(&cb) < 0) {
880 printf("Error: alignment failed!\n");
885 if (cb.start != 0 || cb.len != LEN2 || cb.end != cb.len - 1) {
886 printf("Error: buffer alignment is wrong!");
891 * align left when start < end and start in right half
895 * reinitialize circular buffer
897 memset(buf, 0, sizeof(buf));
898 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
899 printf("Error: failed to reinitialize circular buffer!\n");
903 /* push start into the right half */
904 for (i = 0; i < HALF_OFFSET; i++)
905 cirbuf_add_head_safe(&cb, 'h');
907 /* push end into left half > start */
908 for (i = 0; i < SMALL_OFFSET; i++)
909 cirbuf_del_tail_safe(&cb);
912 if (cirbuf_align_left(&cb) < 0) {
913 printf("Error: alignment failed!\n");
918 if (cb.start != 0 || cb.len != LEN3 || cb.end != cb.len - 1) {
919 printf("Error: buffer alignment is wrong!");
924 * align left when start > end and start in right half
928 * reinitialize circular buffer
930 memset(buf, 0, sizeof(buf));
931 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
932 printf("Error: failed to reinitialize circular buffer!\n");
936 /* push start into the right half */
937 for (i = 0; i < HALF_OFFSET - 1; i++)
938 cirbuf_add_head_safe(&cb, 'h');
940 /* push end into left half < start */
941 for (i = 0; i < SMALL_OFFSET; i++)
942 cirbuf_add_tail_safe(&cb, 't');
945 if (cirbuf_align_left(&cb) < 0) {
946 printf("Error: alignment failed!\n");
951 if (cb.start != 0 || cb.len != LEN4 ||
952 cb.end != cb.len - 1) {
953 printf("Error: buffer alignment is wrong!");
958 * Verify that alignment doesn't corrupt data
962 * reinitialize circular buffer
964 memset(buf, 0, sizeof(buf));
965 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
966 printf("Error: failed to reinitialize circular buffer!\n");
970 /* add string to tail and head */
971 if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD,
972 sizeof(CIRBUF_STR_HEAD)) < 0 || cirbuf_add_buf_tail(&cb,
973 CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) < 0) {
974 printf("Error: failed to add strings!\n");
979 if (cirbuf_align_left(&cb) < 0) {
980 printf("Error: alignment failed!\n");
984 /* get string from head */
985 if (cirbuf_get_buf_head(&cb, tmp,
986 sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) < 0) {
987 printf("Error: failed to read string from head!\n");
992 if (strncmp(tmp, CIRBUF_STR_HEAD "\0" CIRBUF_STR_TAIL,
993 sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) != 0) {
994 printf("Error: strings mismatch!\n");
998 /* reset tmp buffer */
999 memset(tmp, 0, sizeof(tmp));
1001 /* get string from tail */
1002 if (cirbuf_get_buf_tail(&cb, tmp,
1003 sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) < 0) {
1004 printf("Error: failed to read string from head!\n");
1009 if (strncmp(tmp, CIRBUF_STR_HEAD "\0" CIRBUF_STR_TAIL,
1010 sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) != 0) {
1011 printf("Error: strings mismatch!\n");
1018 /* test right alignment */
1020 test_cirbuf_align_right(void)
1022 #define END_OFFSET CMDLINE_TEST_BUFSIZE - 1
1024 char buf[CMDLINE_TEST_BUFSIZE];
1025 char tmp[CMDLINE_TEST_BUFSIZE];
1030 * align right when start < end and start in left half
1034 * initialize circular buffer
1036 memset(buf, 0, sizeof(buf));
1037 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
1038 printf("Error: failed to initialize circular buffer!\n");
1042 /* push end into left half */
1043 for (i = 0; i < HALF_OFFSET - 1; i++)
1044 cirbuf_add_tail_safe(&cb, 't');
1046 /* push start into left half < end */
1047 for (i = 0; i < SMALL_OFFSET; i++)
1048 cirbuf_del_head_safe(&cb);
1051 cirbuf_align_right(&cb);
1054 if (cb.start != END_OFFSET || cb.len != LEN1 || cb.end != cb.len - 2) {
1055 printf("Error: buffer alignment is wrong!\n");
1060 * align right when start > end and start in left half
1064 * reinitialize circular buffer
1066 memset(buf, 0, sizeof(buf));
1067 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
1068 printf("Error: failed to reinitialize circular buffer!\n");
1072 /* push start into left half */
1073 for (i = 0; i < HALF_OFFSET + 2; i++)
1074 cirbuf_add_head_safe(&cb, 'h');
1076 /* push end into left half > start */
1077 for (i = 0; i < SMALL_OFFSET; i++)
1078 cirbuf_add_tail_safe(&cb, 't');
1081 cirbuf_align_right(&cb);
1084 if (cb.start != END_OFFSET || cb.len != LEN2 || cb.end != cb.len - 2) {
1085 printf("Error: buffer alignment is wrong!");
1090 * align right when start < end and start in right half
1094 * reinitialize circular buffer
1096 memset(buf, 0, sizeof(buf));
1097 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
1098 printf("Error: failed to reinitialize circular buffer!\n");
1102 /* push start into the right half */
1103 for (i = 0; i < HALF_OFFSET; i++)
1104 cirbuf_add_head_safe(&cb, 'h');
1106 /* push end into left half > start */
1107 for (i = 0; i < SMALL_OFFSET; i++)
1108 cirbuf_del_tail_safe(&cb);
1111 cirbuf_align_right(&cb);
1114 if (cb.end != END_OFFSET || cb.len != LEN3 || cb.start != cb.end - cb.len + 1) {
1115 printf("Error: buffer alignment is wrong!");
1120 * align right when start > end and start in right half
1124 * reinitialize circular buffer
1126 memset(buf, 0, sizeof(buf));
1127 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
1128 printf("Error: failed to reinitialize circular buffer!\n");
1132 /* push start into the right half */
1133 for (i = 0; i < HALF_OFFSET - 1; i++)
1134 cirbuf_add_head_safe(&cb, 'h');
1136 /* push end into left half < start */
1137 for (i = 0; i < SMALL_OFFSET; i++)
1138 cirbuf_add_tail_safe(&cb, 't');
1141 cirbuf_align_right(&cb);
1144 if (cb.end != END_OFFSET || cb.len != LEN4 || cb.start != cb.end - cb.len + 1) {
1145 printf("Error: buffer alignment is wrong!");
1150 * Verify that alignment doesn't corrupt data
1154 * reinitialize circular buffer
1156 memset(buf, 0, sizeof(buf));
1157 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
1158 printf("Error: failed to reinitialize circular buffer!\n");
1162 /* add string to tail and head */
1163 if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL,
1164 sizeof(CIRBUF_STR_TAIL)) < 0 || cirbuf_add_buf_head(&cb,
1165 CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD)) < 0) {
1166 printf("Error: failed to add strings!\n");
1171 if (cirbuf_align_right(&cb) < 0) {
1172 printf("Error: alignment failed!\n");
1176 /* get string from head */
1177 if (cirbuf_get_buf_head(&cb, tmp,
1178 sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) < 0) {
1179 printf("Error: failed to read string from head!\n");
1184 if (strncmp(tmp, CIRBUF_STR_HEAD "\0" CIRBUF_STR_TAIL,
1185 sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) != 0) {
1186 printf("Error: strings mismatch!\n");
1190 /* reset tmp buffer */
1191 memset(tmp, 0, sizeof(tmp));
1193 /* get string from tail */
1194 if (cirbuf_get_buf_tail(&cb, tmp,
1195 sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) < 0) {
1196 printf("Error: failed to read string from head!\n");
1200 if (strncmp(tmp, CIRBUF_STR_HEAD "\0" CIRBUF_STR_TAIL,
1201 sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) != 0) {
1202 printf("Error: strings mismatch!\n");
1209 /* call functions with invalid parameters */
1211 test_cirbuf_invalid_param(void)
1214 char buf[CMDLINE_TEST_BUFSIZE];
1217 if (cirbuf_init(0, buf, 0, sizeof(buf)) == 0)
1220 if (cirbuf_init(&cb, 0, 0, sizeof(buf)) == 0)
1223 if (cirbuf_add_head_safe(0, 'h') == 0)
1225 if (cirbuf_add_tail_safe(0, 't') == 0)
1227 if (cirbuf_del_head_safe(0) == 0)
1229 if (cirbuf_del_tail_safe(0) == 0)
1232 if (cirbuf_add_buf_head(&cb, 0, 0) == 0)
1234 if (cirbuf_add_buf_tail(&cb, 0, 0) == 0)
1237 if (cirbuf_add_buf_head(0, buf, 0) == 0)
1239 if (cirbuf_add_buf_tail(0, buf, 0) == 0)
1242 if (cirbuf_add_buf_head(&cb, buf, 0) == 0)
1244 if (cirbuf_add_buf_tail(&cb, buf, 0) == 0)
1247 if (cirbuf_del_buf_head(0, 0) == 0)
1249 if (cirbuf_del_buf_tail(0, 0) == 0)
1252 if (cirbuf_del_buf_head(&cb, 0) == 0)
1254 if (cirbuf_del_buf_tail(&cb, 0) == 0)
1257 if (cirbuf_get_buf_head(0, 0, 0) == 0)
1259 if (cirbuf_get_buf_tail(0, 0, 0) == 0)
1262 if (cirbuf_get_buf_head(&cb, 0, 0) == 0)
1264 if (cirbuf_get_buf_tail(&cb, 0, 0) == 0)
1266 /* null size, this is valid but should return 0 */
1267 if (cirbuf_get_buf_head(&cb, buf, 0) != 0)
1269 if (cirbuf_get_buf_tail(&cb, buf, 0) != 0)
1272 if (cirbuf_align_left(0) == 0)
1274 if (cirbuf_align_right(0) == 0)
1280 /* test cmdline_cirbuf char functions */
1282 test_cirbuf_char(void)
1286 ret = test_cirbuf_char_add_del();
1290 ret = test_cirbuf_char_fill();
1297 /* test cmdline_cirbuf string functions */
1299 test_cirbuf_string(void)
1301 if (test_cirbuf_string_add_del() < 0)
1304 if (test_cirbuf_string_add_del_reverse() < 0)
1307 if (test_cirbuf_string_add_boundaries() < 0)
1310 if (test_cirbuf_string_get_del_boundaries() < 0)
1313 if (test_cirbuf_string_get_del_partial() < 0)
1316 if (test_cirbuf_string_misc() < 0)
1322 /* test cmdline_cirbuf align functions */
1324 test_cirbuf_align(void)
1326 if (test_cirbuf_align_left() < 0)
1328 if (test_cirbuf_align_right() < 0)