Fix typo in preprocessor conditional
[glibc.git] / benchtests / bench-strncat.c
blob1c7cb80732e9f2c010d1baf2c67603865d61a37f
1 /* Measure strncat functions.
2 Copyright (C) 2013-2014 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 "strncat"
21 #include "bench-string.h"
23 typedef char *(*proto_t) (char *, const char *, size_t);
24 char *stupid_strncat (char *, const char *, size_t);
25 char *simple_strncat (char *, const char *, size_t);
27 IMPL (stupid_strncat, 0)
28 IMPL (strncat, 2)
30 char *
31 stupid_strncat (char *dst, const char *src, size_t n)
33 char *ret = dst;
34 while (*dst++ != '\0');
35 --dst;
36 while (n--)
37 if ( (*dst++ = *src++) == '\0')
38 return ret;
39 *dst = '\0';
40 return ret;
43 static void
44 do_one_test (impl_t *impl, char *dst, const char *src, size_t n)
46 size_t k = strlen (dst), i, iters = INNER_LOOP_ITERS;
47 timing_t start, stop, cur;
49 if (CALL (impl, dst, src, n) != dst)
51 error (0, 0, "Wrong result in function %s %p != %p", impl->name,
52 CALL (impl, dst, src, n), dst);
53 ret = 1;
54 return;
57 size_t len = strlen (src);
58 if (memcmp (dst + k, src, len + 1 > n ? n : len + 1) != 0)
60 error (0, 0, "Incorrect cancatination in function %s",
61 impl->name);
62 ret = 1;
63 return;
65 if (n < len && dst[k + n] != '\0')
67 error (0, 0, "There is no zero in the end of output string in %s",
68 impl->name);
69 ret = 1;
70 return;
73 TIMING_NOW (start);
74 for (i = 0; i < iters; ++i)
76 dst[k] = '\0';
77 CALL (impl, dst, src, n);
79 TIMING_NOW (stop);
81 TIMING_DIFF (cur, start, stop);
83 TIMING_PRINT_MEAN ((double) cur, (double) iters);
86 static void
87 do_test (size_t align1, size_t align2, size_t len1, size_t len2,
88 size_t n, int max_char)
90 size_t i;
91 char *s1, *s2;
93 align1 &= 7;
94 if (align1 + len1 >= page_size)
95 return;
96 if (align1 + n > page_size)
97 return;
98 align2 &= 7;
99 if (align2 + len1 + len2 >= page_size)
100 return;
101 if (align2 + len1 + n > page_size)
102 return;
103 s1 = (char *) (buf1 + align1);
104 s2 = (char *) (buf2 + align2);
106 for (i = 0; i < len1; ++i)
107 s1[i] = 32 + 23 * i % (max_char - 32);
108 s1[len1] = '\0';
110 for (i = 0; i < len2; i++)
111 s2[i] = 32 + 23 * i % (max_char - 32);
113 printf ("Length %4zd/%4zd, alignment %2zd/%2zd, N %4zd:",
114 len1, len2, align1, align2, n);
116 FOR_EACH_IMPL (impl, 0)
118 s2[len2] = '\0';
119 do_one_test (impl, s2, s1, n);
122 putchar ('\n');
126 main (void)
128 size_t i, n;
130 test_init ();
132 printf ("%28s", "");
133 FOR_EACH_IMPL (impl, 0)
134 printf ("\t%s", impl->name);
135 putchar ('\n');
137 for (n = 2; n <= 2048; n*=4)
139 do_test (0, 2, 2, 2, n, 127);
140 do_test (0, 0, 4, 4, n, 127);
141 do_test (4, 0, 4, 4, n, 255);
142 do_test (0, 0, 8, 8, n, 127);
143 do_test (0, 8, 8, 8, n, 127);
145 for (i = 1; i < 8; ++i)
147 do_test (0, 0, 8 << i, 8 << i, n, 127);
148 do_test (8 - i, 2 * i, 8 << i, 8 << i, n, 127);
149 do_test (0, 0, 8 << i, 2 << i, n, 127);
150 do_test (8 - i, 2 * i, 8 << i, 2 << i, n, 127);
153 for (i = 1; i < 8; ++i)
155 do_test (i, 2 * i, 8 << i, 1, n, 127);
156 do_test (2 * i, i, 8 << i, 1, n, 255);
157 do_test (i, i, 8 << i, 10, n, 127);
161 return ret;