Tests for module 'uniwbrk/u8-wordbreaks'.
[gnulib.git] / tests / uniwbrk / test-u8-wordbreaks.c
1 /* Test of word breaks in UTF-8 strings.
2    Copyright (C) 2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    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
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2009.  */
18
19 #include <config.h>
20
21 #include "uniwbrk.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 #define SIZEOF(array) (sizeof (array) / sizeof (array[0]))
27 #define ASSERT(expr) \
28   do                                                                         \
29     {                                                                        \
30       if (!(expr))                                                           \
31         {                                                                    \
32           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
33           fflush (stderr);                                                   \
34           abort ();                                                          \
35         }                                                                    \
36     }                                                                        \
37   while (0)
38
39 int
40 main ()
41 {
42   /* Test case n = 0.  */
43   u8_wordbreaks (NULL, 0, NULL);
44
45   {
46     static const uint8_t input[91] =
47       /* "Grüß Gott. Здравствуйте! x=(-b±sqrt(b²-4ac))/(2a)  日本語,中文,한글" */
48       "Gr\303\274\303\237 Gott. \320\227\320\264\321\200\320\260\320\262\321\201\321\202\320\262\321\203\320\271\321\202\320\265! x=(-b\302\261sqrt(b\302\262-4ac))/(2a)  \346\227\245\346\234\254\350\252\236,\344\270\255\346\226\207,\355\225\234\352\270\200\n";
49     char *p = (char *) malloc (SIZEOF (input));
50     size_t i;
51
52     u8_wordbreaks (input, SIZEOF (input), p);
53
54     for (i = 0; i < 91; i++)
55       {
56         ASSERT (p[i] == ((i >= 6 && i <= 7)
57                          || (i >= 11 && i <= 13)
58                          || (i >= 37 && i <= 44)
59                          || i == 46 || (i >= 50 && i <= 52)
60                          || (i >= 54 && i <= 55)
61                          || (i >= 58 && i <= 62) || (i >= 64 && i <= 67)
62                          || i == 70 || i == 73 || i == 76
63                          || i == 77 || i == 80 || i == 83
64                          || i == 84 || i == 90
65                          ? 1 : 0));
66       }
67     free (p);
68   }
69
70   {
71     /* Same input string, decomposed.  */
72     static const uint8_t input[106] =
73       /* "Grüß Gott. Здравствуйте! x=(-b±sqrt(b²-4ac))/(2a)  日本語,中文,한글" */
74       "Gru\314\210\303\237 Gott. \320\227\320\264\321\200\320\260\320\262\321\201\321\202\320\262\321\203\320\270\314\206\321\202\320\265! x=(-b\302\261sqrt(b\302\262-4ac))/(2a)  \346\227\245\346\234\254\350\252\236,\344\270\255\346\226\207,\341\204\222\341\205\241\341\206\253\341\204\200\341\205\263\341\206\257\n";
75     char *p = (char *) malloc (SIZEOF (input));
76     size_t i;
77
78     u8_wordbreaks (input, SIZEOF (input), p);
79
80     for (i = 0; i < 106; i++)
81       {
82         ASSERT (p[i] == ((i >= 7 && i <= 8)
83                          || (i >= 12 && i <= 14)
84                          || (i >= 40 && i <= 47)
85                          || i == 49 || (i >= 53 && i <= 55)
86                          || (i >= 57 && i <= 58)
87                          || (i >= 61 && i <= 65) || (i >= 67 && i <= 70)
88                          || i == 73 || i == 76 || i == 79
89                          || i == 80 || i == 83 || i == 86
90                          || i == 87 || i == 105
91                          ? 1 : 0));
92       }
93     free (p);
94   }
95
96   return 0;
97 }