errno-h: document Haiku errors can’t be -1
[gnulib.git] / tests / uniwbrk / test-uc-wordbreaks.c
blob11f4f201a548c7e8d43ecc9ecb7429a8676c4556
1 /* Word break function test, using test data from UCD.
2 Copyright (C) 2010-2025 Free Software Foundation, Inc.
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.
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.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Daiki Ueno <ueno@gnu.org>, 2014.
19 Largely based on unigbrk/test-uc-is-grapheme-break.c,
20 written by Ben Pfaff <blp@cs.stanford.edu>, 2010. */
22 #include <config.h>
24 /* Specification. */
25 #include <uniwbrk.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
31 const char *
32 wordbreakproperty_to_string (int wbp)
34 switch (wbp)
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 CASE(ZWJ)
55 CASE(EB)
56 CASE(EM)
57 CASE(GAZ)
58 CASE(EBG)
59 CASE(WSS)
61 abort ();
64 int
65 main (int argc, char *argv[])
67 const char *filename;
68 FILE *stream;
69 int exit_code;
70 int lineno;
71 char line[4096];
73 if (argc != 2)
75 fprintf (stderr, "usage: %s FILENAME\n"
76 "where FILENAME is the location of the WordBreakTest.txt\n"
77 "test file.\n", argv[0]);
78 exit (1);
81 filename = argv[1];
82 stream = fopen (filename, "r");
83 if (stream == NULL)
85 fprintf (stderr, "error during fopen of '%s'\n", filename);
86 exit (1);
89 exit_code = 0;
90 lineno = 0;
91 while (fgets (line, sizeof (line), stream))
93 lineno++;
95 /* Cut off the trailing comment, if any. */
96 char *comment = strchr (line, '#');
97 if (comment != NULL)
98 *comment = '\0';
99 /* Is the remaining line blank? */
100 if (line[strspn (line, " \t\r\n")] == '\0')
101 continue;
103 const char *p;
104 uint32_t input[100];
105 char breaks[100];
106 char breaks_expected[101];
107 int i;
109 i = 0;
110 p = line;
113 p += strspn (p, " \t\r\n");
114 if (!strncmp (p, "\303\267" /* ÷ */, 2))
116 breaks_expected[i] = 1;
117 p += 2;
119 else if (!strncmp (p, "\303\227" /* × */, 2))
121 breaks_expected[i] = 0;
122 p += 2;
124 else
126 fprintf (stderr, "%s:%d.%d: syntax error expecting '÷' or '×'\n",
127 filename, lineno, (int) (p - line + 1));
128 exit (1);
131 p += strspn (p, " \t\r\n");
132 if (*p != '\0')
134 unsigned int next_int;
135 int n;
137 if (sscanf (p, "%x%n", &next_int, &n) != 1)
139 fprintf (stderr, "%s:%d.%d: syntax error at '%s' "
140 "expecting hexadecimal Unicode code point number\n",
141 filename, lineno, (int) (p - line + 1), p);
142 exit (1);
144 p += n;
146 input[i] = next_int;
149 p += strspn (p, " \t\r\n");
150 i++;
152 while (*p != '\0');
154 u32_wordbreaks (input, i - 1, breaks);
156 /* u32_wordbreaks always set BREAKS[0] to 0. */
157 breaks[0] = breaks_expected[0] = 1;
158 if (memcmp (breaks, breaks_expected, i - 1) != 0)
160 int j;
162 fprintf (stderr, "%s:%d: expected: ", filename, lineno);
163 for (j = 0; j < i - 1; j++)
165 int input_wbp = uc_wordbreak_property (input[j]);
166 fprintf (stderr, "%s U+%04X (%s) ",
167 breaks_expected[j] == 1 ? "\303\267" : "\303\227",
168 input[j], wordbreakproperty_to_string (input_wbp));
170 fprintf (stderr, "\n");
171 fprintf (stderr, "%s:%d: actual: ", filename, lineno);
172 for (j = 0; j < i - 1; j++)
174 int input_wbp = uc_wordbreak_property (input[j]);
175 fprintf (stderr, "%s U+%04X (%s) ",
176 breaks[j] == 1 ? "\303\267" : "\303\227",
177 input[j], wordbreakproperty_to_string (input_wbp));
179 fprintf (stderr, "\n");
180 exit_code = 1;
184 return exit_code;