Fix strchr test
[platform/upstream/glibc.git] / string / test-string.h
1 /* Test and measure string and memory functions.
2    Copyright (C) 1999, 2002, 2004, 2008, 2011 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 #include <sys/cdefs.h>
22
23 typedef struct
24 {
25   const char *name;
26   void (*fn) (void);
27   long test;
28 } impl_t;
29 extern impl_t __start_impls[], __stop_impls[];
30
31 #define IMPL(name, test) \
32   impl_t tst_ ## name                                                   \
33   __attribute__ ((section ("impls"), aligned (sizeof (void *))))        \
34        = { __STRING (name), (void (*) (void))name, test };
35
36 #ifdef TEST_MAIN
37
38 #ifndef _GNU_SOURCE
39 #define _GNU_SOURCE
40 #endif
41
42 #undef __USE_STRING_INLINES
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <sys/mman.h>
48 #include <unistd.h>
49 #include <fcntl.h>
50 #include <error.h>
51 #include <errno.h>
52 #include <time.h>
53 #define GL(x) _##x
54 #define GLRO(x) _##x
55 #include <hp-timing.h>
56
57
58 # define TEST_FUNCTION test_main ()
59 # define TIMEOUT (4 * 60)
60 # define OPT_ITERATIONS 10000
61 # define OPT_RANDOM 10001
62 # define OPT_SEED 10002
63
64 unsigned char *buf1, *buf2;
65 int ret, do_srandom;
66 unsigned int seed;
67 size_t page_size;
68
69 hp_timing_t _dl_hp_timing_overhead;
70
71 # ifndef ITERATIONS
72 size_t iterations = 100000;
73 #  define ITERATIONS_OPTIONS \
74   { "iterations", required_argument, NULL, OPT_ITERATIONS },
75 #  define ITERATIONS_PROCESS \
76   case OPT_ITERATIONS:                          \
77     iterations = strtoul (optarg, NULL, 0);     \
78     break;
79 #  define ITERATIONS iterations
80 # else
81 #  define ITERATIONS_OPTIONS
82 #  define ITERATIONS_PROCESS
83 # endif
84
85 # define CMDLINE_OPTIONS ITERATIONS_OPTIONS \
86   { "random", no_argument, NULL, OPT_RANDOM },  \
87   { "seed", required_argument, NULL, OPT_SEED },
88 # define CMDLINE_PROCESS ITERATIONS_PROCESS \
89   case OPT_RANDOM:                                                      \
90     {                                                                   \
91       int fdr = open ("/dev/urandom", O_RDONLY);                        \
92                                                                         \
93       if (fdr < 0 || read (fdr, &seed, sizeof(seed)) != sizeof (seed))  \
94         seed = time (NULL);                                             \
95       if (fdr >= 0)                                                     \
96         close (fdr);                                                    \
97       do_srandom = 1;                                                   \
98       break;                                                            \
99     }                                                                   \
100                                                                         \
101   case OPT_SEED:                                                        \
102     seed = strtoul (optarg, NULL, 0);                                   \
103     do_srandom = 1;                                                     \
104     break;
105
106 #define CALL(impl, ...) \
107   (* (proto_t) (impl)->fn) (__VA_ARGS__)
108
109 #define FOR_EACH_IMPL(impl, notall) \
110   for (impl_t *impl = __start_impls; impl < __stop_impls; ++impl)       \
111     if (!notall || impl->test)
112
113 #define HP_TIMING_BEST(best_time, start, end)   \
114   do                                                                    \
115     {                                                                   \
116       hp_timing_t tmptime;                                              \
117       HP_TIMING_DIFF (tmptime, start + _dl_hp_timing_overhead, end);    \
118       if (best_time > tmptime)                                          \
119         best_time = tmptime;                                            \
120     }                                                                   \
121   while (0)
122
123 #ifndef BUF1PAGES
124 # define BUF1PAGES 1
125 #endif
126
127 static void
128 test_init (void)
129 {
130   page_size = 2 * getpagesize ();
131 #ifdef MIN_PAGE_SIZE
132   if (page_size < MIN_PAGE_SIZE)
133     page_size = MIN_PAGE_SIZE;
134 #endif
135   buf1 = mmap (0, (BUF1PAGES + 1) * page_size, PROT_READ | PROT_WRITE,
136                MAP_PRIVATE | MAP_ANON, -1, 0);
137   if (buf1 == MAP_FAILED)
138     error (EXIT_FAILURE, errno, "mmap failed");
139   if (mprotect (buf1 + BUF1PAGES * page_size, page_size, PROT_NONE))
140     error (EXIT_FAILURE, errno, "mprotect failed");
141   buf2 = mmap (0, 2 * page_size, PROT_READ | PROT_WRITE,
142                MAP_PRIVATE | MAP_ANON, -1, 0);
143   if (buf2 == MAP_FAILED)
144     error (EXIT_FAILURE, errno, "mmap failed");
145   if (mprotect (buf2 + page_size, page_size, PROT_NONE))
146     error (EXIT_FAILURE, errno, "mprotect failed");
147   HP_TIMING_DIFF_INIT ();
148   if (do_srandom)
149     {
150       printf ("Setting seed to 0x%x\n", seed);
151       srandom (seed);
152     }
153
154   memset (buf1, 0xa5, BUF1PAGES * page_size);
155   memset (buf2, 0x5a, page_size);
156 }
157
158 #endif