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