Fix jn precision
[glibc.git] / string / test-strstr.c
blobc1da68663a10a7db8e154321827e935716e61621
1 /* Test and measure strstr functions.
2 Copyright (C) 2010 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2010.
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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #define TEST_MAIN
22 #include "test-string.h"
25 #define STRSTR simple_strstr
26 #include "strstr.c"
29 static char *
30 stupid_strstr (const char *s1, const char *s2)
32 ssize_t s1len = strlen (s1);
33 ssize_t s2len = strlen (s2);
35 if (s2len > s1len)
36 return NULL;
38 for (ssize_t i = 0; i <= s1len - s2len; ++i)
40 size_t j;
41 for (j = 0; j < s2len; ++j)
42 if (s1[i + j] != s2[j])
43 break;
44 if (j == s2len)
45 return (char *) s1 + i;
48 return NULL;
52 typedef char *(*proto_t) (const char *, const char *);
54 IMPL (stupid_strstr, 0)
55 IMPL (simple_strstr, 0)
56 IMPL (strstr, 1)
59 static void
60 do_one_test (impl_t *impl, const char *s1, const char *s2, char *exp_result)
62 char *result = CALL (impl, s1, s2);
63 if (result != exp_result)
65 error (0, 0, "Wrong result in function %s %s %s", impl->name,
66 result, exp_result);
67 ret = 1;
68 return;
71 if (HP_TIMING_AVAIL)
73 hp_timing_t start __attribute ((unused));
74 hp_timing_t stop __attribute ((unused));
75 hp_timing_t best_time = ~(hp_timing_t) 0;
76 size_t i;
78 for (i = 0; i < 32; ++i)
80 HP_TIMING_NOW (start);
81 CALL (impl, s1, s2);
82 HP_TIMING_NOW (stop);
83 HP_TIMING_BEST (best_time, start, stop);
86 printf ("\t%zd", (size_t) best_time);
91 static void
92 do_test (size_t align1, size_t align2, size_t len1, size_t len2,
93 int fail)
95 char *s1 = (char *) (buf1 + align1);
96 char *s2 = (char *) (buf2 + align2);
98 static const char d[] = "1234567890abcdef";
99 #define dl (sizeof (d) - 1)
100 char *ss2 = s2;
101 for (size_t l = len2; l > 0; l = l > dl ? l - dl : 0)
103 size_t t = l > dl ? dl : l;
104 ss2 = mempcpy (ss2, d, t);
106 s2[len2] = '\0';
108 if (fail)
110 char *ss1 = s1;
111 for (size_t l = len1; l > 0; l = l > dl ? l - dl : 0)
113 size_t t = l > dl ? dl : l;
114 memcpy (ss1, d, t);
115 ++ss1[len2 > 7 ? 7 : len2 - 1];
116 ss1 += t;
119 else
121 memset (s1, '0', len1);
122 memcpy (s1 + len1 - len2, s2, len2);
124 s1[len1] = '\0';
126 if (HP_TIMING_AVAIL)
127 printf ("Length %4zd/%zd, alignment %2zd/%2zd, %s:",
128 len1, len2, align1, align2, fail ? "fail" : "found");
130 FOR_EACH_IMPL (impl, 0)
131 do_one_test (impl, s1, s2, fail ? NULL : s1 + len1 - len2);
133 if (HP_TIMING_AVAIL)
134 putchar ('\n');
138 static int
139 test_main (void)
141 test_init ();
143 printf ("%23s", "");
144 FOR_EACH_IMPL (impl, 0)
145 printf ("\t%s", impl->name);
146 putchar ('\n');
148 for (size_t klen = 2; klen < 32; ++klen)
149 for (size_t hlen = 2 * klen; hlen < 16 * klen; hlen += klen)
151 do_test (0, 0, hlen, klen, 0);
152 do_test (0, 0, hlen, klen, 1);
153 do_test (0, 3, hlen, klen, 0);
154 do_test (0, 3, hlen, klen, 1);
155 do_test (0, 9, hlen, klen, 0);
156 do_test (0, 9, hlen, klen, 1);
157 do_test (0, 15, hlen, klen, 0);
158 do_test (0, 15, hlen, klen, 1);
160 do_test (3, 0, hlen, klen, 0);
161 do_test (3, 0, hlen, klen, 1);
162 do_test (3, 3, hlen, klen, 0);
163 do_test (3, 3, hlen, klen, 1);
164 do_test (3, 9, hlen, klen, 0);
165 do_test (3, 9, hlen, klen, 1);
166 do_test (3, 15, hlen, klen, 0);
167 do_test (3, 15, hlen, klen, 1);
169 do_test (9, 0, hlen, klen, 0);
170 do_test (9, 0, hlen, klen, 1);
171 do_test (9, 3, hlen, klen, 0);
172 do_test (9, 3, hlen, klen, 1);
173 do_test (9, 9, hlen, klen, 0);
174 do_test (9, 9, hlen, klen, 1);
175 do_test (9, 15, hlen, klen, 0);
176 do_test (9, 15, hlen, klen, 1);
178 do_test (15, 0, hlen, klen, 0);
179 do_test (15, 0, hlen, klen, 1);
180 do_test (15, 3, hlen, klen, 0);
181 do_test (15, 3, hlen, klen, 1);
182 do_test (15, 9, hlen, klen, 0);
183 do_test (15, 9, hlen, klen, 1);
184 do_test (15, 15, hlen, klen, 0);
185 do_test (15, 15, hlen, klen, 1);
188 do_test (0, 0, page_size - 1, 16, 0);
189 do_test (0, 0, page_size - 1, 16, 1);
191 return ret;
194 #include "../test-skeleton.c"