2 * Copyright © 2009 Codethink Limited
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * See the included COPYING file for more information.
13 * Author: Ryan Lortie <desrt@desrt.ca>
19 #define MAX_PIECE_SIZE 100
25 char buffer[MAX_PIECE_SIZE * 2];
28 symbols = g_test_rand_int_range (1, MAX_PIECE_SIZE + 1);
32 gint c = g_test_rand_int_range (0, 30);
51 buffer[i++] = c + 'a';
55 g_assert_cmpint (i, <=, sizeof buffer);
58 return g_strndup (buffer, i);
67 pieces = g_test_rand_int_range (0, MAX_PIECES + 1);
68 array = g_new (char *, pieces + 1);
72 array[pieces] = cook_piece ();
79 GInputStream parent_instance;
81 gboolean built_to_fail;
88 typedef GInputStreamClass SleepyStreamClass;
90 GType sleepy_stream_get_type (void);
92 G_DEFINE_TYPE (SleepyStream, sleepy_stream, G_TYPE_INPUT_STREAM)
95 sleepy_stream_read (GInputStream *stream,
98 GCancellable *cancellable,
101 SleepyStream *sleepy = (SleepyStream *) stream;
103 if (sleepy->pieces[sleepy->index] == NULL)
105 if (sleepy->built_to_fail)
107 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "fail");
115 if (!sleepy->current)
116 sleepy->current = sleepy->pieces[sleepy->index++];
118 length = MIN (strlen (sleepy->current), length);
119 memcpy (buffer, sleepy->current, length);
121 sleepy->current += length;
122 if (*sleepy->current == '\0')
123 sleepy->current = NULL;
130 sleepy_stream_init (SleepyStream *sleepy)
132 sleepy->pieces = cook_pieces ();
133 sleepy->built_to_fail = FALSE;
138 sleepy_stream_finalize (GObject *object)
140 SleepyStream *sleepy = (SleepyStream *) object;
142 g_strfreev (sleepy->pieces);
143 G_OBJECT_CLASS (sleepy_stream_parent_class)
148 sleepy_stream_class_init (SleepyStreamClass *class)
150 G_OBJECT_CLASS (class)->finalize = sleepy_stream_finalize;
151 class->read_fn = sleepy_stream_read;
153 /* no read_async implementation.
154 * main thread will sleep while read runs in a worker.
158 static SleepyStream *
159 sleepy_stream_new (void)
161 return g_object_new (sleepy_stream_get_type (), NULL);
165 read_line (GDataInputStream *stream,
173 str = g_data_input_stream_read_line (stream, &length, NULL, error);
178 g_assert (strstr (str, eol) == NULL);
179 g_assert (strlen (str) == length);
181 g_string_append (string, str);
182 g_string_append (string, eol);
189 build_comparison (GString *str,
190 SleepyStream *stream)
192 /* build this for comparison */
195 for (i = 0; stream->pieces[i]; i++)
196 g_string_append (str, stream->pieces[i]);
198 if (str->len && str->str[str->len - 1] != '\n')
199 g_string_append_c (str, '\n');
206 SleepyStream *stream = sleepy_stream_new ();
207 GDataInputStream *data;
208 GError *error = NULL;
212 one = g_string_new (NULL);
213 two = g_string_new (NULL);
215 data = g_data_input_stream_new (G_INPUT_STREAM (stream));
216 g_data_input_stream_set_newline_type (data, G_DATA_STREAM_NEWLINE_TYPE_LF);
217 build_comparison (one, stream);
219 while (read_line (data, two, "\n", &error));
221 g_assert_cmpstr (one->str, ==, two->str);
222 g_string_free (one, TRUE);
223 g_string_free (two, TRUE);
224 g_object_unref (stream);
225 g_object_unref (data);
228 static GDataInputStream *data;
229 static GString *one, *two;
230 static GMainLoop *loop;
231 static const gchar *eol;
234 asynch_ready (GObject *object,
235 GAsyncResult *result,
238 GError *error = NULL;
242 g_assert (data == G_DATA_INPUT_STREAM (object));
244 str = g_data_input_stream_read_line_finish (data, result, &length, &error);
248 g_main_loop_quit (loop);
250 g_error_free (error);
254 g_assert (length == strlen (str));
255 g_string_append (two, str);
256 g_string_append (two, eol);
260 g_data_input_stream_read_line_async (data, 0, NULL, asynch_ready, NULL);
268 SleepyStream *sleepy = sleepy_stream_new ();
270 data = g_data_input_stream_new (G_INPUT_STREAM (sleepy));
271 one = g_string_new (NULL);
272 two = g_string_new (NULL);
275 build_comparison (one, sleepy);
276 g_data_input_stream_read_line_async (data, 0, NULL, asynch_ready, NULL);
277 g_main_loop_run (loop = g_main_loop_new (NULL, FALSE));
279 g_assert_cmpstr (one->str, ==, two->str);
280 g_string_free (one, TRUE);
281 g_string_free (two, TRUE);
282 g_object_unref (sleepy);
283 g_object_unref (data);
287 main (int argc, char **argv)
289 g_test_init (&argc, &argv, NULL);
291 g_test_add_func ("/filter-stream/input", test);
292 g_test_add_func ("/filter-stream/async", asynch);