Remove building with NOCRYPTO option
[minix3.git] / tests / lib / libc / string / t_strlen.c
blob66158fd7113f016a24780ffe97117bdaa60551a0
1 /* $NetBSD: t_strlen.c,v 1.5 2011/07/14 07:33:20 jruoho Exp $ */
3 /*
4 * Written by J.T. Conklin <jtc@acorntoolworks.com>
5 * Public domain.
6 */
8 #include <atf-c.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <dlfcn.h>
14 #include <unistd.h>
16 static void write_num(int);
18 static void
19 write_num(int val)
21 char buf[20];
22 int i;
24 for (i = sizeof buf; --i >= 0;) {
25 buf[i] = '0' + val % 10;
26 val /= 10;
27 if (val == 0) {
28 write(2, buf + i, sizeof buf - i);
29 return;
32 write(2, "overflow", 8);
35 ATF_TC(strlen_basic);
36 ATF_TC_HEAD(strlen_basic, tc)
38 atf_tc_set_md_var(tc, "descr", "Test strlen(3) results");
41 ATF_TC_BODY(strlen_basic, tc)
43 /* try to trick the compiler */
44 size_t (*strlen_fn)(const char *);
46 unsigned int a, t;
47 size_t len;
48 char buf[64];
50 struct tab {
51 const char* val;
52 size_t len;
55 const struct tab tab[] = {
57 * patterns that check for all combinations of leading and
58 * trailing unaligned characters (on a 64 bit processor)
61 { "", 0 },
62 { "a", 1 },
63 { "ab", 2 },
64 { "abc", 3 },
65 { "abcd", 4 },
66 { "abcde", 5 },
67 { "abcdef", 6 },
68 { "abcdefg", 7 },
69 { "abcdefgh", 8 },
70 { "abcdefghi", 9 },
71 { "abcdefghij", 10 },
72 { "abcdefghijk", 11 },
73 { "abcdefghijkl", 12 },
74 { "abcdefghijklm", 13 },
75 { "abcdefghijklmn", 14 },
76 { "abcdefghijklmno", 15 },
77 { "abcdefghijklmnop", 16 },
78 { "abcdefghijklmnopq", 17 },
79 { "abcdefghijklmnopqr", 18 },
80 { "abcdefghijklmnopqrs", 19 },
81 { "abcdefghijklmnopqrst", 20 },
82 { "abcdefghijklmnopqrstu", 21 },
83 { "abcdefghijklmnopqrstuv", 22 },
84 { "abcdefghijklmnopqrstuvw", 23 },
87 * patterns that check for the cases where the expression:
89 * ((word - 0x7f7f..7f) & 0x8080..80)
91 * returns non-zero even though there are no zero bytes in
92 * the word.
95 { "" "\xff\xff\xff\xff\xff\xff\xff\xff" "abcdefgh", 16 },
96 { "a" "\xff\xff\xff\xff\xff\xff\xff\xff" "bcdefgh", 16 },
97 { "ab" "\xff\xff\xff\xff\xff\xff\xff\xff" "cdefgh", 16 },
98 { "abc" "\xff\xff\xff\xff\xff\xff\xff\xff" "defgh", 16 },
99 { "abcd" "\xff\xff\xff\xff\xff\xff\xff\xff" "efgh", 16 },
100 { "abcde" "\xff\xff\xff\xff\xff\xff\xff\xff" "fgh", 16 },
101 { "abcdef" "\xff\xff\xff\xff\xff\xff\xff\xff" "gh", 16 },
102 { "abcdefg" "\xff\xff\xff\xff\xff\xff\xff\xff" "h", 16 },
103 { "abcdefgh" "\xff\xff\xff\xff\xff\xff\xff\xff" "", 16 },
107 * During testing it is useful have the rest of the program
108 * use a known good version!
110 strlen_fn = dlsym(dlopen(NULL, RTLD_LAZY), "test_strlen");
111 if (!strlen_fn)
112 strlen_fn = strlen;
114 for (a = 0; a < sizeof(long); ++a) {
115 for (t = 0; t < (sizeof(tab) / sizeof(tab[0])); ++t) {
117 memcpy(&buf[a], tab[t].val, tab[t].len + 1);
118 len = strlen_fn(&buf[a]);
120 if (len != tab[t].len) {
121 /* Write error without using printf / strlen */
122 write(2, "alignment ", 10);
123 write_num(a);
124 write(2, ", test ", 7);
125 write_num(t);
126 write(2, ", got len ", 10);
127 write_num(len);
128 write(2, ", not ", 6);
129 write_num(tab[t].len);
130 write(2, ", for '", 7);
131 write(2, tab[t].val, tab[t].len);
132 write(2, "'\n", 2);
133 atf_tc_fail("See stderr for details");
139 ATF_TC(strlen_huge);
140 ATF_TC_HEAD(strlen_huge, tc)
142 atf_tc_set_md_var(tc, "descr", "Test strlen(3) with huge strings");
145 ATF_TC_BODY(strlen_huge, tc)
147 long page;
148 char *str;
149 size_t i;
151 page = sysconf(_SC_PAGESIZE);
152 ATF_REQUIRE(page >= 0);
154 for (i = 1; i < 1000; i = i + 100) {
156 str = malloc(i * page + 1);
158 if (str == NULL)
159 continue;
161 (void)memset(str, 'x', i * page);
162 str[i * page] = '\0';
164 ATF_REQUIRE(strlen(str) == i * page);
165 free(str);
169 ATF_TC(strnlen_basic);
170 ATF_TC_HEAD(strnlen_basic, tc)
172 atf_tc_set_md_var(tc, "descr", "A naive test of strnlen(3)");
175 ATF_TC_BODY(strnlen_basic, tc)
177 char buf[1];
179 buf[0] = '\0';
181 ATF_CHECK(strnlen(buf, 000) == 0);
182 ATF_CHECK(strnlen(buf, 111) == 0);
184 ATF_CHECK(strnlen("xxx", 0) == 0);
185 ATF_CHECK(strnlen("xxx", 1) == 1);
186 ATF_CHECK(strnlen("xxx", 2) == 2);
187 ATF_CHECK(strnlen("xxx", 3) == 3);
188 ATF_CHECK(strnlen("xxx", 9) == 3);
191 ATF_TP_ADD_TCS(tp)
194 ATF_TP_ADD_TC(tp, strlen_basic);
195 ATF_TP_ADD_TC(tp, strlen_huge);
196 ATF_TP_ADD_TC(tp, strnlen_basic);
198 return atf_no_error();