unigbrk: New modules for grapheme clusters.
[gnulib.git] / tests / unigbrk / test-u8-grapheme-next.c
1 /* Next grapheme cluster length 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
27 #include "macros.h"
28
29 static void
30 test_u8_grapheme_next (const uint8_t *s, size_t n, size_t len)
31 {
32   const uint8_t *next = u8_grapheme_next (s, s + n);
33   if (next != s + len)
34     {
35       size_t i;
36
37       if (next == NULL)
38         fputs ("u8_grapheme_next returned NULL", stderr);
39       else
40         fprintf (stderr, "u8_grapheme_next skipped %zu bytes", next - s);
41       fprintf (stderr, ", expected %zu:\n", len);
42       for (i = 0; i < n; i++)
43         fprintf (stderr, " %02x", s[i]);
44       putc ('\n', stderr);
45       abort ();
46     }
47 }
48
49 int
50 main (void)
51 {
52   static const uint8_t s[] = "abc";
53
54   /* Empty string. */
55   ASSERT (u8_grapheme_next (NULL, NULL) == NULL);
56   ASSERT (u8_grapheme_next (s, s) == NULL);
57
58   /* Standalone 1-unit graphemes.  */
59   test_u8_grapheme_next ("a", 1, 1);
60   test_u8_grapheme_next ("ab", 2, 1);
61   test_u8_grapheme_next ("abc", 3, 1);
62
63   /* Multi-unit, single code point graphemes. */
64 #define HIRAGANA_A "\343\201\202" /* あ: Hiragana letter 'a'. */
65   test_u8_grapheme_next (HIRAGANA_A, 3, 3);
66   test_u8_grapheme_next (HIRAGANA_A"x", 4, 3);
67   test_u8_grapheme_next (HIRAGANA_A HIRAGANA_A, 6, 3);
68
69   /* Combining accents. */
70 #define GRAVE "\314\200"        /* Combining grave accent. */
71 #define ACUTE "\314\201"        /* Combining acute accent. */
72   test_u8_grapheme_next ("e"ACUTE, 3, 3);
73   test_u8_grapheme_next ("e"ACUTE GRAVE, 5, 5);
74   test_u8_grapheme_next ("e"ACUTE"x", 4, 3);
75   test_u8_grapheme_next ("e"ACUTE "e"ACUTE, 6, 3);
76
77   return 0;
78 }