1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
11 main(int argc, char *argv[])
16 if (!strcmp(argv[1], "string_join")) {
17 char *foo = string_join("bar", "baz", "quux", NULL);
18 if (strcmp(foo, "barbazquux") != 0) {
19 printf("%s != barbazquux\n", foo);
22 foo = string_join("hello", NULL);
23 if (strcmp(foo, "hello") != 0) {
24 printf("%s != hello\n", foo);
29 else if (!strcmp(argv[1], "fread_line")) {
30 FILE *fp = fopen(TESTDATADIR "/_fread_line.txt", "r");
35 perror("Failed to open " TESTDATADIR "/_fread_line.txt");
38 line = fread_line(fp, &len);
39 printf("len = %d orig = %d\n", len,
40 strlen("Hello world!\n"));
41 if (strcmp(line, "Hello world!\n") != 0) {
42 printf("'%s' != 'Hello world!\\n'\n", line);
46 line = fread_line(fp, &len);
47 /* A line of exactly 127 characters. */
48 printf("len = %d orig = %d\n", len,
49 strlen("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456\n"));
50 if (strcmp(line, "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456\n") != 0) {
51 printf("'%s' != '123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456\\n'\n", line);
55 /* A very long line. */
56 line = fread_line(fp, &len);
57 printf("len = %d orig = %d\n", len,
58 strlen("All work and no play makes Jack a very dull boy. All work and no play makes Jack a very dull boy. All work and no play makes Jack a very dull boy. All work and no play makes Jack a very dull boy. All work and no play makes Jack a very dull boy. All work and no play makes Jack a very dull boy. \n"));
59 if (strcmp(line, "All work and no play makes Jack a very dull boy. All work and no play makes Jack a very dull boy. All work and no play makes Jack a very dull boy. All work and no play makes Jack a very dull boy. All work and no play makes Jack a very dull boy. All work and no play makes Jack a very dull boy. \n") != 0) {
60 printf("'%s' != 'All work and no play makes Jack a very dull boy. All work and no play makes Jack a very dull boy. All work and no play makes Jack a very dull boy. All work and no play makes Jack a very dull boy. All work and no play makes Jack a very dull boy. All work and no play makes Jack a very dull boy. \\n'\n", line);
64 line = fread_line(fp, &len);
66 printf("%p != NULL\n", line);
70 else if (!strcmp(argv[1], "string_trim")) {
71 char *foo = ckd_salloc("\t foo bar baz \n");
72 string_trim(foo, STRING_BOTH);
73 if (strcmp(foo, "foo bar baz") != 0) {
74 printf("'%s' != 'foo bar baz'\n", foo);
77 string_trim(foo, STRING_BOTH);
78 if (strcmp(foo, "foo bar baz") != 0) {
79 printf("'%s' != 'foo bar baz'\n", foo);
82 strcpy(foo, "foo\nbar\n\n");
83 string_trim(foo, STRING_END);
84 if (strcmp(foo, "foo\nbar") != 0) {
85 printf("'%s' != 'foo\\nbar'\n", foo);
88 strcpy(foo, " \t \t foobar\n");
89 string_trim(foo, STRING_START);
90 if (strcmp(foo, "foobar\n") != 0) {
91 printf("'%s' != 'foobar\\n'\n", foo);
95 else if (!strcmp(argv[1], "str2words")) {
96 char *line = strdup(" foo bar baz argh");
100 n = str2words(line, NULL, 0);
102 printf("%d != 4\n", n);
105 words = ckd_calloc(n, sizeof(*words));
106 n = str2words(line, words, n);
108 printf("%d != 4\n", n);
111 if (strcmp(words[0], "foo") != 0
112 || strcmp(words[1], "bar") != 0
113 || strcmp(words[2], "baz") != 0
114 || strcmp(words[3], "argh") != 0) {
115 printf("%s, %s, %s, %s != foo, bar, baz, argh\n",
116 words[0], words[1], words[2], words[3]);
121 else if (!strcmp(argv[1], "nextword")) {
122 char *line = strdup(" \tfoo bar\nbaz argh");
124 const char *delim = " \t\n";
128 n = nextword(line, delim, &word, &delimfound);
129 if (strcmp(word, "foo") != 0) {
130 printf("%s != foo\n", word);
133 if (delimfound != ' ') {
134 printf("didn't find ' '\n");
137 word[n] = delimfound;
139 n = nextword(line, delim, &word, &delimfound);
140 if (strcmp(word, "bar") != 0) {
141 printf("%s != bar\n", word);
144 if (delimfound != '\n') {
145 printf("didn't find '\\n'\n");
148 word[n] = delimfound;
150 n = nextword(line, delim, &word, &delimfound);
151 if (strcmp(word, "baz") != 0) {
152 printf("%s != baz\n", word);
155 if (delimfound != ' ') {
156 printf("didn't find ' '\n");
159 word[n] = delimfound;
161 n = nextword(line, delim, &word, &delimfound);
162 if (strcmp(word, "argh") != 0) {
163 printf("%s != argh\n", word);
166 if (delimfound != '\0') {
167 printf("didn't find NUL\n");
170 word[n] = delimfound;
172 n = nextword(line, delim, &word, &delimfound);
174 printf("didn't get -1 at end of string\n");
177 line = strdup("FOO!");
178 n = nextword(line, delim, &word, &delimfound);
179 if (strcmp(word, "FOO!") != 0) {
180 printf("%s != FOO!\n", word);
183 if (delimfound != '\0') {
184 printf("didn't find NUL\n");