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/>. */
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
), 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
);
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",
65 if (n
< len
&& dst
[k
+ n
] != '\0')
67 error (0, 0, "There is no zero in the end of output string in %s",
74 for (i
= 0; i
< iters
; ++i
)
77 CALL (impl
, dst
, src
, n
);
81 TIMING_DIFF (cur
, start
, stop
);
83 TIMING_PRINT_MEAN ((double) cur
, (double) iters
);
87 do_test (size_t align1
, size_t align2
, size_t len1
, size_t len2
,
88 size_t n
, int max_char
)
94 if (align1
+ len1
>= page_size
)
96 if (align1
+ n
> page_size
)
99 if (align2
+ len1
+ len2
>= page_size
)
101 if (align2
+ len1
+ n
> page_size
)
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);
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)
119 do_one_test (impl
, s2
, s1
, n
);
133 FOR_EACH_IMPL (impl
, 0)
134 printf ("\t%s", impl
->name
);
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);