c32snrtombs: Add tests.
[gnulib.git] / tests / unigbrk / test-u32-grapheme-next.c
blob4dc44de64d2b85e8b23fc92b901d32ada70572f0
1 /* Next grapheme cluster length test.
2 Copyright (C) 2010-2020 Free Software Foundation, Inc.
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.
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.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Ben Pfaff <blp@cs.stanford.edu>, 2010. */
19 #include <config.h>
21 /* Specification. */
22 #include <unigbrk.h>
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
28 #include "macros.h"
30 static void
31 test_u32_grapheme_next (size_t len, ...)
33 const uint32_t *next;
34 uint32_t s[32];
35 va_list args;
36 size_t n;
38 va_start (args, len);
39 n = 0;
40 for (;;)
42 int unit = va_arg (args, int);
43 if (unit == -1)
44 break;
45 else if (n >= sizeof s / sizeof *s)
46 abort ();
48 s[n++] = unit;
50 va_end (args);
52 next = u32_grapheme_next (s, s + n);
53 if (next != s + len)
55 size_t i;
57 if (next == NULL)
58 fputs ("u32_grapheme_next returned NULL", stderr);
59 else
60 fprintf (stderr, "u32_grapheme_next skipped %zu units", next - s);
61 fprintf (stderr, ", expected %zu:\n", len);
62 for (i = 0; i < n; i++)
63 fprintf (stderr, " %04x", s[i]);
64 putc ('\n', stderr);
65 abort ();
69 int
70 main (void)
72 static const uint32_t s[] = { 'a', 'b', 'c' };
74 /* Empty string. */
75 ASSERT (u32_grapheme_next (NULL, NULL) == NULL);
76 ASSERT (u32_grapheme_next (s, s) == NULL);
78 /* Standalone 1-unit graphemes. */
79 test_u32_grapheme_next (1, 'a', -1);
80 test_u32_grapheme_next (1, 'a', 'b', -1);
81 test_u32_grapheme_next (1, 'a', 'b', 'c', -1);
83 /* Multi-unit, single code point graphemes. */
84 #define HIRAGANA_A 0x3042 /* あ: Hiragana letter 'a'. */
85 test_u32_grapheme_next (1, HIRAGANA_A, -1);
86 test_u32_grapheme_next (1, HIRAGANA_A, 'x', -1);
87 test_u32_grapheme_next (1, HIRAGANA_A, HIRAGANA_A, -1);
89 /* Combining accents. */
90 #define GRAVE 0x0300 /* Combining grave accent. */
91 #define ACUTE 0x0301 /* Combining acute accent. */
92 test_u32_grapheme_next (2, 'e', ACUTE, -1);
93 test_u32_grapheme_next (3, 'e', ACUTE, GRAVE, -1);
94 test_u32_grapheme_next (2, 'e', ACUTE, 'x', -1);
95 test_u32_grapheme_next (2, 'e', ACUTE, 'e', ACUTE, -1);
97 /* Outside BMP. */
98 #define NEUTRAL_FACE 0x1f610 /* 😐: neutral face. */
99 test_u32_grapheme_next (1, NEUTRAL_FACE, -1);
100 test_u32_grapheme_next (2, NEUTRAL_FACE, GRAVE, -1);
102 return 0;