Mercurial > hg > octave-shane > gnulib-hg
annotate tests/test-freading.c @ 14351:724011432f7b
Consistent macro naming for macros that use GCC __attribute__.
* lib/di-set.h (_GL_ATTRIBUTE_NONNULL): Renamed from
_ATTRIBUTE_NONNULL_.
* lib/ino-map.h (_GL_ATTRIBUTE_NONNULL): Likewise.
* lib/hash.h (_GL_ATTRIBUTE_WUR): Renamed from ATTRIBUTE_WUR.
* lib/ignore-value.h (_GL_ATTRIBUTE_DEPRECATED): Renamed from
ATTRIBUTE_DEPRECATED.
* lib/openat.h (_GL_ATTRIBUTE_NORETURN): Renamed from
ATTRIBUTE_NORETURN.
* lib/sigpipe-die.h (_GL_ATTRIBUTE_NORETURN): Likewise.
* lib/xmemdup0.h (_GL_ATTRIBUTE_NORETURN): Likewise.
* lib/xstrtol.h (_GL_ATTRIBUTE_NORETURN): Likewise.
* lib/xalloc.h (_GL_ATTRIBUTE_NORETURN): Likewise.
(_GL_ATTRIBUTE_MALLOC): Renamed from ATTRIBUTE_MALLOC.
(_GL_ATTRIBUTE_ALLOC_SIZE): Renamed from ATTRIBUTE_ALLOC_SIZE.
* lib/version-etc.h (_GL_ATTRIBUTE_SENTINEL): Renamed from
ATTRIBUTE_SENTINEL.
* lib/safe-alloc.h (_GL_ATTRIBUTE_RETURN_CHECK): Renamed from
ATTRIBUTE_RETURN_CHECK.
* tests/test-ignore-value.c (_GL_ATTRIBUTE_RETURN_CHECK): Likewise.
* tests/test-argmatch.c (_GL_ATTRIBUTE_NORETURN): Renamed from
ATTRIBUTE_NORETURN.
* tests/test-exclude.c (_GL_ATTRIBUTE_NORETURN): Likewise.
Reported by Paul Eggert.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Sun, 13 Feb 2011 23:44:28 +0100 |
parents | 97fc9a21a8fb |
children | 8250f2777afc |
rev | line source |
---|---|
8720 | 1 /* Test of freading() 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 "freading.h" |
25 | |
8754 | 26 #include <stdio.h> |
12496
a48d3d749ca5
Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents:
12297
diff
changeset
|
27 |
a48d3d749ca5
Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents:
12297
diff
changeset
|
28 #include "macros.h" |
8720 | 29 |
30 #define TESTFILE "t-freading.tmp" | |
31 | |
32 int | |
12197
e45d9bb2233e
tests: avoid several compiler warnings
Eric Blake <ebb9@byu.net>
parents:
9889
diff
changeset
|
33 main (void) |
8720 | 34 { |
35 FILE *fp; | |
36 | |
37 /* Create a file with some contents. Write-only file is never reading. */ | |
38 fp = fopen (TESTFILE, "w"); | |
12297
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
39 ASSERT (fp); |
8720 | 40 ASSERT (!freading (fp)); |
12297
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
41 ASSERT (fwrite ("foobarsh", 1, 8, fp) == 8); |
8720 | 42 ASSERT (!freading (fp)); |
12297
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
43 ASSERT (fclose (fp) == 0); |
8720 | 44 |
45 /* Open it in read-only mode. Read-only file is always reading. */ | |
46 fp = fopen (TESTFILE, "r"); | |
12297
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
47 ASSERT (fp); |
8720 | 48 ASSERT (freading (fp)); |
12297
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
49 ASSERT (fgetc (fp) == 'f'); |
8720 | 50 ASSERT (freading (fp)); |
12297
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
51 ASSERT (fseek (fp, 2, SEEK_CUR) == 0); |
8720 | 52 ASSERT (freading (fp)); |
12297
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
53 ASSERT (fgetc (fp) == 'b'); |
8720 | 54 ASSERT (freading (fp)); |
8750
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
55 fflush (fp); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
56 ASSERT (freading (fp)); |
12297
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
57 ASSERT (fgetc (fp) == 'a'); |
8750
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
58 ASSERT (freading (fp)); |
12297
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
59 ASSERT (fseek (fp, 0, SEEK_END) == 0); |
8720 | 60 ASSERT (freading (fp)); |
12297
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
61 ASSERT (fclose (fp) == 0); |
8720 | 62 |
63 /* Open it in read-write mode. POSIX requires a reposition (fseek, | |
64 fsetpos, rewind) or EOF when transitioning from read to write; | |
65 freading is only deterministic after input or output, but this | |
66 test case should be portable even on open, after reposition, and | |
67 at EOF. */ | |
8751
e66db74e11a7
Oops, fix details and comments of last patch.
Bruno Haible <bruno@clisp.org>
parents:
8750
diff
changeset
|
68 /* First a scenario with only fgetc, fseek, fputc. */ |
8720 | 69 fp = fopen (TESTFILE, "r+"); |
12297
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
70 ASSERT (fp); |
8720 | 71 ASSERT (!freading (fp)); |
12297
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
72 ASSERT (fgetc (fp) == 'f'); |
8720 | 73 ASSERT (freading (fp)); |
12297
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
74 ASSERT (fseek (fp, 2, SEEK_CUR) == 0); |
8750
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
75 /* freading (fp) is undefined here, but fwriting (fp) is false. */ |
12297
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
76 ASSERT (fgetc (fp) == 'b'); |
8720 | 77 ASSERT (freading (fp)); |
8750
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
78 /* 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
|
79 See the description of fopen(), ISO C 99 7.19.5.3.(6). */ |
12297
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
80 ASSERT (fseek (fp, 0, SEEK_CUR) == 0); |
8750
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
81 /* freading (fp) is undefined here, but fwriting (fp) is false. */ |
12297
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
82 ASSERT (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
|
83 ASSERT (!freading (fp)); |
12297
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
84 ASSERT (fseek (fp, 0, SEEK_END) == 0); |
8753
10d7922803f5
freading has an undefined value after repositioning a read-write stream.
Bruno Haible <bruno@clisp.org>
parents:
8751
diff
changeset
|
85 /* 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
|
86 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
|
87 fwriting (fp) is undefined as well. */ |
12297
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
88 ASSERT (fclose (fp) == 0); |
8750
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
89 |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
90 /* 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
|
91 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
|
92 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
|
93 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
|
94 at EOF. */ |
8751
e66db74e11a7
Oops, fix details and comments of last patch.
Bruno Haible <bruno@clisp.org>
parents:
8750
diff
changeset
|
95 /* 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
|
96 fp = fopen (TESTFILE, "r+"); |
12297
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
97 ASSERT (fp); |
8750
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
98 ASSERT (!freading (fp)); |
12297
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
99 ASSERT (fgetc (fp) == 'f'); |
8750
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
100 ASSERT (freading (fp)); |
12297
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
101 ASSERT (fseek (fp, 2, SEEK_CUR) == 0); |
8750
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
102 /* freading (fp) is undefined here, but fwriting (fp) is false. */ |
12297
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
103 ASSERT (fgetc (fp) == 'b'); |
8750
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
104 ASSERT (freading (fp)); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
105 fflush (fp); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
106 /* freading (fp) is undefined here, but fwriting (fp) is false. */ |
12297
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
107 ASSERT (fgetc (fp) == 'x'); |
8750
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
108 ASSERT (freading (fp)); |
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
109 /* 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
|
110 See the description of fopen(), ISO C 99 7.19.5.3.(6). */ |
12297
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
111 ASSERT (fseek (fp, 0, SEEK_CUR) == 0); |
8750
67a7b3fd6cf2
Make the tests sharper: also test the interaction with fflush().
Bruno Haible <bruno@clisp.org>
parents:
8720
diff
changeset
|
112 /* freading (fp) is undefined here, but fwriting (fp) is false. */ |
12297
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
113 ASSERT (fputc ('z', fp) == 'z'); |
8720 | 114 ASSERT (!freading (fp)); |
12297
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
115 ASSERT (fseek (fp, 0, SEEK_END) == 0); |
8753
10d7922803f5
freading has an undefined value after repositioning a read-write stream.
Bruno Haible <bruno@clisp.org>
parents:
8751
diff
changeset
|
116 /* 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
|
117 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
|
118 fwriting (fp) is undefined as well. */ |
12297
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
119 ASSERT (fclose (fp) == 0); |
8720 | 120 |
121 /* Open it in append mode. Write-only file is never reading. */ | |
122 fp = fopen (TESTFILE, "a"); | |
12297
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
123 ASSERT (fp); |
8720 | 124 ASSERT (!freading (fp)); |
12297
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
125 ASSERT (fwrite ("bla", 1, 3, fp) == 3); |
8720 | 126 ASSERT (!freading (fp)); |
12297
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
127 ASSERT (fclose (fp) == 0); |
77743721a2f3
test-freading: clean up temporary file
Eric Blake <ebb9@byu.net>
parents:
12255
diff
changeset
|
128 ASSERT (remove (TESTFILE) == 0); |
8720 | 129 return 0; |
130 } |