Unconditionally include <config.h> in unit tests.
[gnulib.git] / tests / test-binary-io.c
1 /* Test of binary mode I/O.
2    Copyright (C) 2005, 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>, 2005.  */
19
20 #include <config.h>
21
22 #include "binary-io.h"
23
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30
31 #define ASSERT(expr) \
32   do                                                                         \
33     {                                                                        \
34       if (!(expr))                                                           \
35         {                                                                    \
36           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
37           abort ();                                                          \
38         }                                                                    \
39     }                                                                        \
40   while (0)
41
42 int
43 main ()
44 {
45   /* Test the O_BINARY macro.  */
46   {
47     int fd = open ("t-bin-out2.tmp", O_CREAT | O_TRUNC | O_RDWR | O_BINARY);
48     if (write (fd, "Hello\n", 6) < 0)
49       exit (1);
50     close (fd);
51   }
52   {
53     struct stat statbuf;
54     if (stat ("t-bin-out2.tmp", &statbuf) < 0)
55       exit (1);
56     ASSERT (statbuf.st_size == 6);
57   }
58   unlink ("t-bin-out2.tmp");
59
60   /* Test the SET_BINARY macro.  */
61   SET_BINARY (1);
62   fputs ("Hello\n", stdout);
63   fclose (stdout);
64   fclose (stderr);
65   {
66     struct stat statbuf;
67     if (stat ("t-bin-out1.tmp", &statbuf) < 0)
68       exit (1);
69     ASSERT (statbuf.st_size == 6);
70   }
71
72   return 0;
73 }