root/maint/gnulib/tests/unistr/test-strncpy.h

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

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. check_single
  2. check

   1 /* Test of uN_strncpy() functions.
   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 static void
  20 check_single (const UNIT *input, size_t length, size_t n)
     /* [previous][next][first][last][top][bottom][index][help] */
  21 {
  22   UNIT *dest;
  23   UNIT *result;
  24   size_t i;
  25 
  26   dest = (UNIT *) malloc ((1 + n + 1) * sizeof (UNIT));
  27   ASSERT (dest != NULL);
  28 
  29   for (i = 0; i < 1 + n + 1; i++)
  30     dest[i] = MAGIC;
  31 
  32   result = U_STRNCPY (dest + 1, input, n);
  33   ASSERT (result == dest + 1);
  34 
  35   ASSERT (dest[0] == MAGIC);
  36   for (i = 0; i < (n <= length ? n : length + 1); i++)
  37     ASSERT (dest[1 + i] == input[i]);
  38   for (; i < n; i++)
  39     ASSERT (dest[1 + i] == 0);
  40   ASSERT (dest[1 + n] == MAGIC);
  41 
  42   free (dest);
  43 }
  44 
  45 static void
  46 check (const UNIT *input, size_t input_length)
     /* [previous][next][first][last][top][bottom][index][help] */
  47 {
  48   size_t length;
  49   size_t n;
  50 
  51   ASSERT (input_length > 0);
  52   ASSERT (input[input_length - 1] == 0);
  53   length = input_length - 1; /* = U_STRLEN (input) */
  54 
  55   for (n = 0; n <= 2 * length + 2; n++)
  56     check_single (input, length, n);
  57 
  58   /* Check that U_STRNCPY (D, S, N) does not look at more than
  59      MIN (U_STRLEN (S) + 1, N) units.  */
  60   {
  61     char *page_boundary = (char *) zerosize_ptr ();
  62 
  63     if (page_boundary != NULL)
  64       {
  65         for (n = 0; n <= 2 * length + 2; n++)
  66           {
  67             size_t n_to_copy = (n <= length ? n : length + 1);
  68             UNIT *copy;
  69             size_t i;
  70 
  71             copy = (UNIT *) page_boundary - n_to_copy;
  72             for (i = 0; i < n_to_copy; i++)
  73               copy[i] = input[i];
  74 
  75             check_single (copy, length, n);
  76           }
  77       }
  78   }
  79 }

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