1 /* Test of getndelim2() function.
2 Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
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)
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.
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. */
18 /* Written by Eric Blake <ebb9@byu.net>, 2008. */
22 #include "getndelim2.h"
38 /* Create test file. */
39 f = fopen ("test-getndelim2.txt", "wb+");
40 if (!f || fwrite ("a\nbc\nd\0f", 1, 8, f) != 8)
42 fputs ("Failed to create sample file.\n", stderr);
43 remove ("test-getndelim2.txt");
48 /* Unlimited lines. */
50 /* Test initial allocation, which must include trailing NUL. */
51 result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, '\n', '\n', f);
53 ASSERT (strcmp (line, "a\n") == 0);
56 /* Test growth of buffer, must not leak. */
60 result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, EOF, '\n', f);
62 ASSERT (strcmp (line, "bc\n") == 0);
65 /* Test embedded NULs and EOF behavior. */
66 result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, '\n', EOF, f);
68 ASSERT (memcmp (line, "d\0f", 4) == 0);
71 result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, '\n', EOF, f);
72 ASSERT (result == -1);
74 /* Using offset and nmax. */
76 /* Limit growth to four bytes, including NUL, but parse to next 'd'. */
80 memset (line, 'e', 8);
82 result = getndelim2 (&line, &len, 6, 10, 'd', 'd', f);
85 ASSERT (strcmp (line, "eeeeeea\nb") == 0);
87 /* No change if offset larger than limit. */
88 result = getndelim2 (&line, &len, len, 1, EOF, EOF, f);
89 ASSERT (result == -1);
91 ASSERT (strcmp (line, "eeeeeea\nb") == 0);
93 /* Consume to end of file, including embedded NUL. */
94 result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, EOF, EOF, f);
97 ASSERT (memcmp (line, "\0f\0eeea\nb", 10) == 0);
99 result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, '\n', '\r', f);
100 ASSERT (result == -1);
102 /* Larger file size. */
106 for (i = 0; i < 16; i++)
107 fprintf (f, "%500x%c", i, i % 2 ? '\n' : '\r');
113 result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, '\n', '\r', f);
114 ASSERT (result == 501);
116 memset (buffer, ' ', 499);
120 ASSERT (strcmp (buffer, line) == 0);
122 result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, '\n', '\r', f);
123 ASSERT (result == 501);
127 ASSERT (strcmp (buffer, line) == 0);
129 result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, 'g', 'f', f);
130 ASSERT (result == 501 * 14 - 1);
131 ASSERT (501 * 14 <= len);
134 ASSERT (strcmp (buffer, line + 501 * 13) == 0);
136 result = getndelim2 (&line, &len, 501 * 14 - 1, GETNLINE_NO_LIMIT,
138 ASSERT (result == 1);
140 ASSERT (strcmp (buffer, line + 501 * 13) == 0);
142 result = getndelim2 (&line, &len, 501 * 14 - 1, GETNLINE_NO_LIMIT,
145 ASSERT (strcmp (buffer, line + 501 * 13) == 0);
146 ASSERT (result == -1);
150 remove ("test-getndelim2.txt");