]>
Commit | Line | Data |
---|---|---|
619fccca | 1 | /* Test and measure STRLEN functions. |
b168057a | 2 | Copyright (C) 1999-2015 Free Software Foundation, Inc. |
58ef9ef7 RM |
3 | This file is part of the GNU C Library. |
4 | Written by Jakub Jelinek <jakub@redhat.com>, 1999. | |
619fccca | 5 | Added wcslen support by Liubov Dmitrieva <liubov.dmitrieva@gmail.com>, 2011 |
58ef9ef7 RM |
6 | |
7 | The GNU C Library is free software; you can redistribute it and/or | |
8 | modify it under the terms of the GNU Lesser General Public | |
9 | License as published by the Free Software Foundation; either | |
10 | version 2.1 of the License, or (at your option) any later version. | |
11 | ||
12 | The GNU C Library is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 PE |
18 | License along with the GNU C Library; if not, see |
19 | <http://www.gnu.org/licenses/>. */ | |
58ef9ef7 RM |
20 | |
21 | #define TEST_MAIN | |
69f07e5f L |
22 | #ifndef WIDE |
23 | # define TEST_NAME "strlen" | |
24 | #else | |
25 | # define TEST_NAME "wcslen" | |
26 | #endif | |
58ef9ef7 RM |
27 | #include "test-string.h" |
28 | ||
619fccca LD |
29 | #ifndef WIDE |
30 | # define STRLEN strlen | |
31 | # define CHAR char | |
32 | # define MAX_CHAR CHAR_MAX | |
33 | #else | |
34 | # include <wchar.h> | |
35 | # define STRLEN wcslen | |
36 | # define CHAR wchar_t | |
37 | # define MAX_CHAR WCHAR_MAX | |
38 | #endif | |
58ef9ef7 | 39 | |
619fccca | 40 | typedef size_t (*proto_t) (const CHAR *); |
58ef9ef7 RM |
41 | |
42 | size_t | |
619fccca | 43 | simple_STRLEN (const CHAR *s) |
58ef9ef7 | 44 | { |
619fccca | 45 | const CHAR *p; |
58ef9ef7 RM |
46 | |
47 | for (p = s; *p; ++p); | |
48 | return p - s; | |
49 | } | |
50 | ||
619fccca | 51 | #ifndef WIDE |
58ef9ef7 | 52 | size_t |
619fccca | 53 | builtin_strlen (const CHAR *p) |
58ef9ef7 RM |
54 | { |
55 | return __builtin_strlen (p); | |
56 | } | |
619fccca LD |
57 | IMPL (builtin_strlen, 0) |
58 | #endif | |
59 | ||
60 | IMPL (simple_STRLEN, 0) | |
61 | IMPL (STRLEN, 1) | |
62 | ||
58ef9ef7 RM |
63 | |
64 | static void | |
619fccca | 65 | do_one_test (impl_t *impl, const CHAR *s, size_t exp_len) |
58ef9ef7 RM |
66 | { |
67 | size_t len = CALL (impl, s); | |
68 | if (len != exp_len) | |
69 | { | |
70 | error (0, 0, "Wrong result in function %s %zd %zd", impl->name, | |
71 | len, exp_len); | |
72 | ret = 1; | |
73 | return; | |
74 | } | |
58ef9ef7 RM |
75 | } |
76 | ||
77 | static void | |
619fccca | 78 | do_test (size_t align, size_t len) |
58ef9ef7 RM |
79 | { |
80 | size_t i; | |
81 | ||
619fccca LD |
82 | align &= 63; |
83 | if (align + sizeof(CHAR) * len >= page_size) | |
58ef9ef7 | 84 | return; |
c9df3df9 | 85 | |
619fccca LD |
86 | CHAR *buf = (CHAR *) (buf1); |
87 | ||
58ef9ef7 | 88 | for (i = 0; i < len; ++i) |
619fccca LD |
89 | buf[align + i] = 1 + 11111 * i % MAX_CHAR; |
90 | buf[align + len] = 0; | |
58ef9ef7 | 91 | |
58ef9ef7 | 92 | FOR_EACH_IMPL (impl, 0) |
619fccca | 93 | do_one_test (impl, (CHAR *) (buf + align), len); |
58ef9ef7 RM |
94 | } |
95 | ||
96 | static void | |
97 | do_random_tests (void) | |
98 | { | |
99 | size_t i, j, n, align, len; | |
619fccca | 100 | CHAR *p = (CHAR *) (buf1 + page_size - 512 * sizeof(CHAR)); |
58ef9ef7 RM |
101 | |
102 | for (n = 0; n < ITERATIONS; n++) | |
103 | { | |
104 | align = random () & 15; | |
105 | len = random () & 511; | |
106 | if (len + align > 510) | |
107 | len = 511 - align - (random () & 7); | |
108 | j = len + align + 64; | |
109 | if (j > 512) | |
110 | j = 512; | |
111 | ||
112 | for (i = 0; i < j; i++) | |
113 | { | |
114 | if (i == len + align) | |
115 | p[i] = 0; | |
116 | else | |
117 | { | |
118 | p[i] = random () & 255; | |
119 | if (i >= align && i < len + align && !p[i]) | |
120 | p[i] = (random () & 127) + 1; | |
121 | } | |
122 | } | |
123 | ||
124 | FOR_EACH_IMPL (impl, 1) | |
619fccca | 125 | if (CALL (impl, (CHAR *) (p + align)) != len) |
58ef9ef7 RM |
126 | { |
127 | error (0, 0, "Iteration %zd - wrong result in function %s (%zd) %zd != %zd, p %p", | |
619fccca | 128 | n, impl->name, align, CALL (impl, (CHAR *) (p + align)), |
0317eaec | 129 | len, p); |
58ef9ef7 RM |
130 | ret = 1; |
131 | } | |
132 | } | |
133 | } | |
134 | ||
135 | int | |
136 | test_main (void) | |
137 | { | |
138 | size_t i; | |
139 | ||
140 | test_init (); | |
141 | ||
142 | printf ("%20s", ""); | |
143 | FOR_EACH_IMPL (impl, 0) | |
144 | printf ("\t%s", impl->name); | |
145 | putchar ('\n'); | |
146 | ||
619fccca | 147 | /* Checking with only 4 * N alignments for wcslen, other alignments are wrong for wchar_t type arrays*/ |
58ef9ef7 RM |
148 | |
149 | for (i = 1; i < 8; ++i) | |
619fccca LD |
150 | { |
151 | do_test (sizeof(CHAR) * i, i); | |
152 | do_test (0, i); | |
153 | } | |
58ef9ef7 | 154 | |
619fccca | 155 | for (i = 2; i <= 12; ++i) |
58ef9ef7 | 156 | { |
619fccca LD |
157 | do_test (0, 1 << i); |
158 | do_test (sizeof(CHAR) * 7, 1 << i); | |
159 | do_test (sizeof(CHAR) * i, 1 << i); | |
160 | do_test (sizeof(CHAR) * i, (size_t)((1 << i) / 1.5)); | |
58ef9ef7 RM |
161 | } |
162 | ||
163 | do_random_tests (); | |
164 | return ret; | |
165 | } | |
166 | ||
167 | #include "../test-skeleton.c" |