root/maint/gnulib/tests/unistr/test-u8-strchr.c

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

DEFINITIONS

This source file includes following definitions.
  1. main

   1 /* Test of u8_strchr() 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 Paolo Bonzini <bonzini@gnu.org>, 2010.  */
  18 
  19 #include <config.h>
  20 
  21 #include "unistr.h"
  22 
  23 #include <stdlib.h>
  24 #include <string.h>
  25 
  26 #include "zerosize-ptr.h"
  27 #include "macros.h"
  28 
  29 #define UNIT uint8_t
  30 #define U_UCTOMB u8_uctomb
  31 #define U32_TO_U u32_to_u8
  32 #define U_STRCHR u8_strchr
  33 #define U_SET u8_set
  34 #include "test-strchr.h"
  35 
  36 int
  37 main (void)
     /* [previous][next][first][last][top][bottom][index][help] */
  38 {
  39   test_strchr ();
  40 
  41   /* Check that u8_strchr() does not read past the end of the string.  */
  42   {
  43     char *page_boundary = (char *) zerosize_ptr ();
  44 
  45     if (page_boundary != NULL)
  46       {
  47         UNIT *mem;
  48 
  49         mem = (UNIT *) (page_boundary - 1 * sizeof (UNIT));
  50         mem[0] = 0;
  51         ASSERT (u8_strchr (mem, 0x55) == NULL);
  52         ASSERT (u8_strchr (mem, 0x123) == NULL);
  53         ASSERT (u8_strchr (mem, 0x3456) == NULL);
  54         ASSERT (u8_strchr (mem, 0x23456) == NULL);
  55 
  56         mem = (UNIT *) (page_boundary - 2 * sizeof (UNIT));
  57         mem[0] = 0x50;
  58         mem[1] = 0;
  59         ASSERT (u8_strchr (mem, 0x55) == NULL);
  60         ASSERT (u8_strchr (mem, 0x123) == NULL);
  61         ASSERT (u8_strchr (mem, 0x3456) == NULL);
  62         ASSERT (u8_strchr (mem, 0x23456) == NULL);
  63 
  64         mem = (UNIT *) (page_boundary - 3 * sizeof (UNIT));
  65         mem[0] = 0x50;
  66         mem[1] = 0x50;
  67         mem[2] = 0;
  68         ASSERT (u8_strchr (mem, 0x55) == NULL);
  69         ASSERT (u8_strchr (mem, 0x123) == NULL);
  70         ASSERT (u8_strchr (mem, 0x3456) == NULL);
  71         ASSERT (u8_strchr (mem, 0x23456) == NULL);
  72 
  73         mem = (UNIT *) (page_boundary - 3 * sizeof (UNIT));
  74         mem[0] = 0xC4; mem[1] = 0xA0; /* U+0120 */
  75         mem[2] = 0;
  76         ASSERT (u8_strchr (mem, 0x55) == NULL);
  77         ASSERT (u8_strchr (mem, 0x123) == NULL);
  78         ASSERT (u8_strchr (mem, 0x3456) == NULL);
  79         ASSERT (u8_strchr (mem, 0x23456) == NULL);
  80 
  81         mem = (UNIT *) (page_boundary - 3 * sizeof (UNIT));
  82         mem[0] = 0xC5; mem[1] = 0xA3; /* U+0163 */
  83         mem[2] = 0;
  84         ASSERT (u8_strchr (mem, 0x55) == NULL);
  85         ASSERT (u8_strchr (mem, 0x123) == NULL);
  86         ASSERT (u8_strchr (mem, 0x3456) == NULL);
  87         ASSERT (u8_strchr (mem, 0x23456) == NULL);
  88 
  89         mem = (UNIT *) (page_boundary - 4 * sizeof (UNIT));
  90         mem[0] = 0x50;
  91         mem[1] = 0x50;
  92         mem[2] = 0x50;
  93         mem[3] = 0;
  94         ASSERT (u8_strchr (mem, 0x55) == NULL);
  95         ASSERT (u8_strchr (mem, 0x123) == NULL);
  96         ASSERT (u8_strchr (mem, 0x3456) == NULL);
  97         ASSERT (u8_strchr (mem, 0x23456) == NULL);
  98 
  99         mem = (UNIT *) (page_boundary - 4 * sizeof (UNIT));
 100         mem[0] = 0x50;
 101         mem[1] = 0xC5; mem[2] = 0xA3; /* U+0163 */
 102         mem[3] = 0;
 103         ASSERT (u8_strchr (mem, 0x55) == NULL);
 104         ASSERT (u8_strchr (mem, 0x123) == NULL);
 105         ASSERT (u8_strchr (mem, 0x3456) == NULL);
 106         ASSERT (u8_strchr (mem, 0x23456) == NULL);
 107         ASSERT (u8_strchr (mem, 0x163) == mem + 1);
 108 
 109         mem = (UNIT *) (page_boundary - 4 * sizeof (UNIT));
 110         mem[0] = 0xE3; mem[1] = 0x91; mem[2] = 0x00; /* U+3450 */
 111         mem[3] = 0;
 112         ASSERT (u8_strchr (mem, 0x55) == NULL);
 113         ASSERT (u8_strchr (mem, 0x123) == NULL);
 114         ASSERT (u8_strchr (mem, 0x3456) == NULL);
 115         ASSERT (u8_strchr (mem, 0x23456) == NULL);
 116 
 117         mem = (UNIT *) (page_boundary - 4 * sizeof (UNIT));
 118         mem[0] = 0xE3; mem[1] = 0x92; mem[2] = 0x96; /* U+3496 */
 119         mem[3] = 0;
 120         ASSERT (u8_strchr (mem, 0x55) == NULL);
 121         ASSERT (u8_strchr (mem, 0x123) == NULL);
 122         ASSERT (u8_strchr (mem, 0x3456) == NULL);
 123         ASSERT (u8_strchr (mem, 0x23456) == NULL);
 124 
 125         mem = (UNIT *) (page_boundary - 5 * sizeof (UNIT));
 126         mem[0] = 0x50;
 127         mem[1] = 0x50;
 128         mem[2] = 0x50;
 129         mem[3] = 0x50;
 130         mem[4] = 0;
 131         ASSERT (u8_strchr (mem, 0x55) == NULL);
 132         ASSERT (u8_strchr (mem, 0x123) == NULL);
 133         ASSERT (u8_strchr (mem, 0x3456) == NULL);
 134         ASSERT (u8_strchr (mem, 0x23456) == NULL);
 135 
 136         mem = (UNIT *) (page_boundary - 5 * sizeof (UNIT));
 137         mem[0] = 0x50;
 138         mem[1] = 0xE3; mem[2] = 0x92; mem[3] = 0x96; /* U+3496 */
 139         mem[4] = 0;
 140         ASSERT (u8_strchr (mem, 0x55) == NULL);
 141         ASSERT (u8_strchr (mem, 0x123) == NULL);
 142         ASSERT (u8_strchr (mem, 0x3456) == NULL);
 143         ASSERT (u8_strchr (mem, 0x23456) == NULL);
 144         ASSERT (u8_strchr (mem, 0x3496) == mem + 1);
 145       }
 146   }
 147 
 148   return 0;
 149 }

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