Refer to C23 in place of C2X in glibc
[glibc.git] / string / test-memswap.c
blob2f0a14e00ff374e2918da1a18753b546dc815df2
1 /* Test and measure memcpy functions.
2 Copyright (C) 2023-2024 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 #include <string.h>
20 #include <support/check.h>
21 #include <memswap.h>
23 #define TEST_MAIN
24 #define BUF1PAGES 3
25 #include "test-string.h"
27 static unsigned char *ref1;
28 static unsigned char *ref2;
30 static void
31 do_one_test (unsigned char *p1, unsigned char *ref1, unsigned char *p2,
32 unsigned char *ref2, size_t len)
34 __memswap (p1, p2, len);
36 TEST_COMPARE_BLOB (p1, len, ref2, len);
37 TEST_COMPARE_BLOB (p2, len, ref1, len);
40 static inline void
41 do_test (size_t align1, size_t align2, size_t len)
43 align1 &= page_size;
44 if (align1 + len >= page_size)
45 return;
47 align2 &= page_size;
48 if (align2 + len >= page_size)
49 return;
51 unsigned char *p1 = buf1 + align1;
52 unsigned char *p2 = buf2 + align2;
53 for (size_t repeats = 0; repeats < 2; ++repeats)
55 size_t i, j;
56 for (i = 0, j = 1; i < len; i++, j += 23)
58 ref1[i] = p1[i] = j;
59 ref2[i] = p2[i] = UCHAR_MAX - j;
62 do_one_test (p1, ref1, p2, ref2, len);
66 static void
67 do_random_tests (void)
69 for (size_t n = 0; n < ITERATIONS; n++)
71 size_t len, size, size1, size2, align1, align2;
73 if (n == 0)
75 len = getpagesize ();
76 size = len + 512;
77 size1 = size;
78 size2 = size;
79 align1 = 512;
80 align2 = 512;
82 else
84 if ((random () & 255) == 0)
85 size = 65536;
86 else
87 size = 768;
88 if (size > page_size)
89 size = page_size;
90 size1 = size;
91 size2 = size;
92 size_t i = random ();
93 if (i & 3)
94 size -= 256;
95 if (i & 1)
96 size1 -= 256;
97 if (i & 2)
98 size2 -= 256;
99 if (i & 4)
101 len = random () % size;
102 align1 = size1 - len - (random () & 31);
103 align2 = size2 - len - (random () & 31);
104 if (align1 > size1)
105 align1 = 0;
106 if (align2 > size2)
107 align2 = 0;
109 else
111 align1 = random () & 63;
112 align2 = random () & 63;
113 len = random () % size;
114 if (align1 + len > size1)
115 align1 = size1 - len;
116 if (align2 + len > size2)
117 align2 = size2 - len;
120 unsigned char *p1 = buf1 + page_size - size1;
121 unsigned char *p2 = buf2 + page_size - size2;
122 size_t j = align1 + len + 256;
123 if (j > size1)
124 j = size1;
125 for (size_t i = 0; i < j; ++i)
126 ref1[i] = p1[i] = random () & 255;
128 j = align2 + len + 256;
129 if (j > size2)
130 j = size2;
132 for (size_t i = 0; i < j; ++i)
133 ref2[i] = p2[i] = random () & 255;
135 do_one_test (p1 + align1, ref1 + align1, p2 + align2, ref2 + align2, len);
139 static int
140 test_main (void)
142 test_init ();
143 /* Use the start of buf1 for reference buffers. */
144 ref1 = buf1;
145 ref2 = buf1 + page_size;
146 buf1 = ref2 + page_size;
148 printf ("%23s", "");
149 printf ("\t__memswap\n");
151 for (size_t i = 0; i < 18; ++i)
153 do_test (0, 0, 1 << i);
154 do_test (i, 0, 1 << i);
155 do_test (0, i, 1 << i);
156 do_test (i, i, 1 << i);
159 for (size_t i = 0; i < 32; ++i)
161 do_test (0, 0, i);
162 do_test (i, 0, i);
163 do_test (0, i, i);
164 do_test (i, i, i);
167 for (size_t i = 3; i < 32; ++i)
169 if ((i & (i - 1)) == 0)
170 continue;
171 do_test (0, 0, 16 * i);
172 do_test (i, 0, 16 * i);
173 do_test (0, i, 16 * i);
174 do_test (i, i, 16 * i);
177 for (size_t i = 19; i <= 25; ++i)
179 do_test (255, 0, 1 << i);
180 do_test (0, 4000, 1 << i);
181 do_test (0, 255, i);
182 do_test (0, 4000, i);
185 do_test (0, 0, getpagesize ());
187 do_random_tests ();
189 return 0;
192 #include <support/test-driver.c>