modified: src1/input.c
[GalaxyCodeBases.git] / c_cpp / lib / klib / test / kstring_test.c
blob9f9b6e63ca6eba1c3a662cedaee7613057eea2af
1 #include <limits.h>
2 #include <stdarg.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
7 #include "kstring.h"
9 int nfail = 0;
11 void check(const char *what, const kstring_t *ks, const char *correct)
13 if (ks->l != strlen(correct) || strcmp(ks->s, correct) != 0) {
14 fprintf(stderr, "%s produced \"%.*s\" (\"%s\" is correct)\tFAIL\n", what, (int)(ks->l), ks->s, correct);
15 nfail++;
19 void test_kputw(kstring_t *ks, int n)
21 char buf[16];
23 ks->l = 0;
24 kputw(n, ks);
26 sprintf(buf, "%d", n);
27 check("kputw()", ks, buf);
30 void test_kputl(kstring_t *ks, long n)
32 char buf[24];
34 ks->l = 0;
35 kputl(n, ks);
37 sprintf(buf, "%ld", n);
38 check("kputl()", ks, buf);
41 static char *mem_gets(char *buf, int buflen, void *vtextp)
43 const char **textp = (const char **) vtextp;
45 const char *nl = strchr(*textp, '\n');
46 size_t n = nl? nl - *textp + 1 : strlen(*textp);
48 if (n == 0) return NULL;
50 if (n > buflen-1) n = buflen-1;
51 memcpy(buf, *textp, n);
52 buf[n] = '\0';
53 *textp += n;
54 return buf;
57 void test_kgetline(kstring_t *ks, const char *text, ...)
59 const char *exp;
60 va_list arg;
62 va_start(arg, text);
63 while ((exp = va_arg(arg, const char *)) != NULL) {
64 ks->l = 0;
65 if (kgetline(ks, mem_gets, &text) != 0) kputs("EOF", ks);
66 check("kgetline()", ks, exp);
68 va_end(arg);
70 ks->l = 0;
71 if (kgetline(ks, mem_gets, &text) == 0) check("kgetline()", ks, "EOF");
74 int main(int argc, char **argv)
76 kstring_t ks;
78 ks.l = ks.m = 0;
79 ks.s = NULL;
81 test_kputw(&ks, 0);
82 test_kputw(&ks, 1);
83 test_kputw(&ks, 37);
84 test_kputw(&ks, 12345);
85 test_kputw(&ks, -12345);
86 test_kputw(&ks, INT_MAX);
87 test_kputw(&ks, -INT_MAX);
88 test_kputw(&ks, INT_MIN);
90 test_kputl(&ks, 0);
91 test_kputl(&ks, 1);
92 test_kputl(&ks, 37);
93 test_kputl(&ks, 12345);
94 test_kputl(&ks, -12345);
95 test_kputl(&ks, INT_MAX);
96 test_kputl(&ks, -INT_MAX);
97 test_kputl(&ks, INT_MIN);
98 test_kputl(&ks, LONG_MAX);
99 test_kputl(&ks, -LONG_MAX);
100 test_kputl(&ks, LONG_MIN);
102 test_kgetline(&ks, "", NULL);
103 test_kgetline(&ks, "apple", "apple", NULL);
104 test_kgetline(&ks, "banana\n", "banana", NULL);
105 test_kgetline(&ks, "carrot\r\n", "carrot", NULL);
106 test_kgetline(&ks, "\n", "", NULL);
107 test_kgetline(&ks, "\n\n", "", "", NULL);
108 test_kgetline(&ks, "foo\nbar", "foo", "bar", NULL);
109 test_kgetline(&ks, "foo\nbar\n", "foo", "bar", NULL);
110 test_kgetline(&ks,
111 "abcdefghijklmnopqrstuvwxyz0123456789\nABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
112 "abcdefghijklmnopqrstuvwxyz0123456789",
113 "ABCDEFGHIJKLMNOPQRSTUVWXYZ", NULL);
115 if (argc > 1) {
116 FILE *f = fopen(argv[1], "r");
117 if (f) {
118 for (ks.l = 0; kgetline(&ks, (kgets_func *)fgets, f) == 0; ks.l = 0)
119 puts(ks.s);
120 fclose(f);
124 free(ks.s);
126 if (nfail > 0) {
127 fprintf(stderr, "Total failures: %d\n", nfail);
128 return EXIT_FAILURE;
131 return EXIT_SUCCESS;