Debian Codegraph @

external/bdwgc/cord/tests/cordtest.c

raw
1 /*
2 * Copyright (c) 1993-1994 by Xerox Corporation. All rights reserved.
3 *
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
6 *
7 * Permission is hereby granted to use or copy this program
8 * for any purpose, provided the above notices are retained on all copies.
9 * Permission to modify the code and to distribute modified code is granted,
10 * provided the above notices are retained, and a notice that the code was
11 * modified is included with the above copyright notice.
12 */
13
14 # include "gc.h" /* For GC_INIT() only */
15 # include "cord.h"
16
17 # include <stdarg.h>
18 # include <string.h>
19 # include <stdio.h>
20 # include <stdlib.h>
21
22 /* This is a very incomplete test of the cord package. It knows about */
23 /* a few internals of the package (e.g. when C strings are returned) */
24 /* that real clients shouldn't rely on. */
25
26 # define ABORT(string) \
27 { fprintf(stderr, "FAILED: %s\n", string); abort(); }
28
29 #if defined(CPPCHECK)
30 # undef CORD_iter
31 # undef CORD_next
32 # undef CORD_pos_fetch
33 # undef CORD_pos_to_cord
34 # undef CORD_pos_to_index
35 # undef CORD_pos_valid
36 # undef CORD_prev
37 #endif
38
39 int count;
40
41 int test_fn(char c, void * client_data)
42 {
43 if (client_data != (void *)13) ABORT("bad client data");
44 if (count < 64*1024+1) {
45 if ((count & 1) == 0) {
46 if (c != 'b') ABORT("bad char");
47 } else {
48 if (c != 'a') ABORT("bad char");
49 }
50 count++;
51 return(0);
52 } else {
53 if (c != 'c') ABORT("bad char");
54 count++;
55 return(1);
56 }
57 }
58
59 char id_cord_fn(size_t i, void * client_data)
60 {
61 if (client_data != 0) ABORT("id_cord_fn: bad client data");
62 return((char)i);
63 }
64
65 void test_basics(void)
66 {
67 CORD x = CORD_from_char_star("ab");
68 int i;
69 CORD y;
70 CORD_pos p;
71
72 x = CORD_cat(x,x);
73 if (x == CORD_EMPTY) ABORT("CORD_cat(x,x) returned empty cord");
74 if (!CORD_IS_STRING(x)) ABORT("short cord should usually be a string");
75 if (strcmp(x, "abab") != 0) ABORT("bad CORD_cat result");
76
77 for (i = 1; i < 16; i++) {
78 x = CORD_cat(x,x);
79 }
80 x = CORD_cat(x,"c");
81 if (CORD_len(x) != 128*1024+1) ABORT("bad length");
82
83 count = 0;
84 if (CORD_iter5(x, 64*1024-1, test_fn, CORD_NO_FN, (void *)13) == 0) {
85 ABORT("CORD_iter5 failed");
86 }
87 if (count != 64*1024 + 2) ABORT("CORD_iter5 failed");
88
89 count = 0;
90 CORD_set_pos(p, x, 64*1024-1);
91 while(CORD_pos_valid(p)) {
92 (void) test_fn(CORD_pos_fetch(p), (void *)13);
93 CORD_next(p);
94 }
95 if (count != 64*1024 + 2) ABORT("Position based iteration failed");
96
97 y = CORD_substr(x, 1023, 5);
98 if (!y) ABORT("CORD_substr returned NULL");
99 if (!CORD_IS_STRING(y)) ABORT("short cord should usually be a string");
100 if (strcmp(y, "babab") != 0) ABORT("bad CORD_substr result");
101
102 y = CORD_substr(x, 1024, 8);
103 if (!y) ABORT("CORD_substr returned NULL");
104 if (!CORD_IS_STRING(y)) ABORT("short cord should usually be a string");
105 if (strcmp(y, "abababab") != 0) ABORT("bad CORD_substr result");
106
107 y = CORD_substr(x, 128*1024-1, 8);
108 if (!y) ABORT("CORD_substr returned NULL");
109 if (!CORD_IS_STRING(y)) ABORT("short cord should usually be a string");
110 if (strcmp(y, "bc") != 0) ABORT("bad CORD_substr result");
111
112 x = CORD_balance(x);
113 if (CORD_len(x) != 128*1024+1) ABORT("bad length");
114
115 count = 0;
116 if (CORD_iter5(x, 64*1024-1, test_fn, CORD_NO_FN, (void *)13) == 0) {
117 ABORT("CORD_iter5 failed");
118 }
119 if (count != 64*1024 + 2) ABORT("CORD_iter5 failed");
120
121 y = CORD_substr(x, 1023, 5);
122 if (!y) ABORT("CORD_substr returned NULL");
123 if (!CORD_IS_STRING(y)) ABORT("short cord should usually be a string");
124 if (strcmp(y, "babab") != 0) ABORT("bad CORD_substr result");
125 y = CORD_from_fn(id_cord_fn, 0, 13);
126 i = 0;
127 CORD_set_pos(p, y, i);
128 while(CORD_pos_valid(p)) {
129 char c = CORD_pos_fetch(p);
130
131 if(c != i) ABORT("Traversal of function node failed");
132 CORD_next(p);
133 i++;
134 }
135 if (i != 13) ABORT("Bad apparent length for function node");
136 # if defined(CPPCHECK)
137 /* TODO: Actually test these functions. */
138 CORD_prev(p);
139 (void)CORD_pos_to_cord(p);
140 (void)CORD_pos_to_index(p);
141 (void)CORD_iter(CORD_EMPTY, test_fn, NULL);
142 (void)CORD_riter(CORD_EMPTY, test_fn, NULL);
143 CORD_dump(y);
144 # endif
145 }
146
147 void test_extras(void)
148 {
149 # define FNAME1 "cordtst1.tmp" /* short name (8+3) for portability */
150 # define FNAME2 "cordtst2.tmp"
151 int i;
152 CORD y = "abcdefghijklmnopqrstuvwxyz0123456789";
153 CORD x = "{}";
154 CORD u, w, z;
155 FILE *f;
156 FILE *f1a, *f1b, *f2;
157
158 w = CORD_cat(CORD_cat(y,y),y);
159 z = CORD_catn(3,y,y,y);
160 if (CORD_cmp(w,z) != 0) ABORT("CORD_catn comparison wrong");
161 for (i = 1; i < 100; i++) {
162 x = CORD_cat(x, y);
163 }
164 z = CORD_balance(x);
165 if (CORD_cmp(x,z) != 0) ABORT("balanced string comparison wrong");
166 if (CORD_cmp(x,CORD_cat(z, CORD_nul(13))) >= 0) ABORT("comparison 2");
167 if (CORD_cmp(CORD_cat(x, CORD_nul(13)), z) <= 0) ABORT("comparison 3");
168 if (CORD_cmp(x,CORD_cat(z, "13")) >= 0) ABORT("comparison 4");
169 if ((f = fopen(FNAME1, "w")) == 0) ABORT("open failed");
170 if (CORD_put(z,f) == EOF) ABORT("CORD_put failed");
171 if (fclose(f) == EOF) ABORT("fclose failed");
172 f1a = fopen(FNAME1, "rb");
173 if (!f1a) ABORT("Unable to open " FNAME1);
174 w = CORD_from_file(f1a);
175 if (CORD_len(w) != CORD_len(z)) ABORT("file length wrong");
176 if (CORD_cmp(w,z) != 0) ABORT("file comparison wrong");
177 if (CORD_cmp(CORD_substr(w, 50*36+2, 36), y) != 0)
178 ABORT("file substr wrong");
179 f1b = fopen(FNAME1, "rb");
180 if (!f1b) ABORT("2nd open failed: " FNAME1);
181 z = CORD_from_file_lazy(f1b);
182 if (CORD_cmp(w,z) != 0) ABORT("File conversions differ");
183 if (CORD_chr(w, 0, '9') != 37) ABORT("CORD_chr failed 1");
184 if (CORD_chr(w, 3, 'a') != 38) ABORT("CORD_chr failed 2");
185 if (CORD_rchr(w, CORD_len(w) - 1, '}') != 1) ABORT("CORD_rchr failed");
186 x = y;
187 for (i = 1; i < 14; i++) {
188 x = CORD_cat(x,x);
189 }
190 if ((f = fopen(FNAME2, "w")) == 0) ABORT("2nd open failed");
191 # ifdef __DJGPP__
192 /* FIXME: DJGPP workaround. Why does this help? */
193 if (fflush(f) != 0) ABORT("fflush failed");
194 # endif
195 if (CORD_put(x,f) == EOF) ABORT("CORD_put failed");
196 if (fclose(f) == EOF) ABORT("fclose failed");
197 f2 = fopen(FNAME2, "rb");
198 if (!f2) ABORT("Unable to open " FNAME2);
199 w = CORD_from_file(f2);
200 if (CORD_len(w) != CORD_len(x)) ABORT("file length wrong");
201 if (CORD_cmp(w,x) != 0) ABORT("file comparison wrong");
202 if (CORD_cmp(CORD_substr(w, 1000*36, 36), y) != 0)
203 ABORT("file substr wrong");
204 if (strcmp(CORD_to_char_star(CORD_substr(w, 1000*36, 36)), y) != 0)
205 ABORT("char * file substr wrong");
206 u = CORD_substr(w, 1000*36, 2);
207 if (!u) ABORT("CORD_substr returned NULL");
208 if (strcmp(u, "ab") != 0)
209 ABORT("short file substr wrong");
210 if (CORD_str(x,1,"9a") != 35) ABORT("CORD_str failed 1");
211 if (CORD_str(x,0,"9abcdefghijk") != 35) ABORT("CORD_str failed 2");
212 if (CORD_str(x,0,"9abcdefghijx") != CORD_NOT_FOUND)
213 ABORT("CORD_str failed 3");
214 if (CORD_str(x,0,"9>") != CORD_NOT_FOUND) ABORT("CORD_str failed 4");
215 /* Note: f1a, f1b, f2 handles are closed lazily by CORD library. */
216 /* TODO: Propose and use CORD_fclose. */
217 *(CORD volatile *)&w = CORD_EMPTY;
218 *(CORD volatile *)&z = CORD_EMPTY;
219 GC_gcollect();
220 GC_invoke_finalizers();
221 /* Of course, this does not guarantee the files are closed. */
222 if (remove(FNAME1) != 0) {
223 /* On some systems, e.g. OS2, this may fail if f1 is still open. */
224 /* But we cannot call fclose as it might lead to double close. */
225 fprintf(stderr, "WARNING: remove failed: " FNAME1 "\n");
226 }
227 if (remove(FNAME2) != 0) {
228 fprintf(stderr, "WARNING: remove failed: " FNAME2 "\n");
229 }
230 }
231
232 int wrap_vprintf(CORD format, ...)
233 {
234 va_list args;
235 int result;
236
237 va_start(args, format);
238 result = CORD_vprintf(format, args);
239 va_end(args);
240 return result;
241 }
242
243 int wrap_vfprintf(FILE * f, CORD format, ...)
244 {
245 va_list args;
246 int result;
247
248 va_start(args, format);
249 result = CORD_vfprintf(f, format, args);
250 va_end(args);
251 return result;
252 }
253
254 #if defined(__DJGPP__) || defined(__STRICT_ANSI__)
255 /* snprintf is missing in DJGPP (v2.0.3) */
256 #else
257 # if defined(_MSC_VER)
258 # if defined(_WIN32_WCE)
259 /* _snprintf is deprecated in WinCE */
260 # define GC_SNPRINTF StringCchPrintfA
261 # else
262 # define GC_SNPRINTF _snprintf
263 # endif
264 # else
265 # define GC_SNPRINTF snprintf
266 # endif
267 #endif
268
269 void test_printf(void)
270 {
271 CORD result;
272 char result2[200];
273 long l = -1;
274 short s = (short)-1;
275 CORD x;
276
277 if (CORD_sprintf(&result, "%7.2f%ln", 3.14159F, &l) != 7)
278 ABORT("CORD_sprintf failed 1");
279 if (CORD_cmp(result, " 3.14") != 0)ABORT("CORD_sprintf goofed 1");
280 if (l != 7) ABORT("CORD_sprintf goofed 2");
281 if (CORD_sprintf(&result, "%-7.2s%hn%c%s", "abcd", &s, 'x', "yz") != 10)
282 ABORT("CORD_sprintf failed 2");
283 if (CORD_cmp(result, "ab xyz") != 0)ABORT("CORD_sprintf goofed 3");
284 if (s != 7) ABORT("CORD_sprintf goofed 4");
285 x = "abcdefghij";
286 x = CORD_cat(x,x);
287 x = CORD_cat(x,x);
288 x = CORD_cat(x,x);
289 if (CORD_sprintf(&result, "->%-120.78r!\n", x) != 124)
290 ABORT("CORD_sprintf failed 3");
291 # ifdef GC_SNPRINTF
292 (void)GC_SNPRINTF(result2, sizeof(result2), "->%-120.78s!\n",
293 CORD_to_char_star(x));
294 # else
295 (void)sprintf(result2, "->%-120.78s!\n", CORD_to_char_star(x));
296 # endif
297 result2[sizeof(result2) - 1] = '\0';
298 if (CORD_cmp(result, result2) != 0)ABORT("CORD_sprintf goofed 5");
299 /* TODO: Better test CORD_[v][f]printf. */
300 (void)CORD_printf(CORD_EMPTY);
301 (void)wrap_vfprintf(stdout, CORD_EMPTY);
302 (void)wrap_vprintf(CORD_EMPTY);
303 }
304
305 int main(void)
306 {
307 # ifdef THINK_C
308 printf("cordtest:\n");
309 # endif
310 GC_INIT();
311 test_basics();
312 test_extras();
313 test_printf();
314 CORD_fprintf(stdout, "SUCCEEDED\n");
315 return(0);
316 }