* tests/test-getdelim.c (main): Use remove, not unlink.
[gnulib.git] / tests / test-getline.c
1 /* Test of getline() 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 3, 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 Eric Blake <ebb9@byu.net>, 2007.  */
19
20 #include <config.h>
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #define ASSERT(expr) \
27   do                                                                         \
28     {                                                                        \
29       if (!(expr))                                                           \
30         {                                                                    \
31           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
32           abort ();                                                          \
33         }                                                                    \
34     }                                                                        \
35   while (0)
36
37 int
38 main (int argc, char **argv)
39 {
40   FILE *f;
41   char *line = NULL;
42   size_t len = 0;
43   ssize_t result;
44
45   /* Create test file.  */
46   f = fopen ("test-getline.txt", "wb");
47   if (!f || fwrite ("a\nbc\nd\0f", 1, 8, f) != 8 || fclose (f) != 0)
48     {
49       fputs ("Failed to create sample file.\n", stderr);
50       remove ("test-getline.txt");
51       return 1;
52     }
53   f = fopen ("test-getline.txt", "rb");
54   if (!f)
55     {
56       fputs ("Failed to reopen sample file.\n", stderr);
57       remove ("test-getline.txt");
58       return 1;
59     }
60
61   /* Test initial allocation, which must include trailing NUL.  */
62   result = getline (&line, &len, f);
63   ASSERT (result == 2);
64   ASSERT (strcmp (line, "a\n") == 0);
65   ASSERT (2 < len);
66
67   /* Test growth of buffer, must not leak.  */
68   free (line);
69   line = malloc (1);
70   len = 0;
71   result = getline (&line, &len, f);
72   ASSERT (result == 3);
73   ASSERT (strcmp (line, "bc\n") == 0);
74   ASSERT (3 < len);
75
76   /* Test embedded NULs and EOF behavior.  */
77   result = getline (&line, &len, f);
78   ASSERT (result == 3);
79   ASSERT (memcmp (line, "d\0f", 4) == 0);
80   ASSERT (3 < len);
81
82   result = getline (&line, &len, f);
83   ASSERT (result == -1);
84
85   free (line);
86   fclose (f);
87   remove ("test-getline.txt");
88   return 0;
89 }