1 /* $NetBSD: t_memset.c,v 1.4 2015/09/11 09:25:52 martin Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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 $");
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 */
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
)
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;
78 ATF_REQUIRE_EQ(memset(b
, 0, 0), b
);
79 ATF_REQUIRE_EQ(memset(c
, 2, sizeof(c
)), c
);
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
)
95 ATF_REQUIRE(buf
!= NULL
);
96 ATF_REQUIRE(ret
!= NULL
);
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);
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;
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");
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
)
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
)
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
)
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);
218 fill(char *buf
, size_t len
, char x
)
222 for (i
= 0; i
< len
; i
++)
227 check(char *buf
, size_t len
, char x
)
231 for (i
= 0; i
< len
; i
++) {
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();