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/>. */
20 #include <support/check.h>
25 #include "test-string.h"
27 static unsigned char *ref1
;
28 static unsigned char *ref2
;
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
);
41 do_test (size_t align1
, size_t align2
, size_t len
)
44 if (align1
+ len
>= page_size
)
48 if (align2
+ len
>= page_size
)
51 unsigned char *p1
= buf1
+ align1
;
52 unsigned char *p2
= buf2
+ align2
;
53 for (size_t repeats
= 0; repeats
< 2; ++repeats
)
56 for (i
= 0, j
= 1; i
< len
; i
++, j
+= 23)
59 ref2
[i
] = p2
[i
] = UCHAR_MAX
- j
;
62 do_one_test (p1
, ref1
, p2
, ref2
, len
);
67 do_random_tests (void)
69 for (size_t n
= 0; n
< ITERATIONS
; n
++)
71 size_t len
, size
, size1
, size2
, align1
, align2
;
84 if ((random () & 255) == 0)
101 len
= random () % size
;
102 align1
= size1
- len
- (random () & 31);
103 align2
= size2
- len
- (random () & 31);
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;
125 for (size_t i
= 0; i
< j
; ++i
)
126 ref1
[i
] = p1
[i
] = random () & 255;
128 j
= align2
+ len
+ 256;
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
);
143 /* Use the start of buf1 for reference buffers. */
145 ref2
= buf1
+ page_size
;
146 buf1
= ref2
+ page_size
;
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
)
167 for (size_t i
= 3; i
< 32; ++i
)
169 if ((i
& (i
- 1)) == 0)
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
);
182 do_test (0, 4000, i
);
185 do_test (0, 0, getpagesize ());
192 #include <support/test-driver.c>