| 1 | /* |
| 2 | * memset test. |
| 3 | * |
| 4 | * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | * See https://llvm.org/LICENSE.txt for license information. |
| 6 | * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | */ |
| 8 | |
| 9 | #include <stdint.h> |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include "stringlib.h" |
| 14 | |
| 15 | static const struct fun |
| 16 | { |
| 17 | const char *name; |
| 18 | void *(*fun)(void *s, int c, size_t n); |
| 19 | } funtab[] = { |
| 20 | #define F(x) {#x, x}, |
| 21 | F(memset) |
| 22 | #if __aarch64__ |
| 23 | F(__memset_aarch64) |
| 24 | #elif __arm__ |
| 25 | F(__memset_arm) |
| 26 | #endif |
| 27 | #undef F |
| 28 | {0, 0} |
| 29 | }; |
| 30 | |
| 31 | static int test_status; |
| 32 | #define ERR(...) (test_status=1, printf(__VA_ARGS__)) |
| 33 | |
| 34 | #define A 32 |
| 35 | #define LEN 250000 |
| 36 | static unsigned char sbuf[LEN+2*A]; |
| 37 | |
| 38 | static void *alignup(void *p) |
| 39 | { |
| 40 | return (void*)(((uintptr_t)p + A-1) & -A); |
| 41 | } |
| 42 | |
| 43 | static void err(const char *name, unsigned char *src, int salign, int c, int len) |
| 44 | { |
| 45 | ERR("%s(align %d, %d, %d) failed\n" , name, salign, c, len); |
| 46 | ERR("got : %.*s\n" , salign+len+1, src); |
| 47 | } |
| 48 | |
| 49 | static void test(const struct fun *fun, int salign, int c, int len) |
| 50 | { |
| 51 | unsigned char *src = alignup(p: sbuf); |
| 52 | unsigned char *s = src + salign; |
| 53 | void *p; |
| 54 | int i; |
| 55 | |
| 56 | if (len > LEN || salign >= A) |
| 57 | abort(); |
| 58 | for (i = 0; i < len+A; i++) |
| 59 | src[i] = '?'; |
| 60 | for (i = 0; i < len; i++) |
| 61 | s[i] = 'a' + i%23; |
| 62 | for (; i<len%A; i++) |
| 63 | s[i] = '*'; |
| 64 | |
| 65 | p = fun->fun(s, c, len); |
| 66 | if (p != s) |
| 67 | ERR("%s(%p,..) returned %p\n" , fun->name, s, p); |
| 68 | |
| 69 | for (i = 0; i < salign; i++) { |
| 70 | if (src[i] != '?') { |
| 71 | err(name: fun->name, src, salign, c, len); |
| 72 | return; |
| 73 | } |
| 74 | } |
| 75 | for (i = salign; i < len; i++) { |
| 76 | if (src[i] != (unsigned char)c) { |
| 77 | err(name: fun->name, src, salign, c, len); |
| 78 | return; |
| 79 | } |
| 80 | } |
| 81 | for (; i < len%A; i++) { |
| 82 | if (src[i] != '*') { |
| 83 | err(name: fun->name, src, salign, c, len); |
| 84 | return; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | int main() |
| 90 | { |
| 91 | int r = 0; |
| 92 | for (int i=0; funtab[i].name; i++) { |
| 93 | test_status = 0; |
| 94 | for (int s = 0; s < A; s++) { |
| 95 | int n; |
| 96 | for (n = 0; n < 100; n++) { |
| 97 | test(fun: funtab+i, salign: s, c: 0, len: n); |
| 98 | test(fun: funtab+i, salign: s, c: 0x25, len: n); |
| 99 | test(fun: funtab+i, salign: s, c: 0xaa25, len: n); |
| 100 | } |
| 101 | for (; n < LEN; n *= 2) { |
| 102 | test(fun: funtab+i, salign: s, c: 0, len: n); |
| 103 | test(fun: funtab+i, salign: s, c: 0x25, len: n); |
| 104 | test(fun: funtab+i, salign: s, c: 0xaa25, len: n); |
| 105 | } |
| 106 | } |
| 107 | printf(format: "%s %s\n" , test_status ? "FAIL" : "PASS" , funtab[i].name); |
| 108 | if (test_status) |
| 109 | r = -1; |
| 110 | } |
| 111 | return r; |
| 112 | } |
| 113 | |