Set GL(dl_nns) to 1 for vDSO in libc.a
[glibc.git] / string / test-strnlen.c
blob547df45ad877d20dfb4a9d536a9c1191977e52b9
1 /* Test and measure strlen functions.
2 Copyright (C) 1999, 2002, 2003, 2005, 2010 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/>. */
20 #define TEST_MAIN
21 #include "test-string.h"
23 typedef size_t (*proto_t) (const char *, size_t);
24 size_t simple_strnlen (const char *, size_t);
26 IMPL (simple_strnlen, 0)
27 IMPL (strnlen, 1)
29 size_t
30 simple_strnlen (const char *s, size_t maxlen)
32 size_t i;
34 for (i = 0; i < maxlen && s[i]; ++i);
35 return i;
38 static void
39 do_one_test (impl_t *impl, const char *s, size_t maxlen, size_t exp_len)
41 size_t len = CALL (impl, s, maxlen);
42 if (len != exp_len)
44 error (0, 0, "Wrong result in function %s %zd %zd", impl->name,
45 len, exp_len);
46 ret = 1;
47 return;
50 if (HP_TIMING_AVAIL)
52 hp_timing_t start __attribute ((unused));
53 hp_timing_t stop __attribute ((unused));
54 hp_timing_t best_time = ~ (hp_timing_t) 0;
55 size_t i;
57 for (i = 0; i < 32; ++i)
59 HP_TIMING_NOW (start);
60 CALL (impl, s, maxlen);
61 HP_TIMING_NOW (stop);
62 HP_TIMING_BEST (best_time, start, stop);
65 printf ("\t%zd", (size_t) best_time);
69 static void
70 do_test (size_t align, size_t len, size_t maxlen, int max_char)
72 size_t i;
74 align &= 7;
75 if (align + len >= page_size)
76 return;
78 for (i = 0; i < len; ++i)
79 buf1[align + i] = 1 + 7 * i % max_char;
80 buf1[align + len] = 0;
82 if (HP_TIMING_AVAIL)
83 printf ("Length %4zd, alignment %2zd:", len, align);
85 FOR_EACH_IMPL (impl, 0)
86 do_one_test (impl, (char *) (buf1 + align), maxlen, MIN (len, maxlen));
88 if (HP_TIMING_AVAIL)
89 putchar ('\n');
92 static void
93 do_random_tests (void)
95 size_t i, j, n, align, len;
96 unsigned char *p = buf1 + page_size - 512;
98 for (n = 0; n < ITERATIONS; n++)
100 align = random () & 15;
101 len = random () & 511;
102 if (len + align > 510)
103 len = 511 - align - (random () & 7);
104 j = len + align + 64;
105 if (j > 512)
106 j = 512;
108 for (i = 0; i < j; i++)
110 if (i == len + align)
111 p[i] = 0;
112 else
114 p[i] = random () & 255;
115 if (i >= align && i < len + align && !p[i])
116 p[i] = (random () & 127) + 1;
120 FOR_EACH_IMPL (impl, 1)
122 if (len > 0
123 && CALL (impl, (char *) (p + align), len - 1) != len - 1)
125 error (0, 0, "Iteration %zd (limited) - wrong result in function %s (%zd) %zd != %zd, p %p",
126 n, impl->name, align,
127 CALL (impl, (char *) (p + align), len - 1), len - 1, p);
128 ret = 1;
130 if (CALL (impl, (char *) (p + align), len) != len)
132 error (0, 0, "Iteration %zd (exact) - wrong result in function %s (%zd) %zd != %zd, p %p",
133 n, impl->name, align,
134 CALL (impl, (char *) (p + align), len), len, p);
135 ret = 1;
137 if (CALL (impl, (char *) (p + align), len + 1) != len)
139 error (0, 0, "Iteration %zd (long) - wrong result in function %s (%zd) %zd != %zd, p %p",
140 n, impl->name, align,
141 CALL (impl, (char *) (p + align), len + 1), len, p);
142 ret = 1;
149 test_main (void)
151 size_t i;
153 test_init ();
155 printf ("%20s", "");
156 FOR_EACH_IMPL (impl, 0)
157 printf ("\t%s", impl->name);
158 putchar ('\n');
160 for (i = 1; i < 8; ++i)
162 do_test (0, i, i - 1, 127);
163 do_test (0, i, i, 127);
164 do_test (0, i, i + 1, 127);
167 for (i = 1; i < 8; ++i)
169 do_test (i, i, i - 1, 127);
170 do_test (i, i, i, 127);
171 do_test (i, i, i + 1, 127);
174 for (i = 2; i <= 10; ++i)
176 do_test (0, 1 << i, 5000, 127);
177 do_test (1, 1 << i, 5000, 127);
180 for (i = 1; i < 8; ++i)
181 do_test (0, i, 5000, 255);
183 for (i = 1; i < 8; ++i)
184 do_test (i, i, 5000, 255);
186 for (i = 2; i <= 10; ++i)
188 do_test (0, 1 << i, 5000, 255);
189 do_test (1, 1 << i, 5000, 255);
192 do_random_tests ();
193 return ret;
196 #include "../test-skeleton.c"