1272343Sngie/* $NetBSD: t_clearerr.c,v 1.1 2011/05/01 16:36:37 jruoho Exp $ */
2272343Sngie
3272343Sngie/*
4272343Sngie * Copyright (c) 2009, Stathis Kamperis
5272343Sngie * All rights reserved.
6272343Sngie
7272343Sngie * Redistribution and use in source and binary forms, with or without
8272343Sngie * modification, are permitted provided that the following conditions
9272343Sngie * are met:
10272343Sngie * 1. Redistributions of source code must retain the above copyright
11272343Sngie *    notice, this list of conditions and the following disclaimer.
12272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
13272343Sngie *    notice, this list of conditions and the following disclaimer in the
14272343Sngie *    documentation and/or other materials provided with the distribution.
15272343Sngie *
16272343Sngie * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18272343Sngie * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19272343Sngie * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
20272343Sngie * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21272343Sngie * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
22272343Sngie * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23272343Sngie * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24272343Sngie * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25272343Sngie * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26272343Sngie * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27272343Sngie * SUCH DAMAGE.
28272343Sngie */
29272343Sngie#include <sys/cdefs.h>
30272343Sngie__RCSID("$NetBSD: t_clearerr.c,v 1.1 2011/05/01 16:36:37 jruoho Exp $");
31272343Sngie
32272343Sngie#include <atf-c.h>
33272343Sngie#include <errno.h>
34272343Sngie#include <stdio.h>
35272343Sngie
36272343Sngiestatic const char	path[] = "/etc/passwd";
37272343Sngie
38272343SngieATF_TC(clearerr_basic);
39272343SngieATF_TC_HEAD(clearerr_basic, tc)
40272343Sngie{
41272343Sngie	atf_tc_set_md_var(tc, "descr", "A basic test of clearerr(3)");
42272343Sngie}
43272343Sngie
44272343SngieATF_TC_BODY(clearerr_basic, tc)
45272343Sngie{
46272343Sngie	char buf[2048];
47272343Sngie	FILE *fp;
48272343Sngie
49272343Sngie	fp = fopen(path, "r");
50272343Sngie	ATF_REQUIRE(fp != NULL);
51272343Sngie
52272343Sngie	while (feof(fp) == 0)
53272343Sngie		(void)fread(buf, sizeof(buf), 1, fp);
54272343Sngie
55272343Sngie	ATF_REQUIRE(feof(fp) != 0);
56272343Sngie
57272343Sngie	errno = 0;
58272343Sngie	clearerr(fp);
59272343Sngie
60272343Sngie	ATF_REQUIRE(errno == 0);
61272343Sngie	ATF_REQUIRE(feof(fp) == 0);
62272343Sngie	ATF_REQUIRE(fclose(fp) != EOF);
63272343Sngie}
64272343Sngie
65272343SngieATF_TC(clearerr_err);
66272343SngieATF_TC_HEAD(clearerr_err, tc)
67272343Sngie{
68272343Sngie	atf_tc_set_md_var(tc, "descr", "Test that clearerr(3) does not fail");
69272343Sngie}
70272343Sngie
71272343SngieATF_TC_BODY(clearerr_err, tc)
72272343Sngie{
73272343Sngie	FILE *fp;
74272343Sngie
75272343Sngie	fp = fopen(path, "r");
76272343Sngie
77272343Sngie	ATF_REQUIRE(fp != NULL);
78272343Sngie	ATF_REQUIRE(fclose(fp) == 0);
79272343Sngie
80272343Sngie	errno = 0;
81272343Sngie	clearerr(fp);
82272343Sngie
83272343Sngie	ATF_REQUIRE(errno == 0);
84272343Sngie}
85272343Sngie
86272343SngieATF_TP_ADD_TCS(tp)
87272343Sngie{
88272343Sngie
89272343Sngie	ATF_TP_ADD_TC(tp, clearerr_basic);
90272343Sngie	ATF_TP_ADD_TC(tp, clearerr_err);
91272343Sngie
92272343Sngie	return atf_no_error();
93272343Sngie}
94