1 /* Measure strcasestr functions.
2    Copyright (C) 2013-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
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.
9 
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.
14 
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/>.  */
18 
19 #define TEST_MAIN
20 #define TEST_NAME "strcasestr"
21 #include "bench-string.h"
22 
23 
24 #define STRCASESTR simple_strcasestr
25 #define NO_ALIAS
26 #define __strncasecmp strncasecmp
27 #define __strnlen strnlen
28 #include "../string/strcasestr.c"
29 
30 typedef char *(*proto_t) (const char *, const char *);
31 
32 IMPL (simple_strcasestr, 0)
33 IMPL (strcasestr, 1)
34 
35 
36 static void
do_one_test(impl_t * impl,const char * s1,const char * s2,char * exp_result)37 do_one_test (impl_t *impl, const char *s1, const char *s2, char *exp_result)
38 {
39   size_t i, iters = INNER_LOOP_ITERS_SMALL;
40   timing_t start, stop, cur;
41 
42   TIMING_NOW (start);
43   for (i = 0; i < iters; ++i)
44     {
45       CALL (impl, s1, s2);
46     }
47   TIMING_NOW (stop);
48 
49   TIMING_DIFF (cur, start, stop);
50 
51   TIMING_PRINT_MEAN ((double) cur, (double) iters);
52 }
53 
54 
55 static void
do_test(size_t align1,size_t align2,size_t len1,size_t len2,int fail)56 do_test (size_t align1, size_t align2, size_t len1, size_t len2,
57 	 int fail)
58 {
59   char *s1 = (char *) (buf1 + align1);
60   char *s2 = (char *) (buf2 + align2);
61 
62   static const char d[] = "1234567890abcxyz";
63 #define dl (sizeof (d) - 1)
64   char *ss2 = s2;
65   for (size_t l = len2; l > 0; l = l > dl ? l - dl : 0)
66     {
67       size_t t = l > dl ? dl : l;
68       ss2 = mempcpy (ss2, d, t);
69     }
70   s2[len2] = '\0';
71 
72   if (fail)
73     {
74       char *ss1 = s1;
75       for (size_t l = len1; l > 0; l = l > dl ? l - dl : 0)
76 	{
77 	  size_t t = l > dl ? dl : l;
78 	  memcpy (ss1, d, t);
79 	  ++ss1[len2 > 7 ? 7 : len2 - 1];
80 	  ss1 += t;
81 	}
82     }
83   else
84     {
85       memset (s1, '0', len1);
86       for (size_t i = 0; i < len2; ++i)
87 	s1[len1 - len2 + i] = toupper (s2[i]);
88     }
89   s1[len1] = '\0';
90 
91   printf ("Length %4zd/%zd, alignment %2zd/%2zd, %s:",
92 	  len1, len2, align1, align2, fail ? "fail" : "found");
93 
94   FOR_EACH_IMPL (impl, 0)
95     do_one_test (impl, s1, s2, fail ? NULL : s1 + len1 - len2);
96 
97   putchar ('\n');
98 }
99 
100 static int
test_main(void)101 test_main (void)
102 {
103   test_init ();
104 
105   printf ("%23s", "");
106   FOR_EACH_IMPL (impl, 0)
107     printf ("\t%s", impl->name);
108   putchar ('\n');
109 
110   for (size_t klen = 2; klen < 32; ++klen)
111     for (size_t hlen = 2 * klen; hlen < 16 * klen; hlen += klen)
112       {
113 	do_test (0, 0, hlen, klen, 0);
114 	do_test (0, 0, hlen, klen, 1);
115 	do_test (0, 3, hlen, klen, 0);
116 	do_test (0, 3, hlen, klen, 1);
117 	do_test (0, 9, hlen, klen, 0);
118 	do_test (0, 9, hlen, klen, 1);
119 	do_test (0, 15, hlen, klen, 0);
120 	do_test (0, 15, hlen, klen, 1);
121 
122 	do_test (3, 0, hlen, klen, 0);
123 	do_test (3, 0, hlen, klen, 1);
124 	do_test (3, 3, hlen, klen, 0);
125 	do_test (3, 3, hlen, klen, 1);
126 	do_test (3, 9, hlen, klen, 0);
127 	do_test (3, 9, hlen, klen, 1);
128 	do_test (3, 15, hlen, klen, 0);
129 	do_test (3, 15, hlen, klen, 1);
130 
131 	do_test (9, 0, hlen, klen, 0);
132 	do_test (9, 0, hlen, klen, 1);
133 	do_test (9, 3, hlen, klen, 0);
134 	do_test (9, 3, hlen, klen, 1);
135 	do_test (9, 9, hlen, klen, 0);
136 	do_test (9, 9, hlen, klen, 1);
137 	do_test (9, 15, hlen, klen, 0);
138 	do_test (9, 15, hlen, klen, 1);
139 
140 	do_test (15, 0, hlen, klen, 0);
141 	do_test (15, 0, hlen, klen, 1);
142 	do_test (15, 3, hlen, klen, 0);
143 	do_test (15, 3, hlen, klen, 1);
144 	do_test (15, 9, hlen, klen, 0);
145 	do_test (15, 9, hlen, klen, 1);
146 	do_test (15, 15, hlen, klen, 0);
147 	do_test (15, 15, hlen, klen, 1);
148       }
149 
150   do_test (0, 0, page_size - 1, 16, 0);
151   do_test (0, 0, page_size - 1, 16, 1);
152 
153   return ret;
154 }
155 
156 #include <support/test-driver.c>
157