Include header file being tested immediately after config.h.
[gnulib.git] / tests / test-getndelim2.c
1 /* Test of getndelim2() function.
2    Copyright (C) 2008, 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>, 2008.  */
19
20 #include <config.h>
21
22 #include "getndelim2.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #define ASSERT(expr) \
29   do                                                                         \
30     {                                                                        \
31       if (!(expr))                                                           \
32         {                                                                    \
33           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
34           fflush (stderr);                                                   \
35           abort ();                                                          \
36         }                                                                    \
37     }                                                                        \
38   while (0)
39
40 int
41 main (void)
42 {
43   FILE *f;
44   char *line = NULL;
45   size_t len = 0;
46   ssize_t result;
47
48   /* Create test file.  */
49   f = fopen ("test-getndelim2.txt", "wb+");
50   if (!f || fwrite ("a\nbc\nd\0f", 1, 8, f) != 8)
51     {
52       fputs ("Failed to create sample file.\n", stderr);
53       remove ("test-getndelim2.txt");
54       return 1;
55     }
56   rewind (f);
57
58   /* Unlimited lines.  */
59
60   /* Test initial allocation, which must include trailing NUL.  */
61   result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, '\n', '\n', f);
62   ASSERT (result == 2);
63   ASSERT (strcmp (line, "a\n") == 0);
64   ASSERT (2 < len);
65
66   /* Test growth of buffer, must not leak.  */
67   free (line);
68   line = malloc (1);
69   len = 0;
70   result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, EOF, '\n', f);
71   ASSERT (result == 3);
72   ASSERT (strcmp (line, "bc\n") == 0);
73   ASSERT (3 < len);
74
75   /* Test embedded NULs and EOF behavior.  */
76   result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, '\n', EOF, f);
77   ASSERT (result == 3);
78   ASSERT (memcmp (line, "d\0f", 4) == 0);
79   ASSERT (3 < len);
80
81   result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, '\n', EOF, f);
82   ASSERT (result == -1);
83
84   /* Using offset and nmax.  */
85
86   /* Limit growth to four bytes, including NUL, but parse to next 'd'.  */
87   free (line);
88   rewind (f);
89   line = malloc (8);
90   memset (line, 'e', 8);
91   len = 8;
92   result = getndelim2 (&line, &len, 6, 10, 'd', 'd', f);
93   ASSERT (result == 3);
94   ASSERT (10 == len);
95   ASSERT (strcmp (line, "eeeeeea\nb") == 0);
96
97   /* No change if offset larger than limit.  */
98   result = getndelim2 (&line, &len, len, 1, EOF, EOF, f);
99   ASSERT (result == -1);
100   ASSERT (10 == len);
101   ASSERT (strcmp (line, "eeeeeea\nb") == 0);
102
103   /* Consume to end of file, including embedded NUL.  */
104   result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, EOF, EOF, f);
105   ASSERT (result == 2);
106   ASSERT (10 == len);
107   ASSERT (memcmp (line, "\0f\0eeea\nb", 10) == 0);
108
109   result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, '\n', '\r', f);
110   ASSERT (result == -1);
111
112   /* Larger file size.  */
113   rewind (f);
114   {
115     int i;
116     for (i = 0; i < 16; i++)
117       fprintf (f, "%500x%c", i, i % 2 ? '\n' : '\r');
118   }
119   rewind (f);
120   {
121     char buffer[502];
122
123     result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, '\n', '\r', f);
124     ASSERT (result == 501);
125     ASSERT (501 < len);
126     memset (buffer, ' ', 499);
127     buffer[499] = '0';
128     buffer[500] = '\r';
129     buffer[501] = '\0';
130     ASSERT (strcmp (buffer, line) == 0);
131
132     result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, '\n', '\r', f);
133     ASSERT (result == 501);
134     ASSERT (501 < len);
135     buffer[499] = '1';
136     buffer[500] = '\n';
137     ASSERT (strcmp (buffer, line) == 0);
138
139     result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, 'g', 'f', f);
140     ASSERT (result == 501 * 14 - 1);
141     ASSERT (501 * 14 <= len);
142     buffer[499] = 'f';
143     buffer[500] = '\0';
144     ASSERT (strcmp (buffer, line + 501 * 13) == 0);
145
146     result = getndelim2 (&line, &len, 501 * 14 - 1, GETNLINE_NO_LIMIT,
147                          EOF, EOF, f);
148     ASSERT (result == 1);
149     buffer[500] = '\n';
150     ASSERT (strcmp (buffer, line + 501 * 13) == 0);
151
152     result = getndelim2 (&line, &len, 501 * 14 - 1, GETNLINE_NO_LIMIT,
153                          EOF, EOF, f);
154     buffer[500] = '\0';
155     ASSERT (strcmp (buffer, line + 501 * 13) == 0);
156     ASSERT (result == -1);
157   }
158
159   fclose (f);
160   remove ("test-getndelim2.txt");
161   return 0;
162 }