root/maint/gnulib/tests/unigbrk/test-u32-grapheme-next.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. test_u32_grapheme_next
  2. main

   1 /* Next grapheme cluster length test.
   2    Copyright (C) 2010-2021 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 <https://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_u32_grapheme_next (size_t len, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
  32 {
  33   const uint32_t *next;
  34   uint32_t s[32];
  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 = u32_grapheme_next (s, s + n);
  53   if (next != s + len)
  54     {
  55       size_t i;
  56 
  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 ();
  66     }
  67 }
  68 
  69 int
  70 main (void)
     /* [previous][next][first][last][top][bottom][index][help] */
  71 {
  72   static const uint32_t s[] = { 'a', 'b', 'c' };
  73 
  74   /* Empty string. */
  75   ASSERT (u32_grapheme_next (NULL, NULL) == NULL);
  76   ASSERT (u32_grapheme_next (s, s) == NULL);
  77 
  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);
  82 
  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);
  88 
  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);
  96 
  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);
 101 
 102   return 0;
 103 }

/* [previous][next][first][last][top][bottom][index][help] */