blob: fed185e1e779ba01d5575e71c78c209a848619a5 [file] [log] [blame]
Eric Haszlakiewicze5c1e872012-04-22 14:13:01 -05001#include <assert.h>
2#include <stddef.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6#include <limits.h>
7
8#include "debug.h"
9#include "printbuf.h"
10
11static void test_basic_printbuf_memset(void);
12static void test_printbuf_memset_length(void);
13
14static void test_basic_printbuf_memset()
15{
16 struct printbuf *pb;
Eric Haszlakiewiczc3068bf2012-09-16 20:43:29 -050017
Eric Haszlakiewicze5c1e872012-04-22 14:13:01 -050018 printf("%s: starting test\n", __func__);
19 pb = printbuf_new();
20 sprintbuf(pb, "blue:%d", 1);
21 printbuf_memset(pb, -1, 'x', 52);
22 printf("Buffer contents:%.*s\n", printbuf_length(pb), pb->buf);
23 printbuf_free(pb);
24 printf("%s: end test\n", __func__);
25}
26
27static void test_printbuf_memset_length()
28{
29 struct printbuf *pb;
30
31 printf("%s: starting test\n", __func__);
32 pb = printbuf_new();
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));
51
52 // No length change should occur
53 printbuf_memset(pb, 0, 'x', 30);
54 printf("Buffer length: %d\n", printbuf_length(pb));
55
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));
59
60 printbuf_free(pb);
61 printf("%s: end test\n", __func__);
62}
63
64static void test_printbuf_memappend(int *before_resize);
65static void test_printbuf_memappend(int *before_resize)
66{
67 struct printbuf *pb;
68 int initial_size;
69
70 printf("%s: starting test\n", __func__);
71 pb = printbuf_new();
72 printf("Buffer length: %d\n", printbuf_length(pb));
73
74 initial_size = pb->size;
75
76 while(pb->size == initial_size)
77 {
78 printbuf_memappend_fast(pb, "x", 1);
79 }
80 *before_resize = printbuf_length(pb) - 1;
81 printf("Appended %d bytes for resize: [%s]\n", *before_resize + 1, pb->buf);
82
83 printbuf_reset(pb);
84 printbuf_memappend_fast(pb, "bluexyz123", 3);
85 printf("Partial append: %d, [%s]\n", printbuf_length(pb), pb->buf);
86
87 char with_nulls[] = { 'a', 'b', '\0', 'c' };
88 printbuf_reset(pb);
89 printbuf_memappend_fast(pb, with_nulls, sizeof(with_nulls));
90 printf("With embedded \\0 character: %d, [%s]\n", printbuf_length(pb), pb->buf);
91
92 printbuf_free(pb);
93 pb = printbuf_new();
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);
98
99 free(data);
100 printbuf_free(pb);
101
102 pb = printbuf_new();
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);
Eric Haszlakiewiczc3068bf2012-09-16 20:43:29 -0500107
Eric Haszlakiewicze5c1e872012-04-22 14:13:01 -0500108 free(data);
109
110 printbuf_free(pb);
111 printf("%s: end test\n", __func__);
112}
113
114static void test_sprintbuf(int before_resize);
115static void test_sprintbuf(int before_resize)
116{
117 struct printbuf *pb;
118
119 printf("%s: starting test\n", __func__);
120 pb = printbuf_new();
121 printf("Buffer length: %d\n", printbuf_length(pb));
122
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);
Eric Haszlakiewicz447b88a2012-11-29 13:29:55 -0600127 free(data);
Mateusz Loskot984303d2012-06-19 20:15:44 +0100128 printf("sprintbuf to just after resize(%d+1): %d, [%s], strlen(buf)=%d\n", before_resize, printbuf_length(pb), pb->buf, (int)strlen(pb->buf));
Eric Haszlakiewicze5c1e872012-04-22 14:13:01 -0500129
130 printbuf_reset(pb);
131 sprintbuf(pb, "plain");
132 printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
133
134 sprintbuf(pb, "%d", 1);
135 printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
136
137 sprintbuf(pb, "%d", INT_MAX);
138 printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
139
140 sprintbuf(pb, "%d", INT_MIN);
141 printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
142
143 sprintbuf(pb, "%s", "%s");
144 printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
145
146 printbuf_free(pb);
147 printf("%s: end test\n", __func__);
148}
149
150int main(int argc, char **argv)
151{
152 int before_resize = 0;
153
154 mc_set_debug(1);
155
156 test_basic_printbuf_memset();
157 printf("========================================\n");
158 test_printbuf_memset_length();
159 printf("========================================\n");
160 test_printbuf_memappend(&before_resize);
161 printf("========================================\n");
162 test_sprintbuf(before_resize);
163 printf("========================================\n");
164
165 return 0;
166}