1 /* Test strlen functions.
2 Copyright (C) 1999-2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Jakub Jelinek <jakub@redhat.com>, 1999.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
22 # define TEST_NAME "strnlen"
24 # define TEST_NAME "wcsnlen"
26 #include "test-string.h"
29 # define STRNLEN strnlen
31 # define BIG_CHAR CHAR_MAX
32 # define MIDDLE_CHAR 127
33 # define SIMPLE_STRNLEN simple_strnlen
36 # define STRNLEN wcsnlen
38 # define BIG_CHAR WCHAR_MAX
39 # define MIDDLE_CHAR 1121
40 # define SIMPLE_STRNLEN simple_wcsnlen
43 typedef size_t (*proto_t
) (const CHAR
*, size_t);
44 size_t SIMPLE_STRNLEN (const CHAR
*, size_t);
46 IMPL (SIMPLE_STRNLEN
, 0)
50 SIMPLE_STRNLEN (const CHAR
*s
, size_t maxlen
)
54 for (i
= 0; i
< maxlen
&& s
[i
]; ++i
);
59 do_one_test (impl_t
*impl
, const CHAR
*s
, size_t maxlen
, size_t exp_len
)
61 size_t len
= CALL (impl
, s
, maxlen
);
64 error (0, 0, "Wrong result in function %s %zd %zd", impl
->name
,
72 do_test (size_t align
, size_t len
, size_t maxlen
, int max_char
)
77 if ((align
+ len
) * sizeof (CHAR
) >= page_size
)
80 CHAR
*buf
= (CHAR
*) (buf1
);
82 for (i
= 0; i
< len
; ++i
)
83 buf
[align
+ i
] = 1 + 11111 * i
% max_char
;
86 FOR_EACH_IMPL (impl
, 0)
87 do_one_test (impl
, (CHAR
*) (buf
+ align
), maxlen
, MIN (len
, maxlen
));
91 do_random_tests (void)
93 size_t i
, j
, n
, align
, len
;
94 CHAR
*p
= (CHAR
*) (buf1
+ page_size
- 512 * sizeof (CHAR
));
96 for (n
= 0; n
< ITERATIONS
; n
++)
98 align
= random () & 15;
99 len
= random () & 511;
100 if (len
+ align
> 510)
101 len
= 511 - align
- (random () & 7);
102 j
= len
+ align
+ 64;
106 for (i
= 0; i
< j
; i
++)
108 if (i
== len
+ align
)
112 p
[i
] = random () & 255;
113 if (i
>= align
&& i
< len
+ align
&& !p
[i
])
114 p
[i
] = (random () & 127) + 1;
118 FOR_EACH_IMPL (impl
, 1)
121 && CALL (impl
, (CHAR
*) (p
+ align
), len
- 1) != len
- 1)
123 error (0, 0, "Iteration %zd (limited) - wrong result in function %s (%zd) %zd != %zd, p %p",
124 n
, impl
->name
, align
,
125 CALL (impl
, (CHAR
*) (p
+ align
), len
- 1), len
- 1, p
);
128 if (CALL (impl
, (CHAR
*) (p
+ align
), len
) != len
)
130 error (0, 0, "Iteration %zd (exact) - wrong result in function %s (%zd) %zd != %zd, p %p",
131 n
, impl
->name
, align
,
132 CALL (impl
, (CHAR
*) (p
+ align
), len
), len
, p
);
135 if (CALL (impl
, (CHAR
*) (p
+ align
), len
+ 1) != len
)
137 error (0, 0, "Iteration %zd (long) - wrong result in function %s (%zd) %zd != %zd, p %p",
138 n
, impl
->name
, align
,
139 CALL (impl
, (CHAR
*) (p
+ align
), len
+ 1), len
, p
);
154 FOR_EACH_IMPL (impl
, 0)
155 printf ("\t%s", impl
->name
);
158 for (i
= 1; i
< 8; ++i
)
160 do_test (0, i
, i
- 1, MIDDLE_CHAR
);
161 do_test (0, i
, i
, MIDDLE_CHAR
);
162 do_test (0, i
, i
+ 1, MIDDLE_CHAR
);
165 for (i
= 1; i
< 8; ++i
)
167 do_test (i
, i
, i
- 1, MIDDLE_CHAR
);
168 do_test (i
, i
, i
, MIDDLE_CHAR
);
169 do_test (i
, i
, i
+ 1, MIDDLE_CHAR
);
172 for (i
= 2; i
<= 10; ++i
)
174 do_test (0, 1 << i
, 5000, MIDDLE_CHAR
);
175 do_test (1, 1 << i
, 5000, MIDDLE_CHAR
);
178 for (i
= 1; i
< 8; ++i
)
179 do_test (0, i
, 5000, BIG_CHAR
);
181 for (i
= 1; i
< 8; ++i
)
182 do_test (i
, i
, 5000, BIG_CHAR
);
184 for (i
= 2; i
<= 10; ++i
)
186 do_test (0, 1 << i
, 5000, BIG_CHAR
);
187 do_test (1, 1 << i
, 5000, BIG_CHAR
);
194 #include "../test-skeleton.c"