02414e5d03591c0fadc0a1a28cecd3b890914b3d
[gnulib.git] / tests / test-freading.c
1 /* Test of freading() 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 "freading.h"
23
24 #include <stdlib.h>
25
26 #define ASSERT(expr) if (!(expr)) abort ();
27
28 #define TESTFILE "t-freading.tmp"
29
30 int
31 main ()
32 {
33   FILE *fp;
34
35   /* Create a file with some contents.  Write-only file is never reading.  */
36   fp = fopen (TESTFILE, "w");
37   if (fp == NULL)
38     goto skip;
39   ASSERT (!freading (fp));
40   if (fwrite ("foobarsh", 1, 8, fp) < 8)
41     goto skip;
42   ASSERT (!freading (fp));
43   if (fclose (fp))
44     goto skip;
45
46   /* Open it in read-only mode.  Read-only file is always reading.  */
47   fp = fopen (TESTFILE, "r");
48   if (fp == NULL)
49     goto skip;
50   ASSERT (freading (fp));
51   if (fgetc (fp) != 'f')
52     goto skip;
53   ASSERT (freading (fp));
54   if (fseek (fp, 2, SEEK_CUR))
55     goto skip;
56   ASSERT (freading (fp));
57   if (fgetc (fp) != 'b')
58     goto skip;
59   ASSERT (freading (fp));
60   if (fseek (fp, 0, SEEK_END))
61     goto skip;
62   ASSERT (freading (fp));
63   if (fclose (fp))
64     goto skip;
65
66   /* Open it in read-write mode.  POSIX requires a reposition (fseek,
67      fsetpos, rewind) or EOF when transitioning from read to write;
68      freading is only deterministic after input or output, but this
69      test case should be portable even on open, after reposition, and
70      at EOF.  */
71   fp = fopen (TESTFILE, "r+");
72   if (fp == NULL)
73     goto skip;
74   ASSERT (!freading (fp));
75   if (fgetc (fp) != 'f')
76     goto skip;
77   ASSERT (freading (fp));
78   if (fseek (fp, 2, SEEK_CUR))
79     goto skip;
80   /* freading (fp)) is undefined here, but fwriting is false.  */
81   if (fgetc (fp) != 'b')
82     goto skip;
83   ASSERT (freading (fp));
84   if (fseek (fp, 0, SEEK_CUR) != 0)
85     goto skip;
86   if (fputc ('z', fp) != 'z')
87     goto skip;
88   ASSERT (!freading (fp));
89   if (fseek (fp, 0, SEEK_END))
90     goto skip;
91   ASSERT (!freading (fp));
92   if (fclose (fp))
93     goto skip;
94
95   /* Open it in append mode.  Write-only file is never reading.  */
96   fp = fopen (TESTFILE, "a");
97   if (fp == NULL)
98     goto skip;
99   ASSERT (!freading (fp));
100   if (fwrite ("bla", 1, 3, fp) < 3)
101     goto skip;
102   ASSERT (!freading (fp));
103   if (fclose (fp))
104     goto skip;
105
106   return 0;
107
108  skip:
109   fprintf (stderr, "Skipping test: file operations failed.\n");
110   return 77;
111 }