1 /* Test of uN_stpncpy() functions.
2 Copyright (C) 2010-2011 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. */
20 check_single (const UNIT *input, size_t length, size_t n)
26 dest = (UNIT *) malloc ((1 + n + 1) * sizeof (UNIT));
27 ASSERT (dest != NULL);
29 for (i = 0; i < 1 + n + 1; i++)
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]);
39 ASSERT (dest[1 + i] == 0);
40 ASSERT (dest[1 + n] == MAGIC);
46 check (const UNIT *input, size_t input_length)
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);
71 copy = (UNIT *) page_boundary - n_to_copy;
72 for (i = 0; i < n_to_copy; i++)
75 check_single (copy, length, n);