1 /* Test of uN_strncat() functions.
2 Copyright (C) 2010-2012 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)
22 static const UNIT base[] = { 'C', 'h', 'a', 'n', 'g', 'i', 'n', 'g', 0 };
25 for (m = 0; m < SIZEOF (base); m++)
31 dest = (UNIT *) malloc ((1 + m + n + 2) * sizeof (UNIT));
32 ASSERT (dest != NULL);
35 for (i = 0; i < m; i++)
36 dest[1 + i] = base[i];
38 for (i = 1; i < n + 2; i++)
39 dest[1 + m + i] = MAGIC;
41 result = U_STRNCAT (dest + 1, input, n);
42 ASSERT (result == dest + 1);
44 ASSERT (dest[0] == MAGIC);
45 for (i = 0; i < m; i++)
46 ASSERT (dest[1 + i] == base[i]);
47 for (i = 0; i < (n <= length ? n : length); i++)
48 ASSERT (dest[1 + m + i] == input[i]);
49 ASSERT (dest[1 + m + i] == 0);
50 ASSERT (dest[1 + m + i + 1] == MAGIC);
57 check (const UNIT *input, size_t input_length)
62 ASSERT (input_length > 0);
63 ASSERT (input[input_length - 1] == 0);
64 length = input_length - 1; /* = U_STRLEN (input) */
66 for (n = 0; n <= 2 * length + 2; n++)
67 check_single (input, length, n);
69 /* Check that U_STRNCAT (D, S, N) does not look at more than
70 MIN (U_STRLEN (S) + 1, N) units. */
72 char *page_boundary = (char *) zerosize_ptr ();
74 if (page_boundary != NULL)
76 for (n = 0; n <= 2 * length + 2; n++)
78 size_t n_to_copy = (n <= length ? n : length + 1);
82 copy = (UNIT *) page_boundary - n_to_copy;
83 for (i = 0; i < n_to_copy; i++)
86 check_single (copy, length, n);