Fix build errors with -DNDEBUG.
[glibc.git] / benchtests / bench-strsep.c
blob70dbb377563232a43895abc940d396f2c65237c6
1 /* Measure strsep functions.
2 Copyright (C) 2013-2016 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/>. */
19 #define TEST_MAIN
20 #define TEST_NAME "strsep"
21 #include "bench-string.h"
23 char *
24 simple_strsep (char **s1, char *s2)
26 char *begin;
27 char *s;
28 size_t j = 0;
30 begin = *s1;
31 s = begin;
32 if (begin == NULL)
33 return NULL;
34 ssize_t s2len = strlen (s2);
35 while (*s)
37 for (j = 0; j < s2len; j++)
39 if (*s == s2[j])
41 s[0] = '\0';
42 *s1 = s + 1;
43 return begin;
46 s++;
48 *s1 = NULL;
49 return begin;
52 typedef char *(*proto_t) (const char **, const char *);
54 IMPL (simple_strsep, 0)
55 IMPL (strsep, 1)
57 static void
58 do_one_test (impl_t * impl, const char *s1, const char *s2)
60 size_t i, iters = INNER_LOOP_ITERS;
61 timing_t start, stop, cur;
63 TIMING_NOW (start);
64 for (i = 0; i < iters; ++i)
66 CALL (impl, &s1, s2);
68 TIMING_NOW (stop);
70 TIMING_DIFF (cur, start, stop);
72 TIMING_PRINT_MEAN ((double) cur, (double) iters);
75 static void
76 do_test (size_t align1, size_t align2, size_t len1, size_t len2, int fail)
78 char *s2 = (char *) (buf2 + align2);
79 static const char d[] = "1234567890abcdef";
80 #define dl (sizeof (d) - 1)
81 char *ss2 = s2;
82 for (size_t l = len2; l > 0; l = l > dl ? l - dl : 0)
84 size_t t = l > dl ? dl : l;
85 ss2 = mempcpy (ss2, d, t);
87 s2[len2] = '\0';
89 printf ("Length %4zd/%zd, alignment %2zd/%2zd, %s:",
90 len1, len2, align1, align2, fail ? "fail" : "found");
92 FOR_EACH_IMPL (impl, 0)
94 char *s1 = (char *) (buf1 + align1);
95 if (fail)
97 char *ss1 = s1;
98 for (size_t l = len1; l > 0; l = l > dl ? l - dl : 0)
100 size_t t = l > dl ? dl : l;
101 memcpy (ss1, d, t);
102 ++ss1[len2 > 7 ? 7 : len2 - 1];
103 ss1 += t;
106 else
108 memset (s1, '0', len1);
109 memcpy (s1 + (len1 - len2) - 2, s2, len2);
110 if ((len1 / len2) > 4)
111 memcpy (s1 + (len1 - len2) - (3 * len2), s2, len2);
113 s1[len1] = '\0';
114 do_one_test (impl, s1, s2);
116 putchar ('\n');
119 static int
120 test_main (void)
122 test_init ();
124 printf ("%23s", "");
125 FOR_EACH_IMPL (impl, 0)
126 printf ("\t%s", impl->name);
127 putchar ('\n');
129 for (size_t klen = 2; klen < 32; ++klen)
130 for (size_t hlen = 2 * klen; hlen < 16 * klen; hlen += klen)
132 do_test (0, 0, hlen, klen, 0);
133 do_test (0, 0, hlen, klen, 1);
134 do_test (0, 3, hlen, klen, 0);
135 do_test (0, 3, hlen, klen, 1);
136 do_test (0, 9, hlen, klen, 0);
137 do_test (0, 9, hlen, klen, 1);
138 do_test (0, 15, hlen, klen, 0);
139 do_test (0, 15, hlen, klen, 1);
141 do_test (3, 0, hlen, klen, 0);
142 do_test (3, 0, hlen, klen, 1);
143 do_test (3, 3, hlen, klen, 0);
144 do_test (3, 3, hlen, klen, 1);
145 do_test (3, 9, hlen, klen, 0);
146 do_test (3, 9, hlen, klen, 1);
147 do_test (3, 15, hlen, klen, 0);
148 do_test (3, 15, hlen, klen, 1);
150 do_test (9, 0, hlen, klen, 0);
151 do_test (9, 0, hlen, klen, 1);
152 do_test (9, 3, hlen, klen, 0);
153 do_test (9, 3, hlen, klen, 1);
154 do_test (9, 9, hlen, klen, 0);
155 do_test (9, 9, hlen, klen, 1);
156 do_test (9, 15, hlen, klen, 0);
157 do_test (9, 15, hlen, klen, 1);
159 do_test (15, 0, hlen, klen, 0);
160 do_test (15, 0, hlen, klen, 1);
161 do_test (15, 3, hlen, klen, 0);
162 do_test (15, 3, hlen, klen, 1);
163 do_test (15, 9, hlen, klen, 0);
164 do_test (15, 9, hlen, klen, 1);
165 do_test (15, 15, hlen, klen, 0);
166 do_test (15, 15, hlen, klen, 1);
168 do_test (0, 0, page_size - 1, 16, 0);
169 do_test (0, 0, page_size - 1, 16, 1);
171 return ret;
174 #include "../test-skeleton.c"