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

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

DEFINITIONS

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

   1 /* Previous grapheme cluster 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_prev (size_t len, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
  32 {
  33   const uint32_t *prev;
  34   const uint32_t *end;
  35   uint32_t s[16];
  36   va_list args;
  37   size_t n;
  38 
  39   va_start (args, len);
  40   n = 0;
  41   for (;;)
  42     {
  43       int unit = va_arg (args, int);
  44       if (unit == -1)
  45         break;
  46       else if (n >= sizeof s / sizeof *s)
  47         abort ();
  48 
  49       s[n++] = unit;
  50     }
  51   va_end (args);
  52 
  53   end = s + n;
  54   prev = u32_grapheme_prev (end, s);
  55   if (prev != end - len)
  56     {
  57       size_t i;
  58 
  59       if (prev == NULL)
  60         fputs ("u32_grapheme_prev returned NULL", stderr);
  61       else
  62         fprintf (stderr, "u32_grapheme_prev skipped %zu units", end - prev);
  63       fprintf (stderr, ", expected %zu:\n", len);
  64       for (i = 0; i < n; i++)
  65         fprintf (stderr, " %04x", s[i]);
  66       putc ('\n', stderr);
  67       abort ();
  68     }
  69 }
  70 
  71 int
  72 main (void)
     /* [previous][next][first][last][top][bottom][index][help] */
  73 {
  74   static const uint32_t s[] = { 'a', 'b', 'c' };
  75 
  76   /* Empty string. */
  77   ASSERT (u32_grapheme_prev (NULL, NULL) == NULL);
  78   ASSERT (u32_grapheme_prev (s, s) == NULL);
  79 
  80   /* Standalone 1-unit graphemes.  */
  81   test_u32_grapheme_prev (1, 'a', -1);
  82   test_u32_grapheme_prev (1, 'a', 'b', -1);
  83   test_u32_grapheme_prev (1, 'a', 'b', 'c', -1);
  84 
  85   /* Multi-unit, single code point graphemes. */
  86 #define HIRAGANA_A 0x3042       /* あ: Hiragana letter 'a'. */
  87   test_u32_grapheme_prev (1, HIRAGANA_A, -1);
  88   test_u32_grapheme_prev (1, HIRAGANA_A, 'x', -1);
  89   test_u32_grapheme_prev (1, HIRAGANA_A, HIRAGANA_A, -1);
  90 
  91   /* Combining accents. */
  92 #define GRAVE 0x0300            /* Combining grave accent. */
  93 #define ACUTE 0x0301            /* Combining acute accent. */
  94   test_u32_grapheme_prev (2, 'e', ACUTE, -1);
  95   test_u32_grapheme_prev (3, 'e', ACUTE, GRAVE, -1);
  96   test_u32_grapheme_prev (1, 'e', ACUTE, 'x', -1);
  97   test_u32_grapheme_prev (2, 'e', ACUTE, 'e', ACUTE, -1);
  98 
  99   /* Outside BMP. */
 100 #define NEUTRAL_FACE 0x1f610    /* 😐: neutral face. */
 101   test_u32_grapheme_prev (1, NEUTRAL_FACE, -1);
 102   test_u32_grapheme_prev (2, NEUTRAL_FACE, GRAVE, -1);
 103 
 104   return 0;
 105 }

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