1 /* Test of getline() function.
2 Copyright (C) 2007-2020 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 <https://www.gnu.org/licenses/>. */
17 /* Written by Eric Blake <ebb9@byu.net>, 2007. */
23 #include "signature.h"
24 SIGNATURE_CHECK (getline
, ssize_t
, (char **, size_t *, FILE *));
39 /* Create test file. */
40 f
= fopen ("test-getline.txt", "wb");
41 if (!f
|| fwrite ("a\nA\nbc\nd\0f", 1, 10, f
) != 10 || fclose (f
) != 0)
43 fputs ("Failed to create sample file.\n", stderr
);
44 remove ("test-getline.txt");
47 f
= fopen ("test-getline.txt", "rb");
50 fputs ("Failed to reopen sample file.\n", stderr
);
51 remove ("test-getline.txt");
55 /* Test initial allocation, which must include trailing NUL. */
58 result
= getline (&line
, &len
, f
);
60 ASSERT (strcmp (line
, "a\n") == 0);
64 /* Test initial allocation again, with line = NULL and len != 0. */
66 len
= (size_t)(~0) / 4;
67 result
= getline (&line
, &len
, f
);
69 ASSERT (strcmp (line
, "A\n") == 0);
73 /* Test growth of buffer, must not leak. */
76 result
= getline (&line
, &len
, f
);
78 ASSERT (strcmp (line
, "bc\n") == 0);
81 /* Test embedded NULs and EOF behavior. */
82 result
= getline (&line
, &len
, f
);
84 ASSERT (memcmp (line
, "d\0f", 4) == 0);
87 result
= getline (&line
, &len
, f
);
88 ASSERT (result
== -1);
92 remove ("test-getline.txt");