Mercurial > hg > octave-lojdl > gnulib-hg
annotate tests/test-fwriting.c @ 14079:97fc9a21a8fb
maint: update almost all copyright ranges to include 2011
Run the new "make update-copyright" rule.
author | Jim Meyering <meyering@redhat.com> |
---|---|
date | Sat, 01 Jan 2011 20:17:23 +0100 |
parents | bc2866336bbb |
children | 8250f2777afc |
rev | line source |
---|---|
8720 | 1 /* Test of fwriting() function. |
14079
97fc9a21a8fb
maint: update almost all copyright ranges to include 2011
Jim Meyering <meyering@redhat.com>
parents:
12724
diff
changeset
|
2 Copyright (C) 2007-2011 Free Software Foundation, Inc. |
8720 | 3 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8754
diff
changeset
|
4 This program is free software: you can redistribute it and/or modify |
8720 | 5 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8754
diff
changeset
|
6 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8754
diff
changeset
|
7 (at your option) any later version. |
8720 | 8 |
9 This program is distributed in the hope that it will be useful, | |
10 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 GNU General Public License for more details. | |
13 | |
14 You should have received a copy of the GNU General Public License | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8754
diff
changeset
|
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
8720 | 16 |
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */ | |
18 | |
19 #include <config.h> | |
20 | |
12724 | 21 /* None of the files accessed by this test are large, so disable the |
22 fseek link warning if we are not using the gnulib fseek module. */ | |
23 #define _GL_NO_LARGE_FILES | |
8720 | 24 #include "fwriting.h" |
25 | |
8754 | 26 #include <stdio.h> |
12496
a48d3d749ca5
Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
27 |
a48d3d749ca5
Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
28 #include "macros.h" |
8720 | 29 |
30 #define TESTFILE "t-fwriting.tmp" | |
31 | |
32 int | |
33 main () | |
34 { | |
35 FILE *fp; | |
36 | |
37 /* Create a file with some contents. Write-only file is always writing. */ | |
38 fp = fopen (TESTFILE, "w"); | |
39 if (fp == NULL) | |
40 goto skip; | |
41 ASSERT (fwriting (fp)); | |
42 if (fwrite ("foobarsh", 1, 8, fp) < 8) | |
43 goto skip; | |
44 ASSERT (fwriting (fp)); | |
45 if (fclose (fp)) | |
46 goto skip; | |
47 | |
48 /* Open it in read-only mode. Read-only file is never writing. */ | |
49 fp = fopen (TESTFILE, "r"); | |
50 if (fp == NULL) | |
51 goto skip; | |
52 ASSERT (!fwriting (fp)); | |
53 if (fgetc (fp) != 'f') | |
54 goto skip; | |
55 ASSERT (!fwriting (fp)); | |
56 if (fseek (fp, 2, SEEK_CUR)) | |
57 goto skip; | |
58 ASSERT (!fwriting (fp)); | |
59 if (fgetc (fp) != 'b') | |
60 goto skip; | |
61 ASSERT (!fwriting (fp)); | |
8750
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
62 fflush (fp); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
63 ASSERT (!fwriting (fp)); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
64 if (fgetc (fp) != 'a') |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
65 goto skip; |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
66 ASSERT (!fwriting (fp)); |
8720 | 67 if (fseek (fp, 0, SEEK_END)) |
68 goto skip; | |
69 ASSERT (!fwriting (fp)); | |
70 if (fclose (fp)) | |
71 goto skip; | |
72 | |
73 /* Open it in read-write mode. POSIX requires a reposition (fseek, | |
74 fsetpos, rewind) or fflush when transitioning from write to read, | |
75 fwriting is only deterministic after input or output, but this | |
76 test case should be portable even on open, after reposition, and | |
77 after fflush. */ | |
8751
e66db74e11a7
Oops, fix details and comments of last patch.
Bruno Haible <bruno@clisp.org>
parents:
8750
diff
changeset
|
78 /* First a scenario with only fgetc, fseek, fputc. */ |
8720 | 79 fp = fopen (TESTFILE, "r+"); |
80 if (fp == NULL) | |
81 goto skip; | |
82 ASSERT (!fwriting (fp)); | |
83 if (fgetc (fp) != 'f') | |
84 goto skip; | |
85 ASSERT (!fwriting (fp)); | |
86 if (fseek (fp, 2, SEEK_CUR)) | |
87 goto skip; | |
88 ASSERT (!fwriting (fp)); | |
89 if (fgetc (fp) != 'b') | |
90 goto skip; | |
91 ASSERT (!fwriting (fp)); | |
8750
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
92 /* This fseek call is necessary when switching from reading to writing. |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
93 See the description of fopen(), ISO C 99 7.19.5.3.(6). */ |
8720 | 94 if (fseek (fp, 0, SEEK_CUR) != 0) |
95 goto skip; | |
8750
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
96 ASSERT (!fwriting (fp)); |
8751
e66db74e11a7
Oops, fix details and comments of last patch.
Bruno Haible <bruno@clisp.org>
parents:
8750
diff
changeset
|
97 if (fputc ('x', fp) != 'x') |
8720 | 98 goto skip; |
99 ASSERT (fwriting (fp)); | |
100 if (fseek (fp, 0, SEEK_END)) | |
101 goto skip; | |
8753
10d7922803f5
freading has an undefined value after repositioning a read-write stream.
Bruno Haible <bruno@clisp.org>
parents:
8751
diff
changeset
|
102 /* freading (fp) is undefined here, because on some implementations (e.g. |
10d7922803f5
freading has an undefined value after repositioning a read-write stream.
Bruno Haible <bruno@clisp.org>
parents:
8751
diff
changeset
|
103 glibc) fseek causes a buffer to be read. |
10d7922803f5
freading has an undefined value after repositioning a read-write stream.
Bruno Haible <bruno@clisp.org>
parents:
8751
diff
changeset
|
104 fwriting (fp) is undefined as well. */ |
8750
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
105 if (fclose (fp)) |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
106 goto skip; |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
107 |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
108 /* Open it in read-write mode. POSIX requires a reposition (fseek, |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
109 fsetpos, rewind) or fflush when transitioning from write to read, |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
110 fwriting is only deterministic after input or output, but this |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
111 test case should be portable even on open, after reposition, and |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
112 after fflush. */ |
8751
e66db74e11a7
Oops, fix details and comments of last patch.
Bruno Haible <bruno@clisp.org>
parents:
8750
diff
changeset
|
113 /* Here a scenario that includes fflush. */ |
8750
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
114 fp = fopen (TESTFILE, "r+"); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
115 if (fp == NULL) |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
116 goto skip; |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
117 ASSERT (!fwriting (fp)); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
118 if (fgetc (fp) != 'f') |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
119 goto skip; |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
120 ASSERT (!fwriting (fp)); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
121 if (fseek (fp, 2, SEEK_CUR)) |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
122 goto skip; |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
123 ASSERT (!fwriting (fp)); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
124 if (fgetc (fp) != 'b') |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
125 goto skip; |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
126 ASSERT (!fwriting (fp)); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
127 fflush (fp); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
128 ASSERT (!fwriting (fp)); |
8751
e66db74e11a7
Oops, fix details and comments of last patch.
Bruno Haible <bruno@clisp.org>
parents:
8750
diff
changeset
|
129 if (fgetc (fp) != 'x') |
8750
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
130 goto skip; |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
131 ASSERT (!fwriting (fp)); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
132 /* This fseek call is necessary when switching from reading to writing. |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
133 See the description of fopen(), ISO C 99 7.19.5.3.(6). */ |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
134 if (fseek (fp, 0, SEEK_CUR) != 0) |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
135 goto skip; |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
136 ASSERT (!fwriting (fp)); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
137 if (fputc ('z', fp) != 'z') |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
138 goto skip; |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
139 ASSERT (fwriting (fp)); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
140 if (fseek (fp, 0, SEEK_END)) |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
141 goto skip; |
8753
10d7922803f5
freading has an undefined value after repositioning a read-write stream.
Bruno Haible <bruno@clisp.org>
parents:
8751
diff
changeset
|
142 /* freading (fp) is undefined here, because on some implementations (e.g. |
10d7922803f5
freading has an undefined value after repositioning a read-write stream.
Bruno Haible <bruno@clisp.org>
parents:
8751
diff
changeset
|
143 glibc) fseek causes a buffer to be read. |
10d7922803f5
freading has an undefined value after repositioning a read-write stream.
Bruno Haible <bruno@clisp.org>
parents:
8751
diff
changeset
|
144 fwriting (fp) is undefined as well. */ |
8720 | 145 if (fclose (fp)) |
146 goto skip; | |
147 | |
148 /* Open it in append mode. */ | |
149 fp = fopen (TESTFILE, "a"); | |
150 if (fp == NULL) | |
151 goto skip; | |
152 ASSERT (fwriting (fp)); | |
153 if (fwrite ("bla", 1, 3, fp) < 3) | |
154 goto skip; | |
155 ASSERT (fwriting (fp)); | |
156 if (fclose (fp)) | |
157 goto skip; | |
158 | |
159 return 0; | |
160 | |
161 skip: | |
162 fprintf (stderr, "Skipping test: file operations failed.\n"); | |
163 return 77; | |
164 } |