Use OK() and ERR() in unit tests

Also remove prefixes and trailing new lines in err() and die()
This commit is contained in:
Rodrigo Arias 2023-03-21 16:09:01 +01:00 committed by Rodrigo Arias Mallo
parent 648411c686
commit c0afc79d4e
14 changed files with 153 additions and 299 deletions

View File

@ -104,7 +104,7 @@ instr_start(int rank, int nranks)
int curcpu = rank; int curcpu = rank;
dbg("thread %d has cpu %d (ncpus=%d)\n", dbg("thread %d has cpu %d (ncpus=%d)",
gettid(), curcpu, nranks); gettid(), curcpu, nranks);
instr_thread_execute(curcpu, -1, 0); instr_thread_execute(curcpu, -1, 0);

View File

@ -53,7 +53,7 @@ start_delayed(int rank, int nranks)
int curcpu = rank; int curcpu = rank;
dbg("thread %d has cpu %d (ncpus=%d)\n", dbg("thread %d has cpu %d (ncpus=%d)",
gettid(), curcpu, nranks); gettid(), curcpu, nranks);
delta = ((int64_t) rank) * 2LL * 3600LL * 1000LL * 1000LL * 1000LL; delta = ((int64_t) rank) * 2LL * 3600LL * 1000LL * 1000LL * 1000LL;

View File

@ -58,7 +58,7 @@ main(void)
/* Fill the ring buffer */ /* Fill the ring buffer */
long n = 1000000 + 10; long n = 1000000 + 10;
err("using n=%ld events\n", n); err("using n=%ld events", n);
/* Go back 100 ns for each event (with some space) */ /* Go back 100 ns for each event (with some space) */
int64_t delta = 10000; int64_t delta = 10000;
@ -66,7 +66,7 @@ main(void)
for (long i = 0; i < n; i++) { for (long i = 0; i < n; i++) {
if (t <= t0 || t >= t1) if (t <= t0 || t >= t1)
die("bad time\n"); die("bad time");
emit("OB.", t); emit("OB.", t);
t += 33; t += 33;
} }

View File

@ -17,22 +17,22 @@ main(void)
if (nosv_type_init(&type, NULL, NULL, NULL, "adopted", NULL, if (nosv_type_init(&type, NULL, NULL, NULL, "adopted", NULL,
NULL, NOSV_TYPE_INIT_EXTERNAL) NULL, NOSV_TYPE_INIT_EXTERNAL)
!= 0) != 0)
die("nosv_type_init failed\n"); die("nosv_type_init failed");
nosv_task_t task; nosv_task_t task;
if (nosv_attach(&task, type, 0, NULL, 0) != 0) if (nosv_attach(&task, type, 0, NULL, 0) != 0)
die("nosv_attach failed\n"); die("nosv_attach failed");
usleep(100); usleep(100);
if (nosv_detach(0) != 0) if (nosv_detach(0) != 0)
die("nosv_detach failed\n"); die("nosv_detach failed");
if (nosv_type_destroy(type, 0) != 0) if (nosv_type_destroy(type, 0) != 0)
die("nosv_type_destroy failed\n"); die("nosv_type_destroy failed");
if (nosv_shutdown() != 0) if (nosv_shutdown() != 0)
die("nosv_shutdown failed\n"); die("nosv_shutdown failed");
return 0; return 0;
} }

View File

@ -5,6 +5,7 @@
#include "emu/bay.h" #include "emu/bay.h"
#include "common.h" #include "common.h"
#include "unittest.h"
#include <time.h> #include <time.h>
#define N 10000 #define N 10000
@ -28,23 +29,20 @@ populate(struct bay *bay)
channels = calloc(N, sizeof(struct chan)); channels = calloc(N, sizeof(struct chan));
if (channels == NULL) if (channels == NULL)
die("calloc failed\n"); die("calloc failed");
for (long i = 0; i < N; i++) { for (long i = 0; i < N; i++) {
sprintf(name, "%s.%ld", BASE, i); sprintf(name, "%s.%ld", BASE, i);
chan_init(&channels[i], CHAN_SINGLE, name); chan_init(&channels[i], CHAN_SINGLE, name);
if (bay_register(bay, &channels[i]) != 0) OK(bay_register(bay, &channels[i]));
die("bay_register failed\n");
} }
} }
static void static void
dummy_work(struct chan *c) dummy_work(struct chan *c)
{ {
if (chan_set(c, value_int64(dummy_value++)) != 0) OK(chan_set(c, value_int64(dummy_value++)));
die("chan_set failed\n"); OK(chan_flush(c));
if (chan_flush(c) != 0)
die("chan_flush failed\n");
} }
static double static double
@ -62,14 +60,14 @@ measure_hash(struct bay *bay, double T)
sprintf(name, "%s.%ld", BASE, i); sprintf(name, "%s.%ld", BASE, i);
struct chan *c = bay_find(bay, name); struct chan *c = bay_find(bay, name);
if (c == NULL) if (c == NULL)
die("bay_find failed\n"); die("bay_find failed");
dummy_work(c); dummy_work(c);
nlookups++; nlookups++;
} }
double speed = (double) nlookups / T; double speed = (double) nlookups / T;
err("bay_find: %e lookups/s\n", speed); err("%e lookups/s", speed);
return speed; return speed;
} }
@ -90,7 +88,7 @@ measure_direct(double T)
double speed = (double) nlookups / T; double speed = (double) nlookups / T;
err("direct: %e lookups/s\n", speed); err("%e lookups/s", speed);
return speed; return speed;
} }
@ -101,10 +99,10 @@ test_speed(struct bay *bay)
double hash = measure_hash(bay, T); double hash = measure_hash(bay, T);
double direct = measure_direct(T); double direct = measure_direct(T);
double slowdown = hash / direct; double slowdown = hash / direct;
err("slowdown speed_hash/speed_direct = %f\n", slowdown); err("slowdown speed_hash/speed_direct = %f", slowdown);
if (slowdown < 0.2) if (slowdown < 0.2)
die("hash speed is too slow\n"); die("hash speed is too slow");
} }
int main(void) int main(void)

View File

@ -3,6 +3,7 @@
#include "emu/bay.h" #include "emu/bay.h"
#include "common.h" #include "common.h"
#include "unittest.h"
static void static void
test_duplicate(struct bay *bay) test_duplicate(struct bay *bay)
@ -10,22 +11,18 @@ test_duplicate(struct bay *bay)
struct chan chan; struct chan chan;
chan_init(&chan, CHAN_SINGLE, "dup"); chan_init(&chan, CHAN_SINGLE, "dup");
if (bay_register(bay, &chan) != 0) OK(bay_register(bay, &chan));
die("bay_register failed\n"); ERR(bay_register(bay, &chan));
if (bay_register(bay, &chan) == 0)
die("bay_register didn't fail\n");
} }
static int static int
callback(struct chan *chan, void *ptr) callback(struct chan *chan, void *ptr)
{ {
struct value value; struct value value;
if (chan_read(chan, &value) != 0) OK(chan_read(chan, &value));
die("callback: chan_read failed\n");
if (value.type != VALUE_INT64) if (value.type != VALUE_INT64)
die("callback: unexpected value type\n"); die("unexpected value type");
int64_t *ival = ptr; int64_t *ival = ptr;
*ival = value.i; *ival = value.i;
@ -39,28 +36,25 @@ test_callback(struct bay *bay)
struct chan chan; struct chan chan;
chan_init(&chan, CHAN_SINGLE, "testchan"); chan_init(&chan, CHAN_SINGLE, "testchan");
if (bay_register(bay, &chan) != 0) OK(bay_register(bay, &chan));
die("bay_register failed\n");
int64_t data = 0; int64_t data = 0;
if (bay_add_cb(bay, BAY_CB_DIRTY, &chan, callback, &data, 1) == NULL) if (bay_add_cb(bay, BAY_CB_DIRTY, &chan, callback, &data, 1) == NULL)
die("bay_add_cb failed\n"); die("bay_add_cb failed");
if (data != 0) if (data != 0)
die("data changed after bay_chan_append_cb\n"); die("data changed after bay_chan_append_cb");
if (chan_set(&chan, value_int64(1)) != 0) OK(chan_set(&chan, value_int64(1)));
die("chan_set failed\n");
if (data != 0) if (data != 0)
die("data changed after chan_set\n"); die("data changed after chan_set");
/* Now the callback should modify 'data' */ /* Now the callback should modify 'data' */
if (bay_propagate(bay) != 0) OK(bay_propagate(bay));
die("bay_propagate failed\n");
if (data != 1) if (data != 1)
die("data didn't change after bay_propagate\n"); die("data didn't change after bay_propagate");
} }
int main(void) int main(void)

View File

@ -3,6 +3,7 @@
#include "emu/chan.h" #include "emu/chan.h"
#include "common.h" #include "common.h"
#include "unittest.h"
static void static void
test_dirty(void) test_dirty(void)
@ -10,37 +11,30 @@ test_dirty(void)
struct chan chan; struct chan chan;
chan_init(&chan, CHAN_SINGLE, "testchan"); chan_init(&chan, CHAN_SINGLE, "testchan");
if (chan_set(&chan, value_int64(1)) != 0) OK(chan_set(&chan, value_int64(1)));
die("chan_set failed\n");
/* Now channel is dirty */ /* Now channel is dirty */
if (chan_set(&chan, value_int64(2)) == 0) ERR(chan_set(&chan, value_int64(2)));
die("chan_set didn't fail\n");
if (chan_flush(&chan) != 0) OK(chan_flush(&chan));
die("chan_flush failed\n");
chan_prop_set(&chan, CHAN_DIRTY_WRITE, 1); chan_prop_set(&chan, CHAN_DIRTY_WRITE, 1);
if (chan_set(&chan, value_int64(3)) != 0) OK(chan_set(&chan, value_int64(3)));
die("chan_set failed\n");
/* Now is dirty, but it should allow multiple writes */ /* Now is dirty, but it should allow multiple writes */
if (chan_set(&chan, value_int64(4)) != 0) OK(chan_set(&chan, value_int64(4)));
die("chan_set failed\n");
struct value value; struct value value;
struct value four = value_int64(4); struct value four = value_int64(4);
if (chan_read(&chan, &value) != 0) OK(chan_read(&chan, &value));
die("chan_read failed\n");
if (!value_is_equal(&value, &four)) if (!value_is_equal(&value, &four))
die("chan_read returned unexpected value\n"); die("chan_read returned unexpected value");
if (chan_flush(&chan) != 0) OK(chan_flush(&chan));
die("chan_flush failed\n");
err("OK"); err("OK");
} }
@ -51,29 +45,24 @@ test_single(void)
struct chan chan; struct chan chan;
struct value one = { .type = VALUE_INT64, .i = 1 }; struct value one = { .type = VALUE_INT64, .i = 1 };
struct value two = { .type = VALUE_INT64, .i = 2 }; struct value two = { .type = VALUE_INT64, .i = 2 };
//struct value nil = { .type = VALUE_NULL, .i = 0 };
chan_init(&chan, CHAN_SINGLE, "testchan"); chan_init(&chan, CHAN_SINGLE, "testchan");
/* Ensure we cannot push as stack */ /* Ensure we cannot push as stack */
if (chan_push(&chan, one) == 0) ERR(chan_push(&chan, one));
die("chan_push didn't fail\n");
/* Now we should be able to write with set */ /* Now we should be able to write with set */
if (chan_set(&chan, one) != 0) OK(chan_set(&chan, one));
die("chan_set failed\n");
/* Now is dirty, it shouldn't allow another set */ /* Now is dirty, it shouldn't allow another set */
if (chan_set(&chan, two) == 0) ERR(chan_set(&chan, two));
die("chan_set didn't fail\n");
struct value value; struct value value;
if (chan_read(&chan, &value) != 0) OK(chan_read(&chan, &value));
die("chan_read failed\n");
if (!value_is_equal(&value, &one)) if (!value_is_equal(&value, &one))
die("chan_read returned unexpected value\n"); die("chan_read returned unexpected value");
err("OK"); err("OK");
} }
@ -86,22 +75,17 @@ test_allow_dup(void)
struct chan chan; struct chan chan;
chan_init(&chan, CHAN_SINGLE, "testchan"); chan_init(&chan, CHAN_SINGLE, "testchan");
if (chan_set(&chan, value_int64(1)) != 0) OK(chan_set(&chan, value_int64(1)));
die("chan_set failed\n"); OK(chan_flush(&chan));
if (chan_flush(&chan) != 0)
die("chan_flush failed\n");
/* Attempt to write the same value again */ /* Attempt to write the same value again */
if (chan_set(&chan, value_int64(1)) == 0) ERR(chan_set(&chan, value_int64(1)));
die("chan_set didn't fail\n");
/* Allow duplicates */ /* Allow duplicates */
chan_prop_set(&chan, CHAN_ALLOW_DUP, 1); chan_prop_set(&chan, CHAN_ALLOW_DUP, 1);
/* Then it should allow writting the same value */ /* Then it should allow writting the same value */
if (chan_set(&chan, value_int64(1)) != 0) OK(chan_set(&chan, value_int64(1)));
die("chan_set failed\n");
/* Also ensure the channel is dirty */ /* Also ensure the channel is dirty */
if (!chan.is_dirty) if (!chan.is_dirty)
@ -119,15 +103,12 @@ test_ignore_dup(void)
chan_init(&chan, CHAN_SINGLE, "testchan"); chan_init(&chan, CHAN_SINGLE, "testchan");
chan_prop_set(&chan, CHAN_IGNORE_DUP, 1); chan_prop_set(&chan, CHAN_IGNORE_DUP, 1);
if (chan_set(&chan, value_int64(1)) != 0) OK(chan_set(&chan, value_int64(1)));
die("chan_set failed\n");
if (chan_flush(&chan) != 0) OK(chan_flush(&chan));
die("chan_flush failed\n");
/* Write the same value */ /* Write the same value */
if (chan_set(&chan, value_int64(1)) != 0) OK(chan_set(&chan, value_int64(1)));
die("chan_set failed");
/* And ensure the channel is not dirty */ /* And ensure the channel is not dirty */
if (chan.is_dirty) if (chan.is_dirty)
@ -136,69 +117,12 @@ test_ignore_dup(void)
err("OK"); err("OK");
} }
//static void
//test_stack(void)
//{
// struct chan chan;
//
// chan_init(&chan, CHAN_STACK);
//
// /* Ensure we cannot set as single */
// if (chan_set(&chan, 1) == 0)
// die("chan_set didn't fail\n");
//
// /* Channels are closed after init */
// if (chan_push(&chan, 1) != 0)
// die("chan_push failed\n");
//
// /* Now is closed, it shouldn't allow another value */
// if (chan_push(&chan, 2) == 0)
// die("chan_push didn't fail\n");
//
// struct chan_value value = { 0 };
//
// if (chan_flush(&chan, &value) != 0)
// die("chan_flush failed\n");
//
// if (!value.ok || value.i != 1)
// die("chan_flush returned unexpected value\n");
//
// /* Now it should allow to push another value */
// if (chan_push(&chan, 2) != 0)
// die("chan_push failed\n");
//
// if (chan_flush(&chan, &value) != 0)
// die("chan_flush failed\n");
//
// if (!value.ok || value.i != 2)
// die("chan_flush returned unexpected value\n");
//
// /* Now pop the values */
// if (chan_pop(&chan, 2) != 0)
// die("chan_pop failed\n");
//
// if (chan_flush(&chan, &value) != 0)
// die("chan_flush failed\n");
//
// if (!value.ok || value.i != 1)
// die("chan_flush returned unexpected value\n");
//
// if (chan_pop(&chan, 1) != 0)
// die("chan_pop failed\n");
//
// /* Now the stack should be empty */
//
// if (chan_pop(&chan, 1) == 0)
// die("chan_pop didn't fail\n");
//
//}
int main(void) int main(void)
{ {
test_single(); test_single();
test_dirty(); test_dirty();
test_allow_dup(); test_allow_dup();
test_ignore_dup(); test_ignore_dup();
//test_stack();
return 0; return 0;
} }

View File

@ -5,6 +5,7 @@
#include "emu/clkoff.h" #include "emu/clkoff.h"
#include "common.h" #include "common.h"
#include "unittest.h"
#include <stdio.h> #include <stdio.h>
static int static int
@ -20,22 +21,21 @@ test_ok(void)
FILE *f = fmemopen(table_str, ARRAYLEN(table_str), "r"); FILE *f = fmemopen(table_str, ARRAYLEN(table_str), "r");
if (f == NULL) if (f == NULL)
die("fmemopen failed\n"); die("fmemopen failed:");
struct clkoff table; struct clkoff table;
clkoff_init(&table); clkoff_init(&table);
if (clkoff_load(&table, f) != 0) OK(clkoff_load(&table, f));
die("clkoff_load failed\n");
if (clkoff_count(&table) != 4) if (clkoff_count(&table) != 4)
die("clkoff_count failed\n"); die("clkoff_count failed");
struct clkoff_entry *entry = clkoff_get(&table, 3); struct clkoff_entry *entry = clkoff_get(&table, 3);
if (entry == NULL) if (entry == NULL)
die("clkoff_get returned NULL\n"); die("clkoff_get returned NULL");
if (entry->index != 3) if (entry->index != 3)
die("clkoff_get returned wrong index\n"); die("clkoff_get returned wrong index");
fclose(f); fclose(f);
@ -55,13 +55,12 @@ test_dup(void)
FILE *f = fmemopen(table_str, ARRAYLEN(table_str), "r"); FILE *f = fmemopen(table_str, ARRAYLEN(table_str), "r");
if (f == NULL) if (f == NULL)
die("fmemopen failed\n"); die("fmemopen failed:");
struct clkoff table; struct clkoff table;
clkoff_init(&table); clkoff_init(&table);
if (clkoff_load(&table, f) == 0) ERR(clkoff_load(&table, f));
die("clkoff_load didn't fail\n");
fclose(f); fclose(f);

View File

@ -3,6 +3,7 @@
#include "emu/mux.h" #include "emu/mux.h"
#include "common.h" #include "common.h"
#include "unittest.h"
#define N 10 #define N 10
@ -10,28 +11,22 @@ static void
check_output(struct mux *mux, struct value expected) check_output(struct mux *mux, struct value expected)
{ {
struct value out_value = value_null(); struct value out_value = value_null();
if (chan_read(mux->output, &out_value) != 0) OK(chan_read(mux->output, &out_value));
die("chan_read() failed for output channel\n");
if (!value_is_equal(&out_value, &expected)) { if (!value_is_equal(&out_value, &expected)) {
die("unexpected value found %s in output (expected %s)\n", die("unexpected value found %s in output (expected %s)",
value_str(out_value), value_str(out_value),
value_str(expected)); value_str(expected));
} }
err("----- output ok -----\n");
} }
static void static void
test_select(struct mux *mux, int key) test_select(struct mux *mux, int key)
{ {
if (chan_set(mux->select, value_int64(key)) != 0) OK(chan_set(mux->select, value_int64(key)));
die("chan_set failed\n"); OK(bay_propagate(mux->bay));
if (bay_propagate(mux->bay) != 0)
die("bay_propagate failed\n");
check_output(mux, value_int64(1000 + key)); check_output(mux, value_int64(1000 + key));
err("OK");
} }
static void static void
@ -45,15 +40,12 @@ test_input(struct mux *mux, int key)
/* Then change that channel */ /* Then change that channel */
struct mux_input *mi = mux_get_input(mux, key); struct mux_input *mi = mux_get_input(mux, key);
if (mi == NULL) if (mi == NULL)
die("mux_get_input failed to locate input %d\n", key); die("mux_get_input failed to locate input %d", key);
if (chan_set(mi->chan, value_int64(new_value)) != 0)
die("chan_set failed\n");
if (bay_propagate(mux->bay) != 0)
die("bay_propagate failed\n");
OK(chan_set(mi->chan, value_int64(new_value)));
OK(bay_propagate(mux->bay));
check_output(mux, value_int64(new_value)); check_output(mux, value_int64(new_value));
err("OK");
} }
static void static void
@ -61,24 +53,22 @@ test_select_and_input(struct mux *mux, int key)
{ {
/* Set the select channel to the selected key, but don't /* Set the select channel to the selected key, but don't
* propagate the changes yet */ * propagate the changes yet */
if (chan_set(mux->select, value_int64(key)) != 0) OK(chan_set(mux->select, value_int64(key)));
die("chan_set failed\n");
int new_value = 2000 + key; int new_value = 2000 + key;
/* Also change that channel */ /* Also change that channel */
struct mux_input *mi = mux_get_input(mux, key); struct mux_input *mi = mux_get_input(mux, key);
if (mi == NULL) if (mi == NULL)
die("mux_get_input failed to locate input %d\n", key); die("mux_get_input failed to locate input %d", key);
if (chan_set(mi->chan, value_int64(new_value)) != 0) OK(chan_set(mi->chan, value_int64(new_value)));
die("chan_set failed\n");
/* Write twice to the output */ /* Write twice to the output */
if (bay_propagate(mux->bay) != 0) OK(bay_propagate(mux->bay));
die("bay_propagate failed\n");
check_output(mux, value_int64(new_value)); check_output(mux, value_int64(new_value));
err("OK");
} }
static void static void
@ -89,20 +79,18 @@ test_input_and_select(struct mux *mux, int key)
/* First change the input */ /* First change the input */
struct mux_input *mi = mux_get_input(mux, key); struct mux_input *mi = mux_get_input(mux, key);
if (mi == NULL) if (mi == NULL)
die("mux_get_input failed to locate input %d\n", key); die("mux_get_input failed to locate input %d", key);
if (chan_set(mi->chan, value_int64(new_value)) != 0) OK(chan_set(mi->chan, value_int64(new_value)));
die("chan_set failed\n");
/* Then change select */ /* Then change select */
if (chan_set(mux->select, value_int64(key)) != 0) OK(chan_set(mux->select, value_int64(key)));
die("chan_set failed\n");
/* Write twice to the output */ /* Write twice to the output */
if (bay_propagate(mux->bay) != 0) OK(bay_propagate(mux->bay));
die("bay_propagate failed\n");
check_output(mux, value_int64(new_value)); check_output(mux, value_int64(new_value));
err("OK");
} }
static void static void
@ -112,21 +100,15 @@ test_mid_propagate(struct mux *mux, int key)
struct mux_input *mi = mux_get_input(mux, key); struct mux_input *mi = mux_get_input(mux, key);
if (mi == NULL) if (mi == NULL)
die("mux_get_input failed to locate input %d\n", key); die("mux_get_input failed to locate input %d", key);
if (chan_set(mi->chan, value_int64(new_value)) != 0) OK(chan_set(mi->chan, value_int64(new_value)));
die("chan_set failed\n"); OK(bay_propagate(mux->bay));
OK(chan_set(mux->select, value_int64(key)));
if (bay_propagate(mux->bay) != 0) OK(bay_propagate(mux->bay));
die("bay_propagate failed\n");
if (chan_set(mux->select, value_int64(key)) != 0)
die("chan_set failed\n");
if (bay_propagate(mux->bay) != 0)
die("bay_propagate failed\n");
check_output(mux, value_int64(new_value)); check_output(mux, value_int64(new_value));
err("OK");
} }
static void static void
@ -136,35 +118,27 @@ test_duplicate_output(struct mux *mux, int key1, int key2)
struct mux_input *in1 = mux_get_input(mux, key1); struct mux_input *in1 = mux_get_input(mux, key1);
if (in1 == NULL) if (in1 == NULL)
die("mux_get_input failed to locate input1 %d\n", key1); die("mux_get_input failed to locate input1 %d", key1);
struct mux_input *in2 = mux_get_input(mux, key2); struct mux_input *in2 = mux_get_input(mux, key2);
if (in2 == NULL) if (in2 == NULL)
die("mux_get_input failed to locate input2 %d\n", key2); die("mux_get_input failed to locate input2 %d", key2);
if (chan_set(in1->chan, value_int64(new_value)) != 0) OK(chan_set(in1->chan, value_int64(new_value)));
die("chan_set failed\n"); OK(chan_set(in2->chan, value_int64(new_value)));
if (chan_set(in2->chan, value_int64(new_value)) != 0)
die("chan_set failed\n");
/* Select input 1 */ /* Select input 1 */
if (chan_set(mux->select, value_int64(key1)) != 0) OK(chan_set(mux->select, value_int64(key1)));
die("chan_set failed\n"); OK(bay_propagate(mux->bay));
if (bay_propagate(mux->bay) != 0)
die("bay_propagate failed\n");
check_output(mux, value_int64(new_value)); check_output(mux, value_int64(new_value));
/* Now switch to input 2, which has the same value */ /* Now switch to input 2, which has the same value */
if (chan_set(mux->select, value_int64(key2)) != 0) OK(chan_set(mux->select, value_int64(key2)));
die("chan_set failed\n"); OK(bay_propagate(mux->bay));
if (bay_propagate(mux->bay) != 0)
die("bay_propagate failed\n");
check_output(mux, value_int64(new_value)); check_output(mux, value_int64(new_value));
err("OK");
} }
/* Ensure that the output of a mux is correct while the mux is connected /* Ensure that the output of a mux is correct while the mux is connected
@ -183,42 +157,31 @@ test_delayed_connect(void)
chan_init(&input, CHAN_SINGLE, "input.0"); chan_init(&input, CHAN_SINGLE, "input.0");
/* Register all channels in the bay */ /* Register all channels in the bay */
if (bay_register(&bay, &select) != 0) OK(bay_register(&bay, &select));
die("bay_register failed\n"); OK(bay_register(&bay, &output));
OK(bay_register(&bay, &input));
if (bay_register(&bay, &output) != 0)
die("bay_register failed\n");
if (bay_register(&bay, &input) != 0)
die("bay_register failed\n");
/* Setup channel values */ /* Setup channel values */
if (chan_set(&select, value_int64(0)) != 0) OK(chan_set(&select, value_int64(0)));
die("chan_set failed\n"); OK(chan_set(&input, value_int64(1000)));
if (chan_set(&input, value_int64(1000)) != 0)
die("chan_set failed\n");
/* Propagate now so they are clean */ /* Propagate now so they are clean */
if (bay_propagate(&bay) != 0) OK(bay_propagate(&bay));
die("bay_propagate failed\n");
/* ----- delayed connect ----- */ /* ----- delayed connect ----- */
struct mux mux; struct mux mux;
if (mux_init(&mux, &bay, &select, &output, NULL, 1) != 0) OK(mux_init(&mux, &bay, &select, &output, NULL, 1));
die("mux_init failed\n"); OK(mux_set_input(&mux, 0, &input));
if (mux_set_input(&mux, 0, &input) != 0)
die("mux_add_input failed\n");
/* Don't modify the input of the select until propagation, the /* Don't modify the input of the select until propagation, the
* mux_init must have marked the select as dirty. */ * mux_init must have marked the select as dirty. */
if (bay_propagate(&bay) != 0) OK(bay_propagate(&bay));
die("bay_propagate failed\n");
/* The mux must have selected the first input */ /* The mux must have selected the first input */
check_output(&mux, value_int64(1000)); check_output(&mux, value_int64(1000));
err("OK");
} }
int int
@ -241,39 +204,26 @@ main(void)
} }
/* Register all channels in the bay */ /* Register all channels in the bay */
if (bay_register(&bay, &select) != 0) OK(bay_register(&bay, &select));
die("bay_register failed\n");
for (int i = 0; i < N; i++) { for (int i = 0; i < N; i++)
if (bay_register(&bay, &inputs[i]) != 0) OK(bay_register(&bay, &inputs[i]));
die("bay_register failed\n");
}
struct mux mux; struct mux mux;
/* Attempt to init the mux without registering the output */ /* Attempt to init the mux without registering the output */
if (mux_init(&mux, &bay, &select, &output, NULL, N) == 0) ERR(mux_init(&mux, &bay, &select, &output, NULL, N));
die("mux_init didn't fail\n"); OK(bay_register(&bay, &output));
OK(mux_init(&mux, &bay, &select, &output, NULL, N));
if (bay_register(&bay, &output) != 0) for (int i = 0; i < N; i++)
die("bay_register failed\n"); OK(mux_set_input(&mux, i, &inputs[i]));
if (mux_init(&mux, &bay, &select, &output, NULL, N) != 0)
die("mux_init failed\n");
for (int i = 0; i < N; i++) {
if (mux_set_input(&mux, i, &inputs[i]) != 0)
die("mux_add_input failed\n");
}
/* Write something to the input channels */ /* Write something to the input channels */
for (int i = 0; i < N; i++) { for (int i = 0; i < N; i++)
if (chan_set(&inputs[i], value_int64(1000 + i)) != 0) OK(chan_set(&inputs[i], value_int64(1000 + i)));
die("chan_set failed\n");
}
/* Propagate values and call the callbacks */ /* Propagate values and call the callbacks */
if (bay_propagate(&bay) != 0) OK(bay_propagate(&bay));
die("bay_propagate failed\n");
test_select(&mux, 1); test_select(&mux, 1);
test_input(&mux, 2); test_input(&mux, 2);
@ -283,7 +233,7 @@ main(void)
test_duplicate_output(&mux, 6, 7); test_duplicate_output(&mux, 6, 7);
test_delayed_connect(); test_delayed_connect();
err("OK\n"); err("OK");
return 0; return 0;
} }

View File

@ -16,14 +16,14 @@ test_underflow_trailing(void)
if (memcmp(in, out, sizeof(in)) != 0) { if (memcmp(in, out, sizeof(in)) != 0) {
for (size_t i = 0; i < sizeof(in); i++) { for (size_t i = 0; i < sizeof(in); i++) {
err("i=%3d, in[i]=%02x out[i]=%02x\n", i, err("i=%3d, in[i]=%02x out[i]=%02x", i,
(unsigned char) in[i], (unsigned char) in[i],
(unsigned char) out[i]); (unsigned char) out[i]);
} }
die("path mismatch"); die("path mismatch");
} }
err("OK\n"); err("OK");
} }
int int

View File

@ -31,22 +31,18 @@ test_emit(const char *path)
sprintf(buf, "testchan.%d", i); sprintf(buf, "testchan.%d", i);
chan_init(&chan[i], CHAN_SINGLE, buf); chan_init(&chan[i], CHAN_SINGLE, buf);
if (bay_register(&bay, &chan[i]) != 0) OK(bay_register(&bay, &chan[i]));
die("bay_register failed\n");
} }
for (int i = 0; i < NROWS; i++) for (int i = 0; i < NROWS; i++)
if (prv_register(&prv, i, type, &bay, &chan[i], 0) != 0) OK(prv_register(&prv, i, type, &bay, &chan[i], 0));
die("prv_register failed\n");
for (int i = 0; i < NROWS; i++) for (int i = 0; i < NROWS; i++)
if (chan_set(&chan[i], value_int64(value_base + i)) != 0) OK(chan_set(&chan[i], value_int64(value_base + i)));
die("chan_set failed\n");
OK(prv_advance(&prv, 10000)); OK(prv_advance(&prv, 10000));
if (bay_propagate(&bay) != 0) OK(bay_propagate(&bay));
die("bay_propagate failed\n");
/* Ensure all writes are flushed to the buffer and /* Ensure all writes are flushed to the buffer and
* the header has been fixed */ * the header has been fixed */
@ -59,7 +55,7 @@ test_emit(const char *path)
} }
fclose(f); fclose(f);
err("test emit OK\n"); err("OK");
} }
static void static void
@ -82,29 +78,18 @@ test_duplicate(const char *path)
/* Allow setting the same value in the channel */ /* Allow setting the same value in the channel */
chan_prop_set(&chan, CHAN_ALLOW_DUP, 1); chan_prop_set(&chan, CHAN_ALLOW_DUP, 1);
if (bay_register(&bay, &chan) != 0) OK(bay_register(&bay, &chan));
die("bay_register failed\n"); OK(prv_register(&prv, 0, type, &bay, &chan, 0));
OK(chan_set(&chan, value_int64(1000)));
if (prv_register(&prv, 0, type, &bay, &chan, 0) != 0) OK(prv_advance(&prv, 10000));
die("prv_register failed\n"); OK(bay_propagate(&bay));
if (chan_set(&chan, value_int64(1000)) != 0)
die("chan_set failed\n");
if (prv_advance(&prv, 10000) != 0)
die("prv_advance failed\n");
if (bay_propagate(&bay) != 0)
die("bay_propagate failed\n");
/* Set the same value again, which shouldn't fail */ /* Set the same value again, which shouldn't fail */
if (chan_set(&chan, value_int64(1000)) != 0) OK(chan_set(&chan, value_int64(1000)));
die("chan_set failed\n");
/* Now the propagation phase must fail, as we cannot write the same /* Now the propagation phase must fail, as we cannot write the same
* value in the prv trace */ * value in the prv trace */
if (bay_propagate(&bay) == 0) ERR(bay_propagate(&bay));
die("bay_propagate didn't fail\n");
/* Ensure all writes are flushed to the buffer and /* Ensure all writes are flushed to the buffer and
* the header has been fixed */ * the header has been fixed */
@ -136,7 +121,7 @@ test_same_type(const char *path)
OK(prv_register(&prv, row, type, &bay, &chan, 0)); OK(prv_register(&prv, row, type, &bay, &chan, 0));
ERR(prv_register(&prv, row, type, &bay, &chan, 0)); ERR(prv_register(&prv, row, type, &bay, &chan, 0));
err("ok"); err("OK");
} }
int main(void) int main(void)
@ -145,7 +130,7 @@ int main(void)
char fname[] = "/tmp/ovni.prv.XXXXXX"; char fname[] = "/tmp/ovni.prv.XXXXXX";
int fd = mkstemp(fname); int fd = mkstemp(fname);
if (fd < 0) if (fd < 0)
die("mkstemp failed\n"); die("mkstemp failed:");
test_emit(fname); test_emit(fname);
test_duplicate(fname); test_duplicate(fname);

View File

@ -5,6 +5,7 @@
#include "emu/emu.h" #include "emu/emu.h"
#include "common.h" #include "common.h"
#include "unittest.h"
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
@ -15,7 +16,7 @@ test_ok(char *fname)
FILE *f = fopen(fname, "w"); FILE *f = fopen(fname, "w");
if (f == NULL) if (f == NULL)
die("fopen failed\n"); die("fopen failed:");
/* Write bogus header */ /* Write bogus header */
struct ovni_stream_header header; struct ovni_stream_header header;
@ -23,17 +24,18 @@ test_ok(char *fname)
header.version = OVNI_STREAM_VERSION; header.version = OVNI_STREAM_VERSION;
if (fwrite(&header, sizeof(header), 1, f) != 1) if (fwrite(&header, sizeof(header), 1, f) != 1)
die("fwrite failed\n"); die("fwrite failed:");
fclose(f); fclose(f);
struct stream stream; struct stream stream;
const char *relpath = &fname[5]; const char *relpath = &fname[5];
if (stream_load(&stream, "/tmp", relpath) != 0) OK(stream_load(&stream, "/tmp", relpath));
die("stream_load failed");
if (stream.active) if (stream.active)
die("stream is active\n"); die("stream is active");
err("OK");
} }
static void static void
@ -42,7 +44,7 @@ test_bad(char *fname)
FILE *f = fopen(fname, "w"); FILE *f = fopen(fname, "w");
if (f == NULL) if (f == NULL)
die("fopen failed\n"); die("fopen failed:");
/* Write bogus header */ /* Write bogus header */
struct ovni_stream_header header; struct ovni_stream_header header;
@ -50,14 +52,15 @@ test_bad(char *fname)
header.version = 1234; /* Wrong version */ header.version = 1234; /* Wrong version */
if (fwrite(&header, sizeof(header), 1, f) != 1) if (fwrite(&header, sizeof(header), 1, f) != 1)
die("fwrite failed\n"); die("fwrite failed:");
fclose(f); fclose(f);
struct stream stream; struct stream stream;
const char *relpath = &fname[5]; const char *relpath = &fname[5];
if (stream_load(&stream, "/tmp", relpath) == 0) ERR(stream_load(&stream, "/tmp", relpath));
die("stream_load didn't fail");
err("OK");
} }
int main(void) int main(void)
@ -66,11 +69,12 @@ int main(void)
char fname[] = "/tmp/ovni.stream.XXXXXX"; char fname[] = "/tmp/ovni.stream.XXXXXX";
int fd = mkstemp(fname); int fd = mkstemp(fname);
if (fd < 0) if (fd < 0)
die("mkstemp failed\n"); die("mkstemp failed:");
test_ok(fname); test_ok(fname);
test_bad(fname); test_bad(fname);
close(fd); close(fd);
return 0;
} }

View File

@ -19,9 +19,9 @@ test_holes(void)
* compare two values, so we don't have problems with * compare two values, so we don't have problems with
* unitialized holes due to alignment */ * unitialized holes due to alignment */
if (memcmp(&a, &b, sizeof(struct value)) != 0) if (memcmp(&a, &b, sizeof(struct value)) != 0)
die("values are not the same\n"); die("values are not the same");
err("OK\n"); err("OK");
} }
/* Ensure value_null results in values being equal */ /* Ensure value_null results in values being equal */
@ -39,7 +39,7 @@ test_null_init(void)
if (!value_is_equal(&a, &b)) if (!value_is_equal(&a, &b))
die("null not equal"); die("null not equal");
err("OK\n"); err("OK");
} }
/* Test that we can printf at least 8 values without overwritting the /* Test that we can printf at least 8 values without overwritting the

View File

@ -34,14 +34,14 @@ int main(void)
int tuple[3] = { 0 }; int tuple[3] = { 0 };
if (version_parse(c->version, tuple) != c->rc) if (version_parse(c->version, tuple) != c->rc)
die("wrong return value\n"); die("wrong return value");
if (c->rc != 0) if (c->rc != 0)
continue; continue;
for (int j = 0; j < 3; j++) { for (int j = 0; j < 3; j++) {
if (tuple[j] != c->tuple[j]) if (tuple[j] != c->tuple[j])
die("wrong parsed version\n"); die("wrong parsed version");
} }
} }