xref: /freebsd/contrib/netbsd-tests/lib/libc/string/t_memset.c (revision f4b37ed0f8b307b1f3f0f630ca725d68f1dff30d)
1 /* $NetBSD: t_memset.c,v 1.3 2013/03/17 02:23:31 christos Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jukka Ruohonen.
9  *
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.
18  *
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.
30  */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_memset.c,v 1.3 2013/03/17 02:23:31 christos Exp $");
33 
34 #include <sys/stat.h>
35 
36 #include <atf-c.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 static long	page = 0;
42 static void	fill(char *, size_t, char);
43 static bool	check(char *, size_t, char);
44 
45 ATF_TC(memset_array);
46 ATF_TC_HEAD(memset_array, tc)
47 {
48 	atf_tc_set_md_var(tc, "descr", "Test memset(3) with arrays");
49 }
50 
51 ATF_TC_BODY(memset_array, tc)
52 {
53 	char buf[1024];
54 
55 	(void)memset(buf, 0, sizeof(buf));
56 
57 	if (check(buf, sizeof(buf), 0) != true)
58 		atf_tc_fail("memset(3) did not fill a static buffer");
59 
60 	(void)memset(buf, 'x', sizeof(buf));
61 
62 	if (check(buf, sizeof(buf), 'x') != true)
63 		atf_tc_fail("memset(3) did not fill a static buffer");
64 }
65 
66 ATF_TC(memset_return);
67 ATF_TC_HEAD(memset_return, tc)
68 {
69 	atf_tc_set_md_var(tc, "descr", "Test memset(3) return value");
70 }
71 
72 ATF_TC_BODY(memset_return, tc)
73 {
74 	char *b = (char *)0x1;
75 	char c[2];
76 	ATF_REQUIRE_EQ(memset(b, 0, 0), b);
77 	ATF_REQUIRE_EQ(memset(c, 2, sizeof(c)), c);
78 }
79 
80 ATF_TC(memset_basic);
81 ATF_TC_HEAD(memset_basic, tc)
82 {
83 	atf_tc_set_md_var(tc, "descr", "A basic test of memset(3)");
84 }
85 
86 ATF_TC_BODY(memset_basic, tc)
87 {
88 	char *buf, *ret;
89 
90 	buf = malloc(page);
91 	ret = malloc(page);
92 
93 	ATF_REQUIRE(buf != NULL);
94 	ATF_REQUIRE(ret != NULL);
95 
96 	fill(ret, page, 0);
97 	memset(buf, 0, page);
98 
99 	ATF_REQUIRE(memcmp(ret, buf, page) == 0);
100 
101 	fill(ret, page, 'x');
102 	memset(buf, 'x', page);
103 
104 	ATF_REQUIRE(memcmp(ret, buf, page) == 0);
105 
106 	free(buf);
107 	free(ret);
108 }
109 
110 ATF_TC(memset_nonzero);
111 ATF_TC_HEAD(memset_nonzero, tc)
112 {
113 	atf_tc_set_md_var(tc, "descr", "Test memset(3) with non-zero params");
114 }
115 
116 ATF_TC_BODY(memset_nonzero, tc)
117 {
118 	const size_t n = 0x7f;
119 	char *buf;
120 	size_t i;
121 
122 	buf = malloc(page);
123 	ATF_REQUIRE(buf != NULL);
124 
125 	for (i = 0x21; i < n; i++) {
126 
127 		(void)memset(buf, i, page);
128 
129 		if (check(buf, page, i) != true)
130 			atf_tc_fail("memset(3) did not fill properly");
131 	}
132 
133 	free(buf);
134 }
135 
136 ATF_TC(memset_struct);
137 ATF_TC_HEAD(memset_struct, tc)
138 {
139 	atf_tc_set_md_var(tc, "descr", "Test memset(3) with a structure");
140 }
141 
142 ATF_TC_BODY(memset_struct, tc)
143 {
144 	struct stat st;
145 
146 	st.st_dev = 0;
147 	st.st_ino = 1;
148 	st.st_mode = 2;
149 	st.st_nlink = 3;
150 	st.st_uid = 4;
151 	st.st_gid = 5;
152 	st.st_rdev = 6;
153 	st.st_size = 7;
154 	st.st_atime = 8;
155 	st.st_mtime = 9;
156 
157 	(void)memset(&st, 0, sizeof(struct stat));
158 
159 	ATF_CHECK(st.st_dev == 0);
160 	ATF_CHECK(st.st_ino == 0);
161 	ATF_CHECK(st.st_mode == 0);
162 	ATF_CHECK(st.st_nlink == 0);
163 	ATF_CHECK(st.st_uid == 0);
164 	ATF_CHECK(st.st_gid == 0);
165 	ATF_CHECK(st.st_rdev == 0);
166 	ATF_CHECK(st.st_size == 0);
167 	ATF_CHECK(st.st_atime == 0);
168 	ATF_CHECK(st.st_mtime == 0);
169 }
170 
171 static void
172 fill(char *buf, size_t len, char x)
173 {
174 	size_t i;
175 
176 	for (i = 0; i < len; i++)
177 		buf[i] = x;
178 }
179 
180 static bool
181 check(char *buf, size_t len, char x)
182 {
183 	size_t i;
184 
185 	for (i = 0; i < len; i++) {
186 
187 		if (buf[i] != x)
188 			return false;
189 	}
190 
191 	return true;
192 }
193 
194 ATF_TP_ADD_TCS(tp)
195 {
196 
197 	page = sysconf(_SC_PAGESIZE);
198 	ATF_REQUIRE(page >= 0);
199 
200 	ATF_TP_ADD_TC(tp, memset_array);
201 	ATF_TP_ADD_TC(tp, memset_basic);
202 	ATF_TP_ADD_TC(tp, memset_nonzero);
203 	ATF_TP_ADD_TC(tp, memset_struct);
204 	ATF_TP_ADD_TC(tp, memset_return);
205 
206 	return atf_no_error();
207 }
208