Fix testsuite build for GCC 7 -Wformat-truncation.
[glibc.git] / benchtests / bench-memchr.c
blob16099ac3a22677f9eb6674af743d683edf99c6d9
1 /* Measure memchr functions.
2 Copyright (C) 2013-2017 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 #ifndef WIDE
20 # define CHAR char
21 # define SMALL_CHAR 127
22 #else
23 # include <wchar.h>
24 # define CHAR wchar_t
25 # define SMALL_CHAR 1273
26 #endif /* WIDE */
28 #ifndef USE_AS_MEMRCHR
29 # define TEST_MAIN
30 # ifndef WIDE
31 # define TEST_NAME "memchr"
32 # else
33 # define TEST_NAME "wmemchr"
34 # endif /* WIDE */
35 # include "bench-string.h"
37 # ifndef WIDE
38 # define MEMCHR memchr
39 # define SIMPLE_MEMCHR simple_memchr
40 # else
41 # define MEMCHR wmemchr
42 # define SIMPLE_MEMCHR simple_wmemchr
43 # endif /* WIDE */
45 typedef CHAR *(*proto_t) (const CHAR *, int, size_t);
46 CHAR *SIMPLE_MEMCHR (const CHAR *, int, size_t);
48 IMPL (SIMPLE_MEMCHR, 0)
49 IMPL (MEMCHR, 1)
51 CHAR *
52 SIMPLE_MEMCHR (const CHAR *s, int c, size_t n)
54 while (n--)
55 if (*s++ == (CHAR) c)
56 return (CHAR *) s - 1;
57 return NULL;
59 #endif /* !USE_AS_MEMRCHR */
61 static void
62 do_one_test (impl_t *impl, const CHAR *s, int c, size_t n, CHAR *exp_res)
64 CHAR *res = CALL (impl, s, c, n);
65 size_t i, iters = INNER_LOOP_ITERS;
66 timing_t start, stop, cur;
68 if (res != exp_res)
70 error (0, 0, "Wrong result in function %s %p %p", impl->name,
71 res, exp_res);
72 ret = 1;
73 return;
76 TIMING_NOW (start);
77 for (i = 0; i < iters; ++i)
79 CALL (impl, s, c, n);
81 TIMING_NOW (stop);
83 TIMING_DIFF (cur, start, stop);
85 TIMING_PRINT_MEAN ((double) cur, (double) iters);
88 static void
89 do_test (size_t align, size_t pos, size_t len, int seek_char)
91 size_t i;
92 CHAR *result;
94 align &= 7;
95 if ((align + len) * sizeof (CHAR) >= page_size)
96 return;
98 CHAR *buf = (CHAR *) (buf1);
100 for (i = 0; i < len; ++i)
102 buf[align + i] = 1 + 23 * i % SMALL_CHAR;
103 if (buf[align + i] == seek_char)
104 buf[align + i] = seek_char + 1;
106 buf[align + len] = 0;
108 if (pos < len)
110 buf[align + pos] = seek_char;
111 buf[align + len] = -seek_char;
112 result = (CHAR *) (buf + align + pos);
114 else
116 result = NULL;
117 buf[align + len] = seek_char;
120 printf ("Length %4zd, alignment %2zd:", pos, align);
122 FOR_EACH_IMPL (impl, 0)
123 do_one_test (impl, (CHAR *) (buf + align), seek_char, len, result);
125 putchar ('\n');
129 test_main (void)
131 size_t i;
133 test_init ();
135 printf ("%20s", "");
136 FOR_EACH_IMPL (impl, 0)
137 printf ("\t%s", impl->name);
138 putchar ('\n');
140 for (i = 1; i < 8; ++i)
142 do_test (0, 16 << i, 2048, 23);
143 do_test (i, 64, 256, 23);
144 do_test (0, 16 << i, 2048, 0);
145 do_test (i, 64, 256, 0);
147 for (i = 1; i < 32; ++i)
149 do_test (0, i, i + 1, 23);
150 do_test (0, i, i + 1, 0);
153 return ret;
156 #include <support/test-driver.c>