Commit | Line | Data |
---|---|---|
0c1f2b0e | 1 | /* Copyright (C) 2008, 2014 Free Software Foundation, Inc. |
8e7ff773 LC |
2 | * |
3 | * This library is free software; you can redistribute it and/or | |
53befeb7 NJ |
4 | * modify it under the terms of the GNU Lesser General Public License |
5 | * as published by the Free Software Foundation; either version 3 of | |
6 | * the License, or (at your option) any later version. | |
8e7ff773 | 7 | * |
53befeb7 NJ |
8 | * This library is distributed in the hope that it will be useful, but |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
8e7ff773 LC |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 | * Lesser General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU Lesser General Public | |
14 | * License along with this library; if not, write to the Free Software | |
53befeb7 NJ |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
16 | * 02110-1301 USA | |
8e7ff773 LC |
17 | */ |
18 | ||
19 | /* Exercise `scm_c_read ()' and the port type API. Verify assumptions that | |
20 | can be made by port type implementations. */ | |
21 | ||
22 | #ifdef HAVE_CONFIG_H | |
23 | # include <config.h> | |
24 | #endif | |
25 | ||
0c1f2b0e LC |
26 | #undef NDEBUG |
27 | ||
8e7ff773 LC |
28 | #include <libguile.h> |
29 | #include <assert.h> | |
30 | ||
31 | ||
32 | \f | |
33 | /* Size of our port's internal buffer. */ | |
34 | #define PORT_BUFFER_SIZE 1024 | |
35 | ||
36 | /* Return a new port of type PORT_TYPE. */ | |
37 | static inline SCM | |
38 | make_port (scm_t_bits port_type) | |
39 | { | |
40 | SCM port; | |
41 | char *c_buffer; | |
42 | scm_t_port *c_port; | |
43 | ||
44 | c_buffer = scm_gc_calloc (PORT_BUFFER_SIZE, "custom-port-buffer"); | |
45 | ||
46 | port = scm_new_port_table_entry (port_type); | |
47 | ||
92834759 | 48 | /* Associate C_BUFFER with PORT, for test purposes. */ |
8e7ff773 LC |
49 | SCM_SETSTREAM (port, (scm_t_bits) c_buffer); |
50 | ||
92834759 | 51 | /* Use C_BUFFER as PORT's internal buffer. */ |
8e7ff773 LC |
52 | c_port = SCM_PTAB_ENTRY (port); |
53 | c_port->read_pos = c_port->read_buf = (unsigned char *) c_buffer; | |
54 | c_port->read_end = (unsigned char *) c_buffer + PORT_BUFFER_SIZE; | |
55 | c_port->read_buf_size = PORT_BUFFER_SIZE; | |
56 | ||
57 | /* Mark PORT as open and readable. */ | |
58 | SCM_SET_CELL_TYPE (port, port_type | SCM_OPN | SCM_RDNG); | |
59 | ||
60 | return port; | |
61 | } | |
62 | ||
63 | /* Read one byte from PORT. */ | |
64 | static int | |
65 | fill_input (SCM port) | |
66 | { | |
67 | int result; | |
68 | scm_t_port *c_port = SCM_PTAB_ENTRY (port); | |
69 | ||
70 | /* Make sure that C_PORT's internal buffer wasn't changed behind our back. | |
71 | See http://lists.gnu.org/archive/html/guile-devel/2008-11/msg00042.html | |
72 | for an example where this assumption matters. */ | |
73 | assert (c_port->read_buf == (unsigned char *) SCM_STREAM (port)); | |
74 | assert (c_port->read_buf_size == PORT_BUFFER_SIZE); | |
75 | ||
76 | if (c_port->read_pos >= c_port->read_end) | |
77 | result = EOF; | |
78 | else | |
79 | result = (int) *c_port->read_pos++; | |
80 | ||
81 | return result; | |
82 | } | |
83 | ||
84 | /* Return true (non-zero) if BUF contains only zeros. */ | |
85 | static inline int | |
86 | zeroed_buffer_p (const char *buf, size_t len) | |
87 | { | |
88 | size_t i; | |
89 | ||
90 | for (i = 0; i < len; i++) | |
91 | if (buf[i] != 0) | |
92 | return 0; | |
93 | ||
94 | return 1; | |
95 | } | |
96 | ||
97 | /* Run the test. */ | |
98 | static void * | |
99 | do_start (void *arg) | |
100 | { | |
101 | SCM port; | |
102 | scm_t_bits port_type; | |
103 | char buffer[PORT_BUFFER_SIZE + (PORT_BUFFER_SIZE / 2)]; | |
104 | size_t read, last_read; | |
105 | ||
106 | port_type = scm_make_port_type ("custom-input-port", fill_input, NULL); | |
107 | port = make_port (port_type); | |
108 | ||
109 | read = 0; | |
110 | do | |
111 | { | |
112 | last_read = scm_c_read (port, &buffer[read], 123); | |
113 | assert (last_read <= 123); | |
114 | assert (zeroed_buffer_p (&buffer[read], last_read)); | |
115 | ||
116 | read += last_read; | |
117 | } | |
118 | while (last_read > 0 && read < sizeof (buffer)); | |
119 | ||
120 | /* We shouldn't be able to read more than what's in PORT's buffer. */ | |
121 | assert (read == PORT_BUFFER_SIZE); | |
122 | ||
123 | return NULL; | |
124 | } | |
125 | ||
126 | \f | |
127 | int | |
128 | main (int argc, char *argv[]) | |
129 | { | |
130 | scm_with_guile (do_start, NULL); | |
131 | ||
132 | return 0; | |
133 | } |