1 | /* Measure strcasecmp 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 | #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 | |
27 | IMPL (simple_strcasecmp, 0) |
28 | IMPL (strcasecmp, 1) |
29 | |
30 | static int |
31 | simple_strcasecmp (const char *s1, const char *s2) |
32 | { |
33 | int ret; |
34 | |
35 | while ((ret = ((unsigned char) tolower (*s1) |
36 | - (unsigned char) tolower (*s2))) == 0 |
37 | && *s1++) |
38 | ++s2; |
39 | return ret; |
40 | } |
41 | |
42 | static void |
43 | do_one_test (impl_t *impl, const char *s1, const char *s2, int exp_result) |
44 | { |
45 | size_t i, iters = INNER_LOOP_ITERS; |
46 | timing_t start, stop, cur; |
47 | int result = CALL (impl, s1, s2); |
48 | if ((exp_result == 0 && result != 0) |
49 | || (exp_result < 0 && result >= 0) |
50 | || (exp_result > 0 && result <= 0)) |
51 | { |
52 | error (status: 0, errnum: 0, format: "Wrong result in function %s %d %d" , impl->name, |
53 | result, exp_result); |
54 | ret = 1; |
55 | return; |
56 | } |
57 | |
58 | TIMING_NOW (start); |
59 | for (i = 0; i < iters; ++i) |
60 | { |
61 | CALL (impl, s1, s2); |
62 | } |
63 | TIMING_NOW (stop); |
64 | |
65 | TIMING_DIFF (cur, start, stop); |
66 | |
67 | TIMING_PRINT_MEAN ((double) cur, (double) iters); |
68 | } |
69 | |
70 | static void |
71 | do_test (size_t align1, size_t align2, size_t len, int max_char, |
72 | int exp_result) |
73 | { |
74 | size_t i; |
75 | char *s1, *s2; |
76 | |
77 | if (len == 0) |
78 | return; |
79 | |
80 | align1 &= 7; |
81 | if (align1 + len + 1 >= page_size) |
82 | return; |
83 | |
84 | align2 &= 7; |
85 | if (align2 + len + 1 >= page_size) |
86 | return; |
87 | |
88 | s1 = (char *) (buf1 + align1); |
89 | s2 = (char *) (buf2 + align2); |
90 | |
91 | for (i = 0; i < len; i++) |
92 | { |
93 | s1[i] = toupper (1 + 23 * i % max_char); |
94 | s2[i] = tolower (s1[i]); |
95 | } |
96 | |
97 | s1[len] = s2[len] = 0; |
98 | s1[len + 1] = 23; |
99 | s2[len + 1] = 24 + exp_result; |
100 | if ((s2[len - 1] == 'z' && exp_result == -1) |
101 | || (s2[len - 1] == 'a' && exp_result == 1)) |
102 | s1[len - 1] += exp_result; |
103 | else |
104 | s2[len - 1] -= exp_result; |
105 | |
106 | printf (format: "Length %4zd, alignment %2zd/%2zd:" , len, align1, align2); |
107 | |
108 | FOR_EACH_IMPL (impl, 0) |
109 | do_one_test (impl, s1, s2, exp_result); |
110 | |
111 | putchar (c: '\n'); |
112 | } |
113 | |
114 | int |
115 | test_main (void) |
116 | { |
117 | size_t i; |
118 | |
119 | test_init (); |
120 | |
121 | printf (format: "%23s" , "" ); |
122 | FOR_EACH_IMPL (impl, 0) |
123 | printf (format: "\t%s" , impl->name); |
124 | putchar (c: '\n'); |
125 | |
126 | for (i = 1; i < 16; ++i) |
127 | { |
128 | do_test (align1: i, align2: i, len: i, max_char: 127, exp_result: 0); |
129 | do_test (align1: i, align2: i, len: i, max_char: 127, exp_result: 1); |
130 | do_test (align1: i, align2: i, len: i, max_char: 127, exp_result: -1); |
131 | } |
132 | |
133 | for (i = 1; i < 10; ++i) |
134 | { |
135 | do_test (align1: 0, align2: 0, len: 2 << i, max_char: 127, exp_result: 0); |
136 | do_test (align1: 0, align2: 0, len: 2 << i, max_char: 254, exp_result: 0); |
137 | do_test (align1: 0, align2: 0, len: 2 << i, max_char: 127, exp_result: 1); |
138 | do_test (align1: 0, align2: 0, len: 2 << i, max_char: 254, exp_result: 1); |
139 | do_test (align1: 0, align2: 0, len: 2 << i, max_char: 127, exp_result: -1); |
140 | do_test (align1: 0, align2: 0, len: 2 << i, max_char: 254, exp_result: -1); |
141 | } |
142 | |
143 | for (i = 1; i < 8; ++i) |
144 | { |
145 | do_test (align1: i, align2: 2 * i, len: 8 << i, max_char: 127, exp_result: 0); |
146 | do_test (align1: 2 * i, align2: i, len: 8 << i, max_char: 254, exp_result: 0); |
147 | do_test (align1: i, align2: 2 * i, len: 8 << i, max_char: 127, exp_result: 1); |
148 | do_test (align1: 2 * i, align2: i, len: 8 << i, max_char: 254, exp_result: 1); |
149 | do_test (align1: i, align2: 2 * i, len: 8 << i, max_char: 127, exp_result: -1); |
150 | do_test (align1: 2 * i, align2: i, len: 8 << i, max_char: 254, exp_result: -1); |
151 | } |
152 | |
153 | return ret; |
154 | } |
155 | |
156 | #include <support/test-driver.c> |
157 | |