1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
9 #include <rte_string_fns.h>
11 #include <cmdline_cirbuf.h>
13 #include "test_cmdline.h"
15 /* different length strings */
16 #define CIRBUF_STR_HEAD " HEAD"
17 #define CIRBUF_STR_TAIL "TAIL"
19 /* miscellaneous tests - they make bullseye happy */
21 test_cirbuf_string_misc(void)
24 char buf[CMDLINE_TEST_BUFSIZE];
25 char tmp[CMDLINE_TEST_BUFSIZE];
27 /* initialize buffers */
28 memset(buf, 0, sizeof(buf));
29 memset(tmp, 0, sizeof(tmp));
32 * initialize circular buffer
34 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
35 printf("Error: failed to initialize circular buffer!\n");
40 * add strings to head and tail, but read only tail
41 * this results in read operation that does not transcend
42 * from buffer end to buffer beginning (in other words,
43 * strlen <= cb->maxlen - cb->end)
46 /* add string to head */
47 if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
48 != (sizeof(CIRBUF_STR_HEAD))) {
49 printf("Error: failed to add string to head!\n");
52 /* add string to tail */
53 if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
54 != (sizeof(CIRBUF_STR_TAIL))) {
55 printf("Error: failed to add string to head!\n");
58 /* read string from tail */
59 if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_TAIL))
60 != (sizeof(CIRBUF_STR_TAIL))) {
61 printf("Error: failed to get string from tail!\n");
65 if (strncmp(tmp, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) != 0) {
66 printf("Error: tail strings do not match!\n");
70 memset(tmp, 0, sizeof(tmp));
71 memset(buf, 0, sizeof(buf));
76 * add a string to buffer when start/end is at end of buffer
80 * reinitialize circular buffer with start at the end of cirbuf
82 if (cirbuf_init(&cb, buf, CMDLINE_TEST_BUFSIZE - 2, sizeof(buf)) < 0) {
83 printf("Error: failed to reinitialize circular buffer!\n");
88 /* add string to tail */
89 if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
90 != (sizeof(CIRBUF_STR_TAIL))) {
91 printf("Error: failed to add string to tail!\n");
94 /* read string from tail */
95 if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_TAIL))
96 != (sizeof(CIRBUF_STR_TAIL))) {
97 printf("Error: failed to get string from tail!\n");
101 if (strncmp(tmp, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) != 0) {
102 printf("Error: tail strings do not match!\n");
105 /* clear tmp buffer */
106 memset(tmp, 0, sizeof(tmp));
109 /* add string to head */
110 if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
111 != (sizeof(CIRBUF_STR_HEAD))) {
112 printf("Error: failed to add string to head!\n");
115 /* read string from tail */
116 if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_HEAD))
117 != (sizeof(CIRBUF_STR_HEAD))) {
118 printf("Error: failed to get string from head!\n");
122 if (strncmp(tmp, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD)) != 0) {
123 printf("Error: headstrings do not match!\n");
130 /* test adding and deleting strings */
132 test_cirbuf_string_add_del(void)
135 char buf[CMDLINE_TEST_BUFSIZE];
136 char tmp[CMDLINE_TEST_BUFSIZE];
138 /* initialize buffers */
139 memset(buf, 0, sizeof(buf));
140 memset(tmp, 0, sizeof(tmp));
143 * initialize circular buffer
145 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
146 printf("Error: failed to initialize circular buffer!\n");
150 /* add string to head */
151 if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
152 != (sizeof(CIRBUF_STR_HEAD))) {
153 printf("Error: failed to add string to head!\n");
156 /* read string from head */
157 if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_HEAD))
158 != (sizeof(CIRBUF_STR_HEAD))) {
159 printf("Error: failed to get string from head!\n");
163 if (strncmp(tmp, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD)) != 0) {
164 printf("Error: head strings do not match!\n");
167 /* clear tmp buffer */
168 memset(tmp, 0, sizeof(tmp));
169 /* read string from tail */
170 if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_HEAD))
171 != (sizeof(CIRBUF_STR_HEAD))) {
172 printf("Error: failed to get string from head!\n");
176 if (strncmp(tmp, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD)) != 0) {
177 printf("Error: head strings do not match!\n");
180 /* delete string from head*/
181 if (cirbuf_del_buf_head(&cb, sizeof(CIRBUF_STR_HEAD)) < 0) {
182 printf("Error: failed to delete string from head!\n");
185 /* verify string was deleted */
186 if (cirbuf_del_head_safe(&cb) == 0) {
187 printf("Error: buffer should have been empty!\n");
190 /* clear tmp buffer */
191 memset(tmp, 0, sizeof(tmp));
196 * reinitialize circular buffer
198 memset(buf, 0, sizeof(buf));
199 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
200 printf("Error: failed to reinitialize circular buffer!\n");
204 /* add string to tail */
205 if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
206 != (sizeof(CIRBUF_STR_TAIL))) {
207 printf("Error: failed to add string to tail!\n");
210 /* get string from tail */
211 if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_TAIL))
212 != (sizeof(CIRBUF_STR_TAIL))) {
213 printf("Error: failed to get string from tail!\n");
217 if (strncmp(tmp, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) != 0) {
218 printf("Error: tail strings do not match!\n");
221 /* clear tmp buffer */
222 memset(tmp, 0, sizeof(tmp));
223 /* get string from head */
224 if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_TAIL))
225 != (sizeof(CIRBUF_STR_TAIL))) {
226 printf("Error: failed to get string from tail!\n");
230 if (strncmp(tmp, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) != 0) {
231 printf("Error: tail strings do not match!\n");
234 /* delete string from tail */
235 if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_TAIL)) < 0) {
236 printf("Error: failed to delete string from tail!\n");
239 /* verify string was deleted */
240 if (cirbuf_del_tail_safe(&cb) == 0) {
241 printf("Error: buffer should have been empty!\n");
248 /* test adding from head and deleting from tail, and vice versa */
250 test_cirbuf_string_add_del_reverse(void)
253 char buf[CMDLINE_TEST_BUFSIZE];
254 char tmp[CMDLINE_TEST_BUFSIZE];
256 /* initialize buffers */
257 memset(buf, 0, sizeof(buf));
258 memset(tmp, 0, sizeof(tmp));
261 * initialize circular buffer
263 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
264 printf("Error: failed to initialize circular buffer!\n");
268 /* add string to head */
269 if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
270 != (sizeof(CIRBUF_STR_HEAD))) {
271 printf("Error: failed to add string to head!\n");
274 /* delete string from tail */
275 if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_HEAD)) < 0) {
276 printf("Error: failed to delete string from tail!\n");
279 /* verify string was deleted */
280 if (cirbuf_del_tail_safe(&cb) == 0) {
281 printf("Error: buffer should have been empty!\n");
284 /* clear tmp buffer */
285 memset(tmp, 0, sizeof(tmp));
288 * reinitialize circular buffer
290 memset(buf, 0, sizeof(buf));
291 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
292 printf("Error: failed to reinitialize circular buffer!\n");
296 /* add string to tail */
297 if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
298 != (sizeof(CIRBUF_STR_TAIL))) {
299 printf("Error: failed to add string to tail!\n");
302 /* delete string from head */
303 if (cirbuf_del_buf_head(&cb, sizeof(CIRBUF_STR_TAIL)) < 0) {
304 printf("Error: failed to delete string from head!\n");
307 /* verify string was deleted */
308 if (cirbuf_del_head_safe(&cb) == 0) {
309 printf("Error: buffer should have been empty!\n");
316 /* try to write more than available */
318 test_cirbuf_string_add_boundaries(void)
321 char buf[CMDLINE_TEST_BUFSIZE];
324 /* initialize buffers */
325 memset(buf, 0, sizeof(buf));
328 * initialize circular buffer
330 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
331 printf("Error: failed to initialize circular buffer!\n");
335 /* fill the buffer from tail */
336 for (i = 0; i < CMDLINE_TEST_BUFSIZE - sizeof(CIRBUF_STR_TAIL) + 1; i++)
337 cirbuf_add_tail_safe(&cb, 't');
339 /* try adding a string to tail */
340 if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
342 printf("Error: buffer should have been full!\n");
345 /* try adding a string to head */
346 if (cirbuf_add_buf_head(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
348 printf("Error: buffer should have been full!\n");
353 * reinitialize circular buffer
355 memset(buf, 0, sizeof(buf));
356 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
357 printf("Error: failed to reinitialize circular buffer!\n");
361 /* fill the buffer from head */
362 for (i = 0; i < CMDLINE_TEST_BUFSIZE - sizeof(CIRBUF_STR_HEAD) + 1; i++)
363 cirbuf_add_head_safe(&cb, 'h');
365 /* try adding a string to head */
366 if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
368 printf("Error: buffer should have been full!\n");
371 /* try adding a string to tail */
372 if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
374 printf("Error: buffer should have been full!\n");
381 /* try to read/delete more than written */
383 test_cirbuf_string_get_del_boundaries(void)
386 char buf[CMDLINE_TEST_BUFSIZE];
387 char tmp[CMDLINE_TEST_BUFSIZE];
389 /* initialize buffers */
390 memset(buf, 0, sizeof(buf));
391 memset(tmp, 0, sizeof(tmp));
394 * initialize circular buffer
396 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
397 printf("Error: failed to initialize circular buffer!\n");
402 /* add string to head */
403 if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
404 != (sizeof(CIRBUF_STR_HEAD))) {
405 printf("Error: failed to add string to head!\n");
408 /* read more than written (head) */
409 if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_HEAD) + 1)
410 != sizeof(CIRBUF_STR_HEAD)) {
411 printf("Error: unexpected result when reading too much data!\n");
414 /* read more than written (tail) */
415 if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_HEAD) + 1)
416 != sizeof(CIRBUF_STR_HEAD)) {
417 printf("Error: unexpected result when reading too much data!\n");
420 /* delete more than written (head) */
421 if (cirbuf_del_buf_head(&cb, sizeof(CIRBUF_STR_HEAD) + 1) == 0) {
422 printf("Error: unexpected result when deleting too much data!\n");
425 /* delete more than written (tail) */
426 if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_HEAD) + 1) == 0) {
427 printf("Error: unexpected result when deleting too much data!\n");
432 * reinitialize circular buffer
434 memset(buf, 0, sizeof(buf));
435 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
436 printf("Error: failed to reinitialize circular buffer!\n");
440 /* add string to tail */
441 if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
442 != (sizeof(CIRBUF_STR_TAIL))) {
443 printf("Error: failed to add string to tail!\n");
446 /* read more than written (tail) */
447 if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_TAIL) + 1)
448 != sizeof(CIRBUF_STR_TAIL)) {
449 printf("Error: unexpected result when reading too much data!\n");
452 /* read more than written (head) */
453 if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_TAIL) + 1)
454 != sizeof(CIRBUF_STR_TAIL)) {
455 printf("Error: unexpected result when reading too much data!\n");
458 /* delete more than written (tail) */
459 if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_TAIL) + 1) == 0) {
460 printf("Error: unexpected result when deleting too much data!\n");
463 /* delete more than written (head) */
464 if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_TAIL) + 1) == 0) {
465 printf("Error: unexpected result when deleting too much data!\n");
472 /* try to read/delete less than written */
474 test_cirbuf_string_get_del_partial(void)
477 char buf[CMDLINE_TEST_BUFSIZE];
478 char tmp[CMDLINE_TEST_BUFSIZE];
479 char tmp2[CMDLINE_TEST_BUFSIZE];
481 /* initialize buffers */
482 memset(buf, 0, sizeof(buf));
483 memset(tmp, 0, sizeof(tmp));
484 memset(tmp2, 0, sizeof(tmp));
486 strlcpy(tmp2, CIRBUF_STR_HEAD, sizeof(tmp2));
489 * initialize circular buffer
491 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
492 printf("Error: failed to initialize circular buffer!\n");
496 /* add string to head */
497 if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
498 != (sizeof(CIRBUF_STR_HEAD))) {
499 printf("Error: failed to add string to head!\n");
502 /* read less than written (head) */
503 if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_HEAD) - 1)
504 != sizeof(CIRBUF_STR_HEAD) - 1) {
505 printf("Error: unexpected result when reading from head!\n");
509 if (strncmp(tmp, tmp2, sizeof(CIRBUF_STR_HEAD) - 1) != 0) {
510 printf("Error: strings mismatch!\n");
513 memset(tmp, 0, sizeof(tmp));
514 /* read less than written (tail) */
515 if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_HEAD) - 1)
516 != sizeof(CIRBUF_STR_HEAD) - 1) {
517 printf("Error: unexpected result when reading from tail!\n");
521 if (strncmp(tmp, &tmp2[1], sizeof(CIRBUF_STR_HEAD) - 1) != 0) {
522 printf("Error: strings mismatch!\n");
527 * verify correct deletion
531 memset(tmp, 0, sizeof(tmp));
533 /* delete less than written (head) */
534 if (cirbuf_del_buf_head(&cb, 1) != 0) {
535 printf("Error: delete from head failed!\n");
539 if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_HEAD) - 1)
540 != sizeof(CIRBUF_STR_HEAD) - 1) {
541 printf("Error: unexpected result when reading from head!\n");
544 /* since we deleted from head, first char should be deleted */
545 if (strncmp(tmp, &tmp2[1], sizeof(CIRBUF_STR_HEAD) - 1) != 0) {
546 printf("Error: strings mismatch!\n");
550 memset(tmp, 0, sizeof(tmp));
552 /* delete less than written (tail) */
553 if (cirbuf_del_buf_tail(&cb, 1) != 0) {
554 printf("Error: delete from tail failed!\n");
558 if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_HEAD) - 2)
559 != sizeof(CIRBUF_STR_HEAD) - 2) {
560 printf("Error: unexpected result when reading from head!\n");
563 /* since we deleted from tail, last char should be deleted */
564 if (strncmp(tmp, &tmp2[1], sizeof(CIRBUF_STR_HEAD) - 2) != 0) {
565 printf("Error: strings mismatch!\n");
572 /* test cmdline_cirbuf char add/del functions */
574 test_cirbuf_char_add_del(void)
577 char buf[CMDLINE_TEST_BUFSIZE];
578 char tmp[CMDLINE_TEST_BUFSIZE];
581 memset(buf, 0, sizeof(buf));
582 memset(tmp, 0, sizeof(tmp));
585 * initialize circular buffer
587 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
588 printf("Error: failed to initialize circular buffer!\n");
593 * try to delete something from cirbuf. since it's empty,
596 if (cirbuf_del_head_safe(&cb) == 0) {
597 printf("Error: deleting from empty cirbuf head succeeded!\n");
600 if (cirbuf_del_tail_safe(&cb) == 0) {
601 printf("Error: deleting from empty cirbuf tail succeeded!\n");
606 * add, verify and delete. these should pass.
608 if (cirbuf_add_head_safe(&cb,'h') < 0) {
609 printf("Error: adding to cirbuf head failed!\n");
612 if (cirbuf_get_head(&cb) != 'h') {
613 printf("Error: wrong head content!\n");
616 if (cirbuf_del_head_safe(&cb) < 0) {
617 printf("Error: deleting from cirbuf head failed!\n");
620 if (cirbuf_add_tail_safe(&cb,'t') < 0) {
621 printf("Error: adding to cirbuf tail failed!\n");
624 if (cirbuf_get_tail(&cb) != 't') {
625 printf("Error: wrong tail content!\n");
628 if (cirbuf_del_tail_safe(&cb) < 0) {
629 printf("Error: deleting from cirbuf tail failed!\n");
632 /* do the same for unsafe versions. those are void. */
633 cirbuf_add_head(&cb,'h');
634 if (cirbuf_get_head(&cb) != 'h') {
635 printf("Error: wrong head content!\n");
638 cirbuf_del_head(&cb);
640 /* test if char has been deleted. we can't call cirbuf_get_head
641 * because it's unsafe, but we can call cirbuf_get_buf_head.
643 if (cirbuf_get_buf_head(&cb, tmp, 1) > 0) {
644 printf("Error: buffer should have been empty!\n");
648 cirbuf_add_tail(&cb,'t');
649 if (cirbuf_get_tail(&cb) != 't') {
650 printf("Error: wrong tail content!\n");
653 cirbuf_del_tail(&cb);
655 /* test if char has been deleted. we can't call cirbuf_get_tail
656 * because it's unsafe, but we can call cirbuf_get_buf_tail.
658 if (cirbuf_get_buf_tail(&cb, tmp, 1) > 0) {
659 printf("Error: buffer should have been empty!\n");
666 /* test filling up buffer with chars */
668 test_cirbuf_char_fill(void)
671 char buf[CMDLINE_TEST_BUFSIZE];
675 memset(buf, 0, sizeof(buf));
678 * initialize circular buffer
680 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
681 printf("Error: failed to initialize circular buffer!\n");
686 * fill the buffer from head or tail, verify contents, test boundaries
687 * and clear the buffer
690 /* fill the buffer from tail */
691 for (i = 0; i < CMDLINE_TEST_BUFSIZE; i++)
692 cirbuf_add_tail_safe(&cb, 't');
693 /* verify that contents of the buffer are what they are supposed to be */
694 for (i = 0; i < sizeof(buf); i++) {
696 printf("Error: wrong content in buffer!\n");
700 /* try to add to a full buffer from tail */
701 if (cirbuf_add_tail_safe(&cb, 't') == 0) {
702 printf("Error: buffer should have been full!\n");
705 /* try to add to a full buffer from head */
706 if (cirbuf_add_head_safe(&cb, 'h') == 0) {
707 printf("Error: buffer should have been full!\n");
710 /* delete buffer from tail */
711 for(i = 0; i < CMDLINE_TEST_BUFSIZE; i++)
712 cirbuf_del_tail_safe(&cb);
713 /* try to delete from an empty buffer */
714 if (cirbuf_del_tail_safe(&cb) >= 0) {
715 printf("Error: buffer should have been empty!\n");
719 /* fill the buffer from head */
720 for (i = 0; i < CMDLINE_TEST_BUFSIZE; i++)
721 cirbuf_add_head_safe(&cb, 'h');
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 head */
730 if (cirbuf_add_head_safe(&cb,'h') >= 0) {
731 printf("Error: buffer should have been full!\n");
734 /* try to add to a full buffer from tail */
735 if (cirbuf_add_tail_safe(&cb, 't') == 0) {
736 printf("Error: buffer should have been full!\n");
739 /* delete buffer from head */
740 for(i = 0; i < CMDLINE_TEST_BUFSIZE; i++)
741 cirbuf_del_head_safe(&cb);
742 /* try to delete from an empty buffer */
743 if (cirbuf_del_head_safe(&cb) >= 0) {
744 printf("Error: buffer should have been empty!\n");
749 * fill the buffer from both head and tail, with alternating characters,
750 * verify contents and clear the buffer
753 /* fill half of buffer from tail */
754 for (i = 0; i < CMDLINE_TEST_BUFSIZE / 2; i++)
755 cirbuf_add_tail_safe(&cb, (char) (i % 2 ? 't' : 'T'));
756 /* fill other half of the buffer from head */
757 for (i = 0; i < CMDLINE_TEST_BUFSIZE / 2; i++)
758 cirbuf_add_head_safe(&cb, (char) (i % 2 ? 'H' : 'h')); /* added in reverse */
760 /* verify that contents of the buffer are what they are supposed to be */
761 for (i = 0; i < sizeof(buf) / 2; i++) {
762 if (buf[i] != (char) (i % 2 ? 't' : 'T')) {
763 printf("Error: wrong content in buffer at %u!\n", i);
767 for (i = sizeof(buf) / 2; i < sizeof(buf); i++) {
768 if (buf[i] != (char) (i % 2 ? 'h' : 'H')) {
769 printf("Error: wrong content in buffer %u!\n", i);
777 /* test left alignment */
779 test_cirbuf_align_left(void)
781 #define HALF_OFFSET CMDLINE_TEST_BUFSIZE / 2
782 #define SMALL_OFFSET HALF_OFFSET / 2
783 /* resulting buffer lengths for each of the test cases */
784 #define LEN1 HALF_OFFSET - SMALL_OFFSET - 1
785 #define LEN2 HALF_OFFSET + SMALL_OFFSET + 2
786 #define LEN3 HALF_OFFSET - SMALL_OFFSET
787 #define LEN4 HALF_OFFSET + SMALL_OFFSET - 1
790 char buf[CMDLINE_TEST_BUFSIZE];
791 char tmp[CMDLINE_TEST_BUFSIZE];
795 * align left when start < end and start in left half
799 * initialize circular buffer
801 memset(buf, 0, sizeof(buf));
802 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
803 printf("Error: failed to initialize circular buffer!\n");
807 /* push end into left half */
808 for (i = 0; i < HALF_OFFSET - 1; i++)
809 cirbuf_add_tail_safe(&cb, 't');
811 /* push start into left half < end */
812 for (i = 0; i < SMALL_OFFSET; i++)
813 cirbuf_del_head_safe(&cb);
816 if (cirbuf_align_left(&cb) < 0) {
817 printf("Error: alignment failed!\n");
822 if (cb.start != 0 || cb.len != LEN1 || cb.end != cb.len - 1) {
823 printf("Error: buffer alignment is wrong!\n");
828 * align left when start > end and start in left half
832 * reinitialize circular buffer
834 memset(buf, 0, sizeof(buf));
835 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
836 printf("Error: failed to reinitialize circular buffer!\n");
840 /* push start into left half */
841 for (i = 0; i < HALF_OFFSET + 2; i++)
842 cirbuf_add_head_safe(&cb, 'h');
844 /* push end into left half > start */
845 for (i = 0; i < SMALL_OFFSET; i++)
846 cirbuf_add_tail_safe(&cb, 't');
849 if (cirbuf_align_left(&cb) < 0) {
850 printf("Error: alignment failed!\n");
855 if (cb.start != 0 || cb.len != LEN2 || cb.end != cb.len - 1) {
856 printf("Error: buffer alignment is wrong!");
861 * align left when start < end and start in right half
865 * reinitialize circular buffer
867 memset(buf, 0, sizeof(buf));
868 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
869 printf("Error: failed to reinitialize circular buffer!\n");
873 /* push start into the right half */
874 for (i = 0; i < HALF_OFFSET; i++)
875 cirbuf_add_head_safe(&cb, 'h');
877 /* push end into left half > start */
878 for (i = 0; i < SMALL_OFFSET; i++)
879 cirbuf_del_tail_safe(&cb);
882 if (cirbuf_align_left(&cb) < 0) {
883 printf("Error: alignment failed!\n");
888 if (cb.start != 0 || cb.len != LEN3 || cb.end != cb.len - 1) {
889 printf("Error: buffer alignment is wrong!");
894 * align left when start > end and start in right half
898 * reinitialize circular buffer
900 memset(buf, 0, sizeof(buf));
901 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
902 printf("Error: failed to reinitialize circular buffer!\n");
906 /* push start into the right half */
907 for (i = 0; i < HALF_OFFSET - 1; i++)
908 cirbuf_add_head_safe(&cb, 'h');
910 /* push end into left half < start */
911 for (i = 0; i < SMALL_OFFSET; i++)
912 cirbuf_add_tail_safe(&cb, 't');
915 if (cirbuf_align_left(&cb) < 0) {
916 printf("Error: alignment failed!\n");
921 if (cb.start != 0 || cb.len != LEN4 ||
922 cb.end != cb.len - 1) {
923 printf("Error: buffer alignment is wrong!");
928 * Verify that alignment doesn't corrupt data
932 * reinitialize circular buffer
934 memset(buf, 0, sizeof(buf));
935 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
936 printf("Error: failed to reinitialize circular buffer!\n");
940 /* add string to tail and head */
941 if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD,
942 sizeof(CIRBUF_STR_HEAD)) < 0 || cirbuf_add_buf_tail(&cb,
943 CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) < 0) {
944 printf("Error: failed to add strings!\n");
949 if (cirbuf_align_left(&cb) < 0) {
950 printf("Error: alignment failed!\n");
954 /* get string from head */
955 if (cirbuf_get_buf_head(&cb, tmp,
956 sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) < 0) {
957 printf("Error: failed to read string from head!\n");
962 if (strncmp(tmp, CIRBUF_STR_HEAD "\0" CIRBUF_STR_TAIL,
963 sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) != 0) {
964 printf("Error: strings mismatch!\n");
968 /* reset tmp buffer */
969 memset(tmp, 0, sizeof(tmp));
971 /* get string from tail */
972 if (cirbuf_get_buf_tail(&cb, tmp,
973 sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) < 0) {
974 printf("Error: failed to read string from head!\n");
979 if (strncmp(tmp, CIRBUF_STR_HEAD "\0" CIRBUF_STR_TAIL,
980 sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) != 0) {
981 printf("Error: strings mismatch!\n");
988 /* test right alignment */
990 test_cirbuf_align_right(void)
992 #define END_OFFSET CMDLINE_TEST_BUFSIZE - 1
994 char buf[CMDLINE_TEST_BUFSIZE];
995 char tmp[CMDLINE_TEST_BUFSIZE];
1000 * align right when start < end and start in left half
1004 * initialize circular buffer
1006 memset(buf, 0, sizeof(buf));
1007 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
1008 printf("Error: failed to initialize circular buffer!\n");
1012 /* push end into left half */
1013 for (i = 0; i < HALF_OFFSET - 1; i++)
1014 cirbuf_add_tail_safe(&cb, 't');
1016 /* push start into left half < end */
1017 for (i = 0; i < SMALL_OFFSET; i++)
1018 cirbuf_del_head_safe(&cb);
1021 cirbuf_align_right(&cb);
1024 if (cb.start != END_OFFSET || cb.len != LEN1 || cb.end != cb.len - 2) {
1025 printf("Error: buffer alignment is wrong!\n");
1030 * align right when start > end and start in left half
1034 * reinitialize circular buffer
1036 memset(buf, 0, sizeof(buf));
1037 if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
1038 printf("Error: failed to reinitialize circular buffer!\n");
1042 /* push start into left half */
1043 for (i = 0; i < HALF_OFFSET + 2; i++)
1044 cirbuf_add_head_safe(&cb, 'h');
1046 /* push end into left half > start */
1047 for (i = 0; i < SMALL_OFFSET; i++)
1048 cirbuf_add_tail_safe(&cb, 't');
1051 cirbuf_align_right(&cb);
1054 if (cb.start != END_OFFSET || cb.len != LEN2 || cb.end != cb.len - 2) {
1055 printf("Error: buffer alignment is wrong!");
1060 * align right when start < end and start in right 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 the right half */
1073 for (i = 0; i < HALF_OFFSET; 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_del_tail_safe(&cb);
1081 cirbuf_align_right(&cb);
1084 if (cb.end != END_OFFSET || cb.len != LEN3 || cb.start != cb.end - cb.len + 1) {
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 - 1; 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_add_tail_safe(&cb, 't');
1111 cirbuf_align_right(&cb);
1114 if (cb.end != END_OFFSET || cb.len != LEN4 || cb.start != cb.end - cb.len + 1) {
1115 printf("Error: buffer alignment is wrong!");
1120 * Verify that alignment doesn't corrupt data
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 /* add string to tail and head */
1133 if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL,
1134 sizeof(CIRBUF_STR_TAIL)) < 0 || cirbuf_add_buf_head(&cb,
1135 CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD)) < 0) {
1136 printf("Error: failed to add strings!\n");
1141 if (cirbuf_align_right(&cb) < 0) {
1142 printf("Error: alignment failed!\n");
1146 /* get string from head */
1147 if (cirbuf_get_buf_head(&cb, tmp,
1148 sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) < 0) {
1149 printf("Error: failed to read string from head!\n");
1154 if (strncmp(tmp, CIRBUF_STR_HEAD "\0" CIRBUF_STR_TAIL,
1155 sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) != 0) {
1156 printf("Error: strings mismatch!\n");
1160 /* reset tmp buffer */
1161 memset(tmp, 0, sizeof(tmp));
1163 /* get string from tail */
1164 if (cirbuf_get_buf_tail(&cb, tmp,
1165 sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) < 0) {
1166 printf("Error: failed to read string from head!\n");
1170 if (strncmp(tmp, CIRBUF_STR_HEAD "\0" CIRBUF_STR_TAIL,
1171 sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) != 0) {
1172 printf("Error: strings mismatch!\n");
1179 /* call functions with invalid parameters */
1181 test_cirbuf_invalid_param(void)
1184 char buf[CMDLINE_TEST_BUFSIZE];
1187 if (cirbuf_init(0, buf, 0, sizeof(buf)) == 0)
1190 if (cirbuf_init(&cb, 0, 0, sizeof(buf)) == 0)
1193 if (cirbuf_add_head_safe(0, 'h') == 0)
1195 if (cirbuf_add_tail_safe(0, 't') == 0)
1197 if (cirbuf_del_head_safe(0) == 0)
1199 if (cirbuf_del_tail_safe(0) == 0)
1202 if (cirbuf_add_buf_head(&cb, 0, 0) == 0)
1204 if (cirbuf_add_buf_tail(&cb, 0, 0) == 0)
1207 if (cirbuf_add_buf_head(0, buf, 0) == 0)
1209 if (cirbuf_add_buf_tail(0, buf, 0) == 0)
1212 if (cirbuf_add_buf_head(&cb, buf, 0) == 0)
1214 if (cirbuf_add_buf_tail(&cb, buf, 0) == 0)
1217 if (cirbuf_del_buf_head(0, 0) == 0)
1219 if (cirbuf_del_buf_tail(0, 0) == 0)
1222 if (cirbuf_del_buf_head(&cb, 0) == 0)
1224 if (cirbuf_del_buf_tail(&cb, 0) == 0)
1227 if (cirbuf_get_buf_head(0, 0, 0) == 0)
1229 if (cirbuf_get_buf_tail(0, 0, 0) == 0)
1232 if (cirbuf_get_buf_head(&cb, 0, 0) == 0)
1234 if (cirbuf_get_buf_tail(&cb, 0, 0) == 0)
1236 /* null size, this is valid but should return 0 */
1237 if (cirbuf_get_buf_head(&cb, buf, 0) != 0)
1239 if (cirbuf_get_buf_tail(&cb, buf, 0) != 0)
1242 if (cirbuf_align_left(0) == 0)
1244 if (cirbuf_align_right(0) == 0)
1250 /* test cmdline_cirbuf char functions */
1252 test_cirbuf_char(void)
1256 ret = test_cirbuf_char_add_del();
1260 ret = test_cirbuf_char_fill();
1267 /* test cmdline_cirbuf string functions */
1269 test_cirbuf_string(void)
1271 if (test_cirbuf_string_add_del() < 0)
1274 if (test_cirbuf_string_add_del_reverse() < 0)
1277 if (test_cirbuf_string_add_boundaries() < 0)
1280 if (test_cirbuf_string_get_del_boundaries() < 0)
1283 if (test_cirbuf_string_get_del_partial() < 0)
1286 if (test_cirbuf_string_misc() < 0)
1292 /* test cmdline_cirbuf align functions */
1294 test_cirbuf_align(void)
1296 if (test_cirbuf_align_left() < 0)
1298 if (test_cirbuf_align_right() < 0)