4 * Copyright(c) 2010-2014 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.
38 #include <rte_string_fns.h>
40 #include <cmdline_cirbuf.h>
42 #include "test_cmdline.h"
44 /* different length strings */
45 #define CIRBUF_STR_HEAD " HEAD"
46 #define CIRBUF_STR_TAIL "TAIL"
48 /* miscelaneous tests - they make bullseye happy */
50 test_cirbuf_string_misc(void)
53 char buf[CMDLINE_TEST_BUFSIZE];
54 char tmp[CMDLINE_TEST_BUFSIZE];
56 /* initialize buffers */
57 memset(buf, 0, sizeof(buf));
58 memset(tmp, 0, sizeof(tmp));
61 * initialize circular buffer
63 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
64 printf("Error: failed to initialize circular buffer!\n");
69 * add strings to head and tail, but read only tail
70 * this results in read operation that does not transcend
71 * from buffer end to buffer beginning (in other words,
72 * strlen <= cb->maxlen - cb->end)
75 /* add string to head */
76 if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
77 != (sizeof(CIRBUF_STR_HEAD))) {
78 printf("Error: failed to add string to head!\n");
81 /* add string to tail */
82 if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
83 != (sizeof(CIRBUF_STR_TAIL))) {
84 printf("Error: failed to add string to head!\n");
87 /* read string from tail */
88 if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_TAIL))
89 != (sizeof(CIRBUF_STR_TAIL))) {
90 printf("Error: failed to get string from tail!\n");
94 if (strncmp(tmp, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) != 0) {
95 printf("Error: tail strings do not match!\n");
99 memset(tmp, 0, sizeof(tmp));
100 memset(buf, 0, sizeof(buf));
105 * add a string to buffer when start/end is at end of buffer
109 * reinitialize circular buffer with start at the end of cirbuf
111 if (cirbuf_init(&cb, buf, CMDLINE_TEST_BUFSIZE - 2, sizeof(buf)) < 0) {
112 printf("Error: failed to reinitialize circular buffer!\n");
117 /* add string to tail */
118 if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
119 != (sizeof(CIRBUF_STR_TAIL))) {
120 printf("Error: failed to add string to tail!\n");
123 /* read string from tail */
124 if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_TAIL))
125 != (sizeof(CIRBUF_STR_TAIL))) {
126 printf("Error: failed to get string from tail!\n");
130 if (strncmp(tmp, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) != 0) {
131 printf("Error: tail strings do not match!\n");
134 /* clear tmp buffer */
135 memset(tmp, 0, sizeof(tmp));
138 /* add string to head */
139 if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
140 != (sizeof(CIRBUF_STR_HEAD))) {
141 printf("Error: failed to add string to head!\n");
144 /* read string from tail */
145 if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_HEAD))
146 != (sizeof(CIRBUF_STR_HEAD))) {
147 printf("Error: failed to get string from head!\n");
151 if (strncmp(tmp, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD)) != 0) {
152 printf("Error: headstrings do not match!\n");
159 /* test adding and deleting strings */
161 test_cirbuf_string_add_del(void)
164 char buf[CMDLINE_TEST_BUFSIZE];
165 char tmp[CMDLINE_TEST_BUFSIZE];
167 /* initialize buffers */
168 memset(buf, 0, sizeof(buf));
169 memset(tmp, 0, sizeof(tmp));
172 * initialize circular buffer
174 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
175 printf("Error: failed to initialize circular buffer!\n");
179 /* add string to head */
180 if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
181 != (sizeof(CIRBUF_STR_HEAD))) {
182 printf("Error: failed to add string to head!\n");
185 /* read string from head */
186 if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_HEAD))
187 != (sizeof(CIRBUF_STR_HEAD))) {
188 printf("Error: failed to get string from head!\n");
192 if (strncmp(tmp, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD)) != 0) {
193 printf("Error: head strings do not match!\n");
196 /* clear tmp buffer */
197 memset(tmp, 0, sizeof(tmp));
198 /* read string from tail */
199 if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_HEAD))
200 != (sizeof(CIRBUF_STR_HEAD))) {
201 printf("Error: failed to get string from head!\n");
205 if (strncmp(tmp, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD)) != 0) {
206 printf("Error: head strings do not match!\n");
209 /* delete string from head*/
210 if (cirbuf_del_buf_head(&cb, sizeof(CIRBUF_STR_HEAD)) < 0) {
211 printf("Error: failed to delete string from head!\n");
214 /* verify string was deleted */
215 if (cirbuf_del_head_safe(&cb) == 0) {
216 printf("Error: buffer should have been empty!\n");
219 /* clear tmp buffer */
220 memset(tmp, 0, sizeof(tmp));
225 * reinitialize circular buffer
227 memset(buf, 0, sizeof(buf));
228 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
229 printf("Error: failed to reinitialize circular buffer!\n");
233 /* add string to tail */
234 if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
235 != (sizeof(CIRBUF_STR_TAIL))) {
236 printf("Error: failed to add string to tail!\n");
239 /* get string from tail */
240 if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_TAIL))
241 != (sizeof(CIRBUF_STR_TAIL))) {
242 printf("Error: failed to get string from tail!\n");
246 if (strncmp(tmp, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) != 0) {
247 printf("Error: tail strings do not match!\n");
250 /* clear tmp buffer */
251 memset(tmp, 0, sizeof(tmp));
252 /* get string from head */
253 if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_TAIL))
254 != (sizeof(CIRBUF_STR_TAIL))) {
255 printf("Error: failed to get string from tail!\n");
259 if (strncmp(tmp, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) != 0) {
260 printf("Error: tail strings do not match!\n");
263 /* delete string from tail */
264 if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_TAIL)) < 0) {
265 printf("Error: failed to delete string from tail!\n");
268 /* verify string was deleted */
269 if (cirbuf_del_tail_safe(&cb) == 0) {
270 printf("Error: buffer should have been empty!\n");
277 /* test adding from head and deleting from tail, and vice versa */
279 test_cirbuf_string_add_del_reverse(void)
282 char buf[CMDLINE_TEST_BUFSIZE];
283 char tmp[CMDLINE_TEST_BUFSIZE];
285 /* initialize buffers */
286 memset(buf, 0, sizeof(buf));
287 memset(tmp, 0, sizeof(tmp));
290 * initialize circular buffer
292 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
293 printf("Error: failed to initialize circular buffer!\n");
297 /* add string to head */
298 if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
299 != (sizeof(CIRBUF_STR_HEAD))) {
300 printf("Error: failed to add string to head!\n");
303 /* delete string from tail */
304 if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_HEAD)) < 0) {
305 printf("Error: failed to delete string from tail!\n");
308 /* verify string was deleted */
309 if (cirbuf_del_tail_safe(&cb) == 0) {
310 printf("Error: buffer should have been empty!\n");
313 /* clear tmp buffer */
314 memset(tmp, 0, sizeof(tmp));
317 * reinitialize circular buffer
319 memset(buf, 0, sizeof(buf));
320 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
321 printf("Error: failed to reinitialize circular buffer!\n");
325 /* add string to tail */
326 if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
327 != (sizeof(CIRBUF_STR_TAIL))) {
328 printf("Error: failed to add string to tail!\n");
331 /* delete string from head */
332 if (cirbuf_del_buf_head(&cb, sizeof(CIRBUF_STR_TAIL)) < 0) {
333 printf("Error: failed to delete string from head!\n");
336 /* verify string was deleted */
337 if (cirbuf_del_head_safe(&cb) == 0) {
338 printf("Error: buffer should have been empty!\n");
345 /* try to write more than available */
347 test_cirbuf_string_add_boundaries(void)
350 char buf[CMDLINE_TEST_BUFSIZE];
353 /* initialize buffers */
354 memset(buf, 0, sizeof(buf));
357 * initialize circular buffer
359 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
360 printf("Error: failed to initialize circular buffer!\n");
364 /* fill the buffer from tail */
365 for (i = 0; i < CMDLINE_TEST_BUFSIZE - sizeof(CIRBUF_STR_TAIL) + 1; i++)
366 cirbuf_add_tail_safe(&cb, 't');
368 /* try adding a string to tail */
369 if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
371 printf("Error: buffer should have been full!\n");
374 /* try adding a string to head */
375 if (cirbuf_add_buf_head(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
377 printf("Error: buffer should have been full!\n");
382 * reinitialize circular buffer
384 memset(buf, 0, sizeof(buf));
385 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
386 printf("Error: failed to reinitialize circular buffer!\n");
390 /* fill the buffer from head */
391 for (i = 0; i < CMDLINE_TEST_BUFSIZE - sizeof(CIRBUF_STR_HEAD) + 1; i++)
392 cirbuf_add_head_safe(&cb, 'h');
394 /* try adding a string to head */
395 if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
397 printf("Error: buffer should have been full!\n");
400 /* try adding a string to tail */
401 if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
403 printf("Error: buffer should have been full!\n");
410 /* try to read/delete more than written */
412 test_cirbuf_string_get_del_boundaries(void)
415 char buf[CMDLINE_TEST_BUFSIZE];
416 char tmp[CMDLINE_TEST_BUFSIZE];
418 /* initialize buffers */
419 memset(buf, 0, sizeof(buf));
420 memset(tmp, 0, sizeof(tmp));
423 * initialize circular buffer
425 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
426 printf("Error: failed to initialize circular buffer!\n");
431 /* add string to head */
432 if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
433 != (sizeof(CIRBUF_STR_HEAD))) {
434 printf("Error: failed to add string to head!\n");
437 /* read more than written (head) */
438 if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_HEAD) + 1)
439 != sizeof(CIRBUF_STR_HEAD)) {
440 printf("Error: unexpected result when reading too much data!\n");
443 /* read more than written (tail) */
444 if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_HEAD) + 1)
445 != sizeof(CIRBUF_STR_HEAD)) {
446 printf("Error: unexpected result when reading too much data!\n");
449 /* delete more than written (head) */
450 if (cirbuf_del_buf_head(&cb, sizeof(CIRBUF_STR_HEAD) + 1) == 0) {
451 printf("Error: unexpected result when deleting too much data!\n");
454 /* delete more than written (tail) */
455 if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_HEAD) + 1) == 0) {
456 printf("Error: unexpected result when deleting too much data!\n");
461 * reinitialize circular buffer
463 memset(buf, 0, sizeof(buf));
464 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
465 printf("Error: failed to reinitialize circular buffer!\n");
469 /* add string to tail */
470 if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
471 != (sizeof(CIRBUF_STR_TAIL))) {
472 printf("Error: failed to add string to tail!\n");
475 /* read more than written (tail) */
476 if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_TAIL) + 1)
477 != sizeof(CIRBUF_STR_TAIL)) {
478 printf("Error: unexpected result when reading too much data!\n");
481 /* read more than written (head) */
482 if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_TAIL) + 1)
483 != sizeof(CIRBUF_STR_TAIL)) {
484 printf("Error: unexpected result when reading too much data!\n");
487 /* delete more than written (tail) */
488 if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_TAIL) + 1) == 0) {
489 printf("Error: unexpected result when deleting too much data!\n");
492 /* delete more than written (head) */
493 if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_TAIL) + 1) == 0) {
494 printf("Error: unexpected result when deleting too much data!\n");
501 /* try to read/delete less than written */
503 test_cirbuf_string_get_del_partial(void)
506 char buf[CMDLINE_TEST_BUFSIZE];
507 char tmp[CMDLINE_TEST_BUFSIZE];
508 char tmp2[CMDLINE_TEST_BUFSIZE];
510 /* initialize buffers */
511 memset(buf, 0, sizeof(buf));
512 memset(tmp, 0, sizeof(tmp));
513 memset(tmp2, 0, sizeof(tmp));
515 snprintf(tmp2, sizeof(tmp2), "%s", CIRBUF_STR_HEAD);
518 * initialize circular buffer
520 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
521 printf("Error: failed to initialize circular buffer!\n");
525 /* add string to head */
526 if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
527 != (sizeof(CIRBUF_STR_HEAD))) {
528 printf("Error: failed to add string to head!\n");
531 /* read less than written (head) */
532 if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_HEAD) - 1)
533 != sizeof(CIRBUF_STR_HEAD) - 1) {
534 printf("Error: unexpected result when reading from head!\n");
538 if (strncmp(tmp, tmp2, sizeof(CIRBUF_STR_HEAD) - 1) != 0) {
539 printf("Error: strings mismatch!\n");
542 memset(tmp, 0, sizeof(tmp));
543 /* read less than written (tail) */
544 if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_HEAD) - 1)
545 != sizeof(CIRBUF_STR_HEAD) - 1) {
546 printf("Error: unexpected result when reading from tail!\n");
550 if (strncmp(tmp, &tmp2[1], sizeof(CIRBUF_STR_HEAD) - 1) != 0) {
551 printf("Error: strings mismatch!\n");
556 * verify correct deletion
560 memset(tmp, 0, sizeof(tmp));
562 /* delete less than written (head) */
563 if (cirbuf_del_buf_head(&cb, 1) != 0) {
564 printf("Error: delete from head failed!\n");
568 if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_HEAD) - 1)
569 != sizeof(CIRBUF_STR_HEAD) - 1) {
570 printf("Error: unexpected result when reading from head!\n");
573 /* since we deleted from head, first char should be deleted */
574 if (strncmp(tmp, &tmp2[1], sizeof(CIRBUF_STR_HEAD) - 1) != 0) {
575 printf("Error: strings mismatch!\n");
579 memset(tmp, 0, sizeof(tmp));
581 /* delete less than written (tail) */
582 if (cirbuf_del_buf_tail(&cb, 1) != 0) {
583 printf("Error: delete from tail failed!\n");
587 if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_HEAD) - 2)
588 != sizeof(CIRBUF_STR_HEAD) - 2) {
589 printf("Error: unexpected result when reading from head!\n");
592 /* since we deleted from tail, last char should be deleted */
593 if (strncmp(tmp, &tmp2[1], sizeof(CIRBUF_STR_HEAD) - 2) != 0) {
594 printf("Error: strings mismatch!\n");
601 /* test cmdline_cirbuf char add/del functions */
603 test_cirbuf_char_add_del(void)
606 char buf[CMDLINE_TEST_BUFSIZE];
607 char tmp[CMDLINE_TEST_BUFSIZE];
610 memset(buf, 0, sizeof(buf));
611 memset(tmp, 0, sizeof(tmp));
614 * initialize circular buffer
616 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
617 printf("Error: failed to initialize circular buffer!\n");
622 * try to delete something from cirbuf. since it's empty,
625 if (cirbuf_del_head_safe(&cb) == 0) {
626 printf("Error: deleting from empty cirbuf head succeeded!\n");
629 if (cirbuf_del_tail_safe(&cb) == 0) {
630 printf("Error: deleting from empty cirbuf tail succeeded!\n");
635 * add, verify and delete. these should pass.
637 if (cirbuf_add_head_safe(&cb,'h') < 0) {
638 printf("Error: adding to cirbuf head failed!\n");
641 if (cirbuf_get_head(&cb) != 'h') {
642 printf("Error: wrong head content!\n");
645 if (cirbuf_del_head_safe(&cb) < 0) {
646 printf("Error: deleting from cirbuf head failed!\n");
649 if (cirbuf_add_tail_safe(&cb,'t') < 0) {
650 printf("Error: adding to cirbuf tail failed!\n");
653 if (cirbuf_get_tail(&cb) != 't') {
654 printf("Error: wrong tail content!\n");
657 if (cirbuf_del_tail_safe(&cb) < 0) {
658 printf("Error: deleting from cirbuf tail failed!\n");
661 /* do the same for unsafe versions. those are void. */
662 cirbuf_add_head(&cb,'h');
663 if (cirbuf_get_head(&cb) != 'h') {
664 printf("Error: wrong head content!\n");
667 cirbuf_del_head(&cb);
669 /* test if char has been deleted. we can't call cirbuf_get_head
670 * because it's unsafe, but we can call cirbuf_get_buf_head.
672 if (cirbuf_get_buf_head(&cb, tmp, 1) > 0) {
673 printf("Error: buffer should have been empty!\n");
677 cirbuf_add_tail(&cb,'t');
678 if (cirbuf_get_tail(&cb) != 't') {
679 printf("Error: wrong tail content!\n");
682 cirbuf_del_tail(&cb);
684 /* test if char has been deleted. we can't call cirbuf_get_tail
685 * because it's unsafe, but we can call cirbuf_get_buf_tail.
687 if (cirbuf_get_buf_tail(&cb, tmp, 1) > 0) {
688 printf("Error: buffer should have been empty!\n");
695 /* test filling up buffer with chars */
697 test_cirbuf_char_fill(void)
700 char buf[CMDLINE_TEST_BUFSIZE];
704 memset(buf, 0, sizeof(buf));
707 * initialize circular buffer
709 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
710 printf("Error: failed to initialize circular buffer!\n");
715 * fill the buffer from head or tail, verify contents, test boundaries
716 * and clear the buffer
719 /* fill the buffer from tail */
720 for (i = 0; i < CMDLINE_TEST_BUFSIZE; i++)
721 cirbuf_add_tail_safe(&cb, 't');
722 /* verify that contents of the buffer are what they are supposed to be */
723 for (i = 0; i < sizeof(buf); i++) {
725 printf("Error: wrong content in buffer!\n");
729 /* try to add to a full buffer from tail */
730 if (cirbuf_add_tail_safe(&cb, 't') == 0) {
731 printf("Error: buffer should have been full!\n");
734 /* try to add to a full buffer from head */
735 if (cirbuf_add_head_safe(&cb, 'h') == 0) {
736 printf("Error: buffer should have been full!\n");
739 /* delete buffer from tail */
740 for(i = 0; i < CMDLINE_TEST_BUFSIZE; i++)
741 cirbuf_del_tail_safe(&cb);
742 /* try to delete from an empty buffer */
743 if (cirbuf_del_tail_safe(&cb) >= 0) {
744 printf("Error: buffer should have been empty!\n");
748 /* fill the buffer from head */
749 for (i = 0; i < CMDLINE_TEST_BUFSIZE; i++)
750 cirbuf_add_head_safe(&cb, 'h');
751 /* verify that contents of the buffer are what they are supposed to be */
752 for (i = 0; i < sizeof(buf); i++) {
754 printf("Error: wrong content in buffer!\n");
758 /* try to add to a full buffer from head */
759 if (cirbuf_add_head_safe(&cb,'h') >= 0) {
760 printf("Error: buffer should have been full!\n");
763 /* try to add to a full buffer from tail */
764 if (cirbuf_add_tail_safe(&cb, 't') == 0) {
765 printf("Error: buffer should have been full!\n");
768 /* delete buffer from head */
769 for(i = 0; i < CMDLINE_TEST_BUFSIZE; i++)
770 cirbuf_del_head_safe(&cb);
771 /* try to delete from an empty buffer */
772 if (cirbuf_del_head_safe(&cb) >= 0) {
773 printf("Error: buffer should have been empty!\n");
778 * fill the buffer from both head and tail, with alternating characters,
779 * verify contents and clear the buffer
782 /* fill half of buffer from tail */
783 for (i = 0; i < CMDLINE_TEST_BUFSIZE / 2; i++)
784 cirbuf_add_tail_safe(&cb, (char) (i % 2 ? 't' : 'T'));
785 /* fill other half of the buffer from head */
786 for (i = 0; i < CMDLINE_TEST_BUFSIZE / 2; i++)
787 cirbuf_add_head_safe(&cb, (char) (i % 2 ? 'H' : 'h')); /* added in reverse */
789 /* verify that contents of the buffer are what they are supposed to be */
790 for (i = 0; i < sizeof(buf) / 2; i++) {
791 if (buf[i] != (char) (i % 2 ? 't' : 'T')) {
792 printf("Error: wrong content in buffer at %u!\n", i);
796 for (i = sizeof(buf) / 2; i < sizeof(buf); i++) {
797 if (buf[i] != (char) (i % 2 ? 'h' : 'H')) {
798 printf("Error: wrong content in buffer %u!\n", i);
806 /* test left alignment */
808 test_cirbuf_align_left(void)
810 #define HALF_OFFSET CMDLINE_TEST_BUFSIZE / 2
811 #define SMALL_OFFSET HALF_OFFSET / 2
812 /* resulting buffer lengths for each of the test cases */
813 #define LEN1 HALF_OFFSET - SMALL_OFFSET - 1
814 #define LEN2 HALF_OFFSET + SMALL_OFFSET + 2
815 #define LEN3 HALF_OFFSET - SMALL_OFFSET
816 #define LEN4 HALF_OFFSET + SMALL_OFFSET - 1
819 char buf[CMDLINE_TEST_BUFSIZE];
820 char tmp[CMDLINE_TEST_BUFSIZE];
824 * align left when start < end and start in left half
828 * initialize circular buffer
830 memset(buf, 0, sizeof(buf));
831 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
832 printf("Error: failed to initialize circular buffer!\n");
836 /* push end into left half */
837 for (i = 0; i < HALF_OFFSET - 1; i++)
838 cirbuf_add_tail_safe(&cb, 't');
840 /* push start into left half < end */
841 for (i = 0; i < SMALL_OFFSET; i++)
842 cirbuf_del_head_safe(&cb);
845 if (cirbuf_align_left(&cb) < 0) {
846 printf("Error: alignment failed!\n");
851 if (cb.start != 0 || cb.len != LEN1 || cb.end != cb.len - 1) {
852 printf("Error: buffer alignment is wrong!\n");
857 * align left when start > end and start in left half
861 * reinitialize circular buffer
863 memset(buf, 0, sizeof(buf));
864 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
865 printf("Error: failed to reinitialize circular buffer!\n");
869 /* push start into left half */
870 for (i = 0; i < HALF_OFFSET + 2; i++)
871 cirbuf_add_head_safe(&cb, 'h');
873 /* push end into left half > start */
874 for (i = 0; i < SMALL_OFFSET; i++)
875 cirbuf_add_tail_safe(&cb, 't');
878 if (cirbuf_align_left(&cb) < 0) {
879 printf("Error: alignment failed!\n");
884 if (cb.start != 0 || cb.len != LEN2 || cb.end != cb.len - 1) {
885 printf("Error: buffer alignment is wrong!");
890 * align left when start < end and start in right half
894 * reinitialize circular buffer
896 memset(buf, 0, sizeof(buf));
897 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
898 printf("Error: failed to reinitialize circular buffer!\n");
902 /* push start into the right half */
903 for (i = 0; i < HALF_OFFSET; i++)
904 cirbuf_add_head_safe(&cb, 'h');
906 /* push end into left half > start */
907 for (i = 0; i < SMALL_OFFSET; i++)
908 cirbuf_del_tail_safe(&cb);
911 if (cirbuf_align_left(&cb) < 0) {
912 printf("Error: alignment failed!\n");
917 if (cb.start != 0 || cb.len != LEN3 || cb.end != cb.len - 1) {
918 printf("Error: buffer alignment is wrong!");
923 * align left when start > end and start in right half
927 * reinitialize circular buffer
929 memset(buf, 0, sizeof(buf));
930 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
931 printf("Error: failed to reinitialize circular buffer!\n");
935 /* push start into the right half */
936 for (i = 0; i < HALF_OFFSET - 1; i++)
937 cirbuf_add_head_safe(&cb, 'h');
939 /* push end into left half < start */
940 for (i = 0; i < SMALL_OFFSET; i++)
941 cirbuf_add_tail_safe(&cb, 't');
944 if (cirbuf_align_left(&cb) < 0) {
945 printf("Error: alignment failed!\n");
950 if (cb.start != 0 || cb.len != LEN4 ||
951 cb.end != cb.len - 1) {
952 printf("Error: buffer alignment is wrong!");
957 * Verify that alignment doesn't corrupt data
961 * reinitialize circular buffer
963 memset(buf, 0, sizeof(buf));
964 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
965 printf("Error: failed to reinitialize circular buffer!\n");
969 /* add string to tail and head */
970 if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD,
971 sizeof(CIRBUF_STR_HEAD)) < 0 || cirbuf_add_buf_tail(&cb,
972 CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) < 0) {
973 printf("Error: failed to add strings!\n");
978 if (cirbuf_align_left(&cb) < 0) {
979 printf("Error: alignment failed!\n");
983 /* get string from head */
984 if (cirbuf_get_buf_head(&cb, tmp,
985 sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) < 0) {
986 printf("Error: failed to read string from head!\n");
991 if (strncmp(tmp, CIRBUF_STR_HEAD "\0" CIRBUF_STR_TAIL,
992 sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) != 0) {
993 printf("Error: strings mismatch!\n");
997 /* reset tmp buffer */
998 memset(tmp, 0, sizeof(tmp));
1000 /* get string from tail */
1001 if (cirbuf_get_buf_tail(&cb, tmp,
1002 sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) < 0) {
1003 printf("Error: failed to read string from head!\n");
1008 if (strncmp(tmp, CIRBUF_STR_HEAD "\0" CIRBUF_STR_TAIL,
1009 sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) != 0) {
1010 printf("Error: strings mismatch!\n");
1017 /* test right alignment */
1019 test_cirbuf_align_right(void)
1021 #define END_OFFSET CMDLINE_TEST_BUFSIZE - 1
1023 char buf[CMDLINE_TEST_BUFSIZE];
1024 char tmp[CMDLINE_TEST_BUFSIZE];
1029 * align right when start < end and start in left half
1033 * initialize circular buffer
1035 memset(buf, 0, sizeof(buf));
1036 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
1037 printf("Error: failed to initialize circular buffer!\n");
1041 /* push end into left half */
1042 for (i = 0; i < HALF_OFFSET - 1; i++)
1043 cirbuf_add_tail_safe(&cb, 't');
1045 /* push start into left half < end */
1046 for (i = 0; i < SMALL_OFFSET; i++)
1047 cirbuf_del_head_safe(&cb);
1050 cirbuf_align_right(&cb);
1053 if (cb.start != END_OFFSET || cb.len != LEN1 || cb.end != cb.len - 2) {
1054 printf("Error: buffer alignment is wrong!\n");
1059 * align right when start > end and start in left half
1063 * reinitialize circular buffer
1065 memset(buf, 0, sizeof(buf));
1066 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
1067 printf("Error: failed to reinitialize circular buffer!\n");
1071 /* push start into left half */
1072 for (i = 0; i < HALF_OFFSET + 2; i++)
1073 cirbuf_add_head_safe(&cb, 'h');
1075 /* push end into left half > start */
1076 for (i = 0; i < SMALL_OFFSET; i++)
1077 cirbuf_add_tail_safe(&cb, 't');
1080 cirbuf_align_right(&cb);
1083 if (cb.start != END_OFFSET || cb.len != LEN2 || cb.end != cb.len - 2) {
1084 printf("Error: buffer alignment is wrong!");
1089 * align right when start < end and start in right half
1093 * reinitialize circular buffer
1095 memset(buf, 0, sizeof(buf));
1096 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
1097 printf("Error: failed to reinitialize circular buffer!\n");
1101 /* push start into the right half */
1102 for (i = 0; i < HALF_OFFSET; i++)
1103 cirbuf_add_head_safe(&cb, 'h');
1105 /* push end into left half > start */
1106 for (i = 0; i < SMALL_OFFSET; i++)
1107 cirbuf_del_tail_safe(&cb);
1110 cirbuf_align_right(&cb);
1113 if (cb.end != END_OFFSET || cb.len != LEN3 || cb.start != cb.end - cb.len + 1) {
1114 printf("Error: buffer alignment is wrong!");
1119 * align right when start > end and start in right half
1123 * reinitialize circular buffer
1125 memset(buf, 0, sizeof(buf));
1126 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
1127 printf("Error: failed to reinitialize circular buffer!\n");
1131 /* push start into the right half */
1132 for (i = 0; i < HALF_OFFSET - 1; i++)
1133 cirbuf_add_head_safe(&cb, 'h');
1135 /* push end into left half < start */
1136 for (i = 0; i < SMALL_OFFSET; i++)
1137 cirbuf_add_tail_safe(&cb, 't');
1140 cirbuf_align_right(&cb);
1143 if (cb.end != END_OFFSET || cb.len != LEN4 || cb.start != cb.end - cb.len + 1) {
1144 printf("Error: buffer alignment is wrong!");
1149 * Verify that alignment doesn't corrupt data
1153 * reinitialize circular buffer
1155 memset(buf, 0, sizeof(buf));
1156 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
1157 printf("Error: failed to reinitialize circular buffer!\n");
1161 /* add string to tail and head */
1162 if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL,
1163 sizeof(CIRBUF_STR_TAIL)) < 0 || cirbuf_add_buf_head(&cb,
1164 CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD)) < 0) {
1165 printf("Error: failed to add strings!\n");
1170 if (cirbuf_align_right(&cb) < 0) {
1171 printf("Error: alignment failed!\n");
1175 /* get string from head */
1176 if (cirbuf_get_buf_head(&cb, tmp,
1177 sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) < 0) {
1178 printf("Error: failed to read string from head!\n");
1183 if (strncmp(tmp, CIRBUF_STR_HEAD "\0" CIRBUF_STR_TAIL,
1184 sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) != 0) {
1185 printf("Error: strings mismatch!\n");
1189 /* reset tmp buffer */
1190 memset(tmp, 0, sizeof(tmp));
1192 /* get string from tail */
1193 if (cirbuf_get_buf_tail(&cb, tmp,
1194 sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) < 0) {
1195 printf("Error: failed to read string from head!\n");
1199 if (strncmp(tmp, CIRBUF_STR_HEAD "\0" CIRBUF_STR_TAIL,
1200 sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) != 0) {
1201 printf("Error: strings mismatch!\n");
1208 /* call functions with invalid parameters */
1210 test_cirbuf_invalid_param(void)
1213 char buf[CMDLINE_TEST_BUFSIZE];
1216 if (cirbuf_init(0, buf, 0, sizeof(buf)) == 0)
1219 if (cirbuf_init(&cb, 0, 0, sizeof(buf)) == 0)
1222 if (cirbuf_add_head_safe(0, 'h') == 0)
1224 if (cirbuf_add_tail_safe(0, 't') == 0)
1226 if (cirbuf_del_head_safe(0) == 0)
1228 if (cirbuf_del_tail_safe(0) == 0)
1231 if (cirbuf_add_buf_head(&cb, 0, 0) == 0)
1233 if (cirbuf_add_buf_tail(&cb, 0, 0) == 0)
1236 if (cirbuf_add_buf_head(0, buf, 0) == 0)
1238 if (cirbuf_add_buf_tail(0, buf, 0) == 0)
1241 if (cirbuf_add_buf_head(&cb, buf, 0) == 0)
1243 if (cirbuf_add_buf_tail(&cb, buf, 0) == 0)
1246 if (cirbuf_del_buf_head(0, 0) == 0)
1248 if (cirbuf_del_buf_tail(0, 0) == 0)
1251 if (cirbuf_del_buf_head(&cb, 0) == 0)
1253 if (cirbuf_del_buf_tail(&cb, 0) == 0)
1256 if (cirbuf_get_buf_head(0, 0, 0) == 0)
1258 if (cirbuf_get_buf_tail(0, 0, 0) == 0)
1261 if (cirbuf_get_buf_head(&cb, 0, 0) == 0)
1263 if (cirbuf_get_buf_tail(&cb, 0, 0) == 0)
1265 /* null size, this is valid but should return 0 */
1266 if (cirbuf_get_buf_head(&cb, buf, 0) != 0)
1268 if (cirbuf_get_buf_tail(&cb, buf, 0) != 0)
1271 if (cirbuf_align_left(0) == 0)
1273 if (cirbuf_align_right(0) == 0)
1279 /* test cmdline_cirbuf char functions */
1281 test_cirbuf_char(void)
1285 ret = test_cirbuf_char_add_del();
1289 ret = test_cirbuf_char_fill();
1296 /* test cmdline_cirbuf string functions */
1298 test_cirbuf_string(void)
1300 if (test_cirbuf_string_add_del() < 0)
1303 if (test_cirbuf_string_add_del_reverse() < 0)
1306 if (test_cirbuf_string_add_boundaries() < 0)
1309 if (test_cirbuf_string_get_del_boundaries() < 0)
1312 if (test_cirbuf_string_get_del_partial() < 0)
1315 if (test_cirbuf_string_misc() < 0)
1321 /* test cmdline_cirbuf align functions */
1323 test_cirbuf_align(void)
1325 if (test_cirbuf_align_left() < 0)
1327 if (test_cirbuf_align_right() < 0)