FSF GCC merge 02/23/03
[official-gcc.git] / boehm-gc / cord / cordtest.c
blobd54c65fe0d72426e2757ce2dfa39c9b9f2679195
1 /*
2 * Copyright (c) 1993-1994 by Xerox Corporation. All rights reserved.
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
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.
13 /* Boehm, August 24, 1994 11:58 am PDT */
14 # include "cord.h"
15 # include <string.h>
16 # include <stdio.h>
17 # include <stdlib.h>
18 /* This is a very incomplete test of the cord package. It knows about */
19 /* a few internals of the package (e.g. when C strings are returned) */
20 /* that real clients shouldn't rely on. */
22 # define ABORT(string) \
23 { int x = 0; fprintf(stderr, "FAILED: %s\n", string); x = 1 / x; abort(); }
25 int count;
27 int test_fn(char c, void * client_data)
29 if (client_data != (void *)13) ABORT("bad client data");
30 if (count < 64*1024+1) {
31 if ((count & 1) == 0) {
32 if (c != 'b') ABORT("bad char");
33 } else {
34 if (c != 'a') ABORT("bad char");
36 count++;
37 return(0);
38 } else {
39 if (c != 'c') ABORT("bad char");
40 count++;
41 return(1);
45 char id_cord_fn(size_t i, void * client_data)
47 return((char)i);
50 void test_basics()
52 CORD x = CORD_from_char_star("ab");
53 register int i;
54 char c;
55 CORD y;
56 CORD_pos p;
58 x = CORD_cat(x,x);
59 if (!CORD_IS_STRING(x)) ABORT("short cord should usually be a string");
60 if (strcmp(x, "abab") != 0) ABORT("bad CORD_cat result");
62 for (i = 1; i < 16; i++) {
63 x = CORD_cat(x,x);
65 x = CORD_cat(x,"c");
66 if (CORD_len(x) != 128*1024+1) ABORT("bad length");
68 count = 0;
69 if (CORD_iter5(x, 64*1024-1, test_fn, CORD_NO_FN, (void *)13) == 0) {
70 ABORT("CORD_iter5 failed");
72 if (count != 64*1024 + 2) ABORT("CORD_iter5 failed");
74 count = 0;
75 CORD_set_pos(p, x, 64*1024-1);
76 while(CORD_pos_valid(p)) {
77 (void) test_fn(CORD_pos_fetch(p), (void *)13);
78 CORD_next(p);
80 if (count != 64*1024 + 2) ABORT("Position based iteration failed");
82 y = CORD_substr(x, 1023, 5);
83 if (!CORD_IS_STRING(y)) ABORT("short cord should usually be a string");
84 if (strcmp(y, "babab") != 0) ABORT("bad CORD_substr result");
86 y = CORD_substr(x, 1024, 8);
87 if (!CORD_IS_STRING(y)) ABORT("short cord should usually be a string");
88 if (strcmp(y, "abababab") != 0) ABORT("bad CORD_substr result");
90 y = CORD_substr(x, 128*1024-1, 8);
91 if (!CORD_IS_STRING(y)) ABORT("short cord should usually be a string");
92 if (strcmp(y, "bc") != 0) ABORT("bad CORD_substr result");
94 x = CORD_balance(x);
95 if (CORD_len(x) != 128*1024+1) ABORT("bad length");
97 count = 0;
98 if (CORD_iter5(x, 64*1024-1, test_fn, CORD_NO_FN, (void *)13) == 0) {
99 ABORT("CORD_iter5 failed");
101 if (count != 64*1024 + 2) ABORT("CORD_iter5 failed");
103 y = CORD_substr(x, 1023, 5);
104 if (!CORD_IS_STRING(y)) ABORT("short cord should usually be a string");
105 if (strcmp(y, "babab") != 0) ABORT("bad CORD_substr result");
106 y = CORD_from_fn(id_cord_fn, 0, 13);
107 i = 0;
108 CORD_set_pos(p, y, i);
109 while(CORD_pos_valid(p)) {
110 c = CORD_pos_fetch(p);
111 if(c != i) ABORT("Traversal of function node failed");
112 CORD_next(p); i++;
114 if (i != 13) ABORT("Bad apparent length for function node");
117 void test_extras()
119 # if defined(__OS2__)
120 # define FNAME1 "tmp1"
121 # define FNAME2 "tmp2"
122 # elif defined(AMIGA)
123 # define FNAME1 "T:tmp1"
124 # define FNAME2 "T:tmp2"
125 # else
126 # define FNAME1 "/tmp/cord_test"
127 # define FNAME2 "/tmp/cord_test2"
128 # endif
129 register int i;
130 CORD y = "abcdefghijklmnopqrstuvwxyz0123456789";
131 CORD x = "{}";
132 CORD w, z;
133 FILE *f;
134 FILE *f1a, *f1b, *f2;
136 w = CORD_cat(CORD_cat(y,y),y);
137 z = CORD_catn(3,y,y,y);
138 if (CORD_cmp(w,z) != 0) ABORT("CORD_catn comparison wrong");
139 for (i = 1; i < 100; i++) {
140 x = CORD_cat(x, y);
142 z = CORD_balance(x);
143 if (CORD_cmp(x,z) != 0) ABORT("balanced string comparison wrong");
144 if (CORD_cmp(x,CORD_cat(z, CORD_nul(13))) >= 0) ABORT("comparison 2");
145 if (CORD_cmp(CORD_cat(x, CORD_nul(13)), z) <= 0) ABORT("comparison 3");
146 if (CORD_cmp(x,CORD_cat(z, "13")) >= 0) ABORT("comparison 4");
147 if ((f = fopen(FNAME1, "w")) == 0) ABORT("open failed");
148 if (CORD_put(z,f) == EOF) ABORT("CORD_put failed");
149 if (fclose(f) == EOF) ABORT("fclose failed");
150 w = CORD_from_file(f1a = fopen(FNAME1, "rb"));
151 if (CORD_len(w) != CORD_len(z)) ABORT("file length wrong");
152 if (CORD_cmp(w,z) != 0) ABORT("file comparison wrong");
153 if (CORD_cmp(CORD_substr(w, 50*36+2, 36), y) != 0)
154 ABORT("file substr wrong");
155 z = CORD_from_file_lazy(f1b = fopen(FNAME1, "rb"));
156 if (CORD_cmp(w,z) != 0) ABORT("File conversions differ");
157 if (CORD_chr(w, 0, '9') != 37) ABORT("CORD_chr failed 1");
158 if (CORD_chr(w, 3, 'a') != 38) ABORT("CORD_chr failed 2");
159 if (CORD_rchr(w, CORD_len(w) - 1, '}') != 1) ABORT("CORD_rchr failed");
160 x = y;
161 for (i = 1; i < 14; i++) {
162 x = CORD_cat(x,x);
164 if ((f = fopen(FNAME2, "w")) == 0) ABORT("2nd open failed");
165 if (CORD_put(x,f) == EOF) ABORT("CORD_put failed");
166 if (fclose(f) == EOF) ABORT("fclose failed");
167 w = CORD_from_file(f2 = fopen(FNAME2, "rb"));
168 if (CORD_len(w) != CORD_len(x)) ABORT("file length wrong");
169 if (CORD_cmp(w,x) != 0) ABORT("file comparison wrong");
170 if (CORD_cmp(CORD_substr(w, 1000*36, 36), y) != 0)
171 ABORT("file substr wrong");
172 if (strcmp(CORD_to_char_star(CORD_substr(w, 1000*36, 36)), y) != 0)
173 ABORT("char * file substr wrong");
174 if (strcmp(CORD_substr(w, 1000*36, 2), "ab") != 0)
175 ABORT("short file substr wrong");
176 if (CORD_str(x,1,"9a") != 35) ABORT("CORD_str failed 1");
177 if (CORD_str(x,0,"9abcdefghijk") != 35) ABORT("CORD_str failed 2");
178 if (CORD_str(x,0,"9abcdefghijx") != CORD_NOT_FOUND)
179 ABORT("CORD_str failed 3");
180 if (CORD_str(x,0,"9>") != CORD_NOT_FOUND) ABORT("CORD_str failed 4");
181 if (remove(FNAME1) != 0) {
182 /* On some systems, e.g. OS2, this may fail if f1 is still open. */
183 if ((fclose(f1a) == EOF) & (fclose(f1b) == EOF))
184 ABORT("fclose(f1) failed");
185 if (remove(FNAME1) != 0) ABORT("remove 1 failed");
187 if (remove(FNAME2) != 0) {
188 if (fclose(f2) == EOF) ABORT("fclose(f2) failed");
189 if (remove(FNAME2) != 0) ABORT("remove 2 failed");
193 void test_printf()
195 CORD result;
196 char result2[200];
197 long l;
198 short s;
199 CORD x;
201 if (CORD_sprintf(&result, "%7.2f%ln", 3.14159F, &l) != 7)
202 ABORT("CORD_sprintf failed 1");
203 if (CORD_cmp(result, " 3.14") != 0)ABORT("CORD_sprintf goofed 1");
204 if (l != 7) ABORT("CORD_sprintf goofed 2");
205 if (CORD_sprintf(&result, "%-7.2s%hn%c%s", "abcd", &s, 'x', "yz") != 10)
206 ABORT("CORD_sprintf failed 2");
207 if (CORD_cmp(result, "ab xyz") != 0)ABORT("CORD_sprintf goofed 3");
208 if (s != 7) ABORT("CORD_sprintf goofed 4");
209 x = "abcdefghij";
210 x = CORD_cat(x,x);
211 x = CORD_cat(x,x);
212 x = CORD_cat(x,x);
213 if (CORD_sprintf(&result, "->%-120.78r!\n", x) != 124)
214 ABORT("CORD_sprintf failed 3");
215 (void) sprintf(result2, "->%-120.78s!\n", CORD_to_char_star(x));
216 if (CORD_cmp(result, result2) != 0)ABORT("CORD_sprintf goofed 5");
219 main()
221 # ifdef THINK_C
222 printf("cordtest:\n");
223 # endif
224 test_basics();
225 test_extras();
226 test_printf();
227 CORD_fprintf(stderr, "SUCCEEDED\n");
228 return(0);