xref: /freebsd/contrib/arm-optimized-routines/string/test/strrchr.c (revision 18054d0220cfc8df9c9568c437bd6fbb59d53c3c)
1 /*
2  * strrchr test.
3  *
4  * Copyright (c) 2019-2021, Arm Limited.
5  * SPDX-License-Identifier: MIT
6  */
7 
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <limits.h>
13 #include "mte.h"
14 #include "stringlib.h"
15 #include "stringtest.h"
16 
17 #define F(x, mte) {#x, x, mte},
18 
19 static const struct fun
20 {
21   const char *name;
22   char *(*fun) (const char *s, int c);
23   int test_mte;
24 } funtab[] = {
25   // clang-format off
26   F(strrchr, 0)
27 #if __aarch64__
28   F(__strrchr_aarch64, 0)
29   F(__strrchr_aarch64_mte, 1)
30 # if __ARM_FEATURE_SVE
31   F(__strrchr_aarch64_sve, 1)
32 # endif
33 #endif
34   {0, 0, 0}
35   // clang-format on
36 };
37 #undef F
38 
39 #define ALIGN 32
40 #define LEN 512
41 static char *sbuf;
42 
43 static void *
44 alignup (void *p)
45 {
46   return (void *) (((uintptr_t) p + ALIGN - 1) & -ALIGN);
47 }
48 
49 static void
50 test (const struct fun *fun, int align, int seekpos, int len)
51 {
52   char *src = alignup (sbuf);
53   char *s = src + align;
54   char *f = seekpos != -1 ? s + seekpos : 0;
55   int seekchar = 0x1;
56   void *p;
57 
58   if (err_count >= ERR_LIMIT)
59     return;
60   if (len > LEN || seekpos >= len || align >= ALIGN)
61     abort ();
62 
63   for (int i = 0; src + i < s; i++)
64     src[i] = (i + len) & 1 ? seekchar : 0;
65   for (int i = 1; i <= ALIGN; i++)
66     s[len + i] = (i + len) & 1 ? seekchar : 0;
67   for (int i = 0; i < len; i++)
68     s[i] = 'a' + (i & 31);
69   if (seekpos != -1)
70     s[seekpos / 2] = s[seekpos] = seekchar;
71   if (seekpos > 0 && (len + align) & 1)
72     s[seekpos - 1] = seekchar;
73   s[len] = '\0';
74 
75   s = tag_buffer (s, len + 1, fun->test_mte);
76   p = fun->fun (s, seekchar);
77   untag_buffer (s, len + 1, fun->test_mte);
78   p = untag_pointer (p);
79 
80   if (p != f)
81     {
82       ERR ("%s (%p, 0x%02x) len %d returned %p, expected %p pos %d\n",
83 	   fun->name, s, seekchar, len, p, f, seekpos);
84       quote ("input", s, len);
85     }
86 
87   s = tag_buffer (s, len + 1, fun->test_mte);
88   p = fun->fun (s, 0);
89   untag_buffer (s, len + 1, fun->test_mte);
90 
91   if (p != s + len)
92     {
93       ERR ("%s (%p, 0x%02x) len %d returned %p, expected %p pos %d\n",
94 	   fun->name, s, 0, len, p, s + len, len);
95       quote ("input", s, len);
96     }
97 }
98 
99 int
100 main (void)
101 {
102   sbuf = mte_mmap (LEN + 3 * ALIGN);
103   int r = 0;
104   for (int i = 0; funtab[i].name; i++)
105     {
106       err_count = 0;
107       for (int a = 0; a < ALIGN; a++)
108 	for (int n = 0; n < LEN; n++)
109 	  {
110 	    for (int sp = 0; sp < n; sp++)
111 	      test (funtab + i, a, sp, n);
112 	    test (funtab + i, a, -1, n);
113 	  }
114 
115       char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS";
116       printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name);
117       if (err_count)
118 	r = -1;
119     }
120   return r;
121 }
122