diff options
Diffstat (limited to 'tests/chargen.c')
-rw-r--r-- | tests/chargen.c | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/tests/chargen.c b/tests/chargen.c new file mode 100644 index 0000000..a3b3290 --- /dev/null +++ b/tests/chargen.c | |||
@@ -0,0 +1,109 @@ | |||
1 | /* This file is part of GNU Pies testsuite. | ||
2 | Copyright (C) 2019 Sergey Poznyakoff | ||
3 | |||
4 | GNU Pies 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) | ||
7 | any later version. | ||
8 | |||
9 | GNU Pies 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 GNU Pies. If not, see <http://www.gnu.org/licenses/>. */ | ||
16 | |||
17 | #include <config.h> | ||
18 | #include <stdio.h> | ||
19 | #include <stdlib.h> | ||
20 | #include <c-ctype.h> | ||
21 | #include <libpies.h> | ||
22 | |||
23 | enum { LINESIZ = 72 }; | ||
24 | |||
25 | static int | ||
26 | next_char (int stop) | ||
27 | { | ||
28 | static int ch = 0; | ||
29 | static int i = 0; | ||
30 | static int c; | ||
31 | int ret; | ||
32 | |||
33 | switch (i++) | ||
34 | { | ||
35 | case 0: | ||
36 | do | ||
37 | ch = (ch + 1) % 128; | ||
38 | while (!c_isprint (ch)); | ||
39 | if (ch == stop) | ||
40 | return 0; | ||
41 | c = ch; | ||
42 | break; | ||
43 | |||
44 | case LINESIZ: | ||
45 | return '\r'; | ||
46 | |||
47 | case LINESIZ+1: | ||
48 | i = 0; | ||
49 | return '\n'; | ||
50 | } | ||
51 | |||
52 | ret = c; | ||
53 | do | ||
54 | c = (c + 1) % 128; | ||
55 | while (!c_isprint (c)); | ||
56 | |||
57 | return ret; | ||
58 | } | ||
59 | |||
60 | int | ||
61 | main (int argc, char **argv) | ||
62 | { | ||
63 | struct pies_url *url; | ||
64 | int fd; | ||
65 | FILE *fp; | ||
66 | unsigned n; | ||
67 | int c, first; | ||
68 | char *progname = argv[0]; | ||
69 | |||
70 | if (argc != 2) | ||
71 | { | ||
72 | fprintf (stderr, "usage: %s URL\n", progname); | ||
73 | fprintf (stderr, "Tests the character generator protocol\n"); | ||
74 | return 64; | ||
75 | } | ||
76 | |||
77 | if (pies_url_create (&url, argv[1])) | ||
78 | { | ||
79 | perror (argv[0]); | ||
80 | return 64; | ||
81 | } | ||
82 | |||
83 | fd = url_connect (url, NULL); | ||
84 | fp = fdopen (fd, "r"); | ||
85 | |||
86 | first = next_char (0); | ||
87 | c = first; | ||
88 | do | ||
89 | { | ||
90 | int in = fgetc (fp); | ||
91 | if (in == EOF) | ||
92 | { | ||
93 | fprintf (stderr, "%s: unexpected EOF in %u\n", progname, n); | ||
94 | return 1; | ||
95 | } | ||
96 | if (in != c) | ||
97 | { | ||
98 | fprintf (stderr, "%s: got %d instead of %d in %u\n", | ||
99 | progname, in, c, n); | ||
100 | return 1; | ||
101 | } | ||
102 | n++; | ||
103 | } | ||
104 | while ((c = next_char (first)) != 0); | ||
105 | return 0; | ||
106 | } | ||
107 | |||
108 | |||
109 | |||