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