Mercurial > hg > octave-kai > gnulib-hg
annotate tests/test-getdelim.c @ 16201:8250f2777afc
maint: update all copyright year number ranges
Run "make update-copyright".
author | Jim Meyering <meyering@redhat.com> |
---|---|
date | Sun, 01 Jan 2012 10:04:58 +0100 |
parents | 97fc9a21a8fb |
children | bb182ee4a09d |
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. |
16201
8250f2777afc
maint: update all copyright year number ranges
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
2 Copyright (C) 2007-2012 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 |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
15 along with this program; if not, write to the Free Software Foundation, |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
17 |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
18 /* 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
|
19 |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
20 #include <config.h> |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
21 |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
22 #include <stdio.h> |
12489 | 23 |
24 #include "signature.h" | |
25 SIGNATURE_CHECK (getdelim, ssize_t, (char **, size_t *, int, FILE *)); | |
26 | |
9161
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
27 #include <stdlib.h> |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
28 #include <string.h> |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
29 |
12496
a48d3d749ca5
Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents:
12489
diff
changeset
|
30 #include "macros.h" |
9161
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
31 |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
32 int |
12210
441d8820b5a5
tests: avoid some compiler warnings
Eric Blake <ebb9@byu.net>
parents:
9889
diff
changeset
|
33 main (void) |
9161
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
34 { |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
35 FILE *f; |
12839
c6d23570567d
Work around getdelim() bug on FreeBSD 8.0.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
36 char *line; |
c6d23570567d
Work around getdelim() bug on FreeBSD 8.0.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
37 size_t len; |
9161
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
38 ssize_t result; |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
39 |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
40 /* Create test file. */ |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
41 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
|
42 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
|
43 { |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
44 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
|
45 remove ("test-getdelim.txt"); |
9161
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
46 return 1; |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
47 } |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
48 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
|
49 if (!f) |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
50 { |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
51 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
|
52 remove ("test-getdelim.txt"); |
9161
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
53 return 1; |
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 |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
56 /* 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
|
57 line = NULL; |
c6d23570567d
Work around getdelim() bug on FreeBSD 8.0.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
58 len = 0; |
9161
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
59 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
|
60 ASSERT (result == 2); |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
61 ASSERT (strcmp (line, "an") == 0); |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
62 ASSERT (2 < len); |
12839
c6d23570567d
Work around getdelim() bug on FreeBSD 8.0.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
63 free (line); |
c6d23570567d
Work around getdelim() bug on FreeBSD 8.0.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
64 |
c6d23570567d
Work around getdelim() bug on FreeBSD 8.0.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
65 /* 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
|
66 line = NULL; |
c6d23570567d
Work around getdelim() bug on FreeBSD 8.0.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
67 len = (size_t)(~0) / 4; |
c6d23570567d
Work around getdelim() bug on FreeBSD 8.0.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
68 result = getdelim (&line, &len, 'n', f); |
c6d23570567d
Work around getdelim() bug on FreeBSD 8.0.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
69 ASSERT (result == 2); |
c6d23570567d
Work around getdelim() bug on FreeBSD 8.0.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
70 ASSERT (strcmp (line, "An") == 0); |
c6d23570567d
Work around getdelim() bug on FreeBSD 8.0.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
71 ASSERT (2 < len); |
c6d23570567d
Work around getdelim() bug on FreeBSD 8.0.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
72 free (line); |
9161
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
73 |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
74 /* Test growth of buffer. */ |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
75 line = malloc (1); |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
76 len = 1; |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
77 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
|
78 ASSERT (result == 3); |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
79 ASSERT (strcmp (line, "bcn") == 0); |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
80 ASSERT (3 < len); |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
81 |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
82 /* 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
|
83 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
|
84 ASSERT (result == 3); |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
85 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
|
86 ASSERT (3 < len); |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
87 |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
88 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
|
89 ASSERT (result == -1); |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
90 |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
91 free (line); |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
92 fclose (f); |
9162
60a8f78ca96f
* tests/test-getdelim.c (main): Use remove, not unlink.
Eric Blake <ebb9@byu.net>
parents:
9161
diff
changeset
|
93 remove ("test-getdelim.txt"); |
9161
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
94 return 0; |
c5cedfcd0db0
Move getline and getdelim into stdio.h, per POSIX 200x.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
95 } |