Mercurial > hg > octave-nkf > gnulib-hg
annotate tests/test-fwriting.c @ 9084:2932e92d6e31
* lib/version-etc.c (version_etc_va): Default to GPLv3+.
* NEWS: Document this change.
author | Eric Blake <ebb9@byu.net> |
---|---|
date | Tue, 10 Jul 2007 12:22:36 +0000 |
parents | 1f57552cdb11 |
children | bbbbbf4cd1c5 |
rev | line source |
---|---|
8720 | 1 /* Test of fwriting() function. |
2 Copyright (C) 2007 Free Software Foundation, Inc. | |
3 | |
4 This program is free software; you can redistribute it and/or modify | |
5 it under the terms of the GNU General Public License as published by | |
6 the Free Software Foundation; either version 2, or (at your option) | |
7 any later version. | |
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 | |
15 along with this program; if not, write to the Free Software Foundation, | |
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ | |
17 | |
18 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */ | |
19 | |
20 #include <config.h> | |
21 | |
22 #include "fwriting.h" | |
23 | |
8754 | 24 #include <stdio.h> |
8720 | 25 #include <stdlib.h> |
26 | |
8754 | 27 #define ASSERT(expr) \ |
28 do \ | |
29 { \ | |
30 if (!(expr)) \ | |
31 { \ | |
32 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ | |
33 abort (); \ | |
34 } \ | |
35 } \ | |
36 while (0) | |
8720 | 37 |
38 #define TESTFILE "t-fwriting.tmp" | |
39 | |
40 int | |
41 main () | |
42 { | |
43 FILE *fp; | |
44 | |
45 /* Create a file with some contents. Write-only file is always writing. */ | |
46 fp = fopen (TESTFILE, "w"); | |
47 if (fp == NULL) | |
48 goto skip; | |
49 ASSERT (fwriting (fp)); | |
50 if (fwrite ("foobarsh", 1, 8, fp) < 8) | |
51 goto skip; | |
52 ASSERT (fwriting (fp)); | |
53 if (fclose (fp)) | |
54 goto skip; | |
55 | |
56 /* Open it in read-only mode. Read-only file is never writing. */ | |
57 fp = fopen (TESTFILE, "r"); | |
58 if (fp == NULL) | |
59 goto skip; | |
60 ASSERT (!fwriting (fp)); | |
61 if (fgetc (fp) != 'f') | |
62 goto skip; | |
63 ASSERT (!fwriting (fp)); | |
64 if (fseek (fp, 2, SEEK_CUR)) | |
65 goto skip; | |
66 ASSERT (!fwriting (fp)); | |
67 if (fgetc (fp) != 'b') | |
68 goto skip; | |
69 ASSERT (!fwriting (fp)); | |
8750
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
70 fflush (fp); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
71 ASSERT (!fwriting (fp)); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
72 if (fgetc (fp) != 'a') |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
73 goto skip; |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
74 ASSERT (!fwriting (fp)); |
8720 | 75 if (fseek (fp, 0, SEEK_END)) |
76 goto skip; | |
77 ASSERT (!fwriting (fp)); | |
78 if (fclose (fp)) | |
79 goto skip; | |
80 | |
81 /* Open it in read-write mode. POSIX requires a reposition (fseek, | |
82 fsetpos, rewind) or fflush when transitioning from write to read, | |
83 fwriting is only deterministic after input or output, but this | |
84 test case should be portable even on open, after reposition, and | |
85 after fflush. */ | |
8751
e66db74e11a7
Oops, fix details and comments of last patch.
Bruno Haible <bruno@clisp.org>
parents:
8750
diff
changeset
|
86 /* First a scenario with only fgetc, fseek, fputc. */ |
8720 | 87 fp = fopen (TESTFILE, "r+"); |
88 if (fp == NULL) | |
89 goto skip; | |
90 ASSERT (!fwriting (fp)); | |
91 if (fgetc (fp) != 'f') | |
92 goto skip; | |
93 ASSERT (!fwriting (fp)); | |
94 if (fseek (fp, 2, SEEK_CUR)) | |
95 goto skip; | |
96 ASSERT (!fwriting (fp)); | |
97 if (fgetc (fp) != 'b') | |
98 goto skip; | |
99 ASSERT (!fwriting (fp)); | |
8750
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
100 /* 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
|
101 See the description of fopen(), ISO C 99 7.19.5.3.(6). */ |
8720 | 102 if (fseek (fp, 0, SEEK_CUR) != 0) |
103 goto skip; | |
8750
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
104 ASSERT (!fwriting (fp)); |
8751
e66db74e11a7
Oops, fix details and comments of last patch.
Bruno Haible <bruno@clisp.org>
parents:
8750
diff
changeset
|
105 if (fputc ('x', fp) != 'x') |
8720 | 106 goto skip; |
107 ASSERT (fwriting (fp)); | |
108 if (fseek (fp, 0, SEEK_END)) | |
109 goto skip; | |
8753
10d7922803f5
freading has an undefined value after repositioning a read-write stream.
Bruno Haible <bruno@clisp.org>
parents:
8751
diff
changeset
|
110 /* 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
|
111 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
|
112 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
|
113 if (fclose (fp)) |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
114 goto skip; |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
115 |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
116 /* 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
|
117 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
|
118 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
|
119 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
|
120 after fflush. */ |
8751
e66db74e11a7
Oops, fix details and comments of last patch.
Bruno Haible <bruno@clisp.org>
parents:
8750
diff
changeset
|
121 /* 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
|
122 fp = fopen (TESTFILE, "r+"); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
123 if (fp == NULL) |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
124 goto skip; |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
125 ASSERT (!fwriting (fp)); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
126 if (fgetc (fp) != 'f') |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
127 goto skip; |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
128 ASSERT (!fwriting (fp)); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
129 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
|
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 if (fgetc (fp) != 'b') |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
133 goto skip; |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
134 ASSERT (!fwriting (fp)); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
135 fflush (fp); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
136 ASSERT (!fwriting (fp)); |
8751
e66db74e11a7
Oops, fix details and comments of last patch.
Bruno Haible <bruno@clisp.org>
parents:
8750
diff
changeset
|
137 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
|
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 /* 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
|
141 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
|
142 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
|
143 goto skip; |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
144 ASSERT (!fwriting (fp)); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
145 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
|
146 goto skip; |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
147 ASSERT (fwriting (fp)); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
148 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
|
149 goto skip; |
8753
10d7922803f5
freading has an undefined value after repositioning a read-write stream.
Bruno Haible <bruno@clisp.org>
parents:
8751
diff
changeset
|
150 /* 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
|
151 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
|
152 fwriting (fp) is undefined as well. */ |
8720 | 153 if (fclose (fp)) |
154 goto skip; | |
155 | |
156 /* Open it in append mode. */ | |
157 fp = fopen (TESTFILE, "a"); | |
158 if (fp == NULL) | |
159 goto skip; | |
160 ASSERT (fwriting (fp)); | |
161 if (fwrite ("bla", 1, 3, fp) < 3) | |
162 goto skip; | |
163 ASSERT (fwriting (fp)); | |
164 if (fclose (fp)) | |
165 goto skip; | |
166 | |
167 return 0; | |
168 | |
169 skip: | |
170 fprintf (stderr, "Skipping test: file operations failed.\n"); | |
171 return 77; | |
172 } |