ovni/test/unit/bay.c

74 lines
1.3 KiB
C
Raw Permalink Normal View History

2023-02-27 13:40:20 +01:00
/* Copyright (c) 2021-2023 Barcelona Supercomputing Center (BSC)
* SPDX-License-Identifier: GPL-3.0-or-later */
#include <stddef.h>
#include <stdint.h>
2023-01-12 19:16:52 +01:00
#include "common.h"
#include "emu/bay.h"
#include "emu/chan.h"
#include "unittest.h"
#include "value.h"
2023-01-12 19:16:52 +01:00
2023-01-11 13:15:53 +01:00
static void
test_duplicate(struct bay *bay)
{
struct chan chan;
chan_init(&chan, CHAN_SINGLE, "dup");
OK(bay_register(bay, &chan));
ERR(bay_register(bay, &chan));
2023-01-11 13:15:53 +01:00
}
2023-01-12 19:16:52 +01:00
static int
callback(struct chan *chan, void *ptr)
{
struct value value;
OK(chan_read(chan, &value));
2023-01-12 19:16:52 +01:00
if (value.type != VALUE_INT64)
die("unexpected value type");
2023-01-12 19:16:52 +01:00
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");
OK(bay_register(bay, &chan));
2023-01-12 19:16:52 +01:00
int64_t data = 0;
2023-02-17 17:26:49 +01:00
if (bay_add_cb(bay, BAY_CB_DIRTY, &chan, callback, &data, 1) == NULL)
die("bay_add_cb failed");
2023-01-12 19:16:52 +01:00
if (data != 0)
die("data changed after bay_chan_append_cb");
2023-01-12 19:16:52 +01:00
OK(chan_set(&chan, value_int64(1)));
2023-01-12 19:16:52 +01:00
if (data != 0)
die("data changed after chan_set");
2023-01-12 19:16:52 +01:00
/* Now the callback should modify 'data' */
OK(bay_propagate(bay));
2023-01-12 19:16:52 +01:00
if (data != 1)
die("data didn't change after bay_propagate");
2023-01-11 13:15:53 +01:00
}
int main(void)
{
struct bay bay;
bay_init(&bay);
test_duplicate(&bay);
test_callback(&bay);
2023-01-12 19:16:52 +01:00
return 0;
}