malloca: Silence a warning from clang's memory sanitizer.
[gnulib.git] / tests / unistr / test-stpncpy.h
blob782651fecbb5871ab0852c422216527cbb122c1c
1 /* Test of uN_stpncpy() functions.
2 Copyright (C) 2010-2017 Free Software Foundation, Inc.
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2010. */
19 static void
20 check_single (const UNIT *input, size_t length, size_t n)
22 UNIT *dest;
23 UNIT *result;
24 size_t i;
26 dest = (UNIT *) malloc ((1 + n + 1) * sizeof (UNIT));
27 ASSERT (dest != NULL);
29 for (i = 0; i < 1 + n + 1; i++)
30 dest[i] = MAGIC;
32 result = U_STPNCPY (dest + 1, input, n);
33 ASSERT (result == dest + 1 + (n <= length ? n : length));
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);
42 free (dest);
45 static void
46 check (const UNIT *input, size_t input_length)
48 size_t length;
49 size_t n;
51 ASSERT (input_length > 0);
52 ASSERT (input[input_length - 1] == 0);
53 length = input_length - 1; /* = U_STRLEN (input) */
55 for (n = 0; n <= 2 * length + 2; n++)
56 check_single (input, length, n);
58 /* Check that U_STPNCPY (D, S, N) does not look at more than
59 MIN (U_STRLEN (S) + 1, N) units. */
61 char *page_boundary = (char *) zerosize_ptr ();
63 if (page_boundary != NULL)
65 for (n = 0; n <= 2 * length + 2; n++)
67 size_t n_to_copy = (n <= length ? n : length + 1);
68 UNIT *copy;
69 size_t i;
71 copy = (UNIT *) page_boundary - n_to_copy;
72 for (i = 0; i < n_to_copy; i++)
73 copy[i] = input[i];
75 check_single (copy, length, n);