Imported Upstream version 0.10
[platform/upstream/json-c.git] / tests / test_printbuf.c
1 #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
11 static void test_basic_printbuf_memset(void);
12 static void test_printbuf_memset_length(void);
13
14 static void test_basic_printbuf_memset()
15 {
16         struct printbuf *pb;
17         
18         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
27 static 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
64 static void test_printbuf_memappend(int *before_resize);
65 static 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);
107         
108         free(data);
109
110         printbuf_free(pb);
111         printf("%s: end test\n", __func__);
112 }
113
114 static void test_sprintbuf(int before_resize);
115 static 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);
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));
128
129         printbuf_reset(pb);
130         sprintbuf(pb, "plain");
131         printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
132
133         sprintbuf(pb, "%d", 1);
134         printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
135
136         sprintbuf(pb, "%d", INT_MAX);
137         printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
138
139         sprintbuf(pb, "%d", INT_MIN);
140         printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
141
142         sprintbuf(pb, "%s", "%s");
143         printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
144
145         printbuf_free(pb);
146         printf("%s: end test\n", __func__);
147 }
148
149 int main(int argc, char **argv)
150 {
151         int before_resize = 0;
152
153         mc_set_debug(1);
154
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");
163
164         return 0;
165 }