1 /* Test of getline() 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-getline.txt", "wb");
48 if (!f || fwrite ("a\nbc\nd\0f", 1, 8, f) != 8 || fclose (f) != 0)
50 fputs ("Failed to create sample file.\n", stderr);
51 remove ("test-getline.txt");
54 f = fopen ("test-getline.txt", "rb");
57 fputs ("Failed to reopen sample file.\n", stderr);
58 remove ("test-getline.txt");
62 /* Test initial allocation, which must include trailing NUL. */
63 result = getline (&line, &len, f);
65 ASSERT (strcmp (line, "a\n") == 0);
68 /* Test growth of buffer, must not leak. */
72 result = getline (&line, &len, f);
74 ASSERT (strcmp (line, "bc\n") == 0);
77 /* Test embedded NULs and EOF behavior. */
78 result = getline (&line, &len, f);
80 ASSERT (memcmp (line, "d\0f", 4) == 0);
83 result = getline (&line, &len, f);
84 ASSERT (result == -1);
88 remove ("test-getline.txt");