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