2024-06-21 15:07:41 +02:00
|
|
|
/* Copyright (c) 2021-2024 Barcelona Supercomputing Center (BSC)
|
2023-02-27 13:40:20 +01:00
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later */
|
|
|
|
|
2023-01-12 19:16:52 +01:00
|
|
|
#include "emu/mux.h"
|
2023-03-22 16:01:55 +01:00
|
|
|
#include <stdio.h>
|
2023-01-12 19:16:52 +01:00
|
|
|
#include "common.h"
|
2023-03-22 16:01:55 +01:00
|
|
|
#include "emu/bay.h"
|
|
|
|
#include "emu/chan.h"
|
2023-03-21 16:09:01 +01:00
|
|
|
#include "unittest.h"
|
2023-01-12 19:16:52 +01:00
|
|
|
|
2023-01-11 12:08:28 +01:00
|
|
|
#define N 10
|
2023-01-12 19:16:52 +01:00
|
|
|
|
2023-01-10 18:30:51 +01:00
|
|
|
static void
|
|
|
|
check_output(struct mux *mux, struct value expected)
|
|
|
|
{
|
|
|
|
struct value out_value = value_null();
|
2023-03-21 16:09:01 +01:00
|
|
|
OK(chan_read(mux->output, &out_value));
|
2023-01-10 18:30:51 +01:00
|
|
|
|
|
|
|
if (!value_is_equal(&out_value, &expected)) {
|
2023-03-21 16:09:01 +01:00
|
|
|
die("unexpected value found %s in output (expected %s)",
|
2023-03-13 11:25:30 +01:00
|
|
|
value_str(out_value),
|
|
|
|
value_str(expected));
|
2023-01-10 18:30:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_select(struct mux *mux, int key)
|
|
|
|
{
|
2023-03-21 16:09:01 +01:00
|
|
|
OK(chan_set(mux->select, value_int64(key)));
|
|
|
|
OK(bay_propagate(mux->bay));
|
2023-01-10 18:30:51 +01:00
|
|
|
check_output(mux, value_int64(1000 + key));
|
2023-03-21 16:09:01 +01:00
|
|
|
err("OK");
|
2023-01-10 18:30:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_input(struct mux *mux, int key)
|
|
|
|
{
|
|
|
|
/* Set the select channel to the selected key */
|
|
|
|
test_select(mux, key);
|
|
|
|
|
|
|
|
int new_value = 2000 + key;
|
|
|
|
|
|
|
|
/* Then change that channel */
|
2023-02-17 17:19:49 +01:00
|
|
|
struct mux_input *mi = mux_get_input(mux, key);
|
2023-01-10 18:30:51 +01:00
|
|
|
if (mi == NULL)
|
2023-03-21 16:09:01 +01:00
|
|
|
die("mux_get_input failed to locate input %d", key);
|
2023-01-10 18:30:51 +01:00
|
|
|
|
2023-03-21 16:09:01 +01:00
|
|
|
OK(chan_set(mi->chan, value_int64(new_value)));
|
|
|
|
OK(bay_propagate(mux->bay));
|
2023-01-10 18:30:51 +01:00
|
|
|
check_output(mux, value_int64(new_value));
|
2023-03-21 16:09:01 +01:00
|
|
|
err("OK");
|
2023-01-10 18:30:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_select_and_input(struct mux *mux, int key)
|
|
|
|
{
|
|
|
|
/* Set the select channel to the selected key, but don't
|
|
|
|
* propagate the changes yet */
|
2023-03-21 16:09:01 +01:00
|
|
|
OK(chan_set(mux->select, value_int64(key)));
|
2023-01-10 18:30:51 +01:00
|
|
|
|
|
|
|
int new_value = 2000 + key;
|
|
|
|
|
|
|
|
/* Also change that channel */
|
2023-02-17 17:19:49 +01:00
|
|
|
struct mux_input *mi = mux_get_input(mux, key);
|
2023-01-10 18:30:51 +01:00
|
|
|
if (mi == NULL)
|
2023-03-21 16:09:01 +01:00
|
|
|
die("mux_get_input failed to locate input %d", key);
|
2023-01-10 18:30:51 +01:00
|
|
|
|
2023-03-21 16:09:01 +01:00
|
|
|
OK(chan_set(mi->chan, value_int64(new_value)));
|
2023-01-10 18:30:51 +01:00
|
|
|
|
|
|
|
/* Write twice to the output */
|
2023-03-21 16:09:01 +01:00
|
|
|
OK(bay_propagate(mux->bay));
|
2023-01-10 18:30:51 +01:00
|
|
|
|
|
|
|
check_output(mux, value_int64(new_value));
|
2023-03-21 16:09:01 +01:00
|
|
|
err("OK");
|
2023-01-10 18:30:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_input_and_select(struct mux *mux, int key)
|
|
|
|
{
|
|
|
|
int new_value = 2000 + key;
|
|
|
|
|
|
|
|
/* First change the input */
|
2023-02-17 17:19:49 +01:00
|
|
|
struct mux_input *mi = mux_get_input(mux, key);
|
2023-01-10 18:30:51 +01:00
|
|
|
if (mi == NULL)
|
2023-03-21 16:09:01 +01:00
|
|
|
die("mux_get_input failed to locate input %d", key);
|
2023-01-10 18:30:51 +01:00
|
|
|
|
2023-03-21 16:09:01 +01:00
|
|
|
OK(chan_set(mi->chan, value_int64(new_value)));
|
2023-01-10 18:30:51 +01:00
|
|
|
|
|
|
|
/* Then change select */
|
2023-03-21 16:09:01 +01:00
|
|
|
OK(chan_set(mux->select, value_int64(key)));
|
2023-01-10 18:30:51 +01:00
|
|
|
|
|
|
|
/* Write twice to the output */
|
2023-03-21 16:09:01 +01:00
|
|
|
OK(bay_propagate(mux->bay));
|
2023-01-10 18:30:51 +01:00
|
|
|
|
|
|
|
check_output(mux, value_int64(new_value));
|
2023-03-21 16:09:01 +01:00
|
|
|
err("OK");
|
2023-01-10 18:30:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_mid_propagate(struct mux *mux, int key)
|
|
|
|
{
|
|
|
|
int new_value = 2000 + key;
|
|
|
|
|
2023-02-17 17:19:49 +01:00
|
|
|
struct mux_input *mi = mux_get_input(mux, key);
|
2023-01-10 18:30:51 +01:00
|
|
|
if (mi == NULL)
|
2023-03-21 16:09:01 +01:00
|
|
|
die("mux_get_input failed to locate input %d", key);
|
2023-01-10 18:30:51 +01:00
|
|
|
|
2023-03-21 16:09:01 +01:00
|
|
|
OK(chan_set(mi->chan, value_int64(new_value)));
|
|
|
|
OK(bay_propagate(mux->bay));
|
|
|
|
OK(chan_set(mux->select, value_int64(key)));
|
|
|
|
OK(bay_propagate(mux->bay));
|
2023-01-10 18:30:51 +01:00
|
|
|
|
|
|
|
check_output(mux, value_int64(new_value));
|
2023-03-21 16:09:01 +01:00
|
|
|
err("OK");
|
2023-01-10 18:30:51 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 12:08:28 +01:00
|
|
|
static void
|
|
|
|
test_duplicate_output(struct mux *mux, int key1, int key2)
|
|
|
|
{
|
|
|
|
int new_value = 2000 + key1;
|
|
|
|
|
2023-02-17 17:19:49 +01:00
|
|
|
struct mux_input *in1 = mux_get_input(mux, key1);
|
2023-01-11 12:08:28 +01:00
|
|
|
if (in1 == NULL)
|
2023-03-21 16:09:01 +01:00
|
|
|
die("mux_get_input failed to locate input1 %d", key1);
|
2023-01-11 12:08:28 +01:00
|
|
|
|
2023-02-17 17:19:49 +01:00
|
|
|
struct mux_input *in2 = mux_get_input(mux, key2);
|
2023-01-11 12:08:28 +01:00
|
|
|
if (in2 == NULL)
|
2023-03-21 16:09:01 +01:00
|
|
|
die("mux_get_input failed to locate input2 %d", key2);
|
2023-01-11 12:08:28 +01:00
|
|
|
|
2023-03-21 16:09:01 +01:00
|
|
|
OK(chan_set(in1->chan, value_int64(new_value)));
|
|
|
|
OK(chan_set(in2->chan, value_int64(new_value)));
|
2023-01-11 12:08:28 +01:00
|
|
|
|
|
|
|
/* Select input 1 */
|
2023-03-21 16:09:01 +01:00
|
|
|
OK(chan_set(mux->select, value_int64(key1)));
|
|
|
|
OK(bay_propagate(mux->bay));
|
2023-01-11 12:08:28 +01:00
|
|
|
|
|
|
|
check_output(mux, value_int64(new_value));
|
|
|
|
|
|
|
|
/* Now switch to input 2, which has the same value */
|
2023-03-21 16:09:01 +01:00
|
|
|
OK(chan_set(mux->select, value_int64(key2)));
|
|
|
|
OK(bay_propagate(mux->bay));
|
2023-01-11 12:08:28 +01:00
|
|
|
|
|
|
|
check_output(mux, value_int64(new_value));
|
2023-03-21 16:09:01 +01:00
|
|
|
err("OK");
|
2023-01-11 12:08:28 +01:00
|
|
|
}
|
|
|
|
|
2023-01-12 19:16:52 +01:00
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
|
|
|
struct bay bay;
|
|
|
|
bay_init(&bay);
|
|
|
|
|
|
|
|
struct chan inputs[N];
|
|
|
|
struct chan output;
|
|
|
|
struct chan select;
|
|
|
|
|
|
|
|
chan_init(&output, CHAN_SINGLE, "output");
|
|
|
|
chan_init(&select, CHAN_SINGLE, "select");
|
|
|
|
|
2024-06-21 15:07:41 +02:00
|
|
|
for (int i = 0; i < N; i++)
|
|
|
|
chan_init(&inputs[i], CHAN_SINGLE, "input.%d", i);
|
2023-01-12 19:16:52 +01:00
|
|
|
|
|
|
|
/* Register all channels in the bay */
|
2023-03-21 16:09:01 +01:00
|
|
|
OK(bay_register(&bay, &select));
|
2023-01-11 13:15:21 +01:00
|
|
|
|
2023-03-21 16:09:01 +01:00
|
|
|
for (int i = 0; i < N; i++)
|
|
|
|
OK(bay_register(&bay, &inputs[i]));
|
2023-01-12 19:16:52 +01:00
|
|
|
|
|
|
|
struct mux mux;
|
2023-01-11 13:15:21 +01:00
|
|
|
/* Attempt to init the mux without registering the output */
|
2023-03-21 16:09:01 +01:00
|
|
|
ERR(mux_init(&mux, &bay, &select, &output, NULL, N));
|
|
|
|
OK(bay_register(&bay, &output));
|
|
|
|
OK(mux_init(&mux, &bay, &select, &output, NULL, N));
|
2023-01-11 13:15:21 +01:00
|
|
|
|
2023-03-21 16:09:01 +01:00
|
|
|
for (int i = 0; i < N; i++)
|
|
|
|
OK(mux_set_input(&mux, i, &inputs[i]));
|
2023-01-12 19:16:52 +01:00
|
|
|
|
|
|
|
/* Write something to the input channels */
|
2023-03-21 16:09:01 +01:00
|
|
|
for (int i = 0; i < N; i++)
|
|
|
|
OK(chan_set(&inputs[i], value_int64(1000 + i)));
|
2023-01-12 19:16:52 +01:00
|
|
|
|
|
|
|
/* Propagate values and call the callbacks */
|
2023-03-21 16:09:01 +01:00
|
|
|
OK(bay_propagate(&bay));
|
2023-01-12 19:16:52 +01:00
|
|
|
|
2023-01-10 18:30:51 +01:00
|
|
|
test_select(&mux, 1);
|
|
|
|
test_input(&mux, 2);
|
|
|
|
test_select_and_input(&mux, 3);
|
|
|
|
test_input_and_select(&mux, 4);
|
|
|
|
test_mid_propagate(&mux, 5);
|
2023-01-11 12:08:28 +01:00
|
|
|
test_duplicate_output(&mux, 6, 7);
|
2023-01-12 19:16:52 +01:00
|
|
|
|
2023-03-21 16:09:01 +01:00
|
|
|
err("OK");
|
2023-01-12 19:16:52 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|