Tests for module 'fpurge'.
[gnulib.git] / tests / test-fpurge.c
1 /* Test of fpurge() 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 "fpurge.h"
23
24 #include <stdlib.h>
25 #include <string.h>
26
27 #define ASSERT(expr) if (!(expr)) abort ();
28
29 #define TESTFILE "t-fpurge.tmp"
30
31 int
32 main ()
33 {
34   FILE *fp;
35
36   /* Create a file with some contents.  */
37   fp = fopen (TESTFILE, "w");
38   if (fp == NULL)
39     goto skip;
40   if (fwrite ("foobarsh", 1, 8, fp) < 8)
41     goto skip;
42   if (fclose (fp))
43     goto skip;
44
45   /* Open it in read-write mode.  */
46   fp = fopen (TESTFILE, "r+");
47   if (fp == NULL)
48     goto skip;
49   if (fseek (fp, 3, SEEK_CUR))
50     goto skip;
51   if (fwrite ("g", 1, 1, fp) < 1)
52     goto skip;
53   if (fflush (fp))
54     goto skip;
55   if (fwrite ("az", 1, 2, fp) < 2)
56     goto skip;
57   ASSERT (fpurge (fp) == 0);
58   ASSERT (fclose (fp) == 0);
59
60   /* Open it in read-only mode.  */
61   fp = fopen (TESTFILE, "r");
62   if (fp == NULL)
63     goto skip;
64   {
65     char buf[8];
66     if (fread (buf, 1, 8, fp) < 8)
67       goto skip;
68     ASSERT (memcmp (buf, "foogarsh", 8) == 0);
69   }
70   ASSERT (fpurge (fp) == 0);
71   ASSERT (fclose (fp) == 0);
72
73   return 0;
74
75  skip:
76   fprintf (stderr, "Skipping test: file operations failed.\n");
77   return 77;
78 }