1 /* Test of getndelim2() function.
2 Copyright (C) 2008-2012 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, see <http://www.gnu.org/licenses/>. */
17 /* Written by Eric Blake <ebb9@byu.net>, 2008. */
21 #include "getndelim2.h"
37 /* Create test file. */
38 f = fopen ("test-getndelim2.txt", "wb+");
39 if (!f || fwrite ("a\nbc\nd\0f", 1, 8, f) != 8)
41 fputs ("Failed to create sample file.\n", stderr);
42 remove ("test-getndelim2.txt");
47 /* Unlimited lines. */
49 /* Test initial allocation, which must include trailing NUL. */
50 result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, '\n', '\n', f);
52 ASSERT (strcmp (line, "a\n") == 0);
55 /* Test growth of buffer, must not leak. */
59 result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, EOF, '\n', f);
61 ASSERT (strcmp (line, "bc\n") == 0);
64 /* Test embedded NULs and EOF behavior. */
65 result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, '\n', EOF, f);
67 ASSERT (memcmp (line, "d\0f", 4) == 0);
70 result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, '\n', EOF, f);
71 ASSERT (result == -1);
73 /* Using offset and nmax. */
75 /* Limit growth to four bytes, including NUL, but parse to next 'd'. */
79 memset (line, 'e', 8);
81 result = getndelim2 (&line, &len, 6, 10, 'd', 'd', f);
84 ASSERT (strcmp (line, "eeeeeea\nb") == 0);
86 /* No change if offset larger than limit. */
87 result = getndelim2 (&line, &len, len, 1, EOF, EOF, f);
88 ASSERT (result == -1);
90 ASSERT (strcmp (line, "eeeeeea\nb") == 0);
92 /* Consume to end of file, including embedded NUL. */
93 result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, EOF, EOF, f);
96 ASSERT (memcmp (line, "\0f\0eeea\nb", 10) == 0);
98 result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, '\n', '\r', f);
99 ASSERT (result == -1);
101 /* Larger file size. */
105 for (i = 0; i < 16; i++)
106 fprintf (f, "%500x%c", i, i % 2 ? '\n' : '\r');
112 result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, '\n', '\r', f);
113 ASSERT (result == 501);
115 memset (buffer, ' ', 499);
119 ASSERT (strcmp (buffer, line) == 0);
121 result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, '\n', '\r', f);
122 ASSERT (result == 501);
126 ASSERT (strcmp (buffer, line) == 0);
128 result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, 'g', 'f', f);
129 ASSERT (result == 501 * 14 - 1);
130 ASSERT (501 * 14 <= len);
133 ASSERT (strcmp (buffer, line + 501 * 13) == 0);
135 result = getndelim2 (&line, &len, 501 * 14 - 1, GETNLINE_NO_LIMIT,
137 ASSERT (result == 1);
139 ASSERT (strcmp (buffer, line + 501 * 13) == 0);
141 result = getndelim2 (&line, &len, 501 * 14 - 1, GETNLINE_NO_LIMIT,
144 ASSERT (strcmp (buffer, line + 501 * 13) == 0);
145 ASSERT (result == -1);
149 remove ("test-getndelim2.txt");