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
);
19 void test_kputw(kstring_t
*ks
, int n
)
26 sprintf(buf
, "%d", n
);
27 check("kputw()", ks
, buf
);
30 void test_kputl(kstring_t
*ks
, long n
)
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
);
57 void test_kgetline(kstring_t
*ks
, const char *text
, ...)
63 while ((exp
= va_arg(arg
, const char *)) != NULL
) {
65 if (kgetline(ks
, mem_gets
, &text
) != 0) kputs("EOF", ks
);
66 check("kgetline()", ks
, exp
);
71 if (kgetline(ks
, mem_gets
, &text
) == 0) check("kgetline()", ks
, "EOF");
74 int main(int argc
, char **argv
)
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
);
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
);
111 "abcdefghijklmnopqrstuvwxyz0123456789\nABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
112 "abcdefghijklmnopqrstuvwxyz0123456789",
113 "ABCDEFGHIJKLMNOPQRSTUVWXYZ", NULL
);
116 FILE *f
= fopen(argv
[1], "r");
118 for (ks
.l
= 0; kgetline(&ks
, (kgets_func
*)fgets
, f
) == 0; ks
.l
= 0)
127 fprintf(stderr
, "Total failures: %d\n", nfail
);