Back to index

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