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