Add a test for ids on submenu and section elements
[glib.git] / gio / tests / test-pipe-unix.c
blobf4e07a420b3e8aa50b53555273eb16a581884b33
1 /* Unix pipe-to-self. This is a utility module for tests, not a test.
3 * Copyright © 2008-2010 Red Hat, Inc.
4 * Copyright © 2011 Nokia Corporation
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 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
24 #include <errno.h>
25 #include <unistd.h>
27 #include <gio/gio.h>
29 #include "test-io-stream.h"
31 #ifdef G_OS_UNIX
32 # include <gio/gunixinputstream.h>
33 # include <gio/gunixoutputstream.h>
34 #else
35 # error This module only exists on Unix
36 #endif
39 * test_pipe:
40 * @is: (out) (allow-none): used to return a #GInputStream
41 * @os: (out) (allow-none): used to return a #GOutputStream
42 * @error: used to raise an error
44 * Return a "pipe to self" connecting @is to @os. This can be used
45 * as a unidirectional pipe to or from a child process, for instance.
47 * See test_bidi_pipe() if you want to emulate a bidirectional pipe
48 * via a pair of unidirectional pipes.
50 * Returns: %TRUE on success
52 gboolean
53 test_pipe (GInputStream **is,
54 GOutputStream **os,
55 GError **error)
57 int pipefd[2];
58 int ret;
60 ret = pipe (pipefd);
62 if (ret != 0)
64 int e = errno;
66 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (e),
67 "%s", g_strerror (e));
68 return FALSE;
71 if (is != NULL)
72 *is = g_unix_input_stream_new (pipefd[0], TRUE);
73 else
74 close (pipefd[0]);
76 if (os != NULL)
77 *os = g_unix_output_stream_new (pipefd[1], TRUE);
78 else
79 close (pipefd[1]);
81 return TRUE;
85 * test_bidi_pipe:
86 * @left: (out) (allow-none): used to return one #GIOStream
87 * @right: (out) (allow-none): used to return the other #GIOStream
88 * @error: used to raise an error
90 * Return two #GIOStream<!---->s connected to each other with pipes.
91 * The "left" input stream is connected by a unidirectional pipe
92 * to the "right" output stream, and vice versa. This can be used
93 * as a bidirectional pipe to a child process, for instance.
95 * See test_pipe() if you only need a one-way pipe.
97 * Returns: %TRUE on success
99 gboolean
100 test_bidi_pipe (GIOStream **left,
101 GIOStream **right,
102 GError **error)
104 GInputStream *left_in = NULL;
105 GOutputStream *left_out = NULL;
106 GInputStream *right_in = NULL;
107 GOutputStream *right_out = NULL;
108 gboolean ret;
110 if (!test_pipe (&left_in, &right_out, error))
111 goto out;
113 if (!test_pipe (&right_in, &left_out, error))
114 goto out;
116 if (left != NULL)
117 *left = test_io_stream_new (left_in, left_out);
119 if (right != NULL)
120 *right = test_io_stream_new (right_in, right_out);
122 ret = TRUE;
124 out:
125 g_clear_object (&left_in);
126 g_clear_object (&left_out);
127 g_clear_object (&right_in);
128 g_clear_object (&right_out);
129 return ret;