Remove building with NOCRYPTO option
[minix3.git] / tests / lib / libc / string / t_memset.c
blob5a2be2896654b7cc93116d711c9451a3b108f8b9
1 /* $NetBSD: t_memset.c,v 1.4 2015/09/11 09:25:52 martin Exp $ */
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jukka Ruohonen.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_memset.c,v 1.4 2015/09/11 09:25:52 martin Exp $");
34 #include <sys/stat.h>
36 #include <atf-c.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
41 static long page = 0;
42 static void fill(char *, size_t, char);
43 static bool check(char *, size_t, char);
45 int zero; /* always zero, but the compiler does not know */
47 ATF_TC(memset_array);
48 ATF_TC_HEAD(memset_array, tc)
50 atf_tc_set_md_var(tc, "descr", "Test memset(3) with arrays");
53 ATF_TC_BODY(memset_array, tc)
55 char buf[1024];
57 (void)memset(buf, 0, sizeof(buf));
59 if (check(buf, sizeof(buf), 0) != true)
60 atf_tc_fail("memset(3) did not fill a static buffer");
62 (void)memset(buf, 'x', sizeof(buf));
64 if (check(buf, sizeof(buf), 'x') != true)
65 atf_tc_fail("memset(3) did not fill a static buffer");
68 ATF_TC(memset_return);
69 ATF_TC_HEAD(memset_return, tc)
71 atf_tc_set_md_var(tc, "descr", "Test memset(3) return value");
74 ATF_TC_BODY(memset_return, tc)
76 char *b = (char *)0x1;
77 char c[2];
78 ATF_REQUIRE_EQ(memset(b, 0, 0), b);
79 ATF_REQUIRE_EQ(memset(c, 2, sizeof(c)), c);
82 ATF_TC(memset_basic);
83 ATF_TC_HEAD(memset_basic, tc)
85 atf_tc_set_md_var(tc, "descr", "A basic test of memset(3)");
88 ATF_TC_BODY(memset_basic, tc)
90 char *buf, *ret;
92 buf = malloc(page);
93 ret = malloc(page);
95 ATF_REQUIRE(buf != NULL);
96 ATF_REQUIRE(ret != NULL);
98 fill(ret, page, 0);
99 memset(buf, 0, page);
101 ATF_REQUIRE(memcmp(ret, buf, page) == 0);
103 fill(ret, page, 'x');
104 memset(buf, 'x', page);
106 ATF_REQUIRE(memcmp(ret, buf, page) == 0);
108 free(buf);
109 free(ret);
112 ATF_TC(memset_nonzero);
113 ATF_TC_HEAD(memset_nonzero, tc)
115 atf_tc_set_md_var(tc, "descr", "Test memset(3) with non-zero params");
118 ATF_TC_BODY(memset_nonzero, tc)
120 const size_t n = 0x7f;
121 char *buf;
122 size_t i;
124 buf = malloc(page);
125 ATF_REQUIRE(buf != NULL);
127 for (i = 0x21; i < n; i++) {
129 (void)memset(buf, i, page);
131 if (check(buf, page, i) != true)
132 atf_tc_fail("memset(3) did not fill properly");
135 free(buf);
138 ATF_TC(memset_zero_size);
140 ATF_TC_HEAD(memset_zero_size, tc)
142 atf_tc_set_md_var(tc, "descr", "Test memset(3) with zero size");
145 ATF_TC_BODY(memset_zero_size, tc)
147 char buf[1024];
149 (void)memset(buf, 'x', sizeof(buf));
151 if (check(buf, sizeof(buf), 'x') != true)
152 atf_tc_fail("memset(3) did not fill a static buffer");
154 (void)memset(buf+sizeof(buf)/2, 0, zero);
156 if (check(buf, sizeof(buf), 'x') != true)
157 atf_tc_fail("memset(3) with 0 size did change the buffer");
160 ATF_TC(bzero_zero_size);
162 ATF_TC_HEAD(bzero_zero_size, tc)
164 atf_tc_set_md_var(tc, "descr", "Test bzero(3) with zero size");
167 ATF_TC_BODY(bzero_zero_size, tc)
169 char buf[1024];
171 (void)memset(buf, 'x', sizeof(buf));
173 if (check(buf, sizeof(buf), 'x') != true)
174 atf_tc_fail("memset(3) did not fill a static buffer");
176 (void)bzero(buf+sizeof(buf)/2, zero);
178 if (check(buf, sizeof(buf), 'x') != true)
179 atf_tc_fail("bzero(3) with 0 size did change the buffer");
182 ATF_TC(memset_struct);
183 ATF_TC_HEAD(memset_struct, tc)
185 atf_tc_set_md_var(tc, "descr", "Test memset(3) with a structure");
188 ATF_TC_BODY(memset_struct, tc)
190 struct stat st;
192 st.st_dev = 0;
193 st.st_ino = 1;
194 st.st_mode = 2;
195 st.st_nlink = 3;
196 st.st_uid = 4;
197 st.st_gid = 5;
198 st.st_rdev = 6;
199 st.st_size = 7;
200 st.st_atime = 8;
201 st.st_mtime = 9;
203 (void)memset(&st, 0, sizeof(struct stat));
205 ATF_CHECK(st.st_dev == 0);
206 ATF_CHECK(st.st_ino == 0);
207 ATF_CHECK(st.st_mode == 0);
208 ATF_CHECK(st.st_nlink == 0);
209 ATF_CHECK(st.st_uid == 0);
210 ATF_CHECK(st.st_gid == 0);
211 ATF_CHECK(st.st_rdev == 0);
212 ATF_CHECK(st.st_size == 0);
213 ATF_CHECK(st.st_atime == 0);
214 ATF_CHECK(st.st_mtime == 0);
217 static void
218 fill(char *buf, size_t len, char x)
220 size_t i;
222 for (i = 0; i < len; i++)
223 buf[i] = x;
226 static bool
227 check(char *buf, size_t len, char x)
229 size_t i;
231 for (i = 0; i < len; i++) {
233 if (buf[i] != x)
234 return false;
237 return true;
240 ATF_TP_ADD_TCS(tp)
243 page = sysconf(_SC_PAGESIZE);
244 ATF_REQUIRE(page >= 0);
246 ATF_TP_ADD_TC(tp, memset_array);
247 ATF_TP_ADD_TC(tp, memset_basic);
248 ATF_TP_ADD_TC(tp, memset_nonzero);
249 ATF_TP_ADD_TC(tp, memset_struct);
250 ATF_TP_ADD_TC(tp, memset_return);
251 ATF_TP_ADD_TC(tp, memset_zero_size);
252 ATF_TP_ADD_TC(tp, bzero_zero_size);
254 return atf_no_error();