maint: update copyright
[gnulib.git] / tests / unigbrk / test-uc-is-grapheme-break.c
1 /* Grapheme cluster break function test.
2    Copyright (C) 2010-2014 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 Ben Pfaff <blp@cs.stanford.edu>, 2010. */
18
19 #include <config.h>
20
21 /* Specification. */
22 #include <unigbrk.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 const char *
29 graphemebreakproperty_to_string (int gbp)
30 {
31   printf ("%d\n", gbp);
32   switch (gbp)
33     {
34 #define CASE(VALUE) case GBP_##VALUE: return #VALUE;
35       CASE(OTHER)
36       CASE(CR)
37       CASE(LF)
38       CASE(CONTROL)
39       CASE(EXTEND)
40       CASE(PREPEND)
41       CASE(SPACINGMARK)
42       CASE(L)
43       CASE(V)
44       CASE(T)
45       CASE(LV)
46       CASE(LVT)
47     }
48   abort ();
49 }
50
51 int
52 main (int argc, char *argv[])
53 {
54   const char *filename;
55   char line[1024];
56   int exit_code;
57   FILE *stream;
58   int lineno;
59
60   if (argc != 2)
61     {
62       fprintf (stderr, "usage: %s FILENAME\n"
63                "where FILENAME is the location of the GraphemeBreakTest.txt\n"
64                "test file.\n", argv[0]);
65       exit (1);
66     }
67
68   filename = argv[1];
69   stream = fopen (filename, "r");
70   if (stream == NULL)
71     {
72       fprintf (stderr, "error during fopen of '%s'\n", filename);
73       exit (1);
74     }
75
76   exit_code = 0;
77   lineno = 0;
78   while (fgets (line, sizeof line, stream))
79     {
80       char *comment;
81       const char *p;
82       ucs4_t prev;
83
84       lineno++;
85
86       comment = strchr (line, '#');
87       if (comment != NULL)
88         *comment = '\0';
89       if (line[strspn (line, " \t\r\n")] == '\0')
90         continue;
91
92       prev = 0;
93       p = line;
94       do
95         {
96           bool should_break;
97           ucs4_t next;
98
99           p += strspn (p, " \t\r\n");
100           if (!strncmp (p, "\303\267" /* ÷ */, 2))
101             {
102               should_break = true;
103               p += 2;
104             }
105           else if (!strncmp (p, "\303\227" /* × */, 2))
106             {
107               should_break = false;
108               p += 2;
109             }
110           else
111             {
112               fprintf (stderr, "%s:%d.%d: syntax error expecting '÷' or '×'\n",
113                        filename, lineno, (int) (p - line + 1));
114               exit (1);
115             }
116
117           p += strspn (p, " \t\r\n");
118           if (*p == '\0')
119             next = 0;
120           else
121             {
122               unsigned int next_int;
123               int n;
124
125               if (sscanf (p, "%x%n", &next_int, &n) != 1)
126                 {
127                   fprintf (stderr, "%s:%d.%d: syntax error at '%s' "
128                            "expecting hexadecimal Unicode code point number\n",
129                            filename, lineno, (int) (p - line + 1), p);
130                   exit (1);
131                 }
132               p += n;
133
134               next = next_int;
135             }
136
137           if (uc_is_grapheme_break (prev, next) != should_break)
138             {
139               int prev_gbp = uc_graphemeclusterbreak_property (prev);
140               int next_gbp = uc_graphemeclusterbreak_property (next);
141               fprintf (stderr, "%s:%d: should %s U+%04X (%s) and "
142                        "U+%04X (%s)\n",
143                        filename, lineno,
144                        should_break ? "break" : "join",
145                        prev, graphemebreakproperty_to_string (prev_gbp),
146                        next, graphemebreakproperty_to_string (next_gbp));
147               exit_code = 1;
148             }
149
150           p += strspn (p, " \t\r\n");
151           prev = next;
152         }
153       while (*p != '\0');
154     }
155
156   return exit_code;
157 }