1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
/*
* Copyright © 2009 Codethink Limited
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2 of the licence or (at
* your option) any later version.
*
* See the included COPYING file for more information.
*
* Author: Ryan Lortie <desrt@desrt.ca>
*/
#include <gio/gio.h>
#include <string.h>
#define MAX_PIECE_SIZE 100
#define MAX_PIECES 60
static gchar *
cook_piece (void)
{
char buffer[MAX_PIECE_SIZE * 2];
gint symbols, i = 0;
symbols = g_test_rand_int_range (1, MAX_PIECE_SIZE + 1);
while (symbols--)
{
gint c = g_test_rand_int_range (0, 30);
switch (c)
{
case 26:
buffer[i++] = '\n';
case 27:
buffer[i++] = '\r';
break;
case 28:
buffer[i++] = '\r';
case 29:
buffer[i++] = '\n';
break;
default:
buffer[i++] = c + 'a';
break;
}
g_assert_cmpint (i, <=, sizeof buffer);
}
return g_strndup (buffer, i);
}
static gchar **
cook_pieces (void)
{
gchar **array;
gint pieces;
pieces = g_test_rand_int_range (0, MAX_PIECES + 1);
array = g_new (char *, pieces + 1);
array[pieces] = NULL;
while (pieces--)
array[pieces] = cook_piece ();
return array;
}
typedef struct
{
GInputStream parent_instance;
gboolean built_to_fail;
gchar **pieces;
gint index;
const gchar *current;
} SleepyStream;
typedef GInputStreamClass SleepyStreamClass;
GType sleepy_stream_get_type (void);
G_DEFINE_TYPE (SleepyStream, sleepy_stream, G_TYPE_INPUT_STREAM)
static gssize
sleepy_stream_read (GInputStream *stream,
void *buffer,
gsize length,
GCancellable *cancellable,
GError **error)
{
SleepyStream *sleepy = (SleepyStream *) stream;
if (sleepy->pieces[sleepy->index] == NULL)
{
if (sleepy->built_to_fail)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "fail");
return -1;
}
else
return 0;
}
else
{
if (!sleepy->current)
sleepy->current = sleepy->pieces[sleepy->index++];
length = MIN (strlen (sleepy->current), length);
memcpy (buffer, sleepy->current, length);
sleepy->current += length;
if (*sleepy->current == '\0')
sleepy->current = NULL;
return length;
}
}
static void
sleepy_stream_init (SleepyStream *sleepy)
{
sleepy->pieces = cook_pieces ();
sleepy->built_to_fail = FALSE;
sleepy->index = 0;
}
static void
sleepy_stream_finalize (GObject *object)
{
SleepyStream *sleepy = (SleepyStream *) object;
g_strfreev (sleepy->pieces);
G_OBJECT_CLASS (sleepy_stream_parent_class)
->finalize (object);
}
static void
sleepy_stream_class_init (SleepyStreamClass *class)
{
G_OBJECT_CLASS (class)->finalize = sleepy_stream_finalize;
class->read_fn = sleepy_stream_read;
/* no read_async implementation.
* main thread will sleep while read runs in a worker.
*/
}
static SleepyStream *
sleepy_stream_new (void)
{
return g_object_new (sleepy_stream_get_type (), NULL);
}
static gboolean
read_line (GDataInputStream *stream,
GString *string,
const gchar *eol,
GError **error)
{
gsize length;
char *str;
str = g_data_input_stream_read_line (stream, &length, NULL, error);
if (str == NULL)
return FALSE;
g_assert (strstr (str, eol) == NULL);
g_assert (strlen (str) == length);
g_string_append (string, str);
g_string_append (string, eol);
g_free (str);
return TRUE;
}
static void
build_comparison (GString *str,
SleepyStream *stream)
{
/* build this for comparison */
gint i;
for (i = 0; stream->pieces[i]; i++)
g_string_append (str, stream->pieces[i]);
if (str->len && str->str[str->len - 1] != '\n')
g_string_append_c (str, '\n');
}
static void
test (void)
{
SleepyStream *stream = sleepy_stream_new ();
GDataInputStream *data;
GError *error = NULL;
GString *one;
GString *two;
one = g_string_new (NULL);
two = g_string_new (NULL);
data = g_data_input_stream_new (G_INPUT_STREAM (stream));
g_data_input_stream_set_newline_type (data, G_DATA_STREAM_NEWLINE_TYPE_LF);
build_comparison (one, stream);
while (read_line (data, two, "\n", &error));
g_assert_cmpstr (one->str, ==, two->str);
g_string_free (one, TRUE);
g_string_free (two, TRUE);
g_object_unref (stream);
g_object_unref (data);
}
static GDataInputStream *data;
static GString *one, *two;
static GMainLoop *loop;
static const gchar *eol;
static void
asynch_ready (GObject *object,
GAsyncResult *result,
gpointer user_data)
{
GError *error = NULL;
gsize length;
gchar *str;
g_assert (data == G_DATA_INPUT_STREAM (object));
str = g_data_input_stream_read_line_finish (data, result, &length, &error);
if (str == NULL)
{
g_main_loop_quit (loop);
if (error)
g_error_free (error);
}
else
{
g_assert (length == strlen (str));
g_string_append (two, str);
g_string_append (two, eol);
g_free (str);
/* MOAR!! */
g_data_input_stream_read_line_async (data, 0, NULL, asynch_ready, NULL);
}
}
static void
asynch (void)
{
SleepyStream *sleepy = sleepy_stream_new ();
data = g_data_input_stream_new (G_INPUT_STREAM (sleepy));
one = g_string_new (NULL);
two = g_string_new (NULL);
eol = "\n";
build_comparison (one, sleepy);
g_data_input_stream_read_line_async (data, 0, NULL, asynch_ready, NULL);
g_main_loop_run (loop = g_main_loop_new (NULL, FALSE));
g_assert_cmpstr (one->str, ==, two->str);
g_string_free (one, TRUE);
g_string_free (two, TRUE);
g_object_unref (sleepy);
g_object_unref (data);
}
int
main (int argc, char **argv)
{
g_test_init (&argc, &argv, NULL);
g_test_bug_base ("http://bugzilla.gnome.org/");
g_test_add_func ("/filter-stream/input", test);
g_test_add_func ("/filter-stream/async", asynch);
return g_test_run();
}
|