Back to index

plt-scheme  4.2.1
cordtest.c
Go to the documentation of this file.
00001 /* 
00002  * Copyright (c) 1993-1994 by Xerox Corporation.  All rights reserved.
00003  *
00004  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
00005  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
00006  *
00007  * Permission is hereby granted to use or copy this program
00008  * for any purpose,  provided the above notices are retained on all copies.
00009  * Permission to modify the code and to distribute modified code is granted,
00010  * provided the above notices are retained, and a notice that the code was
00011  * modified is included with the above copyright notice.
00012  */
00013 /* Boehm, August 24, 1994 11:58 am PDT */
00014 # include "gc.h"     /* For GC_INIT() only */
00015 # include "cord.h"
00016 # include <string.h>
00017 # include <stdio.h>
00018 # include <stdlib.h>
00019 /* This is a very incomplete test of the cord package.  It knows about       */
00020 /* a few internals of the package (e.g. when C strings are returned)  */
00021 /* that real clients shouldn't rely on.                               */
00022 
00023 # define ABORT(string) \
00024 { int x = 0; fprintf(stderr, "FAILED: %s\n", string); x = 1 / x; abort(); }
00025 
00026 int count;
00027 
00028 int test_fn(char c, void * client_data)
00029 {
00030     if (client_data != (void *)13) ABORT("bad client data");
00031     if (count < 64*1024+1) {
00032         if ((count & 1) == 0) {
00033             if (c != 'b') ABORT("bad char");
00034         } else {
00035             if (c != 'a') ABORT("bad char");
00036         }
00037         count++;
00038         return(0);
00039     } else {
00040         if (c != 'c') ABORT("bad char");
00041         count++;
00042         return(1);
00043     }
00044 }
00045 
00046 char id_cord_fn(size_t i, void * client_data)
00047 {
00048     return((char)i);
00049 }
00050 
00051 void test_basics()
00052 {
00053     CORD x = CORD_from_char_star("ab");
00054     register int i;
00055     char c;
00056     CORD y;
00057     CORD_pos p;
00058     
00059     x = CORD_cat(x,x);
00060     if (!CORD_IS_STRING(x)) ABORT("short cord should usually be a string");
00061     if (strcmp(x, "abab") != 0) ABORT("bad CORD_cat result");
00062     
00063     for (i = 1; i < 16; i++) {
00064         x = CORD_cat(x,x);
00065     }
00066     x = CORD_cat(x,"c");
00067     if (CORD_len(x) != 128*1024+1) ABORT("bad length");
00068     
00069     count = 0;
00070     if (CORD_iter5(x, 64*1024-1, test_fn, CORD_NO_FN, (void *)13) == 0) {
00071         ABORT("CORD_iter5 failed");
00072     }
00073     if (count != 64*1024 + 2) ABORT("CORD_iter5 failed");
00074     
00075     count = 0;
00076     CORD_set_pos(p, x, 64*1024-1);
00077     while(CORD_pos_valid(p)) {
00078               (void) test_fn(CORD_pos_fetch(p), (void *)13);
00079        CORD_next(p);
00080     }
00081     if (count != 64*1024 + 2) ABORT("Position based iteration failed");
00082     
00083     y = CORD_substr(x, 1023, 5);
00084     if (!CORD_IS_STRING(y)) ABORT("short cord should usually be a string");
00085     if (strcmp(y, "babab") != 0) ABORT("bad CORD_substr result");
00086     
00087     y = CORD_substr(x, 1024, 8);
00088     if (!CORD_IS_STRING(y)) ABORT("short cord should usually be a string");
00089     if (strcmp(y, "abababab") != 0) ABORT("bad CORD_substr result");
00090     
00091     y = CORD_substr(x, 128*1024-1, 8);
00092     if (!CORD_IS_STRING(y)) ABORT("short cord should usually be a string");
00093     if (strcmp(y, "bc") != 0) ABORT("bad CORD_substr result");
00094     
00095     x = CORD_balance(x);
00096     if (CORD_len(x) != 128*1024+1) ABORT("bad length");
00097     
00098     count = 0;
00099     if (CORD_iter5(x, 64*1024-1, test_fn, CORD_NO_FN, (void *)13) == 0) {
00100         ABORT("CORD_iter5 failed");
00101     }
00102     if (count != 64*1024 + 2) ABORT("CORD_iter5 failed");
00103     
00104     y = CORD_substr(x, 1023, 5);
00105     if (!CORD_IS_STRING(y)) ABORT("short cord should usually be a string");
00106     if (strcmp(y, "babab") != 0) ABORT("bad CORD_substr result");
00107     y = CORD_from_fn(id_cord_fn, 0, 13);
00108     i = 0;
00109     CORD_set_pos(p, y, i);
00110     while(CORD_pos_valid(p)) {
00111         c = CORD_pos_fetch(p);
00112               if(c != i) ABORT("Traversal of function node failed");
00113        CORD_next(p); i++;
00114     }
00115     if (i != 13) ABORT("Bad apparent length for function node");
00116 }
00117 
00118 void test_extras()
00119 {
00120 #   if defined(__OS2__) || defined(__DJGPP__)
00121 #      define FNAME1 "tmp1"
00122 #      define FNAME2 "tmp2"
00123 #   elif defined(AMIGA)
00124 #      define FNAME1 "T:tmp1"
00125 #      define FNAME2 "T:tmp2"
00126 #   else
00127 #      define FNAME1 "/tmp/cord_test"
00128 #      define FNAME2 "/tmp/cord_test2"
00129 #   endif
00130     register int i;
00131     CORD y = "abcdefghijklmnopqrstuvwxyz0123456789";
00132     CORD x = "{}";
00133     CORD w, z;
00134     FILE *f;
00135     FILE *f1a, *f1b, *f2;
00136     
00137     w = CORD_cat(CORD_cat(y,y),y);
00138     z = CORD_catn(3,y,y,y);
00139     if (CORD_cmp(w,z) != 0) ABORT("CORD_catn comparison wrong");
00140     for (i = 1; i < 100; i++) {
00141         x = CORD_cat(x, y);
00142     }
00143     z = CORD_balance(x);
00144     if (CORD_cmp(x,z) != 0) ABORT("balanced string comparison wrong");
00145     if (CORD_cmp(x,CORD_cat(z, CORD_nul(13))) >= 0) ABORT("comparison 2");
00146     if (CORD_cmp(CORD_cat(x, CORD_nul(13)), z) <= 0) ABORT("comparison 3");
00147     if (CORD_cmp(x,CORD_cat(z, "13")) >= 0) ABORT("comparison 4");
00148     if ((f = fopen(FNAME1, "w")) == 0) ABORT("open failed");
00149     if (CORD_put(z,f) == EOF) ABORT("CORD_put failed");
00150     if (fclose(f) == EOF) ABORT("fclose failed");
00151     w = CORD_from_file(f1a = fopen(FNAME1, "rb"));
00152     if (CORD_len(w) != CORD_len(z)) ABORT("file length wrong");
00153     if (CORD_cmp(w,z) != 0) ABORT("file comparison wrong");
00154     if (CORD_cmp(CORD_substr(w, 50*36+2, 36), y) != 0)
00155        ABORT("file substr wrong");
00156     z = CORD_from_file_lazy(f1b = fopen(FNAME1, "rb"));
00157     if (CORD_cmp(w,z) != 0) ABORT("File conversions differ");
00158     if (CORD_chr(w, 0, '9') != 37) ABORT("CORD_chr failed 1");
00159     if (CORD_chr(w, 3, 'a') != 38) ABORT("CORD_chr failed 2");
00160     if (CORD_rchr(w, CORD_len(w) - 1, '}') != 1) ABORT("CORD_rchr failed");
00161     x = y;
00162     for (i = 1; i < 14; i++) {
00163         x = CORD_cat(x,x);
00164     }
00165     if ((f = fopen(FNAME2, "w")) == 0) ABORT("2nd open failed");
00166 #   ifdef __DJGPP__
00167       /* FIXME: DJGPP workaround.  Why does this help? */
00168       if (fflush(f) != 0) ABORT("fflush failed");
00169 #   endif
00170     if (CORD_put(x,f) == EOF) ABORT("CORD_put failed");
00171     if (fclose(f) == EOF) ABORT("fclose failed");
00172     w = CORD_from_file(f2 = fopen(FNAME2, "rb"));
00173     if (CORD_len(w) != CORD_len(x)) ABORT("file length wrong");
00174     if (CORD_cmp(w,x) != 0) ABORT("file comparison wrong");
00175     if (CORD_cmp(CORD_substr(w, 1000*36, 36), y) != 0)
00176        ABORT("file substr wrong");
00177     if (strcmp(CORD_to_char_star(CORD_substr(w, 1000*36, 36)), y) != 0)
00178        ABORT("char * file substr wrong");
00179     if (strcmp(CORD_substr(w, 1000*36, 2), "ab") != 0)
00180        ABORT("short file substr wrong");
00181     if (CORD_str(x,1,"9a") != 35) ABORT("CORD_str failed 1");
00182     if (CORD_str(x,0,"9abcdefghijk") != 35) ABORT("CORD_str failed 2");
00183     if (CORD_str(x,0,"9abcdefghijx") != CORD_NOT_FOUND)
00184        ABORT("CORD_str failed 3");
00185     if (CORD_str(x,0,"9>") != CORD_NOT_FOUND) ABORT("CORD_str failed 4");
00186     if (remove(FNAME1) != 0) {
00187        /* On some systems, e.g. OS2, this may fail if f1 is still open. */
00188        if ((fclose(f1a) == EOF) & (fclose(f1b) == EOF))
00189               ABORT("fclose(f1) failed");
00190        if (remove(FNAME1) != 0) ABORT("remove 1 failed");
00191     }
00192     if (remove(FNAME2) != 0) {
00193        if (fclose(f2) == EOF) ABORT("fclose(f2) failed");
00194        if (remove(FNAME2) != 0) ABORT("remove 2 failed");
00195     }
00196 }
00197 
00198 void test_printf()
00199 {
00200     CORD result;
00201     char result2[200];
00202     long l;
00203     short s;
00204     CORD x;
00205     
00206     if (CORD_sprintf(&result, "%7.2f%ln", 3.14159F, &l) != 7)
00207        ABORT("CORD_sprintf failed 1");
00208     if (CORD_cmp(result, "   3.14") != 0)ABORT("CORD_sprintf goofed 1");
00209     if (l != 7) ABORT("CORD_sprintf goofed 2");
00210     if (CORD_sprintf(&result, "%-7.2s%hn%c%s", "abcd", &s, 'x', "yz") != 10)
00211        ABORT("CORD_sprintf failed 2");
00212     if (CORD_cmp(result, "ab     xyz") != 0)ABORT("CORD_sprintf goofed 3");
00213     if (s != 7) ABORT("CORD_sprintf goofed 4");
00214     x = "abcdefghij";
00215     x = CORD_cat(x,x);
00216     x = CORD_cat(x,x);
00217     x = CORD_cat(x,x);
00218     if (CORD_sprintf(&result, "->%-120.78r!\n", x) != 124)
00219        ABORT("CORD_sprintf failed 3");
00220     (void) sprintf(result2, "->%-120.78s!\n", CORD_to_char_star(x));
00221     if (CORD_cmp(result, result2) != 0)ABORT("CORD_sprintf goofed 5");
00222 }
00223 
00224 int main()
00225 {
00226 #   ifdef THINK_C
00227         printf("cordtest:\n");
00228 #   endif
00229     GC_INIT();
00230     test_basics();
00231     test_extras();
00232     test_printf();
00233     CORD_fprintf(stderr, "SUCCEEDED\n");
00234     return(0);
00235 }