11 static void test_basic_printbuf_memset(void);
12 static void test_printbuf_memset_length(void);
14 static void test_basic_printbuf_memset()
18 printf("%s: starting test\n", __func__);
20 sprintbuf(pb, "blue:%d", 1);
21 printbuf_memset(pb, -1, 'x', 52);
22 printf("Buffer contents:%.*s\n", printbuf_length(pb), pb->buf);
24 printf("%s: end test\n", __func__);
27 static void test_printbuf_memset_length()
31 printf("%s: starting test\n", __func__);
33 printbuf_memset(pb, -1, ' ', 0);
34 printbuf_memset(pb, -1, ' ', 0);
35 printbuf_memset(pb, -1, ' ', 0);
36 printbuf_memset(pb, -1, ' ', 0);
37 printbuf_memset(pb, -1, ' ', 0);
38 printf("Buffer length: %d\n", printbuf_length(pb));
39 printbuf_memset(pb, -1, ' ', 2);
40 printbuf_memset(pb, -1, ' ', 4);
41 printbuf_memset(pb, -1, ' ', 6);
42 printf("Buffer length: %d\n", printbuf_length(pb));
43 printbuf_memset(pb, -1, ' ', 6);
44 printf("Buffer length: %d\n", printbuf_length(pb));
45 printbuf_memset(pb, -1, ' ', 8);
46 printbuf_memset(pb, -1, ' ', 10);
47 printbuf_memset(pb, -1, ' ', 10);
48 printbuf_memset(pb, -1, ' ', 10);
49 printbuf_memset(pb, -1, ' ', 20);
50 printf("Buffer length: %d\n", printbuf_length(pb));
52 // No length change should occur
53 printbuf_memset(pb, 0, 'x', 30);
54 printf("Buffer length: %d\n", printbuf_length(pb));
56 // This should extend it by one.
57 printbuf_memset(pb, 0, 'x', printbuf_length(pb) + 1);
58 printf("Buffer length: %d\n", printbuf_length(pb));
61 printf("%s: end test\n", __func__);
64 static void test_printbuf_memappend(int *before_resize);
65 static void test_printbuf_memappend(int *before_resize)
70 printf("%s: starting test\n", __func__);
72 printf("Buffer length: %d\n", printbuf_length(pb));
74 initial_size = pb->size;
76 while(pb->size == initial_size)
78 printbuf_memappend_fast(pb, "x", 1);
80 *before_resize = printbuf_length(pb) - 1;
81 printf("Appended %d bytes for resize: [%s]\n", *before_resize + 1, pb->buf);
84 printbuf_memappend_fast(pb, "bluexyz123", 3);
85 printf("Partial append: %d, [%s]\n", printbuf_length(pb), pb->buf);
87 char with_nulls[] = { 'a', 'b', '\0', 'c' };
89 printbuf_memappend_fast(pb, with_nulls, sizeof(with_nulls));
90 printf("With embedded \\0 character: %d, [%s]\n", printbuf_length(pb), pb->buf);
94 char *data = malloc(*before_resize);
95 memset(data, 'X', *before_resize);
96 printbuf_memappend_fast(pb, data, *before_resize);
97 printf("Append to just before resize: %d, [%s]\n", printbuf_length(pb), pb->buf);
103 data = malloc(*before_resize + 1);
104 memset(data, 'X', *before_resize + 1);
105 printbuf_memappend_fast(pb, data, *before_resize + 1);
106 printf("Append to just after resize: %d, [%s]\n", printbuf_length(pb), pb->buf);
111 printf("%s: end test\n", __func__);
114 static void test_sprintbuf(int before_resize);
115 static void test_sprintbuf(int before_resize)
119 printf("%s: starting test\n", __func__);
121 printf("Buffer length: %d\n", printbuf_length(pb));
123 char *data = malloc(before_resize + 1 + 1);
124 memset(data, 'X', before_resize + 1 + 1);
125 data[before_resize + 1] = '\0';
126 sprintbuf(pb, "%s", data);
127 printf("sprintbuf to just after resize(%d+1): %d, [%s], strlen(buf)=%d\n", before_resize, printbuf_length(pb), pb->buf, strlen(pb->buf));
130 sprintbuf(pb, "plain");
131 printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
133 sprintbuf(pb, "%d", 1);
134 printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
136 sprintbuf(pb, "%d", INT_MAX);
137 printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
139 sprintbuf(pb, "%d", INT_MIN);
140 printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
142 sprintbuf(pb, "%s", "%s");
143 printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
146 printf("%s: end test\n", __func__);
149 int main(int argc, char **argv)
151 int before_resize = 0;
155 test_basic_printbuf_memset();
156 printf("========================================\n");
157 test_printbuf_memset_length();
158 printf("========================================\n");
159 test_printbuf_memappend(&before_resize);
160 printf("========================================\n");
161 test_sprintbuf(before_resize);
162 printf("========================================\n");