annotate tests/test-getdelim.c @ 40186:8964917f9574

autoupdate
author Karl Berry <karl@freefriends.org>
date Mon, 18 Feb 2019 08:02:49 -0800
parents b06060465f09
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9161
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
1 /* Test of getdelim() function.
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 19484
diff changeset
2 Copyright (C) 2007-2019 Free Software Foundation, Inc.
9161
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
3
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
4 This program is free software; you can redistribute it and/or modify
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
6 the Free Software Foundation; either version 3, or (at your option)
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
7 any later version.
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
8
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
12 GNU General Public License for more details.
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
13
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
19190
9759915b2aca all: prefer https: URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 18626
diff changeset
15 along with this program; if not, see <https://www.gnu.org/licenses/>. */
9161
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
16
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
17 /* Written by Eric Blake <ebb9@byu.net>, 2007. */
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
18
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
19 #include <config.h>
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
20
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
21 #include <stdio.h>
12489
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
22
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
23 #include "signature.h"
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
24 SIGNATURE_CHECK (getdelim, ssize_t, (char **, size_t *, int, FILE *));
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
25
9161
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
26 #include <stdlib.h>
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
27 #include <string.h>
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
28
12496
a48d3d749ca5 Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents: 12489
diff changeset
29 #include "macros.h"
9161
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
30
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
31 int
12210
441d8820b5a5 tests: avoid some compiler warnings
Eric Blake <ebb9@byu.net>
parents: 9889
diff changeset
32 main (void)
9161
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
33 {
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
34 FILE *f;
12839
c6d23570567d Work around getdelim() bug on FreeBSD 8.0.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
35 char *line;
c6d23570567d Work around getdelim() bug on FreeBSD 8.0.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
36 size_t len;
9161
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
37 ssize_t result;
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
38
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
39 /* Create test file. */
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
40 f = fopen ("test-getdelim.txt", "wb");
12839
c6d23570567d Work around getdelim() bug on FreeBSD 8.0.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
41 if (!f || fwrite ("anAnbcnd\0f", 1, 10, f) != 10 || fclose (f) != 0)
9161
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
42 {
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
43 fputs ("Failed to create sample file.\n", stderr);
9162
60a8f78ca96f * tests/test-getdelim.c (main): Use remove, not unlink.
Eric Blake <ebb9@byu.net>
parents: 9161
diff changeset
44 remove ("test-getdelim.txt");
9161
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
45 return 1;
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
46 }
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
47 f = fopen ("test-getdelim.txt", "rb");
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
48 if (!f)
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
49 {
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
50 fputs ("Failed to reopen sample file.\n", stderr);
9162
60a8f78ca96f * tests/test-getdelim.c (main): Use remove, not unlink.
Eric Blake <ebb9@byu.net>
parents: 9161
diff changeset
51 remove ("test-getdelim.txt");
9161
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
52 return 1;
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
53 }
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
54
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
55 /* Test initial allocation, which must include trailing NUL. */
12839
c6d23570567d Work around getdelim() bug on FreeBSD 8.0.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
56 line = NULL;
c6d23570567d Work around getdelim() bug on FreeBSD 8.0.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
57 len = 0;
9161
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
58 result = getdelim (&line, &len, 'n', f);
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
59 ASSERT (result == 2);
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
60 ASSERT (strcmp (line, "an") == 0);
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
61 ASSERT (2 < len);
12839
c6d23570567d Work around getdelim() bug on FreeBSD 8.0.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
62 free (line);
c6d23570567d Work around getdelim() bug on FreeBSD 8.0.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
63
c6d23570567d Work around getdelim() bug on FreeBSD 8.0.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
64 /* Test initial allocation again, with line = NULL and len != 0. */
c6d23570567d Work around getdelim() bug on FreeBSD 8.0.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
65 line = NULL;
c6d23570567d Work around getdelim() bug on FreeBSD 8.0.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
66 len = (size_t)(~0) / 4;
c6d23570567d Work around getdelim() bug on FreeBSD 8.0.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
67 result = getdelim (&line, &len, 'n', f);
c6d23570567d Work around getdelim() bug on FreeBSD 8.0.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
68 ASSERT (result == 2);
c6d23570567d Work around getdelim() bug on FreeBSD 8.0.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
69 ASSERT (strcmp (line, "An") == 0);
c6d23570567d Work around getdelim() bug on FreeBSD 8.0.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
70 ASSERT (2 < len);
c6d23570567d Work around getdelim() bug on FreeBSD 8.0.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
71 free (line);
9161
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
72
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
73 /* Test growth of buffer. */
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
74 line = malloc (1);
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
75 len = 1;
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
76 result = getdelim (&line, &len, 'n', f);
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
77 ASSERT (result == 3);
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
78 ASSERT (strcmp (line, "bcn") == 0);
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
79 ASSERT (3 < len);
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
80
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
81 /* Test embedded NULs and EOF behavior. */
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
82 result = getdelim (&line, &len, 'n', f);
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
83 ASSERT (result == 3);
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
84 ASSERT (memcmp (line, "d\0f", 4) == 0);
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
85 ASSERT (3 < len);
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
86
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
87 result = getdelim (&line, &len, 'n', f);
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
88 ASSERT (result == -1);
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
89
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
90 free (line);
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
91 fclose (f);
9162
60a8f78ca96f * tests/test-getdelim.c (main): Use remove, not unlink.
Eric Blake <ebb9@byu.net>
parents: 9161
diff changeset
92 remove ("test-getdelim.txt");
9161
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
93 return 0;
c5cedfcd0db0 Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
94 }