1 /* Measure strncasecmp functions.
2 Copyright (C) 2013 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 <http://www.gnu.org/licenses/>. */
21 #define TEST_NAME "strncasecmp"
22 #include "bench-string.h"
24 typedef int (*proto_t
) (const char *, const char *, size_t);
25 static int simple_strncasecmp (const char *, const char *, size_t);
26 static int stupid_strncasecmp (const char *, const char *, size_t);
28 IMPL (stupid_strncasecmp
, 0)
29 IMPL (simple_strncasecmp
, 0)
33 simple_strncasecmp (const char *s1
, const char *s2
, size_t n
)
40 while ((ret
= ((unsigned char) tolower (*s1
)
41 - (unsigned char) tolower (*s2
))) == 0
52 stupid_strncasecmp (const char *s1
, const char *s2
, size_t max
)
54 size_t ns1
= strlen (s1
) + 1;
55 size_t ns2
= strlen (s2
) + 1;
56 size_t n
= ns1
< ns2
? ns1
: ns2
;
63 if ((ret
= ((unsigned char) tolower (*s1
)
64 - (unsigned char) tolower (*s2
))) != 0)
73 do_one_test (impl_t
*impl
, const char *s1
, const char *s2
, size_t n
,
78 hp_timing_t start
__attribute ((unused
));
79 hp_timing_t stop
__attribute ((unused
));
80 hp_timing_t best_time
= ~ (hp_timing_t
) 0;
83 for (i
= 0; i
< 32; ++i
)
85 HP_TIMING_NOW (start
);
86 CALL (impl
, s1
, s2
, n
);
88 HP_TIMING_BEST (best_time
, start
, stop
);
91 printf ("\t%zd", (size_t) best_time
);
96 do_test (size_t align1
, size_t align2
, size_t n
, size_t len
, int max_char
,
106 if (align1
+ len
+ 1 >= page_size
)
110 if (align2
+ len
+ 1 >= page_size
)
113 s1
= (char *) (buf1
+ align1
);
114 s2
= (char *) (buf2
+ align2
);
116 for (i
= 0; i
< len
; i
++)
118 s1
[i
] = toupper (1 + 23 * i
% max_char
);
119 s2
[i
] = tolower (s1
[i
]);
122 s1
[len
] = s2
[len
] = 0;
124 s2
[len
+ 1] = 24 + exp_result
;
125 if ((s2
[len
- 1] == 'z' && exp_result
== -1)
126 || (s2
[len
- 1] == 'a' && exp_result
== 1))
127 s1
[len
- 1] += exp_result
;
129 s2
[len
- 1] -= exp_result
;
132 printf ("Length %4zd, alignment %2zd/%2zd:", len
, align1
, align2
);
134 FOR_EACH_IMPL (impl
, 0)
135 do_one_test (impl
, s1
, s2
, n
, exp_result
);
149 FOR_EACH_IMPL (impl
, 0)
150 printf ("\t%s", impl
->name
);
153 for (i
= 1; i
< 16; ++i
)
155 do_test (i
, i
, i
- 1, i
, 127, 0);
157 do_test (i
, i
, i
, i
, 127, 0);
158 do_test (i
, i
, i
, i
, 127, 1);
159 do_test (i
, i
, i
, i
, 127, -1);
161 do_test (i
, i
, i
+ 1, i
, 127, 0);
162 do_test (i
, i
, i
+ 1, i
, 127, 1);
163 do_test (i
, i
, i
+ 1, i
, 127, -1);
166 for (i
= 1; i
< 10; ++i
)
168 do_test (0, 0, (2 << i
) - 1, 2 << i
, 127, 0);
169 do_test (0, 0, 2 << i
, 2 << i
, 254, 0);
170 do_test (0, 0, (2 << i
) + 1, 2 << i
, 127, 0);
172 do_test (0, 0, (2 << i
) + 1, 2 << i
, 254, 0);
174 do_test (0, 0, 2 << i
, 2 << i
, 127, 1);
175 do_test (0, 0, (2 << i
) + 10, 2 << i
, 127, 1);
177 do_test (0, 0, 2 << i
, 2 << i
, 254, 1);
178 do_test (0, 0, (2 << i
) + 10, 2 << i
, 254, 1);
180 do_test (0, 0, 2 << i
, 2 << i
, 127, -1);
181 do_test (0, 0, (2 << i
) + 10, 2 << i
, 127, -1);
183 do_test (0, 0, 2 << i
, 2 << i
, 254, -1);
184 do_test (0, 0, (2 << i
) + 10, 2 << i
, 254, -1);
187 for (i
= 1; i
< 8; ++i
)
189 do_test (i
, 2 * i
, (8 << i
) - 1, 8 << i
, 127, 0);
190 do_test (i
, 2 * i
, 8 << i
, 8 << i
, 127, 0);
191 do_test (i
, 2 * i
, (8 << i
) + 100, 8 << i
, 127, 0);
193 do_test (2 * i
, i
, (8 << i
) - 1, 8 << i
, 254, 0);
194 do_test (2 * i
, i
, 8 << i
, 8 << i
, 254, 0);
195 do_test (2 * i
, i
, (8 << i
) + 100, 8 << i
, 254, 0);
197 do_test (i
, 2 * i
, 8 << i
, 8 << i
, 127, 1);
198 do_test (i
, 2 * i
, (8 << i
) + 100, 8 << i
, 127, 1);
200 do_test (2 * i
, i
, 8 << i
, 8 << i
, 254, 1);
201 do_test (2 * i
, i
, (8 << i
) + 100, 8 << i
, 254, 1);
203 do_test (i
, 2 * i
, 8 << i
, 8 << i
, 127, -1);
204 do_test (i
, 2 * i
, (8 << i
) + 100, 8 << i
, 127, -1);
206 do_test (2 * i
, i
, 8 << i
, 8 << i
, 254, -1);
207 do_test (2 * i
, i
, (8 << i
) + 100, 8 << i
, 254, -1);
213 #include "../test-skeleton.c"