maint: update copyright
[gnulib.git] / tests / unigbrk / test-u16-grapheme-next.c
1 /* Next grapheme cluster length test.
2    Copyright (C) 2010-2014 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 <stdarg.h>
26 #include <stdlib.h>
27
28 #include "macros.h"
29
30 static void
31 test_u16_grapheme_next (size_t len, ...)
32 {
33   const uint16_t *next;
34   uint16_t s[16];
35   va_list args;
36   size_t n;
37
38   va_start (args, len);
39   n = 0;
40   for (;;)
41     {
42       int unit = va_arg (args, int);
43       if (unit == -1)
44         break;
45       else if (n >= sizeof s / sizeof *s)
46         abort ();
47
48       s[n++] = unit;
49     }
50   va_end (args);
51
52   next = u16_grapheme_next (s, s + n);
53   if (next != s + len)
54     {
55       size_t i;
56
57       if (next == NULL)
58         fputs ("u16_grapheme_next returned NULL", stderr);
59       else
60         fprintf (stderr, "u16_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 ();
66     }
67 }
68
69 int
70 main (void)
71 {
72   static const uint16_t s[] = { 'a', 'b', 'c' };
73
74   /* Empty string. */
75   ASSERT (u16_grapheme_next (NULL, NULL) == NULL);
76   ASSERT (u16_grapheme_next (s, s) == NULL);
77
78   /* Standalone 1-unit graphemes.  */
79   test_u16_grapheme_next (1, 'a', -1);
80   test_u16_grapheme_next (1, 'a', 'b', -1);
81   test_u16_grapheme_next (1, 'a', 'b', 'c', -1);
82
83   /* Multi-unit, single code point graphemes. */
84 #define HIRAGANA_A 0x3042       /* あ: Hiragana letter 'a'. */
85   test_u16_grapheme_next (1, HIRAGANA_A, -1);
86   test_u16_grapheme_next (1, HIRAGANA_A, 'x', -1);
87   test_u16_grapheme_next (1, HIRAGANA_A, HIRAGANA_A, -1);
88
89   /* Combining accents. */
90 #define GRAVE 0x0300            /* Combining grave accent. */
91 #define ACUTE 0x0301            /* Combining acute accent. */
92   test_u16_grapheme_next (2, 'e', ACUTE, -1);
93   test_u16_grapheme_next (3, 'e', ACUTE, GRAVE, -1);
94   test_u16_grapheme_next (2, 'e', ACUTE, 'x', -1);
95   test_u16_grapheme_next (2, 'e', ACUTE, 'e', ACUTE, -1);
96
97   /* Surrogate pairs. */
98   test_u16_grapheme_next (2, 0xd83d, 0xde10, -1); /* 😐: neutral face. */
99   test_u16_grapheme_next (3, 0xd83d, 0xde10, GRAVE, -1);
100
101   return 0;
102 }