1397a5879180eb33fa0a3025d0aa70e0df16fa11
[gnulib.git] / tests / unigbrk / test-u8-grapheme-breaks.c
1 /* Grapheme cluster breaks test.
2    Copyright (C) 2010-2012 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 #include "macros.h"
29
30 static void
31 test_u8_grapheme_breaks (const char *input, const char *expected)
32 {
33   const uint8_t *s = (const uint8_t *) input;
34   size_t n = strlen (expected);
35   char *breaks;
36   size_t i;
37
38   breaks = malloc (n);
39   if (!breaks)
40     abort ();
41   memset (breaks, 0xcc, n);
42
43   u8_grapheme_breaks (s, n, breaks);
44   for (i = 0; i < n; i++)
45     if (breaks[i] != (expected[i] == '#'))
46       {
47         size_t j;
48
49         fprintf (stderr, "wrong grapheme breaks:\n");
50
51         fprintf (stderr, "   input:");
52         for (j = 0; j < n; j++)
53           fprintf (stderr, " %02x", s[j]);
54         putc ('\n', stderr);
55
56         fprintf (stderr, "expected:");
57         for (j = 0; j < n; j++)
58           fprintf (stderr, "  %d", expected[j] == '#');
59         putc ('\n', stderr);
60
61         fprintf (stderr, "  actual:");
62         for (j = 0; j < n; j++)
63           fprintf (stderr, "  %d", breaks[j]);
64         putc ('\n', stderr);
65
66         abort ();
67       }
68
69   free (breaks);
70 }
71
72 int
73 main (void)
74 {
75   /* Standalone 1-unit graphemes.  */
76   test_u8_grapheme_breaks ("a", "#");
77   test_u8_grapheme_breaks ("ab", "##");
78   test_u8_grapheme_breaks ("abc", "###");
79
80   /* Multi-unit, single code point graphemes. */
81 #define HIRAGANA_A "\343\201\202" /* あ: Hiragana letter 'a'. */
82   test_u8_grapheme_breaks (HIRAGANA_A, "#__");
83   test_u8_grapheme_breaks (HIRAGANA_A"x", "#__#");
84   test_u8_grapheme_breaks (HIRAGANA_A HIRAGANA_A, "#__#__");
85
86   /* Combining accents. */
87 #define GRAVE "\314\200"        /* Combining grave accent. */
88 #define ACUTE "\314\201"        /* Combining acute accent. */
89   test_u8_grapheme_breaks ("e"ACUTE, "#__");
90   test_u8_grapheme_breaks ("e"ACUTE GRAVE, "#____");
91   test_u8_grapheme_breaks ("e"ACUTE"x", "#__#");
92   test_u8_grapheme_breaks ("e"ACUTE "e"ACUTE, "#__#__");
93
94   return 0;
95 }