1
/* Test of getdelim() function.
2
Copyright (C) 2007-2008 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>, 2007. */
26
#define ASSERT(expr) \
31
fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
39
main (int argc, char **argv)
46
/* Create test file. */
47
f = fopen ("test-getdelim.txt", "wb");
48
if (!f || fwrite ("anbcnd\0f", 1, 8, f) != 8 || fclose (f) != 0)
50
fputs ("Failed to create sample file.\n", stderr);
51
remove ("test-getdelim.txt");
54
f = fopen ("test-getdelim.txt", "rb");
57
fputs ("Failed to reopen sample file.\n", stderr);
58
remove ("test-getdelim.txt");
62
/* Test initial allocation, which must include trailing NUL. */
63
result = getdelim (&line, &len, 'n', f);
65
ASSERT (strcmp (line, "an") == 0);
68
/* Test growth of buffer. */
72
result = getdelim (&line, &len, 'n', f);
74
ASSERT (strcmp (line, "bcn") == 0);
77
/* Test embedded NULs and EOF behavior. */
78
result = getdelim (&line, &len, 'n', f);
80
ASSERT (memcmp (line, "d\0f", 4) == 0);
83
result = getdelim (&line, &len, 'n', f);
84
ASSERT (result == -1);
88
remove ("test-getdelim.txt");