test left alignment */
| 776 | |
| 777 | /* test left alignment */ |
| 778 | static int |
| 779 | test_cirbuf_align_left(void) |
| 780 | { |
| 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 |
| 788 | |
| 789 | struct cirbuf cb; |
| 790 | char buf[CMDLINE_TEST_BUFSIZE]; |
| 791 | char tmp[CMDLINE_TEST_BUFSIZE]; |
| 792 | unsigned i; |
| 793 | |
| 794 | /* |
| 795 | * align left when start < end and start in left half |
| 796 | */ |
| 797 | |
| 798 | /* |
| 799 | * initialize circular buffer |
| 800 | */ |
| 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"); |
| 804 | return -1; |
| 805 | } |
| 806 | |
| 807 | /* push end into left half */ |
| 808 | for (i = 0; i < HALF_OFFSET - 1; i++) |
| 809 | cirbuf_add_tail_safe(&cb, 't'); |
| 810 | |
| 811 | /* push start into left half < end */ |
| 812 | for (i = 0; i < SMALL_OFFSET; i++) |
| 813 | cirbuf_del_head_safe(&cb); |
| 814 | |
| 815 | /* align */ |
| 816 | if (cirbuf_align_left(&cb) < 0) { |
| 817 | printf("Error: alignment failed!\n"); |
| 818 | return -1; |
| 819 | } |
| 820 | |
| 821 | /* verify result */ |
| 822 | if (cb.start != 0 || cb.len != LEN1 || cb.end != cb.len - 1) { |
| 823 | printf("Error: buffer alignment is wrong!\n"); |
| 824 | return -1; |
| 825 | } |
| 826 | |
| 827 | /* |
| 828 | * align left when start > end and start in left half |
| 829 | */ |
| 830 | |
| 831 | /* |
| 832 | * reinitialize circular buffer |
| 833 | */ |
| 834 | memset(buf, 0, sizeof(buf)); |
| 835 | if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) { |
no test coverage detected