1 /* Measure strncat functions.
2 Copyright (C) 2013 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/>. */
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)
31 stupid_strncat (char *dst
, const char *src
, size_t n
)
34 while (*dst
++ != '\0');
37 if ( (*dst
++ = *src
++) == '\0')
44 do_one_test (impl_t
*impl
, char *dst
, const char *src
, size_t n
)
46 size_t k
= strlen (dst
);
47 if (CALL (impl
, dst
, src
, n
) != dst
)
49 error (0, 0, "Wrong result in function %s %p != %p", impl
->name
,
50 CALL (impl
, dst
, src
, n
), dst
);
55 size_t len
= strlen (src
);
56 if (memcmp (dst
+ k
, src
, len
+ 1 > n
? n
: len
+ 1) != 0)
58 error (0, 0, "Incorrect cancatination in function %s",
63 if (n
< len
&& dst
[k
+ n
] != '\0')
65 error (0, 0, "There is no zero in the end of output string in %s",
72 hp_timing_t start
__attribute ((unused
));
73 hp_timing_t stop
__attribute ((unused
));
74 hp_timing_t best_time
= ~ (hp_timing_t
) 0;
77 for (i
= 0; i
< 32; ++i
)
80 HP_TIMING_NOW (start
);
81 CALL (impl
, dst
, src
, n
);
83 HP_TIMING_BEST (best_time
, start
, stop
);
86 printf ("\t%zd", (size_t) best_time
);
91 do_test (size_t align1
, size_t align2
, size_t len1
, size_t len2
,
92 size_t n
, int max_char
)
98 if (align1
+ len1
>= page_size
)
100 if (align1
+ n
> page_size
)
103 if (align2
+ len1
+ len2
>= page_size
)
105 if (align2
+ len1
+ n
> page_size
)
107 s1
= (char *) (buf1
+ align1
);
108 s2
= (char *) (buf2
+ align2
);
110 for (i
= 0; i
< len1
; ++i
)
111 s1
[i
] = 32 + 23 * i
% (max_char
- 32);
114 for (i
= 0; i
< len2
; i
++)
115 s2
[i
] = 32 + 23 * i
% (max_char
- 32);
118 printf ("Length %4zd/%4zd, alignment %2zd/%2zd, N %4zd:",
119 len1
, len2
, align1
, align2
, n
);
121 FOR_EACH_IMPL (impl
, 0)
124 do_one_test (impl
, s2
, s1
, n
);
139 FOR_EACH_IMPL (impl
, 0)
140 printf ("\t%s", impl
->name
);
143 for (n
= 2; n
<= 2048; n
*=4)
145 do_test (0, 2, 2, 2, n
, 127);
146 do_test (0, 0, 4, 4, n
, 127);
147 do_test (4, 0, 4, 4, n
, 255);
148 do_test (0, 0, 8, 8, n
, 127);
149 do_test (0, 8, 8, 8, n
, 127);
151 for (i
= 1; i
< 8; ++i
)
153 do_test (0, 0, 8 << i
, 8 << i
, n
, 127);
154 do_test (8 - i
, 2 * i
, 8 << i
, 8 << i
, n
, 127);
155 do_test (0, 0, 8 << i
, 2 << i
, n
, 127);
156 do_test (8 - i
, 2 * i
, 8 << i
, 2 << i
, n
, 127);
159 for (i
= 1; i
< 8; ++i
)
161 do_test (i
, 2 * i
, 8 << i
, 1, n
, 127);
162 do_test (2 * i
, i
, 8 << i
, 1, n
, 255);
163 do_test (i
, i
, 8 << i
, 10, n
, 127);