1 /* Test of uN_strncat() functions.
2 Copyright (C) 2010-2023 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)
21 {
22 static const UNIT base[] = { 'C', 'h', 'a', 'n', 'g', 'i', 'n', 'g', 0 };
23 size_t m;
24
25 for (m = 0; m < SIZEOF (base); m++)
26 {
27 UNIT *dest;
28 UNIT *result;
29 size_t i;
30
31 dest = (UNIT *) malloc ((1 + m + n + 2) * sizeof (UNIT));
32 ASSERT (dest != NULL);
33
34 dest[0] = MAGIC;
35 for (i = 0; i < m; i++)
36 dest[1 + i] = base[i];
37 dest[1 + m] = 0;
38 for (i = 1; i < n + 2; i++)
39 dest[1 + m + i] = MAGIC;
40
41 result = U_STRNCAT (dest + 1, input, n);
42 ASSERT (result == dest + 1);
43
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);
51
52 free (dest);
53 }
54 }
55
56 static void
57 check (const UNIT *input, size_t input_length)
58 {
59 size_t length;
60 size_t n;
61
62 ASSERT (input_length > 0);
63 ASSERT (input[input_length - 1] == 0);
64 length = input_length - 1; /* = U_STRLEN (input) */
65
66 for (n = 0; n <= 2 * length + 2; n++)
67 check_single (input, length, n);
68
69 /* Check that U_STRNCAT (D, S, N) does not look at more than
70 MIN (U_STRLEN (S) + 1, N) units. */
71 {
72 char *page_boundary = (char *) zerosize_ptr ();
73
74 if (page_boundary != NULL)
75 {
76 for (n = 0; n <= 2 * length + 2; n++)
77 {
78 size_t n_to_copy = (n <= length ? n : length + 1);
79 UNIT *copy;
80 size_t i;
81
82 copy = (UNIT *) page_boundary - n_to_copy;
83 for (i = 0; i < n_to_copy; i++)
84 copy[i] = input[i];
85
86 check_single (copy, length, n);
87 }
88 }
89 }
90 }