Don't call vg_assert inside sync_signalhandler_from_kernel
[valgrind.git] / memcheck / tests / wcsncpy.c
blob59bee5fe6ac18e5b98ad66512e6426f03e0cf8c4
1 /* Test strncpy functions.
2 Copyright (C) 1999-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 /* This code is copied from GNU libc 2.38 string/test-strncpy.c
20 * Necessary bits have been copied from other headers in GNU libc.
21 * The code has been de-MACROed a bit and made specific for
22 * wcsncpy. And re-indented. */
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <wchar.h>
28 #include <unistd.h>
30 # define BIG_CHAR WCHAR_MAX
31 # define SMALL_CHAR 1273
33 size_t page_size;
34 unsigned char buf1[10240];
35 unsigned char buf2[10240];
37 typedef wchar_t *(*impl_t)(wchar_t *, const wchar_t *, size_t);
39 static void do_one_test(impl_t impl, wchar_t *dst, const wchar_t *src, size_t len, size_t n)
41 if (impl(dst, src, n) != dst)
43 fprintf(stderr, "Wrong result in function wscncpy %p %p", impl(dst, src, n), dst);
44 return;
47 if (memcmp(dst, src, (len > n ? n : len) * sizeof(wchar_t)) != 0)
49 fprintf(stderr, "Wrong result in function wcsncpy");
50 return;
53 if (n > len)
55 size_t i;
57 for (i = len; i < n; ++i)
59 if (dst[i] != '\0')
61 fprintf(stderr, "Wrong result in function wcsncpy");
62 return;
68 static void do_test(size_t align1, size_t align2, size_t len, size_t n, int max_char)
70 size_t i;
71 wchar_t *s1, *s2;
73 /* For wcsncpy: align1 and align2 here mean alignment not in bytes,
74 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t)). */
75 align1 &= 7;
76 if ((align1 + len) * sizeof(wchar_t) >= page_size)
77 return;
79 align2 &= 7;
80 if ((align2 + len) * sizeof(wchar_t) >= page_size)
81 return;
83 s1 = (wchar_t *)(buf1) + align1;
84 s2 = (wchar_t *)(buf2) + align2;
86 for (i = 0; i < len; ++i)
87 s1[i] = 32 + 23 * i % (max_char - 32);
88 s1[len] = 0;
89 for (i = len + 1; (i + align1) * sizeof(wchar_t) < page_size && i < len + 64; ++i)
90 s1[i] = 32 + 32 * i % (max_char - 32);
92 do_one_test(wcsncpy, s2, s1, len, n);
95 int main(void)
97 wchar_t *bar = calloc(121, sizeof(wchar_t));
98 wmemset(bar, 1, 120);
100 wchar_t *foo = calloc(256, sizeof(wchar_t));
101 wcsncpy(foo, bar, 255);
103 page_size = sysconf (_SC_PAGE_SIZE);
105 size_t i;
106 for (i = 1; i < 8; ++i)
108 do_test(i, i, 16, 16, SMALL_CHAR);
109 do_test(i, i, 16, 16, BIG_CHAR);
110 do_test(i, 2 * i, 16, 16, SMALL_CHAR);
111 do_test(2 * i, i, 16, 16, BIG_CHAR);
112 do_test(8 - i, 2 * i, 1 << i, 2 << i, SMALL_CHAR);
113 do_test(2 * i, 8 - i, 2 << i, 1 << i, SMALL_CHAR);
114 do_test(8 - i, 2 * i, 1 << i, 2 << i, BIG_CHAR);
115 do_test(2 * i, 8 - i, 2 << i, 1 << i, BIG_CHAR);
118 for (i = 1; i < 8; ++i)
120 do_test(0, 0, 4 << i, 8 << i, SMALL_CHAR);
121 do_test(0, 0, 16 << i, 8 << i, SMALL_CHAR);
122 do_test(8 - i, 2 * i, 4 << i, 8 << i, SMALL_CHAR);
123 do_test(8 - i, 2 * i, 16 << i, 8 << i, SMALL_CHAR);