Debian Codegraph @

testsuite/test-stream.c

raw symbols SCIP
1 /* test-stream.c
2 *
3 * Copyright 2022 Christian Hergert <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2.1 of the
8 * License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: LGPL-2.1-or-later
19 */
20
21 #include <config.h>
22
23 #include <libdex.h>
24
25 #include <gio/gio.h>
26
27 #include "dex-future-private.h"
28
29 #define ASSERT_STATUS(f,status) g_assert_cmpint(status, ==, dex_future_get_status(DEX_FUTURE(f)))
30
31 static DexFuture *
32 quit_cb (DexFuture *future,
33 gpointer user_data)
34 {
35 g_main_loop_quit (user_data);
36 return NULL;
37 }
38
39 static DexFuture *
40 await_future (DexFuture *future)
41 {
42 GMainLoop *main_loop = g_main_loop_new (NULL, FALSE);
43
44 future = dex_future_then (future, quit_cb, main_loop, NULL);
45 g_main_loop_run (main_loop);
46
47 g_main_loop_unref (main_loop);
48
49 return future;
50 }
51
52 static void
53 test_read_bytes (void)
54 {
55 GFile *file = g_file_new_for_path ("/etc/os-release");
56 GError *error = NULL;
57 GInputStream *stream = G_INPUT_STREAM (g_file_read (file, NULL, &error));
58 GMainLoop *main_loop = g_main_loop_new (NULL, FALSE);
59 DexFuture *future;
60 const GValue *value;
61 GBytes *bytes;
62
63 if (stream == NULL)
64 {
65 g_clear_error (&error);
66 g_clear_object (&file);
67 g_test_skip ("/etc/os-release not available");
68 return;
69 }
70
71 future = dex_input_stream_read_bytes (stream, 4096, 0);
72 future = dex_future_then (future, quit_cb, main_loop, NULL);
73
74 g_main_loop_run (main_loop);
75
76 g_object_unref (stream);
77 g_object_unref (file);
78
79 value = dex_future_get_value (future, &error);
80 g_assert_no_error (error);
81 g_assert_nonnull (value);
82 g_assert_true (G_VALUE_HOLDS (value, G_TYPE_BYTES));
83
84 bytes = g_value_get_boxed (value);
85 g_assert_nonnull (bytes);
86
87 g_main_loop_quit (main_loop);
88 g_main_loop_unref (main_loop);
89 dex_unref (future);
90 }
91
92 static void
93 test_data_input_stream_read_line_utf8 (void)
94 {
95 static const char data[] = "alpha\nbeta\n";
96 GInputStream * base = NULL;
97 GDataInputStream * stream = NULL;
98 GError * error = NULL;
99 DexFuture *future;
100 const GValue *value;
101
102 base = g_memory_input_stream_new_from_data (data, strlen (data), NULL);
103 stream = g_data_input_stream_new (base);
104
105 future = await_future (dex_data_input_stream_read_line_utf8 (stream, 0));
106 value = dex_future_get_value (future, &error);
107
108 g_assert_no_error (error);
109 g_assert_nonnull (value);
110 g_assert_true (G_VALUE_HOLDS (value, G_TYPE_STRING));
111 g_assert_cmpstr (g_value_get_string (value), ==, "alpha");
112
113 g_clear_object (&stream);
114 g_clear_object (&base);
115 dex_unref (future);
116 }
117
118 static void
119 test_data_input_stream_read_upto (void)
120 {
121 static const char data[] = "alpha,beta";
122 GInputStream * base = NULL;
123 GDataInputStream * stream = NULL;
124 GError * error = NULL;
125 DexFuture *future;
126 const GValue *value;
127
128 base = g_memory_input_stream_new_from_data (data, strlen (data), NULL);
129 stream = g_data_input_stream_new (base);
130
131 future = await_future (dex_data_input_stream_read_upto (stream, ",", -1, 0));
132 value = dex_future_get_value (future, &error);
133
134 g_assert_no_error (error);
135 g_assert_nonnull (value);
136 g_assert_true (G_VALUE_HOLDS (value, G_TYPE_STRING));
137 g_assert_cmpstr (g_value_get_string (value), ==, "alpha");
138
139 g_clear_object (&stream);
140 g_clear_object (&base);
141 dex_unref (future);
142 }
143
144 static void
145 test_output_stream_writev_all (void)
146 {
147 static const char one[] = "alpha";
148 static const char two[] = "beta";
149 GOutputVector vectors[] = {
150 { one, strlen (one) },
151 { two, strlen (two) },
152 };
153 GOutputStream * stream = NULL;
154 GError * error = NULL;
155 DexFuture *future;
156 const GValue *value;
157
158 stream = g_memory_output_stream_new_resizable ();
159
160 future = await_future (dex_output_stream_writev_all (stream, vectors, G_N_ELEMENTS (vectors), 0));
161 value = dex_future_get_value (future, &error);
162
163 g_assert_no_error (error);
164 g_assert_nonnull (value);
165 g_assert_true (G_VALUE_HOLDS (value, G_TYPE_UINT64));
166 g_assert_cmpuint (g_value_get_uint64 (value), ==, strlen (one) + strlen (two));
167 g_assert_cmpmem (g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (stream)),
168 g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (stream)),
169 "alphabeta",
170 strlen ("alphabeta"));
171
172 g_clear_object (&stream);
173 dex_unref (future);
174 }
175
176 int
177 main (int argc,
178 char *argv[])
179 {
180 dex_init ();
181 g_test_init (&argc, &argv, NULL);
182 g_test_add_func ("/Dex/TestSuite/InputStream/read_bytes", test_read_bytes);
183 g_test_add_func ("/Dex/TestSuite/DataInputStream/read_line_utf8",
184 test_data_input_stream_read_line_utf8);
185 g_test_add_func ("/Dex/TestSuite/DataInputStream/read_upto",
186 test_data_input_stream_read_upto);
187 g_test_add_func ("/Dex/TestSuite/OutputStream/writev_all",
188 test_output_stream_writev_all);
189 return g_test_run ();
190 }