string-buffer tests: Avoid test failure on native Windows.
[gnulib.git] / tests / unigbrk / test-u16-grapheme-breaks.c
blob651496ebd998f0fa3ac4f7688ebadeded8b1ce00
1 /* Grapheme cluster breaks test.
2 Copyright (C) 2010-2024 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 <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include "macros.h"
31 static void
32 test_u16_grapheme_breaks (const char *expected, ...)
34 size_t n = strlen (expected);
35 uint16_t s[16];
36 va_list args;
37 char breaks[16];
38 size_t i;
40 ASSERT (n <= 16);
42 memset (breaks, 0xcc, n);
44 va_start (args, expected);
45 for (i = 0; i < n; i++)
47 int unit = va_arg (args, int);
48 ASSERT (unit >= 0);
49 s[i] = unit;
51 ASSERT (va_arg (args, int) == -1);
52 va_end (args);
54 u16_grapheme_breaks (s, n, breaks);
55 for (i = 0; i < n; i++)
56 if (breaks[i] != (expected[i] == '#'))
58 size_t j;
60 fprintf (stderr, "wrong grapheme breaks:\n");
62 fprintf (stderr, " input:");
63 for (j = 0; j < n; j++)
64 fprintf (stderr, " %04X", s[j]);
65 putc ('\n', stderr);
67 fprintf (stderr, "expected:");
68 for (j = 0; j < n; j++)
69 fprintf (stderr, " %d", expected[j] == '#');
70 putc ('\n', stderr);
72 fprintf (stderr, " actual:");
73 for (j = 0; j < n; j++)
74 fprintf (stderr, " %d", breaks[j]);
75 putc ('\n', stderr);
77 fflush (stderr);
78 abort ();
82 int
83 main (void)
85 /* Standalone 1-unit graphemes. */
86 test_u16_grapheme_breaks ("#", 'a', -1);
87 test_u16_grapheme_breaks ("##", 'a', 'b', -1);
88 test_u16_grapheme_breaks ("###", 'a', 'b', 'c', -1);
90 #define HIRAGANA_A 0x3042 /* あ: Hiragana letter 'a'. */
91 test_u16_grapheme_breaks ("#", HIRAGANA_A, -1);
92 test_u16_grapheme_breaks ("##", HIRAGANA_A, 'x', -1);
93 test_u16_grapheme_breaks ("##", HIRAGANA_A, HIRAGANA_A, -1);
95 /* Combining accents. */
96 #define GRAVE 0x0300 /* Combining grave accent. */
97 #define ACUTE 0x0301 /* Combining acute accent. */
98 test_u16_grapheme_breaks ("#_", 'e', ACUTE, -1);
99 test_u16_grapheme_breaks ("#__", 'e', ACUTE, GRAVE, -1);
100 test_u16_grapheme_breaks ("#_#", 'e', ACUTE, 'x', -1);
101 test_u16_grapheme_breaks ("#_#_", 'e', ACUTE, 'e', GRAVE, -1);
103 /* CR LF handling. */
104 test_u16_grapheme_breaks ("######_#",
105 'a', '\n', 'b', '\r', 'c', '\r', '\n', 'd',
106 -1);
108 /* Emoji modifier / ZWJ sequence. */
109 test_u16_grapheme_breaks ("#____",
110 0x2605, 0x0305, 0x0347, 0x200D, 0x2600,
111 -1);
113 /* Regional indicators. */
114 test_u16_grapheme_breaks ("##___#___#",
115 '.', 0xD83C, 0xDDE9, 0xD83C, 0xDDEA, 0xD83C, 0xDDEB, 0xD83C, 0xDDF7, '.',
116 -1);
118 return test_exit_status;