1 /* $NetBSD: t_fflush.c,v 1.1 2011/09/11 05:15:55 jruoho Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_fflush.c,v 1.1 2011/09/11 05:15:55 jruoho Exp $");
39 static const char *path
= "fflush";
41 ATF_TC_WITH_CLEANUP(fflush_err
);
42 ATF_TC_HEAD(fflush_err
, tc
)
44 atf_tc_set_md_var(tc
, "descr", "Test errors from fflush(3)");
47 ATF_TC_BODY(fflush_err
, tc
)
53 ATF_REQUIRE(f
!= NULL
);
54 ATF_REQUIRE(fflush(NULL
) == 0);
55 ATF_REQUIRE(fclose(f
) == 0);
58 ATF_REQUIRE(f
!= NULL
);
61 * In NetBSD the call should fail if the supplied
62 * parameteris not an open stream or the stream is
63 * not open for writing.
66 ATF_REQUIRE_ERRNO(EBADF
, fflush(f
) == EOF
);
68 ATF_REQUIRE(fclose(f
) == 0);
71 ATF_REQUIRE_ERRNO(EBADF
, fflush(f
) == EOF
);
76 ATF_TC_CLEANUP(fflush_err
, tc
)
81 ATF_TC_WITH_CLEANUP(fflush_seek
);
82 ATF_TC_HEAD(fflush_seek
, tc
)
84 atf_tc_set_md_var(tc
, "descr", "Test file offsets with fflush(3)");
87 ATF_TC_BODY(fflush_seek
, tc
)
94 * IEEE Std 1003.1-2008:
96 * "For a stream open for reading, if the file
97 * is not already at EOF, and the file is one
98 * capable of seeking, the file offset of the
99 * underlying open file description shall be
100 * adjusted so that the next operation on the
101 * open file description deals with the byte
102 * after the last one read from or written to
103 * the stream being flushed."
105 f
= fopen(path
, "w");
106 ATF_REQUIRE(f
!= NULL
);
108 ATF_REQUIRE(fwrite("garbage", 1, 7, f
) == 7);
109 ATF_REQUIRE(fclose(f
) == 0);
111 f
= fopen(path
, "r+");
112 ATF_REQUIRE(f
!= NULL
);
115 ATF_REQUIRE(fd
!= -1);
117 ATF_REQUIRE(fread(buf
, 1, 3, f
) == 3);
118 ATF_REQUIRE(fflush(f
) == 0);
119 ATF_REQUIRE(fseek(f
, 0, SEEK_CUR
) == 0);
122 * Verify that the offsets are right and that
123 * a read operation resumes at the correct location.
125 ATF_REQUIRE(ftell(f
) == 3);
126 ATF_REQUIRE(lseek(fd
, 0, SEEK_CUR
) == 3);
127 ATF_REQUIRE(fgetc(f
) == 'b');
129 ATF_REQUIRE(fclose(f
) == 0);
130 ATF_REQUIRE(unlink(path
) == 0);
133 ATF_TC_CLEANUP(fflush_seek
, tc
)
138 ATF_TC_WITH_CLEANUP(fpurge_err
);
139 ATF_TC_HEAD(fpurge_err
, tc
)
141 atf_tc_set_md_var(tc
, "descr", "Test errors from fpurge(3)");
144 ATF_TC_BODY(fpurge_err
, tc
)
148 f
= fopen(path
, "w");
149 ATF_REQUIRE(f
!= NULL
);
150 ATF_REQUIRE(fclose(f
) == 0);
153 ATF_REQUIRE_ERRNO(EBADF
, fpurge(f
) == EOF
);
158 ATF_TC_CLEANUP(fpurge_err
, tc
)
166 ATF_TP_ADD_TC(tp
, fflush_err
);
167 ATF_TP_ADD_TC(tp
, fflush_seek
);
168 ATF_TP_ADD_TC(tp
, fpurge_err
);
170 return atf_no_error();