| 1 | /* |
| 2 | * strncmp 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 | int (*fun)(const char *, const char *, size_t); |
| 19 | } funtab[] = { |
| 20 | #define F(x) {#x, x}, |
| 21 | F(strncmp) |
| 22 | #if __aarch64__ |
| 23 | F(__strncmp_aarch64) |
| 24 | # if __ARM_FEATURE_SVE |
| 25 | F(__strncmp_aarch64_sve) |
| 26 | # endif |
| 27 | #endif |
| 28 | #undef F |
| 29 | {0, 0} |
| 30 | }; |
| 31 | |
| 32 | static int test_status; |
| 33 | #define ERR(...) (test_status=1, printf(__VA_ARGS__)) |
| 34 | |
| 35 | #define A 32 |
| 36 | #define LEN 250000 |
| 37 | static char s1buf[LEN+2*A]; |
| 38 | static char s2buf[LEN+2*A]; |
| 39 | |
| 40 | static void *alignup(void *p) |
| 41 | { |
| 42 | return (void*)(((uintptr_t)p + A-1) & -A); |
| 43 | } |
| 44 | |
| 45 | static void test(const struct fun *fun, int s1align, int s2align, int maxlen, int diffpos, int len) |
| 46 | { |
| 47 | char *src1 = alignup(p: s1buf); |
| 48 | char *src2 = alignup(p: s2buf); |
| 49 | char *s1 = src1 + s1align; |
| 50 | char *s2 = src2 + s2align; |
| 51 | int r; |
| 52 | |
| 53 | if (len > LEN || s1align >= A || s2align >= A) |
| 54 | abort(); |
| 55 | if (diffpos > 1 && diffpos >= len-1) |
| 56 | abort(); |
| 57 | |
| 58 | for (int i = 0; i < len+A; i++) |
| 59 | src1[i] = src2[i] = '?'; |
| 60 | for (int i = 0; i < len-1; i++) |
| 61 | s1[i] = s2[i] = 'a' + i%23; |
| 62 | if (diffpos > 1) |
| 63 | s1[diffpos]++; |
| 64 | s1[len] = s2[len] = '\0'; |
| 65 | |
| 66 | r = fun->fun(s1, s2, maxlen); |
| 67 | |
| 68 | diffpos = maxlen <= diffpos ? 0 : diffpos; |
| 69 | |
| 70 | if (((diffpos <= 1) && r != 0) || (diffpos > 1 && r == 0)) { |
| 71 | ERR("%s(align %d, align %d, %d (%d)) failed, returned %d (%d)\n" , |
| 72 | fun->name, s1align, s2align, maxlen, len, r, diffpos); |
| 73 | ERR("src1: %.*s\n" , s1align+len+1, src1); |
| 74 | ERR("src2: %.*s\n" , s2align+len+1, src2); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | int main() |
| 79 | { |
| 80 | int r = 0; |
| 81 | for (int i=0; funtab[i].name; i++) { |
| 82 | test_status = 0; |
| 83 | for (int d = 0; d < A; d++) |
| 84 | for (int s = 0; s < A; s++) { |
| 85 | int n; |
| 86 | for (n = 0; n < 100; n++) { |
| 87 | test(fun: funtab+i, s1align: d, s2align: s, maxlen: n, diffpos: 0, len: n); |
| 88 | test(fun: funtab+i, s1align: d, s2align: s, maxlen: n, diffpos: n/2, len: n); |
| 89 | test(fun: funtab+i, s1align: d, s2align: s, maxlen: n/2, diffpos: 0, len: n); |
| 90 | test(fun: funtab+i, s1align: d, s2align: s, maxlen: n/2, diffpos: n/2, len: n); |
| 91 | } |
| 92 | for (; n < LEN; n *= 2) { |
| 93 | test(fun: funtab+i, s1align: d, s2align: s, maxlen: n, diffpos: 0, len: n); |
| 94 | test(fun: funtab+i, s1align: d, s2align: s, maxlen: n, diffpos: n/2, len: n); |
| 95 | test(fun: funtab+i, s1align: d, s2align: s, maxlen: n/2, diffpos: 0, len: n); |
| 96 | test(fun: funtab+i, s1align: d, s2align: s, maxlen: n/2, diffpos: n/2, len: n); |
| 97 | } |
| 98 | } |
| 99 | printf(format: "%s %s\n" , test_status ? "FAIL" : "PASS" , funtab[i].name); |
| 100 | if (test_status) |
| 101 | r = -1; |
| 102 | } |
| 103 | return r; |
| 104 | } |
| 105 | |