annotate tests/test-fwritable.c @ 16201:8250f2777afc

maint: update all copyright year number ranges Run "make update-copyright".
author Jim Meyering <meyering@redhat.com>
date Sun, 01 Jan 2012 10:04:58 +0100
parents 97fc9a21a8fb
children e542fd46ad6f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8690
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Test of fwritable() function.
16201
8250f2777afc maint: update all copyright year number ranges
Jim Meyering <meyering@redhat.com>
parents: 14079
diff changeset
2 Copyright (C) 2007-2012 Free Software Foundation, Inc.
8690
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
8690
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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.
8690
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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/>. */
8690
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 #include <config.h>
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20
12724
bc2866336bbb stdio: warn on suspicious uses
Eric Blake <ebb9@byu.net>
parents: 12559
diff changeset
21 /* None of the files accessed by this test are large, so disable the
bc2866336bbb stdio: warn on suspicious uses
Eric Blake <ebb9@byu.net>
parents: 12559
diff changeset
22 fseek link warning if we are not using the gnulib fseek module. */
bc2866336bbb stdio: warn on suspicious uses
Eric Blake <ebb9@byu.net>
parents: 12559
diff changeset
23 #define _GL_NO_LARGE_FILES
8690
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #include "fwritable.h"
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25
8754
1f57552cdb11 Better ASSERT macro.
Bruno Haible <bruno@clisp.org>
parents: 8690
diff changeset
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"
8690
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 #define TESTFILE "t-fwritable.tmp"
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 int
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 main ()
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 {
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 FILE *fp;
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 /* Create a file with some contents. */
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 fp = fopen (TESTFILE, "w");
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 if (fp == NULL)
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 goto skip;
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 ASSERT (fwritable (fp));
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 if (fwrite ("foobarsh", 1, 8, fp) < 8)
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 goto skip;
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 ASSERT (fwritable (fp));
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 if (fclose (fp))
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 goto skip;
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 /* Open it in read-only mode. */
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 fp = fopen (TESTFILE, "r");
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 if (fp == NULL)
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 goto skip;
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 ASSERT (!fwritable (fp));
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 if (fgetc (fp) != 'f')
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 goto skip;
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 ASSERT (!fwritable (fp));
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 if (fseek (fp, 2, SEEK_CUR))
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 goto skip;
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 ASSERT (!fwritable (fp));
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 if (fgetc (fp) != 'b')
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 goto skip;
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 ASSERT (!fwritable (fp));
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 fflush (fp);
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 ASSERT (!fwritable (fp));
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 if (fgetc (fp) != 'a')
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 goto skip;
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 ASSERT (!fwritable (fp));
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 if (fseek (fp, 0, SEEK_END))
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 goto skip;
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 ASSERT (!fwritable (fp));
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 if (fclose (fp))
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 goto skip;
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 /* Open it in read-write mode. */
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 fp = fopen (TESTFILE, "r+");
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 if (fp == NULL)
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 goto skip;
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 ASSERT (fwritable (fp));
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 if (fgetc (fp) != 'f')
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 goto skip;
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 ASSERT (fwritable (fp));
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 if (fseek (fp, 2, SEEK_CUR))
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 goto skip;
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 ASSERT (fwritable (fp));
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 if (fgetc (fp) != 'b')
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 goto skip;
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 ASSERT (fwritable (fp));
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 fflush (fp);
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 ASSERT (fwritable (fp));
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 if (fgetc (fp) != 'a')
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 goto skip;
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 ASSERT (fwritable (fp));
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 if (fputc ('z', fp) != 'z')
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 goto skip;
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 ASSERT (fwritable (fp));
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 if (fseek (fp, 0, SEEK_END))
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 goto skip;
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 ASSERT (fwritable (fp));
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 if (fclose (fp))
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 goto skip;
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 /* Open it in append mode. */
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 fp = fopen (TESTFILE, "a");
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 if (fp == NULL)
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 goto skip;
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 ASSERT (fwritable (fp));
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 if (fwrite ("bla", 1, 3, fp) < 3)
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 goto skip;
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 ASSERT (fwritable (fp));
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 if (fclose (fp))
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 goto skip;
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 return 0;
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 skip:
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 fprintf (stderr, "Skipping test: file operations failed.\n");
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116 return 77;
3283ac3682b6 Tests for module 'fwritable'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 }