Mercurial > hg > octave-lojdl > gnulib-hg
annotate tests/test-freading.c @ 9309:bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Sun, 07 Oct 2007 19:14:58 +0200 |
parents | 1f57552cdb11 |
children | 0be6f1ab456d |
rev | line source |
---|---|
8720 | 1 /* Test of freading() function. |
2 Copyright (C) 2007 Free Software Foundation, Inc. | |
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 | |
21 #include "freading.h" | |
22 | |
8754 | 23 #include <stdio.h> |
8720 | 24 #include <stdlib.h> |
25 | |
8754 | 26 #define ASSERT(expr) \ |
27 do \ | |
28 { \ | |
29 if (!(expr)) \ | |
30 { \ | |
31 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ | |
32 abort (); \ | |
33 } \ | |
34 } \ | |
35 while (0) | |
8720 | 36 |
37 #define TESTFILE "t-freading.tmp" | |
38 | |
39 int | |
40 main () | |
41 { | |
42 FILE *fp; | |
43 | |
44 /* Create a file with some contents. Write-only file is never reading. */ | |
45 fp = fopen (TESTFILE, "w"); | |
46 if (fp == NULL) | |
47 goto skip; | |
48 ASSERT (!freading (fp)); | |
49 if (fwrite ("foobarsh", 1, 8, fp) < 8) | |
50 goto skip; | |
51 ASSERT (!freading (fp)); | |
52 if (fclose (fp)) | |
53 goto skip; | |
54 | |
55 /* Open it in read-only mode. Read-only file is always reading. */ | |
56 fp = fopen (TESTFILE, "r"); | |
57 if (fp == NULL) | |
58 goto skip; | |
59 ASSERT (freading (fp)); | |
60 if (fgetc (fp) != 'f') | |
61 goto skip; | |
62 ASSERT (freading (fp)); | |
63 if (fseek (fp, 2, SEEK_CUR)) | |
64 goto skip; | |
65 ASSERT (freading (fp)); | |
66 if (fgetc (fp) != 'b') | |
67 goto skip; | |
68 ASSERT (freading (fp)); | |
8750
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
69 fflush (fp); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
70 ASSERT (freading (fp)); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
71 if (fgetc (fp) != 'a') |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
72 goto skip; |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
73 ASSERT (freading (fp)); |
8720 | 74 if (fseek (fp, 0, SEEK_END)) |
75 goto skip; | |
76 ASSERT (freading (fp)); | |
77 if (fclose (fp)) | |
78 goto skip; | |
79 | |
80 /* Open it in read-write mode. POSIX requires a reposition (fseek, | |
81 fsetpos, rewind) or EOF when transitioning from read to write; | |
82 freading is only deterministic after input or output, but this | |
83 test case should be portable even on open, after reposition, and | |
84 at EOF. */ | |
8751
e66db74e11a7
Oops, fix details and comments of last patch.
Bruno Haible <bruno@clisp.org>
parents:
8750
diff
changeset
|
85 /* First a scenario with only fgetc, fseek, fputc. */ |
8720 | 86 fp = fopen (TESTFILE, "r+"); |
87 if (fp == NULL) | |
88 goto skip; | |
89 ASSERT (!freading (fp)); | |
90 if (fgetc (fp) != 'f') | |
91 goto skip; | |
92 ASSERT (freading (fp)); | |
93 if (fseek (fp, 2, SEEK_CUR)) | |
94 goto skip; | |
8750
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
95 /* freading (fp) is undefined here, but fwriting (fp) is false. */ |
8720 | 96 if (fgetc (fp) != 'b') |
97 goto skip; | |
98 ASSERT (freading (fp)); | |
8750
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
99 /* 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
|
100 See the description of fopen(), ISO C 99 7.19.5.3.(6). */ |
8720 | 101 if (fseek (fp, 0, SEEK_CUR) != 0) |
102 goto skip; | |
8750
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
103 /* freading (fp) is undefined here, but fwriting (fp) is false. */ |
8751
e66db74e11a7
Oops, fix details and comments of last patch.
Bruno Haible <bruno@clisp.org>
parents:
8750
diff
changeset
|
104 if (fputc ('x', fp) != 'x') |
8750
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
105 goto skip; |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
106 ASSERT (!freading (fp)); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
107 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
|
108 goto skip; |
8753
10d7922803f5
freading has an undefined value after repositioning a read-write stream.
Bruno Haible <bruno@clisp.org>
parents:
8751
diff
changeset
|
109 /* 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
|
110 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
|
111 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
|
112 if (fclose (fp)) |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
113 goto skip; |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
114 |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
115 /* 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
|
116 fsetpos, rewind) or EOF when transitioning from read to write; |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
117 freading 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
|
118 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
|
119 at EOF. */ |
8751
e66db74e11a7
Oops, fix details and comments of last patch.
Bruno Haible <bruno@clisp.org>
parents:
8750
diff
changeset
|
120 /* 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
|
121 fp = fopen (TESTFILE, "r+"); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
122 if (fp == NULL) |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
123 goto skip; |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
124 ASSERT (!freading (fp)); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
125 if (fgetc (fp) != 'f') |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
126 goto skip; |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
127 ASSERT (freading (fp)); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
128 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
|
129 goto skip; |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
130 /* freading (fp) is undefined here, but fwriting (fp) is false. */ |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
131 if (fgetc (fp) != 'b') |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
132 goto skip; |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
133 ASSERT (freading (fp)); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
134 fflush (fp); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
135 /* freading (fp) is undefined here, but fwriting (fp) is false. */ |
8751
e66db74e11a7
Oops, fix details and comments of last patch.
Bruno Haible <bruno@clisp.org>
parents:
8750
diff
changeset
|
136 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
|
137 goto skip; |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
138 ASSERT (freading (fp)); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
139 /* 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
|
140 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
|
141 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
|
142 goto skip; |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
143 /* freading (fp) is undefined here, but fwriting (fp) is false. */ |
8720 | 144 if (fputc ('z', fp) != 'z') |
145 goto skip; | |
146 ASSERT (!freading (fp)); | |
147 if (fseek (fp, 0, SEEK_END)) | |
148 goto skip; | |
8753
10d7922803f5
freading has an undefined value after repositioning a read-write stream.
Bruno Haible <bruno@clisp.org>
parents:
8751
diff
changeset
|
149 /* 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
|
150 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
|
151 fwriting (fp) is undefined as well. */ |
8720 | 152 if (fclose (fp)) |
153 goto skip; | |
154 | |
155 /* Open it in append mode. Write-only file is never reading. */ | |
156 fp = fopen (TESTFILE, "a"); | |
157 if (fp == NULL) | |
158 goto skip; | |
159 ASSERT (!freading (fp)); | |
160 if (fwrite ("bla", 1, 3, fp) < 3) | |
161 goto skip; | |
162 ASSERT (!freading (fp)); | |
163 if (fclose (fp)) | |
164 goto skip; | |
165 | |
166 return 0; | |
167 | |
168 skip: | |
169 fprintf (stderr, "Skipping test: file operations failed.\n"); | |
170 return 77; | |
171 } |