xref: /minix3/tests/lib/libc/string/t_memset.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_memset.c,v 1.4 2015/09/11 09:25:52 martin Exp $ */
211be35a1SLionel Sambuc 
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc  * Copyright (c) 2011 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc  * All rights reserved.
611be35a1SLionel Sambuc  *
711be35a1SLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
811be35a1SLionel Sambuc  * by Jukka Ruohonen.
911be35a1SLionel Sambuc  *
1011be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
1111be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
1211be35a1SLionel Sambuc  * are met:
1311be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
1411be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
1511be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
1611be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
1711be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
1811be35a1SLionel Sambuc  *
1911be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2011be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2111be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2211be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2311be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2411be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2511be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2611be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2711be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2811be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2911be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
3011be35a1SLionel Sambuc  */
3111be35a1SLionel Sambuc #include <sys/cdefs.h>
32*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: t_memset.c,v 1.4 2015/09/11 09:25:52 martin Exp $");
3311be35a1SLionel Sambuc 
3411be35a1SLionel Sambuc #include <sys/stat.h>
3511be35a1SLionel Sambuc 
3611be35a1SLionel Sambuc #include <atf-c.h>
3711be35a1SLionel Sambuc #include <stdlib.h>
3811be35a1SLionel Sambuc #include <string.h>
3911be35a1SLionel Sambuc #include <unistd.h>
4011be35a1SLionel Sambuc 
4111be35a1SLionel Sambuc static long	page = 0;
4211be35a1SLionel Sambuc static void	fill(char *, size_t, char);
4311be35a1SLionel Sambuc static bool	check(char *, size_t, char);
4411be35a1SLionel Sambuc 
45*0a6a1f1dSLionel Sambuc int zero;	/* always zero, but the compiler does not know */
46*0a6a1f1dSLionel Sambuc 
4711be35a1SLionel Sambuc ATF_TC(memset_array);
ATF_TC_HEAD(memset_array,tc)4811be35a1SLionel Sambuc ATF_TC_HEAD(memset_array, tc)
4911be35a1SLionel Sambuc {
5011be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test memset(3) with arrays");
5111be35a1SLionel Sambuc }
5211be35a1SLionel Sambuc 
ATF_TC_BODY(memset_array,tc)5311be35a1SLionel Sambuc ATF_TC_BODY(memset_array, tc)
5411be35a1SLionel Sambuc {
5511be35a1SLionel Sambuc 	char buf[1024];
5611be35a1SLionel Sambuc 
5711be35a1SLionel Sambuc 	(void)memset(buf, 0, sizeof(buf));
5811be35a1SLionel Sambuc 
5911be35a1SLionel Sambuc 	if (check(buf, sizeof(buf), 0) != true)
6011be35a1SLionel Sambuc 		atf_tc_fail("memset(3) did not fill a static buffer");
6111be35a1SLionel Sambuc 
6211be35a1SLionel Sambuc 	(void)memset(buf, 'x', sizeof(buf));
6311be35a1SLionel Sambuc 
6411be35a1SLionel Sambuc 	if (check(buf, sizeof(buf), 'x') != true)
6511be35a1SLionel Sambuc 		atf_tc_fail("memset(3) did not fill a static buffer");
6611be35a1SLionel Sambuc }
6711be35a1SLionel Sambuc 
6811be35a1SLionel Sambuc ATF_TC(memset_return);
ATF_TC_HEAD(memset_return,tc)6911be35a1SLionel Sambuc ATF_TC_HEAD(memset_return, tc)
7011be35a1SLionel Sambuc {
7111be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test memset(3) return value");
7211be35a1SLionel Sambuc }
7311be35a1SLionel Sambuc 
ATF_TC_BODY(memset_return,tc)7411be35a1SLionel Sambuc ATF_TC_BODY(memset_return, tc)
7511be35a1SLionel Sambuc {
7611be35a1SLionel Sambuc 	char *b = (char *)0x1;
7711be35a1SLionel Sambuc 	char c[2];
7811be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(memset(b, 0, 0), b);
7911be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(memset(c, 2, sizeof(c)), c);
8011be35a1SLionel Sambuc }
8111be35a1SLionel Sambuc 
8211be35a1SLionel Sambuc ATF_TC(memset_basic);
ATF_TC_HEAD(memset_basic,tc)8311be35a1SLionel Sambuc ATF_TC_HEAD(memset_basic, tc)
8411be35a1SLionel Sambuc {
8511be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "A basic test of memset(3)");
8611be35a1SLionel Sambuc }
8711be35a1SLionel Sambuc 
ATF_TC_BODY(memset_basic,tc)8811be35a1SLionel Sambuc ATF_TC_BODY(memset_basic, tc)
8911be35a1SLionel Sambuc {
9011be35a1SLionel Sambuc 	char *buf, *ret;
9111be35a1SLionel Sambuc 
9211be35a1SLionel Sambuc 	buf = malloc(page);
9311be35a1SLionel Sambuc 	ret = malloc(page);
9411be35a1SLionel Sambuc 
9511be35a1SLionel Sambuc 	ATF_REQUIRE(buf != NULL);
9611be35a1SLionel Sambuc 	ATF_REQUIRE(ret != NULL);
9711be35a1SLionel Sambuc 
9811be35a1SLionel Sambuc 	fill(ret, page, 0);
9911be35a1SLionel Sambuc 	memset(buf, 0, page);
10011be35a1SLionel Sambuc 
10111be35a1SLionel Sambuc 	ATF_REQUIRE(memcmp(ret, buf, page) == 0);
10211be35a1SLionel Sambuc 
10311be35a1SLionel Sambuc 	fill(ret, page, 'x');
10411be35a1SLionel Sambuc 	memset(buf, 'x', page);
10511be35a1SLionel Sambuc 
10611be35a1SLionel Sambuc 	ATF_REQUIRE(memcmp(ret, buf, page) == 0);
10711be35a1SLionel Sambuc 
10811be35a1SLionel Sambuc 	free(buf);
10911be35a1SLionel Sambuc 	free(ret);
11011be35a1SLionel Sambuc }
11111be35a1SLionel Sambuc 
11211be35a1SLionel Sambuc ATF_TC(memset_nonzero);
ATF_TC_HEAD(memset_nonzero,tc)11311be35a1SLionel Sambuc ATF_TC_HEAD(memset_nonzero, tc)
11411be35a1SLionel Sambuc {
11511be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test memset(3) with non-zero params");
11611be35a1SLionel Sambuc }
11711be35a1SLionel Sambuc 
ATF_TC_BODY(memset_nonzero,tc)11811be35a1SLionel Sambuc ATF_TC_BODY(memset_nonzero, tc)
11911be35a1SLionel Sambuc {
12011be35a1SLionel Sambuc 	const size_t n = 0x7f;
12111be35a1SLionel Sambuc 	char *buf;
12211be35a1SLionel Sambuc 	size_t i;
12311be35a1SLionel Sambuc 
12411be35a1SLionel Sambuc 	buf = malloc(page);
12511be35a1SLionel Sambuc 	ATF_REQUIRE(buf != NULL);
12611be35a1SLionel Sambuc 
12711be35a1SLionel Sambuc 	for (i = 0x21; i < n; i++) {
12811be35a1SLionel Sambuc 
12911be35a1SLionel Sambuc 		(void)memset(buf, i, page);
13011be35a1SLionel Sambuc 
13111be35a1SLionel Sambuc 		if (check(buf, page, i) != true)
13211be35a1SLionel Sambuc 			atf_tc_fail("memset(3) did not fill properly");
13311be35a1SLionel Sambuc 	}
13411be35a1SLionel Sambuc 
13511be35a1SLionel Sambuc 	free(buf);
13611be35a1SLionel Sambuc }
13711be35a1SLionel Sambuc 
138*0a6a1f1dSLionel Sambuc ATF_TC(memset_zero_size);
139*0a6a1f1dSLionel Sambuc 
ATF_TC_HEAD(memset_zero_size,tc)140*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(memset_zero_size, tc)
141*0a6a1f1dSLionel Sambuc {
142*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test memset(3) with zero size");
143*0a6a1f1dSLionel Sambuc }
144*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(memset_zero_size,tc)145*0a6a1f1dSLionel Sambuc ATF_TC_BODY(memset_zero_size, tc)
146*0a6a1f1dSLionel Sambuc {
147*0a6a1f1dSLionel Sambuc 	char buf[1024];
148*0a6a1f1dSLionel Sambuc 
149*0a6a1f1dSLionel Sambuc 	(void)memset(buf, 'x', sizeof(buf));
150*0a6a1f1dSLionel Sambuc 
151*0a6a1f1dSLionel Sambuc 	if (check(buf, sizeof(buf), 'x') != true)
152*0a6a1f1dSLionel Sambuc 		atf_tc_fail("memset(3) did not fill a static buffer");
153*0a6a1f1dSLionel Sambuc 
154*0a6a1f1dSLionel Sambuc 	(void)memset(buf+sizeof(buf)/2, 0, zero);
155*0a6a1f1dSLionel Sambuc 
156*0a6a1f1dSLionel Sambuc 	if (check(buf, sizeof(buf), 'x') != true)
157*0a6a1f1dSLionel Sambuc 		atf_tc_fail("memset(3) with 0 size did change the buffer");
158*0a6a1f1dSLionel Sambuc }
159*0a6a1f1dSLionel Sambuc 
160*0a6a1f1dSLionel Sambuc ATF_TC(bzero_zero_size);
161*0a6a1f1dSLionel Sambuc 
ATF_TC_HEAD(bzero_zero_size,tc)162*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(bzero_zero_size, tc)
163*0a6a1f1dSLionel Sambuc {
164*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test bzero(3) with zero size");
165*0a6a1f1dSLionel Sambuc }
166*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(bzero_zero_size,tc)167*0a6a1f1dSLionel Sambuc ATF_TC_BODY(bzero_zero_size, tc)
168*0a6a1f1dSLionel Sambuc {
169*0a6a1f1dSLionel Sambuc 	char buf[1024];
170*0a6a1f1dSLionel Sambuc 
171*0a6a1f1dSLionel Sambuc 	(void)memset(buf, 'x', sizeof(buf));
172*0a6a1f1dSLionel Sambuc 
173*0a6a1f1dSLionel Sambuc 	if (check(buf, sizeof(buf), 'x') != true)
174*0a6a1f1dSLionel Sambuc 		atf_tc_fail("memset(3) did not fill a static buffer");
175*0a6a1f1dSLionel Sambuc 
176*0a6a1f1dSLionel Sambuc 	(void)bzero(buf+sizeof(buf)/2, zero);
177*0a6a1f1dSLionel Sambuc 
178*0a6a1f1dSLionel Sambuc 	if (check(buf, sizeof(buf), 'x') != true)
179*0a6a1f1dSLionel Sambuc 		atf_tc_fail("bzero(3) with 0 size did change the buffer");
180*0a6a1f1dSLionel Sambuc }
181*0a6a1f1dSLionel Sambuc 
18211be35a1SLionel Sambuc ATF_TC(memset_struct);
ATF_TC_HEAD(memset_struct,tc)18311be35a1SLionel Sambuc ATF_TC_HEAD(memset_struct, tc)
18411be35a1SLionel Sambuc {
18511be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test memset(3) with a structure");
18611be35a1SLionel Sambuc }
18711be35a1SLionel Sambuc 
ATF_TC_BODY(memset_struct,tc)18811be35a1SLionel Sambuc ATF_TC_BODY(memset_struct, tc)
18911be35a1SLionel Sambuc {
19011be35a1SLionel Sambuc 	struct stat st;
19111be35a1SLionel Sambuc 
19211be35a1SLionel Sambuc 	st.st_dev = 0;
19311be35a1SLionel Sambuc 	st.st_ino = 1;
19411be35a1SLionel Sambuc 	st.st_mode = 2;
19511be35a1SLionel Sambuc 	st.st_nlink = 3;
19611be35a1SLionel Sambuc 	st.st_uid = 4;
19711be35a1SLionel Sambuc 	st.st_gid = 5;
19811be35a1SLionel Sambuc 	st.st_rdev = 6;
19911be35a1SLionel Sambuc 	st.st_size = 7;
20011be35a1SLionel Sambuc 	st.st_atime = 8;
20111be35a1SLionel Sambuc 	st.st_mtime = 9;
20211be35a1SLionel Sambuc 
20311be35a1SLionel Sambuc 	(void)memset(&st, 0, sizeof(struct stat));
20411be35a1SLionel Sambuc 
20511be35a1SLionel Sambuc 	ATF_CHECK(st.st_dev == 0);
20611be35a1SLionel Sambuc 	ATF_CHECK(st.st_ino == 0);
20711be35a1SLionel Sambuc 	ATF_CHECK(st.st_mode == 0);
20811be35a1SLionel Sambuc 	ATF_CHECK(st.st_nlink == 0);
20911be35a1SLionel Sambuc 	ATF_CHECK(st.st_uid == 0);
21011be35a1SLionel Sambuc 	ATF_CHECK(st.st_gid == 0);
21111be35a1SLionel Sambuc 	ATF_CHECK(st.st_rdev == 0);
21211be35a1SLionel Sambuc 	ATF_CHECK(st.st_size == 0);
21311be35a1SLionel Sambuc 	ATF_CHECK(st.st_atime == 0);
21411be35a1SLionel Sambuc 	ATF_CHECK(st.st_mtime == 0);
21511be35a1SLionel Sambuc }
21611be35a1SLionel Sambuc 
21711be35a1SLionel Sambuc static void
fill(char * buf,size_t len,char x)21811be35a1SLionel Sambuc fill(char *buf, size_t len, char x)
21911be35a1SLionel Sambuc {
22011be35a1SLionel Sambuc 	size_t i;
22111be35a1SLionel Sambuc 
22211be35a1SLionel Sambuc 	for (i = 0; i < len; i++)
22311be35a1SLionel Sambuc 		buf[i] = x;
22411be35a1SLionel Sambuc }
22511be35a1SLionel Sambuc 
22611be35a1SLionel Sambuc static bool
check(char * buf,size_t len,char x)22711be35a1SLionel Sambuc check(char *buf, size_t len, char x)
22811be35a1SLionel Sambuc {
22911be35a1SLionel Sambuc 	size_t i;
23011be35a1SLionel Sambuc 
23111be35a1SLionel Sambuc 	for (i = 0; i < len; i++) {
23211be35a1SLionel Sambuc 
23311be35a1SLionel Sambuc 		if (buf[i] != x)
23411be35a1SLionel Sambuc 			return false;
23511be35a1SLionel Sambuc 	}
23611be35a1SLionel Sambuc 
23711be35a1SLionel Sambuc 	return true;
23811be35a1SLionel Sambuc }
23911be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)24011be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
24111be35a1SLionel Sambuc {
24211be35a1SLionel Sambuc 
24311be35a1SLionel Sambuc 	page = sysconf(_SC_PAGESIZE);
24411be35a1SLionel Sambuc 	ATF_REQUIRE(page >= 0);
24511be35a1SLionel Sambuc 
24611be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, memset_array);
24711be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, memset_basic);
24811be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, memset_nonzero);
24911be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, memset_struct);
25011be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, memset_return);
251*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, memset_zero_size);
252*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, bzero_zero_size);
25311be35a1SLionel Sambuc 
25411be35a1SLionel Sambuc 	return atf_no_error();
25511be35a1SLionel Sambuc }
256