1 /* Test of getdelim() function.
2 Copyright (C) 2007-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>, 2007. */
24 #include "signature.h"
25 SIGNATURE_CHECK (getdelim, ssize_t, (char **, size_t *, int, FILE *));
40 /* Create test file. */
41 f = fopen ("test-getdelim.txt", "wb");
42 if (!f || fwrite ("anAnbcnd\0f", 1, 10, f) != 10 || fclose (f) != 0)
44 fputs ("Failed to create sample file.\n", stderr);
45 remove ("test-getdelim.txt");
48 f = fopen ("test-getdelim.txt", "rb");
51 fputs ("Failed to reopen sample file.\n", stderr);
52 remove ("test-getdelim.txt");
56 /* Test initial allocation, which must include trailing NUL. */
59 result = getdelim (&line, &len, 'n', f);
61 ASSERT (strcmp (line, "an") == 0);
65 /* Test initial allocation again, with line = NULL and len != 0. */
67 len = (size_t)(~0) / 4;
68 result = getdelim (&line, &len, 'n', f);
70 ASSERT (strcmp (line, "An") == 0);
74 /* Test growth of buffer. */
77 result = getdelim (&line, &len, 'n', f);
79 ASSERT (strcmp (line, "bcn") == 0);
82 /* Test embedded NULs and EOF behavior. */
83 result = getdelim (&line, &len, 'n', f);
85 ASSERT (memcmp (line, "d\0f", 4) == 0);
88 result = getdelim (&line, &len, 'n', f);
89 ASSERT (result == -1);
93 remove ("test-getdelim.txt");