| 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" |
| 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 *)(GC_word)13) |
| 44 |
ABORT("bad client data"); |
| 45 |
if (count < 64*1024+1) { |
| 46 |
if ((count & 1) == 0) { |
| 47 |
if (c != 'b') ABORT("bad char"); |
| 48 |
} else { |
| 49 |
if (c != 'a') ABORT("bad char"); |
| 50 |
} |
| 51 |
count++; |
| 52 |
return(0); |
| 53 |
} else { |
| 54 |
if (c != 'c') ABORT("bad char"); |
| 55 |
count++; |
| 56 |
return(1); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
char id_cord_fn(size_t i, void * client_data) |
| 61 |
{ |
| 62 |
if (client_data != 0) ABORT("id_cord_fn: bad client data"); |
| 63 |
return((char)i); |
| 64 |
} |
| 65 |
|
| 66 |
void test_basics(void) |
| 67 |
{ |
| 68 |
CORD x = CORD_from_char_star("ab"); |
| 69 |
size_t i; |
| 70 |
CORD y; |
| 71 |
CORD_pos p; |
| 72 |
|
| 73 |
x = CORD_cat(x,x); |
| 74 |
if (x == CORD_EMPTY) ABORT("CORD_cat(x,x) returned empty cord"); |
| 75 |
if (!CORD_IS_STRING(x)) ABORT("short cord should usually be a string"); |
| 76 |
if (strcmp(x, "abab") != 0) ABORT("bad CORD_cat result"); |
| 77 |
|
| 78 |
for (i = 1; i < 16; i++) { |
| 79 |
x = CORD_cat(x,x); |
| 80 |
} |
| 81 |
x = CORD_cat(x,"c"); |
| 82 |
if (CORD_len(x) != 128*1024+1) ABORT("bad length"); |
| 83 |
|
| 84 |
count = 0; |
| 85 |
if (CORD_iter5(x, 64*1024-1, test_fn, CORD_NO_FN, |
| 86 |
(void *)(GC_word)13) == 0) { |
| 87 |
ABORT("CORD_iter5 failed"); |
| 88 |
} |
| 89 |
if (count != 64*1024 + 2) ABORT("CORD_iter5 failed"); |
| 90 |
|
| 91 |
count = 0; |
| 92 |
CORD_set_pos(p, x, 64*1024-1); |
| 93 |
while(CORD_pos_valid(p)) { |
| 94 |
(void)test_fn(CORD_pos_fetch(p), (void *)(GC_word)13); |
| 95 |
CORD_next(p); |
| 96 |
} |
| 97 |
if (count != 64*1024 + 2) ABORT("Position based iteration failed"); |
| 98 |
|
| 99 |
y = CORD_substr(x, 1023, 5); |
| 100 |
if (!y) ABORT("CORD_substr returned NULL"); |
| 101 |
if (!CORD_IS_STRING(y)) ABORT("short cord should usually be a string"); |
| 102 |
if (strcmp(y, "babab") != 0) ABORT("bad CORD_substr result"); |
| 103 |
|
| 104 |
y = CORD_substr(x, 1024, 8); |
| 105 |
if (!y) ABORT("CORD_substr returned NULL"); |
| 106 |
if (!CORD_IS_STRING(y)) ABORT("short cord should usually be a string"); |
| 107 |
if (strcmp(y, "abababab") != 0) ABORT("bad CORD_substr result"); |
| 108 |
|
| 109 |
y = CORD_substr(x, 128*1024-1, 8); |
| 110 |
if (!y) ABORT("CORD_substr returned NULL"); |
| 111 |
if (!CORD_IS_STRING(y)) ABORT("short cord should usually be a string"); |
| 112 |
if (strcmp(y, "bc") != 0) ABORT("bad CORD_substr result"); |
| 113 |
|
| 114 |
x = CORD_balance(x); |
| 115 |
if (CORD_len(x) != 128*1024+1) ABORT("bad length"); |
| 116 |
|
| 117 |
count = 0; |
| 118 |
if (CORD_iter5(x, 64*1024-1, test_fn, CORD_NO_FN, |
| 119 |
(void *)(GC_word)13) == 0) { |
| 120 |
ABORT("CORD_iter5 failed"); |
| 121 |
} |
| 122 |
if (count != 64*1024 + 2) ABORT("CORD_iter5 failed"); |
| 123 |
|
| 124 |
y = CORD_substr(x, 1023, 5); |
| 125 |
if (!y) ABORT("CORD_substr returned NULL"); |
| 126 |
if (!CORD_IS_STRING(y)) ABORT("short cord should usually be a string"); |
| 127 |
if (strcmp(y, "babab") != 0) ABORT("bad CORD_substr result"); |
| 128 |
y = CORD_from_fn(id_cord_fn, 0, 13); |
| 129 |
i = 0; |
| 130 |
CORD_set_pos(p, y, i); |
| 131 |
while(CORD_pos_valid(p)) { |
| 132 |
char c = CORD_pos_fetch(p); |
| 133 |
|
| 134 |
if ((size_t)(unsigned char)c != i) |
| 135 |
ABORT("Traversal of function node failed"); |
| 136 |
CORD_next(p); |
| 137 |
i++; |
| 138 |
} |
| 139 |
if (i != 13) ABORT("Bad apparent length for function node"); |
| 140 |
# if defined(CPPCHECK) |
| 141 |
/* TODO: Actually test these functions. */ |
| 142 |
CORD_prev(p); |
| 143 |
(void)CORD_pos_to_cord(p); |
| 144 |
(void)CORD_pos_to_index(p); |
| 145 |
(void)CORD_iter(CORD_EMPTY, test_fn, NULL); |
| 146 |
(void)CORD_riter(CORD_EMPTY, test_fn, NULL); |
| 147 |
CORD_dump(y); |
| 148 |
# endif |
| 149 |
} |
| 150 |
|
| 151 |
void test_extras(void) |
| 152 |
{ |
| 153 |
# define FNAME1 "cordtst1.tmp" /* short name (8+3) for portability */ |
| 154 |
# define FNAME2 "cordtst2.tmp" |
| 155 |
int i; |
| 156 |
CORD y = "abcdefghijklmnopqrstuvwxyz0123456789"; |
| 157 |
CORD x = "{}"; |
| 158 |
CORD u, w, z; |
| 159 |
FILE *f; |
| 160 |
FILE *f1a, *f1b, *f2; |
| 161 |
|
| 162 |
w = CORD_cat(CORD_cat(y,y),y); |
| 163 |
z = CORD_catn(3,y,y,y); |
| 164 |
if (CORD_cmp(w,z) != 0) ABORT("CORD_catn comparison wrong"); |
| 165 |
for (i = 1; i < 100; i++) { |
| 166 |
x = CORD_cat(x, y); |
| 167 |
} |
| 168 |
z = CORD_balance(x); |
| 169 |
if (CORD_cmp(x,z) != 0) ABORT("balanced string comparison wrong"); |
| 170 |
if (CORD_cmp(x,CORD_cat(z, CORD_nul(13))) >= 0) ABORT("comparison 2"); |
| 171 |
if (CORD_cmp(CORD_cat(x, CORD_nul(13)), z) <= 0) ABORT("comparison 3"); |
| 172 |
if (CORD_cmp(x,CORD_cat(z, "13")) >= 0) ABORT("comparison 4"); |
| 173 |
if ((f = fopen(FNAME1, "w")) == 0) ABORT("open failed"); |
| 174 |
if (CORD_put(z,f) == EOF) ABORT("CORD_put failed"); |
| 175 |
if (fclose(f) == EOF) ABORT("fclose failed"); |
| 176 |
f1a = fopen(FNAME1, "rb"); |
| 177 |
if (!f1a) ABORT("Unable to open " FNAME1); |
| 178 |
w = CORD_from_file(f1a); |
| 179 |
if (CORD_len(w) != CORD_len(z)) ABORT("file length wrong"); |
| 180 |
if (CORD_cmp(w,z) != 0) ABORT("file comparison wrong"); |
| 181 |
if (CORD_cmp(CORD_substr(w, 50*36+2, 36), y) != 0) |
| 182 |
ABORT("file substr wrong"); |
| 183 |
f1b = fopen(FNAME1, "rb"); |
| 184 |
if (!f1b) ABORT("2nd open failed: " FNAME1); |
| 185 |
z = CORD_from_file_lazy(f1b); |
| 186 |
if (CORD_cmp(w,z) != 0) ABORT("File conversions differ"); |
| 187 |
if (CORD_chr(w, 0, '9') != 37) ABORT("CORD_chr failed 1"); |
| 188 |
if (CORD_chr(w, 3, 'a') != 38) ABORT("CORD_chr failed 2"); |
| 189 |
if (CORD_rchr(w, CORD_len(w) - 1, '}') != 1) ABORT("CORD_rchr failed"); |
| 190 |
x = y; |
| 191 |
for (i = 1; i < 14; i++) { |
| 192 |
x = CORD_cat(x,x); |
| 193 |
} |
| 194 |
if ((f = fopen(FNAME2, "w")) == 0) ABORT("2nd open failed"); |
| 195 |
# ifdef __DJGPP__ |
| 196 |
/* FIXME: DJGPP workaround. Why does this help? */ |
| 197 |
if (fflush(f) != 0) ABORT("fflush failed"); |
| 198 |
# endif |
| 199 |
if (CORD_put(x,f) == EOF) ABORT("CORD_put failed"); |
| 200 |
if (fclose(f) == EOF) ABORT("fclose failed"); |
| 201 |
f2 = fopen(FNAME2, "rb"); |
| 202 |
if (!f2) ABORT("Unable to open " FNAME2); |
| 203 |
w = CORD_from_file(f2); |
| 204 |
if (CORD_len(w) != CORD_len(x)) ABORT("file length wrong"); |
| 205 |
if (CORD_cmp(w,x) != 0) ABORT("file comparison wrong"); |
| 206 |
if (CORD_cmp(CORD_substr(w, 1000*36, 36), y) != 0) |
| 207 |
ABORT("file substr wrong"); |
| 208 |
if (strcmp(CORD_to_char_star(CORD_substr(w, 1000*36, 36)), y) != 0) |
| 209 |
ABORT("char * file substr wrong"); |
| 210 |
u = CORD_substr(w, 1000*36, 2); |
| 211 |
if (!u) ABORT("CORD_substr returned NULL"); |
| 212 |
if (strcmp(u, "ab") != 0) |
| 213 |
ABORT("short file substr wrong"); |
| 214 |
if (CORD_str(x,1,"9a") != 35) ABORT("CORD_str failed 1"); |
| 215 |
if (CORD_str(x,0,"9abcdefghijk") != 35) ABORT("CORD_str failed 2"); |
| 216 |
if (CORD_str(x,0,"9abcdefghijx") != CORD_NOT_FOUND) |
| 217 |
ABORT("CORD_str failed 3"); |
| 218 |
if (CORD_str(x,0,"9>") != CORD_NOT_FOUND) ABORT("CORD_str failed 4"); |
| 219 |
/* Note: f1a, f1b, f2 handles are closed lazily by CORD library. */ |
| 220 |
/* TODO: Propose and use CORD_fclose. */ |
| 221 |
*(CORD volatile *)&w = CORD_EMPTY; |
| 222 |
*(CORD volatile *)&z = CORD_EMPTY; |
| 223 |
GC_gcollect(); |
| 224 |
# ifndef GC_NO_FINALIZATION |
| 225 |
GC_invoke_finalizers(); |
| 226 |
/* Of course, this does not guarantee the files are closed. */ |
| 227 |
# endif |
| 228 |
if (remove(FNAME1) != 0) { |
| 229 |
/* On some systems, e.g. OS2, this may fail if f1 is still open. */ |
| 230 |
/* But we cannot call fclose as it might lead to double close. */ |
| 231 |
fprintf(stderr, "WARNING: remove failed: " FNAME1 "\n"); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
int wrap_vprintf(CORD format, ...) |
| 236 |
{ |
| 237 |
va_list args; |
| 238 |
int result; |
| 239 |
|
| 240 |
va_start(args, format); |
| 241 |
result = CORD_vprintf(format, args); |
| 242 |
va_end(args); |
| 243 |
return result; |
| 244 |
} |
| 245 |
|
| 246 |
int wrap_vfprintf(FILE * f, CORD format, ...) |
| 247 |
{ |
| 248 |
va_list args; |
| 249 |
int result; |
| 250 |
|
| 251 |
va_start(args, format); |
| 252 |
result = CORD_vfprintf(f, format, args); |
| 253 |
va_end(args); |
| 254 |
return result; |
| 255 |
} |
| 256 |
|
| 257 |
#if defined(__DJGPP__) || defined(__STRICT_ANSI__) |
| 258 |
/* snprintf is missing in DJGPP (v2.0.3) */ |
| 259 |
#else |
| 260 |
# if defined(_MSC_VER) |
| 261 |
# if defined(_WIN32_WCE) |
| 262 |
/* _snprintf is deprecated in WinCE */ |
| 263 |
# define GC_SNPRINTF StringCchPrintfA |
| 264 |
# else |
| 265 |
# define GC_SNPRINTF _snprintf |
| 266 |
# endif |
| 267 |
# else |
| 268 |
# define GC_SNPRINTF snprintf |
| 269 |
# endif |
| 270 |
#endif |
| 271 |
|
| 272 |
/* no static */ /* no const */ char *zu_format = (char*)"%zu"; |
| 273 |
|
| 274 |
void test_printf(void) |
| 275 |
{ |
| 276 |
CORD result; |
| 277 |
char result2[200]; |
| 278 |
long l = -1; |
| 279 |
short s = (short)-1; |
| 280 |
CORD x; |
| 281 |
int res; |
| 282 |
|
| 283 |
if (CORD_sprintf(&result, "%7.2f%ln", 3.14159F, &l) != 7) |
| 284 |
ABORT("CORD_sprintf failed 1"); |
| 285 |
if (CORD_cmp(result, " 3.14") != 0) ABORT("CORD_sprintf goofed 1"); |
| 286 |
if (l != 7) ABORT("CORD_sprintf goofed 2"); |
| 287 |
if (CORD_sprintf(&result, "%-7.2s%hn%c%s", "abcd", &s, 'x', "yz") != 10) |
| 288 |
ABORT("CORD_sprintf failed 2"); |
| 289 |
if (CORD_cmp(result, "ab xyz") != 0) ABORT("CORD_sprintf goofed 3"); |
| 290 |
if (s != 7) ABORT("CORD_sprintf goofed 4"); |
| 291 |
x = "abcdefghij"; |
| 292 |
x = CORD_cat(x,x); |
| 293 |
x = CORD_cat(x,x); |
| 294 |
x = CORD_cat(x,x); |
| 295 |
if (CORD_sprintf(&result, "->%-120.78r!\n", x) != 124) |
| 296 |
ABORT("CORD_sprintf failed 3"); |
| 297 |
# ifdef GC_SNPRINTF |
| 298 |
(void)GC_SNPRINTF(result2, sizeof(result2), "->%-120.78s!\n", |
| 299 |
CORD_to_char_star(x)); |
| 300 |
# else |
| 301 |
(void)sprintf(result2, "->%-120.78s!\n", CORD_to_char_star(x)); |
| 302 |
# endif |
| 303 |
result2[sizeof(result2) - 1] = '\0'; |
| 304 |
if (CORD_cmp(result, result2) != 0) ABORT("CORD_sprintf goofed 5"); |
| 305 |
|
| 306 |
# ifdef GC_SNPRINTF |
| 307 |
/* Check whether "%zu" specifier is supported; pass the format */ |
| 308 |
/* string via a variable to avoid a compiler warning if not. */ |
| 309 |
res = GC_SNPRINTF(result2, sizeof(result2), zu_format, (size_t)0); |
| 310 |
# else |
| 311 |
res = sprintf(result2, zu_format, (size_t)0); |
| 312 |
# endif |
| 313 |
result2[sizeof(result2) - 1] = '\0'; |
| 314 |
if (res == 1) /* is "%z" supported by printf? */ { |
| 315 |
if (CORD_sprintf(&result, "%zu %zd 0x%0zx", |
| 316 |
(size_t)123, (size_t)4567, (size_t)0x4abc) != 15) |
| 317 |
ABORT("CORD_sprintf failed 5"); |
| 318 |
if (CORD_cmp(result, "123 4567 0x4abc") != 0) |
| 319 |
ABORT("CORD_sprintf goofed 5"); |
| 320 |
} else { |
| 321 |
(void)CORD_printf("printf lacks support of 'z' modifier\n"); |
| 322 |
} |
| 323 |
|
| 324 |
/* TODO: Better test CORD_[v][f]printf. */ |
| 325 |
(void)CORD_printf(CORD_EMPTY); |
| 326 |
(void)wrap_vfprintf(stdout, CORD_EMPTY); |
| 327 |
(void)wrap_vprintf(CORD_EMPTY); |
| 328 |
} |
| 329 |
|
| 330 |
int main(void) |
| 331 |
{ |
| 332 |
# ifdef THINK_C |
| 333 |
printf("cordtest:\n"); |
| 334 |
# endif |
| 335 |
GC_INIT(); |
| 336 |
# ifndef NO_INCREMENTAL |
| 337 |
GC_enable_incremental(); |
| 338 |
# endif |
| 339 |
if (GC_get_find_leak()) |
| 340 |
printf("This test program is not designed for leak detection mode\n"); |
| 341 |
test_basics(); |
| 342 |
test_extras(); |
| 343 |
test_printf(); |
| 344 |
|
| 345 |
GC_gcollect(); /* to close f2 before the file removal */ |
| 346 |
if (remove(FNAME2) != 0) { |
| 347 |
fprintf(stderr, "WARNING: remove failed: " FNAME2 "\n"); |
| 348 |
} |
| 349 |
CORD_fprintf(stdout, "SUCCEEDED\n"); |
| 350 |
return(0); |
| 351 |
} |