Home | History | Annotate | Line # | Download | only in string
t_memset.c revision 1.1
      1 /* $NetBSD: t_memset.c,v 1.1 2011/06/03 06:39:52 jruoho 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.1 2011/06/03 06:39:52 jruoho 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_basic);
     67 ATF_TC_HEAD(memset_basic, tc)
     68 {
     69 	atf_tc_set_md_var(tc, "descr", "A basic test of memset(3)");
     70 }
     71 
     72 ATF_TC_BODY(memset_basic, tc)
     73 {
     74 	char *buf, *ret;
     75 
     76 	buf = malloc(page);
     77 	ret = malloc(page);
     78 
     79 	ATF_REQUIRE(buf != NULL);
     80 	ATF_REQUIRE(ret != NULL);
     81 
     82 	fill(ret, page, 0);
     83 	memset(buf, 0, page);
     84 
     85 	ATF_REQUIRE(memcmp(ret, buf, page) == 0);
     86 
     87 	fill(ret, page, 'x');
     88 	memset(buf, 'x', page);
     89 
     90 	ATF_REQUIRE(memcmp(ret, buf, page) == 0);
     91 
     92 	free(buf);
     93 	free(ret);
     94 }
     95 
     96 ATF_TC(memset_nonzero);
     97 ATF_TC_HEAD(memset_nonzero, tc)
     98 {
     99 	atf_tc_set_md_var(tc, "descr", "Test memset(3) with non-zero params");
    100 }
    101 
    102 ATF_TC_BODY(memset_nonzero, tc)
    103 {
    104 	const size_t n = 0x7f;
    105 	char *buf;
    106 	size_t i;
    107 
    108 	buf = malloc(page);
    109 	ATF_REQUIRE(buf != NULL);
    110 
    111 	for (i = 0x21; i < n; i++) {
    112 
    113 		(void)memset(buf, i, page);
    114 
    115 		if (check(buf, page, i) != true)
    116 			atf_tc_fail("memset(3) did not fill properly");
    117 	}
    118 
    119 	free(buf);
    120 }
    121 
    122 ATF_TC(memset_struct);
    123 ATF_TC_HEAD(memset_struct, tc)
    124 {
    125 	atf_tc_set_md_var(tc, "descr", "Test memset(3) with a structure");
    126 }
    127 
    128 ATF_TC_BODY(memset_struct, tc)
    129 {
    130 	struct stat st;
    131 
    132 	st.st_dev = 0;
    133 	st.st_ino = 1;
    134 	st.st_mode = 2;
    135 	st.st_nlink = 3;
    136 	st.st_uid = 4;
    137 	st.st_gid = 5;
    138 	st.st_rdev = 6;
    139 	st.st_size = 7;
    140 	st.st_atime = 8;
    141 	st.st_mtime = 9;
    142 
    143 	(void)memset(&st, 0, sizeof(struct stat));
    144 
    145 	ATF_REQUIRE(st.st_dev == 0);
    146 	ATF_REQUIRE(st.st_ino == 0);
    147 	ATF_REQUIRE(st.st_mode == 0);
    148 	ATF_REQUIRE(st.st_nlink == 0);
    149 	ATF_REQUIRE(st.st_uid == 0);
    150 	ATF_REQUIRE(st.st_gid == 0);
    151 	ATF_REQUIRE(st.st_rdev == 0);
    152 	ATF_REQUIRE(st.st_size == 0);
    153 	ATF_REQUIRE(st.st_atime == 0);
    154 	ATF_REQUIRE(st.st_mtime == 0);
    155 }
    156 
    157 static void
    158 fill(char *buf, size_t len, char x)
    159 {
    160 	size_t i;
    161 
    162 	for (i = 0; i < len; i++)
    163 		buf[i] = x;
    164 }
    165 
    166 static bool
    167 check(char *buf, size_t len, char x)
    168 {
    169 	size_t i;
    170 
    171 	for (i = 0; i < len; i++) {
    172 
    173 		if (buf[i] != x)
    174 			return false;
    175 	}
    176 
    177 	return true;
    178 }
    179 
    180 ATF_TP_ADD_TCS(tp)
    181 {
    182 
    183 	page = sysconf(_SC_PAGESIZE);
    184 	ATF_REQUIRE(page >= 0);
    185 
    186 	ATF_TP_ADD_TC(tp, memset_array);
    187 	ATF_TP_ADD_TC(tp, memset_basic);
    188 	ATF_TP_ADD_TC(tp, memset_nonzero);
    189 	ATF_TP_ADD_TC(tp, memset_struct);
    190 
    191 	return atf_no_error();
    192 }
    193