2023-01-12 19:16:52 +01:00
|
|
|
#include "emu/bay.h"
|
|
|
|
#include "common.h"
|
|
|
|
|
2023-01-11 13:15:53 +01:00
|
|
|
static void
|
|
|
|
test_remove(struct bay *bay)
|
|
|
|
{
|
|
|
|
struct chan chan;
|
|
|
|
chan_init(&chan, CHAN_SINGLE, "removeme");
|
|
|
|
|
|
|
|
if (bay_register(bay, &chan) != 0)
|
|
|
|
die("bay_register failed\n");
|
|
|
|
|
|
|
|
if (bay_find(bay, chan.name) == NULL)
|
|
|
|
die("bay_find failed\n");
|
|
|
|
|
|
|
|
if (bay_remove(bay, &chan) != 0)
|
|
|
|
die("bay_remove failed\n");
|
|
|
|
|
|
|
|
if (bay_find(bay, chan.name) != NULL)
|
|
|
|
die("bay_find didn't fail\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_duplicate(struct bay *bay)
|
|
|
|
{
|
|
|
|
struct chan chan;
|
|
|
|
chan_init(&chan, CHAN_SINGLE, "dup");
|
|
|
|
|
|
|
|
if (bay_register(bay, &chan) != 0)
|
|
|
|
die("bay_register failed\n");
|
|
|
|
|
|
|
|
if (bay_register(bay, &chan) == 0)
|
|
|
|
die("bay_register didn't fail\n");
|
|
|
|
|
|
|
|
if (bay_remove(bay, &chan) != 0)
|
|
|
|
die("bay_remove failed\n");
|
|
|
|
}
|
|
|
|
|
2023-01-12 19:16:52 +01:00
|
|
|
static int
|
|
|
|
callback(struct chan *chan, void *ptr)
|
|
|
|
{
|
|
|
|
struct value value;
|
|
|
|
if (chan_read(chan, &value) != 0)
|
|
|
|
die("callback: chan_read failed\n");
|
|
|
|
|
|
|
|
if (value.type != VALUE_INT64)
|
|
|
|
die("callback: unexpected value type\n");
|
|
|
|
|
|
|
|
int64_t *ival = ptr;
|
|
|
|
*ival = value.i;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-11 13:15:53 +01:00
|
|
|
static void
|
|
|
|
test_callback(struct bay *bay)
|
2023-01-12 19:16:52 +01:00
|
|
|
{
|
|
|
|
struct chan chan;
|
|
|
|
chan_init(&chan, CHAN_SINGLE, "testchan");
|
|
|
|
|
2023-01-11 13:15:53 +01:00
|
|
|
if (bay_register(bay, &chan) != 0)
|
|
|
|
die("bay_register failed\n");
|
2023-01-12 19:16:52 +01:00
|
|
|
|
|
|
|
int64_t data = 0;
|
2023-01-16 15:21:56 +01:00
|
|
|
if (bay_add_cb(bay, BAY_CB_DIRTY, &chan, callback, &data) != 0)
|
2023-01-11 13:15:53 +01:00
|
|
|
die("bay_add_cb failed\n");
|
2023-01-12 19:16:52 +01:00
|
|
|
|
|
|
|
if (data != 0)
|
|
|
|
die("data changed after bay_chan_append_cb\n");
|
|
|
|
|
2023-01-11 13:15:53 +01:00
|
|
|
if (chan_set(&chan, value_int64(1)) != 0)
|
2023-01-12 19:16:52 +01:00
|
|
|
die("chan_set failed\n");
|
|
|
|
|
|
|
|
if (data != 0)
|
|
|
|
die("data changed after chan_set\n");
|
|
|
|
|
|
|
|
/* Now the callback should modify 'data' */
|
2023-01-11 13:15:53 +01:00
|
|
|
if (bay_propagate(bay) != 0)
|
2023-01-12 19:16:52 +01:00
|
|
|
die("bay_propagate failed\n");
|
|
|
|
|
|
|
|
if (data != 1)
|
|
|
|
die("data didn't change after bay_propagate\n");
|
|
|
|
|
2023-01-11 13:15:53 +01:00
|
|
|
if (bay_remove(bay, &chan) != 0)
|
|
|
|
die("bay_remove failed\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
struct bay bay;
|
|
|
|
bay_init(&bay);
|
|
|
|
|
|
|
|
test_remove(&bay);
|
|
|
|
test_duplicate(&bay);
|
|
|
|
test_callback(&bay);
|
|
|
|
|
2023-01-12 19:16:52 +01:00
|
|
|
return 0;
|
|
|
|
}
|