Mercurial > hg > octave-kai > gnulib-hg
annotate tests/test-readtokens.c @ 16614:1e2b27c67c69
test-readtokens.c: use const; remove unwarranted cast
* tests/test-readtokens.c: Declare delim to be const, to avoid a cast.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Thu, 08 Mar 2012 12:04:16 +0100 |
parents | 1e35ae24b8f2 |
children | e542fd46ad6f |
rev | line source |
---|---|
16598 | 1 /* Test the readtokens module. |
2 Copyright (C) 2012 Free Software Foundation, Inc. | |
3 | |
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 of the License, or | |
7 (at your option) any later version. | |
8 | |
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. | |
13 | |
14 You should have received a copy of the GNU General Public License | |
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
16 | |
17 #include <config.h> | |
18 #include <stdbool.h> | |
19 #include <sys/types.h> | |
20 #include <sys/stat.h> | |
21 #include <fcntl.h> | |
22 #include <string.h> | |
23 #include <stdio.h> | |
24 #include <unistd.h> | |
25 | |
26 #include "readtokens.h" | |
27 #include "closeout.h" | |
28 #include "macros.h" | |
29 | |
30 static void | |
31 basic (void) | |
32 { | |
16609
1e35ae24b8f2
test-readtokens.c: avoid const-related compilation warnings
Jim Meyering <meyering@redhat.com>
parents:
16598
diff
changeset
|
33 char const *filename = "in.827"; |
16598 | 34 int fd = open (filename, O_CREAT | O_WRONLY, 0600); |
35 ASSERT (fd >= 0); | |
36 ASSERT (write (fd, "a|b;c+d", 7) == 7); | |
37 ASSERT (close (fd) == 0); | |
38 | |
39 { | |
40 token_buffer tb; | |
41 FILE *fp = fopen (filename, "r"); | |
42 ASSERT (fp); | |
43 | |
44 init_tokenbuffer (&tb); | |
45 ASSERT (readtoken (fp, "|;", 2, &tb) == 1 && tb.buffer[0] == 'a'); | |
46 ASSERT (readtoken (fp, "|;", 2, &tb) == 1 && tb.buffer[0] == 'b'); | |
47 ASSERT (readtoken (fp, "+", 1, &tb) == 1 && tb.buffer[0] == 'c'); | |
48 ASSERT (readtoken (fp, "-", 1, &tb) == 1 && tb.buffer[0] == 'd'); | |
49 ASSERT (readtoken (fp, "%", 0, &tb) == (size_t) -1); | |
50 ASSERT ( ! ferror (fp)); | |
51 ASSERT (fclose (fp) == 0); | |
52 } | |
53 } | |
54 | |
55 int | |
56 main (int argc, char **argv) | |
57 { | |
58 token_buffer tb; | |
16614
1e2b27c67c69
test-readtokens.c: use const; remove unwarranted cast
Bruno Haible <bruno@clisp.org>
parents:
16609
diff
changeset
|
59 char const *delim; |
16598 | 60 size_t delim_len; |
61 | |
62 atexit (close_stdout); | |
63 | |
64 if (argc == 1) | |
65 { | |
66 basic (); | |
67 return 0; | |
68 } | |
69 | |
70 init_tokenbuffer (&tb); | |
71 | |
72 if (argc != 2) | |
73 return 99; | |
74 | |
75 delim = argv[1]; | |
76 delim_len = strlen (delim); | |
77 | |
78 if (STREQ (delim, "\\0")) | |
79 { | |
16614
1e2b27c67c69
test-readtokens.c: use const; remove unwarranted cast
Bruno Haible <bruno@clisp.org>
parents:
16609
diff
changeset
|
80 delim = ""; |
16598 | 81 delim_len = 1; |
82 } | |
83 | |
84 while (1) | |
85 { | |
86 size_t token_length = readtoken (stdin, delim, delim_len, &tb); | |
87 if (token_length == (size_t) -1) | |
88 break; | |
89 fwrite (tb.buffer, 1, token_length, stdout); | |
90 putchar (':'); | |
91 } | |
92 putchar ('\n'); | |
93 free (tb.buffer); | |
94 | |
95 ASSERT ( ! ferror (stdin)); | |
96 | |
97 return 0; | |
98 } |