2023-02-27 13:40:20 +01:00
|
|
|
/* Copyright (c) 2021-2023 Barcelona Supercomputing Center (BSC)
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later */
|
|
|
|
|
2023-01-12 19:16:52 +01:00
|
|
|
#include "emu/chan.h"
|
|
|
|
#include "common.h"
|
2023-03-21 16:09:01 +01:00
|
|
|
#include "unittest.h"
|
2023-01-12 19:16:52 +01:00
|
|
|
|
|
|
|
static void
|
2023-01-10 18:30:51 +01:00
|
|
|
test_dirty(void)
|
|
|
|
{
|
|
|
|
struct chan chan;
|
|
|
|
chan_init(&chan, CHAN_SINGLE, "testchan");
|
|
|
|
|
2023-03-21 16:09:01 +01:00
|
|
|
OK(chan_set(&chan, value_int64(1)));
|
2023-01-10 18:30:51 +01:00
|
|
|
|
|
|
|
/* Now channel is dirty */
|
|
|
|
|
2023-03-21 16:09:01 +01:00
|
|
|
ERR(chan_set(&chan, value_int64(2)));
|
2023-01-10 18:30:51 +01:00
|
|
|
|
2023-03-21 16:09:01 +01:00
|
|
|
OK(chan_flush(&chan));
|
2023-01-10 18:30:51 +01:00
|
|
|
|
2023-01-11 11:58:23 +01:00
|
|
|
chan_prop_set(&chan, CHAN_DIRTY_WRITE, 1);
|
2023-01-10 18:30:51 +01:00
|
|
|
|
2023-03-21 16:09:01 +01:00
|
|
|
OK(chan_set(&chan, value_int64(3)));
|
2023-01-10 18:30:51 +01:00
|
|
|
|
|
|
|
/* Now is dirty, but it should allow multiple writes */
|
2023-03-21 16:09:01 +01:00
|
|
|
OK(chan_set(&chan, value_int64(4)));
|
2023-01-10 18:30:51 +01:00
|
|
|
|
|
|
|
struct value value;
|
|
|
|
struct value four = value_int64(4);
|
|
|
|
|
2023-03-21 16:09:01 +01:00
|
|
|
OK(chan_read(&chan, &value));
|
2023-01-10 18:30:51 +01:00
|
|
|
|
|
|
|
if (!value_is_equal(&value, &four))
|
2023-03-21 16:09:01 +01:00
|
|
|
die("chan_read returned unexpected value");
|
2023-01-10 18:30:51 +01:00
|
|
|
|
2023-03-21 16:09:01 +01:00
|
|
|
OK(chan_flush(&chan));
|
2023-03-13 12:17:28 +01:00
|
|
|
|
|
|
|
err("OK");
|
2023-01-10 18:30:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_single(void)
|
2023-01-12 19:16:52 +01:00
|
|
|
{
|
|
|
|
struct chan chan;
|
|
|
|
struct value one = { .type = VALUE_INT64, .i = 1 };
|
|
|
|
struct value two = { .type = VALUE_INT64, .i = 2 };
|
|
|
|
|
|
|
|
chan_init(&chan, CHAN_SINGLE, "testchan");
|
|
|
|
|
|
|
|
/* Ensure we cannot push as stack */
|
2023-03-21 16:09:01 +01:00
|
|
|
ERR(chan_push(&chan, one));
|
2023-01-12 19:16:52 +01:00
|
|
|
|
|
|
|
/* Now we should be able to write with set */
|
2023-03-21 16:09:01 +01:00
|
|
|
OK(chan_set(&chan, one));
|
2023-01-12 19:16:52 +01:00
|
|
|
|
|
|
|
/* Now is dirty, it shouldn't allow another set */
|
2023-03-21 16:09:01 +01:00
|
|
|
ERR(chan_set(&chan, two));
|
2023-01-12 19:16:52 +01:00
|
|
|
|
|
|
|
struct value value;
|
|
|
|
|
2023-03-21 16:09:01 +01:00
|
|
|
OK(chan_read(&chan, &value));
|
2023-01-12 19:16:52 +01:00
|
|
|
|
|
|
|
if (!value_is_equal(&value, &one))
|
2023-03-21 16:09:01 +01:00
|
|
|
die("chan_read returned unexpected value");
|
2023-03-13 12:17:28 +01:00
|
|
|
|
|
|
|
err("OK");
|
2023-01-12 19:16:52 +01:00
|
|
|
}
|
|
|
|
|
2023-03-13 12:17:28 +01:00
|
|
|
/* Test that writting the same value to a channel raises an error, but not if we
|
|
|
|
* set the CHAN_ALLOW_DUP flag */
|
2023-01-11 11:58:23 +01:00
|
|
|
static void
|
2023-03-13 12:17:28 +01:00
|
|
|
test_allow_dup(void)
|
2023-01-11 11:58:23 +01:00
|
|
|
{
|
|
|
|
struct chan chan;
|
|
|
|
chan_init(&chan, CHAN_SINGLE, "testchan");
|
|
|
|
|
2023-03-21 16:09:01 +01:00
|
|
|
OK(chan_set(&chan, value_int64(1)));
|
|
|
|
OK(chan_flush(&chan));
|
2023-01-11 11:58:23 +01:00
|
|
|
|
|
|
|
/* Attempt to write the same value again */
|
2023-03-21 16:09:01 +01:00
|
|
|
ERR(chan_set(&chan, value_int64(1)));
|
2023-01-11 11:58:23 +01:00
|
|
|
|
2023-03-13 12:17:28 +01:00
|
|
|
/* Allow duplicates */
|
2023-03-13 11:49:44 +01:00
|
|
|
chan_prop_set(&chan, CHAN_ALLOW_DUP, 1);
|
2023-01-11 11:58:23 +01:00
|
|
|
|
|
|
|
/* Then it should allow writting the same value */
|
2023-03-21 16:09:01 +01:00
|
|
|
OK(chan_set(&chan, value_int64(1)));
|
2023-03-13 12:17:28 +01:00
|
|
|
|
|
|
|
/* Also ensure the channel is dirty */
|
|
|
|
if (!chan.is_dirty)
|
|
|
|
die("chan is not dirty");
|
|
|
|
|
|
|
|
err("OK");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test that writting the same value to a channel with the flag CHAN_IGNORE_DUP,
|
|
|
|
* causes the channel to remain clean (without the is_dirty flag set) */
|
|
|
|
static void
|
|
|
|
test_ignore_dup(void)
|
|
|
|
{
|
|
|
|
struct chan chan;
|
|
|
|
chan_init(&chan, CHAN_SINGLE, "testchan");
|
|
|
|
chan_prop_set(&chan, CHAN_IGNORE_DUP, 1);
|
|
|
|
|
2023-03-21 16:09:01 +01:00
|
|
|
OK(chan_set(&chan, value_int64(1)));
|
2023-03-13 12:17:28 +01:00
|
|
|
|
2023-03-21 16:09:01 +01:00
|
|
|
OK(chan_flush(&chan));
|
2023-03-13 12:17:28 +01:00
|
|
|
|
|
|
|
/* Write the same value */
|
2023-03-21 16:09:01 +01:00
|
|
|
OK(chan_set(&chan, value_int64(1)));
|
2023-03-13 12:17:28 +01:00
|
|
|
|
|
|
|
/* And ensure the channel is not dirty */
|
|
|
|
if (chan.is_dirty)
|
|
|
|
die("chan is dirty");
|
|
|
|
|
|
|
|
err("OK");
|
2023-01-11 11:58:23 +01:00
|
|
|
}
|
|
|
|
|
2023-01-12 19:16:52 +01:00
|
|
|
int main(void)
|
|
|
|
{
|
2023-01-10 18:30:51 +01:00
|
|
|
test_single();
|
|
|
|
test_dirty();
|
2023-03-13 12:17:28 +01:00
|
|
|
test_allow_dup();
|
|
|
|
test_ignore_dup();
|
2023-03-21 16:09:01 +01:00
|
|
|
|
2023-01-12 19:16:52 +01:00
|
|
|
return 0;
|
|
|
|
}
|