chiark / gitweb /
Placate gcc -flto
[disorder] / libtests / t-words.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2005, 2007, 2008 Richard Kettlewell
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 #include "test.h"
19
20 struct {
21   const char *in;
22   const char *expect[10];
23 } wtest[] = {
24   /* Empty string */
25   { "", { 0 } },
26   /* Only whitespace and punctuation */
27   { "    ", { 0 } },
28   { " '   ", { 0 } },
29   { " !  ", { 0 } },
30   { " \"\"  ", { 0 } },
31   { " @  ", { 0 } },
32   /* Basics */
33   { "wibble", { "wibble", 0 } },
34   { " wibble", { "wibble", 0 } },
35   { " wibble ", { "wibble", 0 } },
36   { "wibble ", { "wibble", 0 } },
37   { "wibble spong", { "wibble", "spong", 0 } },
38   { " wibble  spong", { "wibble", "spong", 0 } },
39   { " wibble  spong   ", { "wibble", "spong", 0 } },
40   { "wibble   spong  ", { "wibble", "spong", 0 } },
41   { "wibble   spong splat foo zot  ", { "wibble", "spong", "splat", "foo", "zot", 0 } },
42   /* Apostrophes */
43   { "wibble 'spong", { "wibble", "spong", 0 } },
44   { " wibble's", { "wibble's", 0 } },
45   { " wibblespong'   ", { "wibblespong", 0 } },
46   { "wibble   sp''ong  ", { "wibble", "sp", "ong", 0 } },
47 };
48 #define NWTEST (sizeof wtest / sizeof *wtest)
49
50 static void test_words(void) {
51   size_t t, nexpect, ngot, i;
52   int right;
53   
54   for(t = 0; t < NWTEST; ++t) {
55     char **got = utf8_word_split(wtest[t].in, strlen(wtest[t].in), &ngot, 0);
56
57     assert(got != NULL);
58     for(nexpect = 0; wtest[t].expect[nexpect]; ++nexpect)
59       ;
60     if(nexpect == ngot) {
61       for(i = 0; i < ngot; ++i)
62         if(strcmp(wtest[t].expect[i], got[i]))
63           break;
64       right = i == ngot;
65     } else
66       right = 0;
67     if(!right) {
68       fprintf(stderr, "word split %zu failed\n", t);
69       fprintf(stderr, "input: %s\n", wtest[t].in);
70       fprintf(stderr, "    | %-30s | %-30s\n",
71               "expected", "got");
72       for(i = 0; i < nexpect || i < ngot; ++i) {
73         const char *e = i < nexpect ? wtest[t].expect[i] : "<none>";
74         const char *g = i < ngot ? got[i] : "<none>";
75         fprintf(stderr, " %2zu | %-30s | %-30s\n", i, e, g);
76       }
77       count_error();
78     }
79     ++tests;
80   }
81 }
82
83 TEST(words);
84
85 /*
86 Local Variables:
87 c-basic-offset:2
88 comment-column:40
89 fill-column:79
90 indent-tabs-mode:nil
91 End:
92 */