Debian Codegraph @

testsuite/test-stream.c

raw
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 void
40 test_read_bytes (void)
41 {
42 GFile *file = g_file_new_for_path ("/etc/os-release");
43 GError *error = NULL;
44 GInputStream *stream = G_INPUT_STREAM (g_file_read (file, NULL, &error));
45 GMainLoop *main_loop = g_main_loop_new (NULL, FALSE);
46 DexFuture *future;
47 const GValue *value;
48 GBytes *bytes;
49
50 if (stream == NULL)
51 {
52 g_clear_error (&error);
53 g_test_skip ("/etc/os-release not available");
54 return;
55 }
56
57 future = dex_input_stream_read_bytes (stream, 4096, 0);
58 future = dex_future_then (future, quit_cb, main_loop, NULL);
59
60 g_main_loop_run (main_loop);
61
62 g_object_unref (stream);
63 g_object_unref (file);
64
65 value = dex_future_get_value (future, &error);
66 g_assert_no_error (error);
67 g_assert_nonnull (value);
68 g_assert_true (G_VALUE_HOLDS (value, G_TYPE_BYTES));
69
70 bytes = g_value_get_boxed (value);
71 g_assert_nonnull (bytes);
72
73 g_main_loop_quit (main_loop);
74 g_main_loop_unref (main_loop);
75 dex_unref (future);
76 }
77
78 int
79 main (int argc,
80 char *argv[])
81 {
82 dex_init ();
83 g_test_init (&argc, &argv, NULL);
84 g_test_add_func ("/Dex/TestSuite/InputStream/read_bytes", test_read_bytes);
85 return g_test_run ();
86 }