xref: /freebsd/contrib/netbsd-tests/lib/libc/stdio/t_clearerr.c (revision 9268022b74279434ed6300244e3f977e56a8ceb5)
1*57718be8SEnji Cooper /* $NetBSD: t_clearerr.c,v 1.1 2011/05/01 16:36:37 jruoho Exp $ */
2*57718be8SEnji Cooper 
3*57718be8SEnji Cooper /*
4*57718be8SEnji Cooper  * Copyright (c) 2009, Stathis Kamperis
5*57718be8SEnji Cooper  * All rights reserved.
6*57718be8SEnji Cooper 
7*57718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
8*57718be8SEnji Cooper  * modification, are permitted provided that the following conditions
9*57718be8SEnji Cooper  * are met:
10*57718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
11*57718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
12*57718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
13*57718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
14*57718be8SEnji Cooper  *    documentation and/or other materials provided with the distribution.
15*57718be8SEnji Cooper  *
16*57718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17*57718be8SEnji Cooper  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18*57718be8SEnji Cooper  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19*57718be8SEnji Cooper  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
20*57718be8SEnji Cooper  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21*57718be8SEnji Cooper  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
22*57718be8SEnji Cooper  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23*57718be8SEnji Cooper  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24*57718be8SEnji Cooper  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25*57718be8SEnji Cooper  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26*57718be8SEnji Cooper  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*57718be8SEnji Cooper  * SUCH DAMAGE.
28*57718be8SEnji Cooper  */
29*57718be8SEnji Cooper #include <sys/cdefs.h>
30*57718be8SEnji Cooper __RCSID("$NetBSD: t_clearerr.c,v 1.1 2011/05/01 16:36:37 jruoho Exp $");
31*57718be8SEnji Cooper 
32*57718be8SEnji Cooper #include <atf-c.h>
33*57718be8SEnji Cooper #include <errno.h>
34*57718be8SEnji Cooper #include <stdio.h>
35*57718be8SEnji Cooper 
36*57718be8SEnji Cooper static const char	path[] = "/etc/passwd";
37*57718be8SEnji Cooper 
38*57718be8SEnji Cooper ATF_TC(clearerr_basic);
ATF_TC_HEAD(clearerr_basic,tc)39*57718be8SEnji Cooper ATF_TC_HEAD(clearerr_basic, tc)
40*57718be8SEnji Cooper {
41*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "A basic test of clearerr(3)");
42*57718be8SEnji Cooper }
43*57718be8SEnji Cooper 
ATF_TC_BODY(clearerr_basic,tc)44*57718be8SEnji Cooper ATF_TC_BODY(clearerr_basic, tc)
45*57718be8SEnji Cooper {
46*57718be8SEnji Cooper 	char buf[2048];
47*57718be8SEnji Cooper 	FILE *fp;
48*57718be8SEnji Cooper 
49*57718be8SEnji Cooper 	fp = fopen(path, "r");
50*57718be8SEnji Cooper 	ATF_REQUIRE(fp != NULL);
51*57718be8SEnji Cooper 
52*57718be8SEnji Cooper 	while (feof(fp) == 0)
53*57718be8SEnji Cooper 		(void)fread(buf, sizeof(buf), 1, fp);
54*57718be8SEnji Cooper 
55*57718be8SEnji Cooper 	ATF_REQUIRE(feof(fp) != 0);
56*57718be8SEnji Cooper 
57*57718be8SEnji Cooper 	errno = 0;
58*57718be8SEnji Cooper 	clearerr(fp);
59*57718be8SEnji Cooper 
60*57718be8SEnji Cooper 	ATF_REQUIRE(errno == 0);
61*57718be8SEnji Cooper 	ATF_REQUIRE(feof(fp) == 0);
62*57718be8SEnji Cooper 	ATF_REQUIRE(fclose(fp) != EOF);
63*57718be8SEnji Cooper }
64*57718be8SEnji Cooper 
65*57718be8SEnji Cooper ATF_TC(clearerr_err);
ATF_TC_HEAD(clearerr_err,tc)66*57718be8SEnji Cooper ATF_TC_HEAD(clearerr_err, tc)
67*57718be8SEnji Cooper {
68*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Test that clearerr(3) does not fail");
69*57718be8SEnji Cooper }
70*57718be8SEnji Cooper 
ATF_TC_BODY(clearerr_err,tc)71*57718be8SEnji Cooper ATF_TC_BODY(clearerr_err, tc)
72*57718be8SEnji Cooper {
73*57718be8SEnji Cooper 	FILE *fp;
74*57718be8SEnji Cooper 
75*57718be8SEnji Cooper 	fp = fopen(path, "r");
76*57718be8SEnji Cooper 
77*57718be8SEnji Cooper 	ATF_REQUIRE(fp != NULL);
78*57718be8SEnji Cooper 	ATF_REQUIRE(fclose(fp) == 0);
79*57718be8SEnji Cooper 
80*57718be8SEnji Cooper 	errno = 0;
81*57718be8SEnji Cooper 	clearerr(fp);
82*57718be8SEnji Cooper 
83*57718be8SEnji Cooper 	ATF_REQUIRE(errno == 0);
84*57718be8SEnji Cooper }
85*57718be8SEnji Cooper 
ATF_TP_ADD_TCS(tp)86*57718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
87*57718be8SEnji Cooper {
88*57718be8SEnji Cooper 
89*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, clearerr_basic);
90*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, clearerr_err);
91*57718be8SEnji Cooper 
92*57718be8SEnji Cooper 	return atf_no_error();
93*57718be8SEnji Cooper }
94