cordtest.c 7.8 KB

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