benchtests: Fix validate_benchout.py exceptions
[platform/upstream/glibc.git] / benchtests / bench-strsep.c
1 /* Measure strsep functions.
2    Copyright (C) 2013-2021 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
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.
9
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.
14
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    <https://www.gnu.org/licenses/>.  */
18
19 #define TEST_MAIN
20 #define TEST_NAME "strsep"
21 #include "bench-string.h"
22
23 char *
24 simple_strsep (char **s1, char *s2)
25 {
26   char *begin;
27   char *s;
28   size_t j = 0;
29
30   begin = *s1;
31   s = begin;
32   if (begin == NULL)
33     return NULL;
34   ssize_t s2len = strlen (s2);
35   while (*s)
36     {
37       for (j = 0; j < s2len; j++)
38         {
39           if (*s == s2[j])
40             {
41               s[0] = '\0';
42               *s1 = s + 1;
43               return begin;
44             }
45         }
46       s++;
47     }
48   *s1 = NULL;
49   return begin;
50 }
51
52 char *
53 oldstrsep (char **stringp, const char *delim)
54 {
55   char *begin, *end;
56
57   begin = *stringp;
58   if (begin == NULL)
59     return NULL;
60
61   /* A frequent case is when the delimiter string contains only one
62      character.  Here we don't need to call the expensive `strpbrk'
63      function and instead work using `strchr'.  */
64   if (delim[0] == '\0' || delim[1] == '\0')
65     {
66       char ch = delim[0];
67
68       if (ch == '\0')
69         end = NULL;
70       else
71         {
72           if (*begin == ch)
73             end = begin;
74           else if (*begin == '\0')
75             end = NULL;
76           else
77             end = strchr (begin + 1, ch);
78         }
79     }
80   else
81     /* Find the end of the token.  */
82     end = strpbrk (begin, delim);
83
84   if (end)
85     {
86       /* Terminate the token and set *STRINGP past NUL character.  */
87       *end++ = '\0';
88       *stringp = end;
89     }
90   else
91     /* No more delimiters; this is the last token.  */
92     *stringp = NULL;
93
94   return begin;
95 }
96
97 typedef char *(*proto_t) (const char **, const char *);
98
99 IMPL (simple_strsep, 0)
100 IMPL (strsep, 1)
101 IMPL (oldstrsep, 2)
102
103 static void
104 do_one_test (impl_t * impl, const char *s1, const char *s2)
105 {
106   size_t i, iters = INNER_LOOP_ITERS_SMALL;
107   timing_t start, stop, cur;
108
109   TIMING_NOW (start);
110   for (i = 0; i < iters; ++i)
111     {
112       const char *s1a = s1;
113       CALL (impl, &s1a, s2);
114       if (s1a != NULL)
115         ((char*)s1a)[-1] = '1';
116     }
117   TIMING_NOW (stop);
118
119   TIMING_DIFF (cur, start, stop);
120
121   TIMING_PRINT_MEAN ((double) cur, (double) iters);
122 }
123
124 static void
125 do_test (size_t align1, size_t align2, size_t len1, size_t len2, int fail)
126 {
127   char *s2 = (char *) (buf2 + align2);
128
129   /* Search for a delimiter in a string containing mostly '0', so don't
130      use '0' as a delimiter.  */
131   static const char d[] = "123456789abcdefg";
132 #define dl (sizeof (d) - 1)
133   char *ss2 = s2;
134   for (size_t l = len2; l > 0; l = l > dl ? l - dl : 0)
135     {
136       size_t t = l > dl ? dl : l;
137       ss2 = mempcpy (ss2, d, t);
138     }
139   s2[len2] = '\0';
140
141   printf ("Length %4zd/%zd, alignment %2zd/%2zd, %s:",
142           len1, len2, align1, align2, fail ? "fail" : "found");
143
144   FOR_EACH_IMPL (impl, 0)
145   {
146     char *s1 = (char *) (buf1 + align1);
147     memset (s1, '0', len1);
148     if (!fail)
149       s1[len1 / 2] = '1';
150     s1[len1] = '\0';
151     do_one_test (impl, s1, s2);
152   }
153   putchar ('\n');
154 }
155
156 static int
157 test_main (void)
158 {
159   test_init ();
160
161   printf ("%23s", "");
162   FOR_EACH_IMPL (impl, 0)
163     printf ("\t%s", impl->name);
164   putchar ('\n');
165
166   for (size_t klen = 2; klen < 32; ++klen)
167     for (size_t hlen = 4 * klen; hlen < 8 * klen; hlen += klen)
168       {
169         do_test (0, 0, hlen, klen, 0);
170         do_test (0, 0, hlen, klen, 1);
171         do_test (0, 3, hlen, klen, 0);
172         do_test (0, 3, hlen, klen, 1);
173         do_test (0, 9, hlen, klen, 0);
174         do_test (0, 9, hlen, klen, 1);
175         do_test (0, 15, hlen, klen, 0);
176         do_test (0, 15, hlen, klen, 1);
177
178         do_test (3, 0, hlen, klen, 0);
179         do_test (3, 0, hlen, klen, 1);
180         do_test (3, 3, hlen, klen, 0);
181         do_test (3, 3, hlen, klen, 1);
182         do_test (3, 9, hlen, klen, 0);
183         do_test (3, 9, hlen, klen, 1);
184         do_test (3, 15, hlen, klen, 0);
185         do_test (3, 15, hlen, klen, 1);
186
187         do_test (9, 0, hlen, klen, 0);
188         do_test (9, 0, hlen, klen, 1);
189         do_test (9, 3, hlen, klen, 0);
190         do_test (9, 3, hlen, klen, 1);
191         do_test (9, 9, hlen, klen, 0);
192         do_test (9, 9, hlen, klen, 1);
193         do_test (9, 15, hlen, klen, 0);
194         do_test (9, 15, hlen, klen, 1);
195
196         do_test (15, 0, hlen, klen, 0);
197         do_test (15, 0, hlen, klen, 1);
198         do_test (15, 3, hlen, klen, 0);
199         do_test (15, 3, hlen, klen, 1);
200         do_test (15, 9, hlen, klen, 0);
201         do_test (15, 9, hlen, klen, 1);
202         do_test (15, 15, hlen, klen, 0);
203         do_test (15, 15, hlen, klen, 1);
204       }
205   do_test (0, 0, page_size - 1, 16, 0);
206   do_test (0, 0, page_size - 1, 16, 1);
207
208   return ret;
209 }
210
211 #include <support/test-driver.c>