PageRenderTime 26ms CodeModel.GetById 8ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/gc/cord/cordtest.c

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