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