Fix formatting in malloc_info
[glibc.git] / benchtests / bench-strncpy.c
bloba35c71f8b4276aa3a8c0240d73db5b6d561c5943
1 /* Measure strncpy 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 #ifndef STRNCPY_RESULT
20 # define STRNCPY_RESULT(dst, len, n) dst
21 # define TEST_MAIN
22 # define TEST_NAME "strncpy"
23 # include "bench-string.h"
25 char *simple_strncpy (char *, const char *, size_t);
26 char *stupid_strncpy (char *, const char *, size_t);
28 IMPL (stupid_strncpy, 0)
29 IMPL (simple_strncpy, 0)
30 IMPL (strncpy, 1)
32 char *
33 simple_strncpy (char *dst, const char *src, size_t n)
35 char *ret = dst;
36 while (n--)
37 if ((*dst++ = *src++) == '\0')
39 while (n--)
40 *dst++ = '\0';
41 return ret;
43 return ret;
46 char *
47 stupid_strncpy (char *dst, const char *src, size_t n)
49 size_t nc = strnlen (src, n);
50 size_t i;
52 for (i = 0; i < nc; ++i)
53 dst[i] = src[i];
54 for (; i < n; ++i)
55 dst[i] = '\0';
56 return dst;
58 #endif
60 typedef char *(*proto_t) (char *, const char *, size_t);
62 static void
63 do_one_test (impl_t *impl, char *dst, const char *src, size_t len, size_t n)
65 size_t i, iters = INNER_LOOP_ITERS;
66 timing_t start, stop, cur;
68 if (CALL (impl, dst, src, n) != STRNCPY_RESULT (dst, len, n))
70 error (0, 0, "Wrong result in function %s %p %p", impl->name,
71 CALL (impl, dst, src, n), dst);
72 ret = 1;
73 return;
76 if (memcmp (dst, src, len > n ? n : len) != 0)
78 error (0, 0, "Wrong result in function %s", impl->name);
79 ret = 1;
80 return;
83 if (n > len)
85 size_t i;
87 for (i = len; i < n; ++i)
88 if (dst [i] != '\0')
90 error (0, 0, "Wrong result in function %s", impl->name);
91 ret = 1;
92 return;
96 TIMING_NOW (start);
97 for (i = 0; i < iters; ++i)
99 CALL (impl, dst, src, n);
101 TIMING_NOW (stop);
103 TIMING_DIFF (cur, start, stop);
105 TIMING_PRINT_MEAN ((double) cur, (double) iters);
108 static void
109 do_test (size_t align1, size_t align2, size_t len, size_t n, int max_char)
111 size_t i;
112 char *s1, *s2;
114 align1 &= 7;
115 if (align1 + len >= page_size)
116 return;
118 align2 &= 7;
119 if (align2 + len >= page_size)
120 return;
122 s1 = (char *) (buf1 + align1);
123 s2 = (char *) (buf2 + align2);
125 for (i = 0; i < len; ++i)
126 s1[i] = 32 + 23 * i % (max_char - 32);
127 s1[len] = 0;
128 for (i = len + 1; i + align1 < page_size && i < len + 64; ++i)
129 s1[i] = 32 + 32 * i % (max_char - 32);
131 printf ("Length %4zd, n %4zd, alignment %2zd/%2zd:", len, n, align1, align2);
133 FOR_EACH_IMPL (impl, 0)
134 do_one_test (impl, s2, s1, len, n);
136 putchar ('\n');
140 test_main (void)
142 size_t i;
144 test_init ();
146 printf ("%28s", "");
147 FOR_EACH_IMPL (impl, 0)
148 printf ("\t%s", impl->name);
149 putchar ('\n');
151 for (i = 1; i < 8; ++i)
153 do_test (i, i, 16, 16, 127);
154 do_test (i, i, 16, 16, 255);
155 do_test (i, 2 * i, 16, 16, 127);
156 do_test (2 * i, i, 16, 16, 255);
157 do_test (8 - i, 2 * i, 1 << i, 2 << i, 127);
158 do_test (2 * i, 8 - i, 2 << i, 1 << i, 127);
159 do_test (8 - i, 2 * i, 1 << i, 2 << i, 255);
160 do_test (2 * i, 8 - i, 2 << i, 1 << i, 255);
163 for (i = 1; i < 8; ++i)
165 do_test (0, 0, 4 << i, 8 << i, 127);
166 do_test (0, 0, 16 << i, 8 << i, 127);
167 do_test (8 - i, 2 * i, 4 << i, 8 << i, 127);
168 do_test (8 - i, 2 * i, 16 << i, 8 << i, 127);
171 return ret;
174 #include "../test-skeleton.c"