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