Initial import to Tizen
[profile/ivi/sphinxbase.git] / test / unit / test_string / strtest.c
1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
3 #include <stdio.h>
4 #include <string.h>
5
6 #include "strfuncs.h"
7 #include "pio.h"
8 #include "ckd_alloc.h"
9
10 int
11 main(int argc, char *argv[])
12 {
13     if (argc < 2)
14         return 1;
15
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);
20             return 1;
21         }
22         foo = string_join("hello", NULL);
23         if (strcmp(foo, "hello") != 0) {
24             printf("%s != hello\n", foo);
25             return 1;
26         }
27         return 0;
28     }
29     else if (!strcmp(argv[1], "fread_line")) {
30         FILE *fp = fopen(TESTDATADIR "/_fread_line.txt", "r");
31         char *line;
32         size_t len;
33
34         if (fp == NULL) {
35             perror("Failed to open " TESTDATADIR "/_fread_line.txt");
36             return 1;
37         }
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);
43             return 1;
44         }
45         ckd_free(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);
52             return 1;
53         }
54         ckd_free(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);
61             return 1;
62         }
63         ckd_free(line);
64         line = fread_line(fp, &len);
65         if (line != NULL) {
66             printf("%p != NULL\n", line);
67             return 1;
68         }
69     }
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);
75             return 1;
76         }
77         string_trim(foo, STRING_BOTH);
78         if (strcmp(foo, "foo bar baz") != 0) {
79             printf("'%s' != 'foo bar baz'\n", foo);
80             return 1;
81         }
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);
86             return 1;
87         }
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);
92             return 1;
93         }
94     }
95     else if (!strcmp(argv[1], "str2words")) {
96         char *line = strdup("    foo bar baz argh");
97         char **words;
98         int n;
99
100         n = str2words(line, NULL, 0);
101         if (n != 4) {
102             printf("%d != 4\n", n);
103             return 1;
104         }
105         words = ckd_calloc(n, sizeof(*words));
106         n = str2words(line, words, n);
107         if (n != 4) {
108             printf("%d != 4\n", n);
109             return 1;
110         }
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]);
117             return 1;
118         }
119         return 0;
120     }
121     else if (!strcmp(argv[1], "nextword")) {
122         char *line = strdup(" \tfoo bar\nbaz argh");
123         char *word;
124         const char *delim = " \t\n";
125         char delimfound;
126         int n;
127
128         n = nextword(line, delim, &word, &delimfound);
129         if (strcmp(word, "foo") != 0) {
130             printf("%s != foo\n", word);
131             return 1;
132         }
133         if (delimfound != ' ') {
134             printf("didn't find ' '\n");
135             return 1;
136         }
137         word[n] = delimfound;
138         line = word + n;
139         n = nextword(line, delim, &word, &delimfound);
140         if (strcmp(word, "bar") != 0) {
141             printf("%s != bar\n", word);
142             return 1;
143         }
144         if (delimfound != '\n') {
145             printf("didn't find '\\n'\n");
146             return 1;
147         }
148         word[n] = delimfound;
149         line = word + n;
150         n = nextword(line, delim, &word, &delimfound);
151         if (strcmp(word, "baz") != 0) {
152             printf("%s != baz\n", word);
153             return 1;
154         }
155         if (delimfound != ' ') {
156             printf("didn't find ' '\n");
157             return 1;
158         }
159         word[n] = delimfound;
160         line = word + n;
161         n = nextword(line, delim, &word, &delimfound);
162         if (strcmp(word, "argh") != 0) {
163             printf("%s != argh\n", word);
164             return 1;
165         }
166         if (delimfound != '\0') {
167             printf("didn't find NUL\n");
168             return 1;
169         }
170         word[n] = delimfound;
171         line = word + n;
172         n = nextword(line, delim, &word, &delimfound);
173         if (n != -1) {
174             printf("didn't get -1 at end of string\n");
175         }
176
177         line = strdup("FOO!");
178         n = nextword(line, delim, &word, &delimfound);
179         if (strcmp(word, "FOO!") != 0) {
180             printf("%s != FOO!\n", word);
181             return 1;
182         }
183         if (delimfound != '\0') {
184             printf("didn't find NUL\n");
185             return 1;
186         }
187
188         return 0;
189     }
190     return 0;
191 }