1 /* Test of getndelim2() function.
2 Copyright (C) 2008, 2009 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. */
26 #include "getndelim2.h"
28 #define ASSERT(expr) \
33 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
48 /* Create test file. */
49 f = fopen ("test-getndelim2.txt", "wb+");
50 if (!f || fwrite ("a\nbc\nd\0f", 1, 8, f) != 8)
52 fputs ("Failed to create sample file.\n", stderr);
53 remove ("test-getndelim2.txt");
58 /* Unlimited lines. */
60 /* Test initial allocation, which must include trailing NUL. */
61 result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, '\n', '\n', f);
63 ASSERT (strcmp (line, "a\n") == 0);
66 /* Test growth of buffer, must not leak. */
70 result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, EOF, '\n', f);
72 ASSERT (strcmp (line, "bc\n") == 0);
75 /* Test embedded NULs and EOF behavior. */
76 result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, '\n', EOF, f);
78 ASSERT (memcmp (line, "d\0f", 4) == 0);
81 result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, '\n', EOF, f);
82 ASSERT (result == -1);
84 /* Using offset and nmax. */
86 /* Limit growth to four bytes, including NUL, but parse to next 'd'. */
90 memset (line, 'e', 8);
92 result = getndelim2 (&line, &len, 6, 10, 'd', 'd', f);
95 ASSERT (strcmp (line, "eeeeeea\nb") == 0);
97 /* No change if offset larger than limit. */
98 result = getndelim2 (&line, &len, len, 1, EOF, EOF, f);
99 ASSERT (result == -1);
101 ASSERT (strcmp (line, "eeeeeea\nb") == 0);
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);
107 ASSERT (memcmp (line, "\0f\0eeea\nb", 10) == 0);
109 result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, '\n', '\r', f);
110 ASSERT (result == -1);
112 /* Larger file size. */
116 for (i = 0; i < 16; i++)
117 fprintf (f, "%500x%c", i, i % 2 ? '\n' : '\r');
123 result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, '\n', '\r', f);
124 ASSERT (result == 501);
126 memset (buffer, ' ', 499);
130 ASSERT (strcmp (buffer, line) == 0);
132 result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, '\n', '\r', f);
133 ASSERT (result == 501);
137 ASSERT (strcmp (buffer, line) == 0);
139 result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, 'g', 'f', f);
140 ASSERT (result == 501 * 14 - 1);
141 ASSERT (501 * 14 <= len);
144 ASSERT (strcmp (buffer, line + 501 * 13) == 0);
146 result = getndelim2 (&line, &len, 501 * 14 - 1, GETNLINE_NO_LIMIT,
148 ASSERT (result == 1);
150 ASSERT (strcmp (buffer, line + 501 * 13) == 0);
152 result = getndelim2 (&line, &len, 501 * 14 - 1, GETNLINE_NO_LIMIT,
155 ASSERT (strcmp (buffer, line + 501 * 13) == 0);
156 ASSERT (result == -1);
160 remove ("test-getndelim2.txt");