| 1 |
/* |
| 2 |
* test-channel.c |
| 3 |
* |
| 4 |
* Copyright 2022 Christian Hergert <[email protected]> |
| 5 |
* |
| 6 |
* This program 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. |
| 10 |
* |
| 11 |
* This program 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. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public License |
| 17 |
* along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
|
| 24 |
#include <libdex.h> |
| 25 |
|
| 26 |
#define ASSERT_STATUS(f,status) g_assert_cmpint(status, ==, dex_future_get_status(DEX_FUTURE(f))) |
| 27 |
|
| 28 |
#define ASSERT_CMP(future, kind, get, op, v) \ |
| 29 |
G_STMT_START { \ |
| 30 |
GError *error = NULL; \ |
| 31 |
const GValue *value = dex_future_get_value (DEX_FUTURE (future), &error); \ |
| 32 |
g_assert_no_error (error); \ |
| 33 |
g_assert_nonnull (value); \ |
| 34 |
G_PASTE (g_assert_cmp, kind) (G_PASTE (g_value_get_, get) (value), op, v); \ |
| 35 |
} G_STMT_END |
| 36 |
#define ASSERT_CMPINT(future, op, value) ASSERT_CMP(future, int, int, op, value) |
| 37 |
#define ASSERT_CMPUINT(future, op, value) ASSERT_CMP(future, int, uint, op, value) |
| 38 |
|
| 39 |
static void |
| 40 |
test_channel_basic (void) |
| 41 |
{ |
| 42 |
DexChannel *channel; |
| 43 |
DexFuture *value1 = NULL; |
| 44 |
DexFuture *value2 = NULL; |
| 45 |
DexFuture *value3 = NULL; |
| 46 |
DexFuture *send1 = NULL; |
| 47 |
DexFuture *send2 = NULL; |
| 48 |
DexFuture *send3 = NULL; |
| 49 |
DexFuture *recv1 = NULL; |
| 50 |
DexFuture *recv2 = NULL; |
| 51 |
DexFuture *recv3 = NULL; |
| 52 |
|
| 53 |
channel = dex_channel_new (2); |
| 54 |
g_assert_true (dex_channel_can_send (channel)); |
| 55 |
g_assert_true (dex_channel_can_receive (channel)); |
| 56 |
|
| 57 |
value1 = dex_future_new_for_int (1); |
| 58 |
value2 = dex_future_new_for_int (2); |
| 59 |
value3 = dex_future_new_for_int (3); |
| 60 |
ASSERT_CMPINT (value1, ==, 1); |
| 61 |
ASSERT_CMPINT (value2, ==, 2); |
| 62 |
ASSERT_CMPINT (value3, ==, 3); |
| 63 |
|
| 64 |
send1 = dex_channel_send (channel, dex_ref (value1)); |
| 65 |
g_assert_true ((gpointer)send1 != (gpointer)value1); |
| 66 |
g_assert_true (dex_channel_can_send (channel)); |
| 67 |
g_assert_true (dex_channel_can_receive (channel)); |
| 68 |
ASSERT_STATUS (send1, DEX_FUTURE_STATUS_RESOLVED); |
| 69 |
ASSERT_CMPUINT (send1, ==, 1); |
| 70 |
|
| 71 |
send2 = dex_channel_send (channel, dex_ref (value2)); |
| 72 |
g_assert_true (dex_channel_can_send (channel)); |
| 73 |
g_assert_true (dex_channel_can_receive (channel)); |
| 74 |
ASSERT_STATUS (send2, DEX_FUTURE_STATUS_RESOLVED); |
| 75 |
ASSERT_CMPUINT (send2, ==, 2); |
| 76 |
|
| 77 |
send3 = dex_channel_send (channel, dex_ref (value3)); |
| 78 |
g_assert_true (dex_channel_can_send (channel)); |
| 79 |
g_assert_true (dex_channel_can_receive (channel)); |
| 80 |
ASSERT_STATUS (send3, DEX_FUTURE_STATUS_PENDING); |
| 81 |
|
| 82 |
dex_channel_close_send (channel); |
| 83 |
g_assert_false (dex_channel_can_send (channel)); |
| 84 |
g_assert_true (dex_channel_can_receive (channel)); |
| 85 |
ASSERT_STATUS (send3, DEX_FUTURE_STATUS_PENDING); |
| 86 |
|
| 87 |
recv1 = dex_channel_receive (channel); |
| 88 |
ASSERT_STATUS (send3, DEX_FUTURE_STATUS_RESOLVED); |
| 89 |
ASSERT_STATUS (recv1, DEX_FUTURE_STATUS_RESOLVED); |
| 90 |
ASSERT_CMPUINT (send3, ==, 2); |
| 91 |
ASSERT_CMPINT (recv1, ==, 1); |
| 92 |
|
| 93 |
recv2 = dex_channel_receive (channel); |
| 94 |
ASSERT_STATUS (recv2, DEX_FUTURE_STATUS_RESOLVED); |
| 95 |
ASSERT_CMPINT (recv2, ==, 2); |
| 96 |
|
| 97 |
dex_channel_close_receive (channel); |
| 98 |
g_assert_false (dex_channel_can_send (channel)); |
| 99 |
g_assert_false (dex_channel_can_receive (channel)); |
| 100 |
|
| 101 |
recv3 = dex_channel_receive (channel); |
| 102 |
ASSERT_STATUS (recv3, DEX_FUTURE_STATUS_REJECTED); |
| 103 |
|
| 104 |
dex_clear (&value1); |
| 105 |
dex_clear (&value2); |
| 106 |
dex_clear (&value3); |
| 107 |
dex_clear (&send1); |
| 108 |
dex_clear (&send2); |
| 109 |
dex_clear (&send3); |
| 110 |
dex_clear (&recv1); |
| 111 |
dex_clear (&recv2); |
| 112 |
dex_clear (&recv3); |
| 113 |
dex_clear (&channel); |
| 114 |
} |
| 115 |
|
| 116 |
static void |
| 117 |
test_channel_recv_first (void) |
| 118 |
{ |
| 119 |
DexChannel *channel = dex_channel_new (2); |
| 120 |
DexFuture *recv1 = dex_channel_receive (channel); |
| 121 |
DexFuture *recv2 = dex_channel_receive (channel); |
| 122 |
DexFuture *recv3 = dex_channel_receive (channel); |
| 123 |
DexFuture *recv4; |
| 124 |
DexFuture *value1 = dex_future_new_for_int (123); |
| 125 |
DexFuture *send1; |
| 126 |
|
| 127 |
ASSERT_STATUS (recv1, DEX_FUTURE_STATUS_PENDING); |
| 128 |
ASSERT_STATUS (recv2, DEX_FUTURE_STATUS_PENDING); |
| 129 |
|
| 130 |
send1 = dex_channel_send (channel, dex_ref (value1)); |
| 131 |
ASSERT_STATUS (send1, DEX_FUTURE_STATUS_RESOLVED); |
| 132 |
ASSERT_STATUS (recv1, DEX_FUTURE_STATUS_RESOLVED); |
| 133 |
ASSERT_STATUS (recv2, DEX_FUTURE_STATUS_PENDING); |
| 134 |
|
| 135 |
dex_channel_close_send (channel); |
| 136 |
ASSERT_STATUS (recv2, DEX_FUTURE_STATUS_REJECTED); |
| 137 |
ASSERT_STATUS (recv3, DEX_FUTURE_STATUS_REJECTED); |
| 138 |
|
| 139 |
recv4 = dex_channel_receive (channel); |
| 140 |
ASSERT_STATUS (recv4, DEX_FUTURE_STATUS_REJECTED); |
| 141 |
|
| 142 |
dex_clear (&channel); |
| 143 |
dex_clear (&recv1); |
| 144 |
dex_clear (&recv2); |
| 145 |
dex_clear (&recv3); |
| 146 |
dex_clear (&recv4); |
| 147 |
dex_clear (&value1); |
| 148 |
dex_clear (&send1); |
| 149 |
} |
| 150 |
|
| 151 |
int |
| 152 |
main (int argc, |
| 153 |
char *argv[]) |
| 154 |
{ |
| 155 |
dex_init (); |
| 156 |
g_test_init (&argc, &argv, NULL); |
| 157 |
g_test_add_func ("/Dex/TestSuite/Channel/basic", test_channel_basic); |
| 158 |
g_test_add_func ("/Dex/TestSuite/Channel/recv_first", test_channel_recv_first); |
| 159 |
return g_test_run (); |
| 160 |
} |