root/maint/gnulib/tests/unistr/test-u16-prev.c

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

DEFINITIONS

This source file includes following definitions.
  1. check
  2. check_invalid
  3. main

   1 /* Test of u16_prev() function.
   2    Copyright (C) 2010-2021 Free Software Foundation, Inc.
   3 
   4    This program is free software: you can redistribute it and/or modify
   5    it under the terms of the GNU General Public License as published by
   6    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
  12    GNU General Public License for more details.
  13 
  14    You should have received a copy of the GNU General Public License
  15    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  16 
  17 /* Written by Bruno Haible <bruno@clisp.org>, 2010.  */
  18 
  19 #include <config.h>
  20 
  21 #include "unistr.h"
  22 
  23 #include "macros.h"
  24 
  25 static int
  26 check (const uint16_t *input, size_t input_length, ucs4_t *puc)
     /* [previous][next][first][last][top][bottom][index][help] */
  27 {
  28   ucs4_t uc;
  29 
  30   /* Test recognition when at the beginning of the string.  */
  31   if (u16_prev (&uc, input + input_length, input) != input)
  32     return 1;
  33 
  34   /* Test recognition when preceded by a 1-unit character.  */
  35   {
  36     uint16_t buf[100];
  37     uint16_t *ptr;
  38     size_t i;
  39     ucs4_t uc1;
  40 
  41     ptr = buf;
  42     *ptr++ = 0x2102;
  43     for (i = 0; i < input_length; i++)
  44       ptr[i] = input[i];
  45 
  46     if (u16_prev (&uc1, ptr + input_length, buf) != ptr)
  47       return 2;
  48     if (uc1 != uc)
  49       return 3;
  50   }
  51 
  52   /* Test recognition when preceded by a 2-unit character.  */
  53   {
  54     uint16_t buf[100];
  55     uint16_t *ptr;
  56     size_t i;
  57     ucs4_t uc1;
  58 
  59     ptr = buf;
  60     *ptr++ = 0xD835;
  61     *ptr++ = 0xDD1E;
  62     for (i = 0; i < input_length; i++)
  63       ptr[i] = input[i];
  64 
  65     if (u16_prev (&uc1, ptr + input_length, buf) != ptr)
  66       return 4;
  67     if (uc1 != uc)
  68       return 5;
  69   }
  70 
  71   *puc = uc;
  72   return 0;
  73 }
  74 
  75 static int
  76 check_invalid (const uint16_t *input, size_t input_length)
     /* [previous][next][first][last][top][bottom][index][help] */
  77 {
  78   ucs4_t uc;
  79 
  80   /* Test recognition when at the beginning of the string.  */
  81   uc = 0xBADFACE;
  82   if (u16_prev (&uc, input + input_length, input) != NULL)
  83     return 1;
  84   if (uc != 0xBADFACE)
  85     return 2;
  86 
  87   /* Test recognition when preceded by a 1-unit character.  */
  88   {
  89     uint16_t buf[100];
  90     uint16_t *ptr;
  91     size_t i;
  92 
  93     ptr = buf;
  94     *ptr++ = 0x2102;
  95     for (i = 0; i < input_length; i++)
  96       ptr[i] = input[i];
  97 
  98     uc = 0xBADFACE;
  99     if (u16_prev (&uc, ptr + input_length, buf) != NULL)
 100       return 3;
 101     if (uc != 0xBADFACE)
 102       return 4;
 103   }
 104 
 105   /* Test recognition when preceded by a 2-unit character.  */
 106   {
 107     uint16_t buf[100];
 108     uint16_t *ptr;
 109     size_t i;
 110 
 111     ptr = buf;
 112     *ptr++ = 0xD835;
 113     *ptr++ = 0xDD1E;
 114     for (i = 0; i < input_length; i++)
 115       ptr[i] = input[i];
 116 
 117     uc = 0xBADFACE;
 118     if (u16_prev (&uc, ptr + input_length, buf) != NULL)
 119       return 5;
 120     if (uc != 0xBADFACE)
 121       return 6;
 122   }
 123 
 124   return 0;
 125 }
 126 
 127 int
 128 main ()
     /* [previous][next][first][last][top][bottom][index][help] */
 129 {
 130   ucs4_t uc;
 131 
 132   /* Test ISO 646 unit input.  */
 133   {
 134     ucs4_t c;
 135     uint16_t buf[1];
 136 
 137     for (c = 0; c < 0x80; c++)
 138       {
 139         buf[0] = c;
 140         uc = 0xBADFACE;
 141         ASSERT (check (buf, 1, &uc) == 0);
 142         ASSERT (uc == c);
 143       }
 144   }
 145 
 146   /* Test BMP unit input.  */
 147   {
 148     static const uint16_t input[] = { 0x20AC };
 149     uc = 0xBADFACE;
 150     ASSERT (check (input, SIZEOF (input), &uc) == 0);
 151     ASSERT (uc == 0x20AC);
 152   }
 153 
 154   /* Test 2-units character input.  */
 155   {
 156     static const uint16_t input[] = { 0xD835, 0xDD1F };
 157     uc = 0xBADFACE;
 158     ASSERT (check (input, SIZEOF (input), &uc) == 0);
 159     ASSERT (uc == 0x1D51F);
 160   }
 161 
 162   /* Test incomplete/invalid 1-unit input.  */
 163   {
 164     static const uint16_t input[] = { 0xD835 };
 165     ASSERT (check_invalid (input, SIZEOF (input)) == 0);
 166   }
 167   {
 168     static const uint16_t input[] = { 0xDD1F };
 169     ASSERT (check_invalid (input, SIZEOF (input)) == 0);
 170   }
 171 
 172   return 0;
 173 }

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