Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/klib/blob/master/test/kstring_test.c # klib [Ported from github.com/attractivechaos/klib] added block arena Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `klib / test / kstring_test.c` 132 lines ยท 2793 bytes Raw Blame History 1 #include 2 #include 3 #include 4 #include 5 #include 6 7 #include "kstring.h" 8 9 int nfail = 0; 10 11 void check(const char *what, const kstring_t *ks, const char *correct) 12 { 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++; 16 } 17 } 18 19 void test_kputw(kstring_t *ks, int n) 20 { 21 char buf[16]; 22 23 ks->l = 0; 24 kputw(n, ks); 25 26 sprintf(buf, "%d", n); 27 check("kputw()", ks, buf); 28 } 29 30 void test_kputl(kstring_t *ks, long n) 31 { 32 char buf[24]; 33 34 ks->l = 0; 35 kputl(n, ks); 36 37 sprintf(buf, "%ld", n); 38 check("kputl()", ks, buf); 39 } 40 41 static char *mem_gets(char *buf, int buflen, void *vtextp) 42 { 43 const char **textp = (const char **) vtextp; 44 45 const char *nl = strchr(*textp, '\n'); 46 size_t n = nl? nl - *textp + 1 : strlen(*textp); 47 48 if (n == 0) return NULL; 49 50 if (n > buflen-1) n = buflen-1; 51 memcpy(buf, *textp, n); 52 buf[n] = '\0'; 53 *textp += n; 54 return buf; 55 } 56 57 void test_kgetline(kstring_t *ks, const char *text, ...) 58 { 59 const char *exp; 60 va_list arg; 61 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); 67 } 68 va_end(arg); 69 70 ks->l = 0; 71 if (kgetline(ks, mem_gets, &text) == 0) check("kgetline()", ks, "EOF"); 72 } 73 74 int main(int argc, char **argv) 75 { 76 kstring_t ks; 77 78 ks.l = ks.m = 0; 79 ks.s = NULL; 80 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); 89 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); 101 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); 114 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); 121 } 122 } 123 124 free(ks.s); 125 126 if (nfail > 0) { 127 fprintf(stderr, "Total failures: %d\n", nfail); 128 return EXIT_FAILURE; 129 } 130 131 return EXIT_SUCCESS; 132 }