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