1 /* Measure strncpy 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/>. */
19 #ifndef STRNCPY_RESULT
20 # define STRNCPY_RESULT(dst, len, n) dst
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)
33 simple_strncpy (char *dst
, const char *src
, size_t n
)
37 if ((*dst
++ = *src
++) == '\0')
47 stupid_strncpy (char *dst
, const char *src
, size_t n
)
49 size_t nc
= strnlen (src
, n
);
52 for (i
= 0; i
< nc
; ++i
)
60 typedef char *(*proto_t
) (char *, const char *, size_t);
63 do_one_test (impl_t
*impl
, char *dst
, const char *src
, size_t len
, size_t n
)
65 if (CALL (impl
, dst
, src
, n
) != STRNCPY_RESULT (dst
, len
, n
))
67 error (0, 0, "Wrong result in function %s %p %p", impl
->name
,
68 CALL (impl
, dst
, src
, n
), dst
);
73 if (memcmp (dst
, src
, len
> n
? n
: len
) != 0)
75 error (0, 0, "Wrong result in function %s", impl
->name
);
84 for (i
= len
; i
< n
; ++i
)
87 error (0, 0, "Wrong result in function %s", impl
->name
);
95 hp_timing_t start
__attribute__ ((unused
));
96 hp_timing_t stop
__attribute__ ((unused
));
97 hp_timing_t best_time
= ~ (hp_timing_t
) 0;
100 for (i
= 0; i
< 32; ++i
)
102 HP_TIMING_NOW (start
);
103 CALL (impl
, dst
, src
, n
);
104 HP_TIMING_NOW (stop
);
105 HP_TIMING_BEST (best_time
, start
, stop
);
108 printf ("\t%zd", (size_t) best_time
);
113 do_test (size_t align1
, size_t align2
, size_t len
, size_t n
, int max_char
)
119 if (align1
+ len
>= page_size
)
123 if (align2
+ len
>= page_size
)
126 s1
= (char *) (buf1
+ align1
);
127 s2
= (char *) (buf2
+ align2
);
129 for (i
= 0; i
< len
; ++i
)
130 s1
[i
] = 32 + 23 * i
% (max_char
- 32);
132 for (i
= len
+ 1; i
+ align1
< page_size
&& i
< len
+ 64; ++i
)
133 s1
[i
] = 32 + 32 * i
% (max_char
- 32);
136 printf ("Length %4zd, n %4zd, alignment %2zd/%2zd:", len
, n
, align1
, align2
);
138 FOR_EACH_IMPL (impl
, 0)
139 do_one_test (impl
, s2
, s1
, len
, n
);
153 FOR_EACH_IMPL (impl
, 0)
154 printf ("\t%s", impl
->name
);
157 for (i
= 1; i
< 8; ++i
)
159 do_test (i
, i
, 16, 16, 127);
160 do_test (i
, i
, 16, 16, 255);
161 do_test (i
, 2 * i
, 16, 16, 127);
162 do_test (2 * i
, i
, 16, 16, 255);
163 do_test (8 - i
, 2 * i
, 1 << i
, 2 << i
, 127);
164 do_test (2 * i
, 8 - i
, 2 << i
, 1 << i
, 127);
165 do_test (8 - i
, 2 * i
, 1 << i
, 2 << i
, 255);
166 do_test (2 * i
, 8 - i
, 2 << i
, 1 << i
, 255);
169 for (i
= 1; i
< 8; ++i
)
171 do_test (0, 0, 4 << i
, 8 << i
, 127);
172 do_test (0, 0, 16 << i
, 8 << i
, 127);
173 do_test (8 - i
, 2 * i
, 4 << i
, 8 << i
, 127);
174 do_test (8 - i
, 2 * i
, 16 << i
, 8 << i
, 127);
180 #include "../test-skeleton.c"