Bump to 1.14.1
[platform/upstream/augeas.git] / tests / uniwbrk / test-uc-wordbreaks.c
1 /* Word break function test, using test data from UCD.
2    Copyright (C) 2010-2016 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify it
5    under the terms of the GNU Lesser General Public License as published
6    by the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Daiki Ueno <ueno@gnu.org>, 2014.
18
19    Largely based on unigbrk/test-uc-is-grapheme-break.c,
20    written by Ben Pfaff <blp@cs.stanford.edu>, 2010.  */
21
22 #include <config.h>
23
24 /* Specification. */
25 #include <uniwbrk.h>
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 const char *
32 wordbreakproperty_to_string (int wbp)
33 {
34   switch (wbp)
35     {
36 #define CASE(VALUE) case WBP_##VALUE: return #VALUE;
37       CASE(OTHER)
38       CASE(CR)
39       CASE(LF)
40       CASE(NEWLINE)
41       CASE(EXTEND)
42       CASE(FORMAT)
43       CASE(KATAKANA)
44       CASE(ALETTER)
45       CASE(MIDNUMLET)
46       CASE(MIDLETTER)
47       CASE(MIDNUM)
48       CASE(NUMERIC)
49       CASE(EXTENDNUMLET)
50       CASE(RI)
51       CASE(DQ)
52       CASE(SQ)
53       CASE(HL)
54     }
55   abort ();
56 }
57
58 int
59 main (int argc, char *argv[])
60 {
61   const char *filename;
62   char line[4096];
63   int exit_code;
64   FILE *stream;
65   int lineno;
66
67   if (argc != 2)
68     {
69       fprintf (stderr, "usage: %s FILENAME\n"
70                "where FILENAME is the location of the WordBreakTest.txt\n"
71                "test file.\n", argv[0]);
72       exit (1);
73     }
74
75   filename = argv[1];
76   stream = fopen (filename, "r");
77   if (stream == NULL)
78     {
79       fprintf (stderr, "error during fopen of '%s'\n", filename);
80       exit (1);
81     }
82
83   exit_code = 0;
84   lineno = 0;
85   while (fgets (line, sizeof line, stream))
86     {
87       char *comment;
88       const char *p;
89       uint32_t input[100];
90       char breaks[101];
91       char breaks_expected[101];
92       int i;
93
94       lineno++;
95
96       memset (breaks, 0, sizeof (breaks));
97       memset (breaks_expected, 0, sizeof (breaks_expected));
98
99       comment = strchr (line, '#');
100       if (comment != NULL)
101         *comment = '\0';
102       if (line[strspn (line, " \t\r\n")] == '\0')
103         continue;
104
105       i = 0;
106       p = line;
107       do
108         {
109           p += strspn (p, " \t\r\n");
110           if (!strncmp (p, "\303\267" /* ÷ */, 2))
111             {
112               breaks_expected[i] = 1;
113               p += 2;
114             }
115           else if (!strncmp (p, "\303\227" /* × */, 2))
116             {
117               breaks_expected[i] = 0;
118               p += 2;
119             }
120           else
121             {
122               fprintf (stderr, "%s:%d.%d: syntax error expecting '÷' or '×'\n",
123                        filename, lineno, (int) (p - line + 1));
124               exit (1);
125             }
126
127           p += strspn (p, " \t\r\n");
128           if (*p != '\0')
129             {
130               unsigned int next_int;
131               int n;
132
133               if (sscanf (p, "%x%n", &next_int, &n) != 1)
134                 {
135                   fprintf (stderr, "%s:%d.%d: syntax error at '%s' "
136                            "expecting hexadecimal Unicode code point number\n",
137                            filename, lineno, (int) (p - line + 1), p);
138                   exit (1);
139                 }
140               p += n;
141
142               input[i] = next_int;
143             }
144
145           p += strspn (p, " \t\r\n");
146           i++;
147         }
148       while (*p != '\0');
149
150       u32_wordbreaks (input, i - 1, breaks);
151
152       /* u32_wordbreaks always set BREAKS[0] to 0.  */
153       breaks[0] = breaks_expected[0] = 1;
154       if (memcmp (breaks, breaks_expected, i - 1) != 0)
155         {
156           int j;
157
158           fprintf (stderr, "%s:%d: expected: ", filename, lineno);
159           for (j = 0; j < i - 1; j++)
160             {
161               int input_wbp = uc_wordbreak_property (input[j]);
162               fprintf (stderr, "%s U+%04X (%s) ",
163                        breaks_expected[j] == 1 ? "\303\267" : "\303\227",
164                        input[j], wordbreakproperty_to_string (input_wbp));
165             }
166           fprintf (stderr, "\n");
167           fprintf (stderr, "%s:%d: actual: ", filename, lineno);
168           for (j = 0; j < i - 1; j++)
169             {
170               int input_wbp = uc_wordbreak_property (input[j]);
171               fprintf (stderr, "%s U+%04X (%s) ",
172                        breaks[j] == 1 ? "\303\267" : "\303\227",
173                        input[j], wordbreakproperty_to_string (input_wbp));
174             }
175           fprintf (stderr, "\n");
176           exit_code = 1;
177         }
178     }
179
180   return exit_code;
181 }