unigbrk.in.h: Fix typo: "ben" => "been".
[gnulib.git] / tests / unigbrk / test-u16-grapheme-len.c
1 /* 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 <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 static void
29 test_u16_grapheme_len (size_t len, ...)
30 {
31   uint16_t s[16];
32   size_t retval;
33   va_list args;
34   size_t n;
35
36   va_start (args, len);
37   n = 0;
38   for (;;)
39     {
40       int unit = va_arg (args, int);
41       if (unit == -1)
42         break;
43       else if (n >= sizeof s / sizeof *s)
44         abort ();
45
46       s[n++] = unit;
47     }
48   va_end (args);
49
50   retval = u16_grapheme_len (s, n);
51   if (retval != len)
52     {
53       size_t i;
54
55       fprintf (stderr, "u16_grapheme_len counted %zu units, expected %zu:",
56                retval, len);
57       for (i = 0; i < n; i++)
58         fprintf (stderr, " %04x", s[i]);
59       putc ('\n', stderr);
60       abort ();
61     }
62 }
63
64
65 int
66 main (void)
67 {
68   /* Empty string. */
69   test_u16_grapheme_len (0, -1);
70
71   /* Standalone 1-unit graphemes.  */
72   test_u16_grapheme_len (1, 'a', -1);
73   test_u16_grapheme_len (1, 'a', 'b', -1);
74   test_u16_grapheme_len (1, 'a', 'b', 'c', -1);
75
76   /* Multi-unit, single code point graphemes. */
77 #define HIRAGANA_A 0x3042       /* あ: Hiragana letter 'a'. */
78   test_u16_grapheme_len (1, HIRAGANA_A, -1);
79   test_u16_grapheme_len (1, HIRAGANA_A, 'x', -1);
80   test_u16_grapheme_len (1, HIRAGANA_A, HIRAGANA_A, -1);
81
82   /* Combining accents. */
83 #define GRAVE 0x0300            /* Combining grave accent. */
84 #define ACUTE 0x0301            /* Combining acute accent. */
85   test_u16_grapheme_len (2, 'e', ACUTE, -1);
86   test_u16_grapheme_len (3, 'e', ACUTE, GRAVE, -1);
87   test_u16_grapheme_len (2, 'e', ACUTE, 'x', -1);
88   test_u16_grapheme_len (2, 'e', ACUTE, 'e', ACUTE, -1);
89
90   /* Surrogate pairs. */
91   test_u16_grapheme_len (2, 0xd83d, 0xde10, -1); /* 😐: neutral face. */
92   test_u16_grapheme_len (3, 0xd83d, 0xde10, GRAVE, -1);
93
94   return 0;
95 }