]>
Commit | Line | Data |
---|---|---|
97020474 | 1 | /* Measure strcasecmp functions. |
f7a9f785 | 2 | Copyright (C) 2013-2016 Free Software Foundation, Inc. |
97020474 SP |
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 | <http://www.gnu.org/licenses/>. */ | |
18 | ||
19 | #include <ctype.h> | |
20 | #define TEST_MAIN | |
21 | #define TEST_NAME "strcasecmp" | |
22 | #include "bench-string.h" | |
23 | ||
24 | typedef int (*proto_t) (const char *, const char *); | |
25 | static int simple_strcasecmp (const char *, const char *); | |
26 | static int stupid_strcasecmp (const char *, const char *); | |
27 | ||
28 | IMPL (stupid_strcasecmp, 0) | |
29 | IMPL (simple_strcasecmp, 0) | |
30 | IMPL (strcasecmp, 1) | |
31 | ||
32 | static int | |
33 | simple_strcasecmp (const char *s1, const char *s2) | |
34 | { | |
35 | int ret; | |
36 | ||
37 | while ((ret = ((unsigned char) tolower (*s1) | |
38 | - (unsigned char) tolower (*s2))) == 0 | |
39 | && *s1++) | |
40 | ++s2; | |
41 | return ret; | |
42 | } | |
43 | ||
44 | static int | |
45 | stupid_strcasecmp (const char *s1, const char *s2) | |
46 | { | |
47 | size_t ns1 = strlen (s1) + 1, ns2 = strlen (s2) + 1; | |
48 | size_t n = ns1 < ns2 ? ns1 : ns2; | |
49 | int ret = 0; | |
50 | ||
51 | while (n--) | |
52 | { | |
53 | if ((ret = ((unsigned char) tolower (*s1) | |
54 | - (unsigned char) tolower (*s2))) != 0) | |
55 | break; | |
56 | ++s1; | |
57 | ++s2; | |
58 | } | |
59 | return ret; | |
60 | } | |
61 | ||
62 | static void | |
63 | do_one_test (impl_t *impl, const char *s1, const char *s2, int exp_result) | |
64 | { | |
44558701 WN |
65 | size_t i, iters = INNER_LOOP_ITERS; |
66 | timing_t start, stop, cur; | |
97020474 SP |
67 | int result = CALL (impl, s1, s2); |
68 | if ((exp_result == 0 && result != 0) | |
69 | || (exp_result < 0 && result >= 0) | |
70 | || (exp_result > 0 && result <= 0)) | |
71 | { | |
72 | error (0, 0, "Wrong result in function %s %d %d", impl->name, | |
73 | result, exp_result); | |
74 | ret = 1; | |
75 | return; | |
76 | } | |
77 | ||
44558701 WN |
78 | TIMING_NOW (start); |
79 | for (i = 0; i < iters; ++i) | |
97020474 | 80 | { |
44558701 | 81 | CALL (impl, s1, s2); |
97020474 | 82 | } |
44558701 WN |
83 | TIMING_NOW (stop); |
84 | ||
85 | TIMING_DIFF (cur, start, stop); | |
86 | ||
87 | TIMING_PRINT_MEAN ((double) cur, (double) iters); | |
97020474 SP |
88 | } |
89 | ||
90 | static void | |
91 | do_test (size_t align1, size_t align2, size_t len, int max_char, | |
92 | int exp_result) | |
93 | { | |
94 | size_t i; | |
95 | char *s1, *s2; | |
96 | ||
97 | if (len == 0) | |
98 | return; | |
99 | ||
100 | align1 &= 7; | |
101 | if (align1 + len + 1 >= page_size) | |
102 | return; | |
103 | ||
104 | align2 &= 7; | |
105 | if (align2 + len + 1 >= page_size) | |
106 | return; | |
107 | ||
108 | s1 = (char *) (buf1 + align1); | |
109 | s2 = (char *) (buf2 + align2); | |
110 | ||
111 | for (i = 0; i < len; i++) | |
112 | { | |
113 | s1[i] = toupper (1 + 23 * i % max_char); | |
114 | s2[i] = tolower (s1[i]); | |
115 | } | |
116 | ||
117 | s1[len] = s2[len] = 0; | |
118 | s1[len + 1] = 23; | |
119 | s2[len + 1] = 24 + exp_result; | |
120 | if ((s2[len - 1] == 'z' && exp_result == -1) | |
121 | || (s2[len - 1] == 'a' && exp_result == 1)) | |
122 | s1[len - 1] += exp_result; | |
123 | else | |
124 | s2[len - 1] -= exp_result; | |
125 | ||
44558701 | 126 | printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2); |
97020474 SP |
127 | |
128 | FOR_EACH_IMPL (impl, 0) | |
129 | do_one_test (impl, s1, s2, exp_result); | |
130 | ||
44558701 | 131 | putchar ('\n'); |
97020474 SP |
132 | } |
133 | ||
134 | int | |
135 | test_main (void) | |
136 | { | |
137 | size_t i; | |
138 | ||
139 | test_init (); | |
140 | ||
141 | printf ("%23s", ""); | |
142 | FOR_EACH_IMPL (impl, 0) | |
143 | printf ("\t%s", impl->name); | |
144 | putchar ('\n'); | |
145 | ||
146 | for (i = 1; i < 16; ++i) | |
147 | { | |
148 | do_test (i, i, i, 127, 0); | |
149 | do_test (i, i, i, 127, 1); | |
150 | do_test (i, i, i, 127, -1); | |
151 | } | |
152 | ||
153 | for (i = 1; i < 10; ++i) | |
154 | { | |
155 | do_test (0, 0, 2 << i, 127, 0); | |
156 | do_test (0, 0, 2 << i, 254, 0); | |
157 | do_test (0, 0, 2 << i, 127, 1); | |
158 | do_test (0, 0, 2 << i, 254, 1); | |
159 | do_test (0, 0, 2 << i, 127, -1); | |
160 | do_test (0, 0, 2 << i, 254, -1); | |
161 | } | |
162 | ||
163 | for (i = 1; i < 8; ++i) | |
164 | { | |
165 | do_test (i, 2 * i, 8 << i, 127, 0); | |
166 | do_test (2 * i, i, 8 << i, 254, 0); | |
167 | do_test (i, 2 * i, 8 << i, 127, 1); | |
168 | do_test (2 * i, i, 8 << i, 254, 1); | |
169 | do_test (i, 2 * i, 8 << i, 127, -1); | |
170 | do_test (2 * i, i, 8 << i, 254, -1); | |
171 | } | |
172 | ||
173 | return ret; | |
174 | } | |
175 | ||
b598e134 | 176 | #include <support/test-driver.c> |