source: branches/libc-0.6/src/libctests/glibc/string/test-strlen.c

Last change on this file was 2036, checked in by bird, 20 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 3.8 KB
Line 
1/* Test and measure strlen functions.
2 Copyright (C) 1999, 2002, 2003 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Jakub Jelinek <jakub@redhat.com>, 1999.
5
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.
10
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.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
20
21#define TEST_MAIN
22#include "test-string.h"
23
24typedef size_t (*proto_t) (const char *);
25size_t simple_strlen (const char *);
26size_t builtin_strlen (const char *);
27
28IMPL (simple_strlen, 0)
29IMPL (builtin_strlen, 0)
30IMPL (strlen, 1)
31
32size_t
33simple_strlen (const char *s)
34{
35 const char *p;
36
37 for (p = s; *p; ++p);
38 return p - s;
39}
40
41size_t
42builtin_strlen (const char *p)
43{
44 return __builtin_strlen (p);
45}
46
47static void
48do_one_test (impl_t *impl, const char *s, size_t exp_len)
49{
50 size_t len = CALL (impl, s);
51 if (len != exp_len)
52 {
53 error (0, 0, "Wrong result in function %s %zd %zd", impl->name,
54 len, exp_len);
55 ret = 1;
56 return;
57 }
58
59 if (HP_TIMING_AVAIL)
60 {
61 hp_timing_t start __attribute ((unused));
62 hp_timing_t stop __attribute ((unused));
63 hp_timing_t best_time = ~ (hp_timing_t) 0;
64 size_t i;
65
66 for (i = 0; i < 32; ++i)
67 {
68 HP_TIMING_NOW (start);
69 CALL (impl, s);
70 HP_TIMING_NOW (stop);
71 HP_TIMING_BEST (best_time, start, stop);
72 }
73
74 printf ("\t%zd", (size_t) best_time);
75 }
76}
77
78static void
79do_test (size_t align, size_t len, int max_char)
80{
81 size_t i;
82
83 align &= 7;
84 if (align + len >= page_size)
85 return;
86
87 for (i = 0; i < len; ++i)
88 buf1[align + i] = 1 + 7 * i % max_char;
89 buf1[align + len] = 0;
90
91 if (HP_TIMING_AVAIL)
92 printf ("Length %4zd, alignment %2zd:", len, align);
93
94 FOR_EACH_IMPL (impl, 0)
95 do_one_test (impl, buf1 + align, len);
96
97 if (HP_TIMING_AVAIL)
98 putchar ('\n');
99}
100
101static void
102do_random_tests (void)
103{
104 size_t i, j, n, align, len;
105 unsigned char *p = buf1 + page_size - 512;
106
107 for (n = 0; n < ITERATIONS; n++)
108 {
109 align = random () & 15;
110 len = random () & 511;
111 if (len + align > 510)
112 len = 511 - align - (random () & 7);
113 j = len + align + 64;
114 if (j > 512)
115 j = 512;
116
117 for (i = 0; i < j; i++)
118 {
119 if (i == len + align)
120 p[i] = 0;
121 else
122 {
123 p[i] = random () & 255;
124 if (i >= align && i < len + align && !p[i])
125 p[i] = (random () & 127) + 1;
126 }
127 }
128
129 FOR_EACH_IMPL (impl, 1)
130 if (CALL (impl, p + align) != len)
131 {
132 error (0, 0, "Iteration %zd - wrong result in function %s (%zd) %zd != %zd, p %p",
133 n, impl->name, align, CALL (impl, p + align), len, p);
134 ret = 1;
135 }
136 }
137}
138
139int
140test_main (void)
141{
142 size_t i;
143
144 test_init ();
145
146 printf ("%20s", "");
147 FOR_EACH_IMPL (impl, 0)
148 printf ("\t%s", impl->name);
149 putchar ('\n');
150
151 for (i = 1; i < 8; ++i)
152 do_test (0, i, 127);
153
154 for (i = 1; i < 8; ++i)
155 do_test (i, i, 127);
156
157 for (i = 2; i <= 10; ++i)
158 {
159 do_test (0, 1 << i, 127);
160 do_test (1, 1 << i, 127);
161 }
162
163 for (i = 1; i < 8; ++i)
164 do_test (0, i, 255);
165
166 for (i = 1; i < 8; ++i)
167 do_test (i, i, 255);
168
169 for (i = 2; i <= 10; ++i)
170 {
171 do_test (0, 1 << i, 255);
172 do_test (1, 1 << i, 255);
173 }
174
175 do_random_tests ();
176 return ret;
177}
178
179#include "../test-skeleton.c"
Note: See TracBrowser for help on using the repository browser.