1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

tests: ported unit tests to C++

This commit is contained in:
Steffen Vogel 2018-08-23 13:32:44 +02:00
parent 1763102931
commit 23abfcd7e6
10 changed files with 56 additions and 48 deletions

View file

@ -23,15 +23,15 @@
if(CRITERION_FOUND)
set(TEST_SRC
config_json.c
io.c
json.c
main.c
mapping.c
memory.c
pool.c
queue.c
queue_signalled.c
config_json.cpp
io.cpp
json.cpp
main.cpp
mapping.cpp
memory.cpp
pool.cpp
queue.cpp
queue_signalled.cpp
)
add_executable(unit-tests ${TEST_SRC})

View file

@ -72,31 +72,37 @@ void fill_sample_data(struct list *signals, struct sample *smps[], unsigned cnt)
now = time_now();
delta = time_from_double(50e-6);
for (int i = 0; i < cnt; i++) {
for (unsigned i = 0; i < cnt; i++) {
struct sample *smp = smps[i];
smps[i]->flags = SAMPLE_HAS_SEQUENCE | SAMPLE_HAS_DATA | SAMPLE_HAS_TS_ORIGIN;
smps[i]->length = list_length(signals);
smps[i]->sequence = 235 + i;
smps[i]->ts.origin = now;
smps[i]->signals = signals;
for (int j = 0; j < list_length(signals); j++) {
for (size_t j = 0; j < list_length(signals); j++) {
struct signal *sig = (struct signal *) list_at(signals, j);
union signal_data *data = &smp->data[j];
switch (sig->type) {
case SIGNAL_TYPE_BOOLEAN:
smps[i]->data[j].b = j * 0.1 + i * 100;
data->b = j * 0.1 + i * 100;
break;
case SIGNAL_TYPE_COMPLEX:
smps[i]->data[j].z = CMPLXF(j * 0.1, i * 100);
case SIGNAL_TYPE_COMPLEX: {
/** @todo: Port to proper C++ */
std::complex<float> z = { j * 0.1f, i * 100.0f };
memcpy(&data->z, &z, sizeof(data->z));
break;
}
case SIGNAL_TYPE_FLOAT:
smps[i]->data[j].f = j * 0.1 + i * 100;
data->f = j * 0.1 + i * 100;
break;
case SIGNAL_TYPE_INTEGER:
smps[i]->data[j].i = j + i * 1000;
data->i = j + i * 1000;
break;
default: { }
@ -203,7 +209,8 @@ ParameterizedTestParameters(io, lowlevel)
ParameterizedTest(struct param *p, io, lowlevel, .init = init_memory)
{
int ret, cnt;
int ret;
unsigned cnt;
char buf[8192];
size_t wbytes, rbytes;
@ -248,7 +255,7 @@ ParameterizedTest(struct param *p, io, lowlevel, .init = init_memory)
cr_assert_eq(rbytes, wbytes, "rbytes != wbytes: %#zx != %#zx", rbytes, wbytes);
for (int i = 0; i < cnt; i++) {
for (unsigned i = 0; i < cnt; i++) {
if (p->bits)
cr_assert_eq_sample_raw(smps[i], smpt[i], f->flags, p->bits);
else

View file

@ -28,7 +28,7 @@
#include <villas/config_helper.h>
struct param {
char *argv[32];
const char *argv[32];
const char *json;
};

View file

@ -39,7 +39,7 @@ int main(int argc, char *argv[])
struct criterion_test_set *tests;
struct log log;
ret = log_init(&log, 2, LOG_ALL);
ret = log_init(&log, "test_logger", 2, LOG_ALL);
if (ret)
error("Failed to initialize logging sub-system");

View file

@ -34,8 +34,8 @@ Test(mapping, parse_nodes)
struct mapping_entry m;
struct list nodes = { .state = STATE_DESTROYED };
char *node_names[3] = { "apple", "cherry", "carrot" };
char *signal_names[3][4] = {
const char *node_names[3] = { "apple", "cherry", "carrot" };
const char *signal_names[3][4] = {
{ "abra", "kadabra", "simsala", "bimm" },
{ "this", "is", "a", "test" },
{ "o", "sole", "mio", "italia" }
@ -43,15 +43,15 @@ Test(mapping, parse_nodes)
list_init(&nodes);
for (int i = 0; i < ARRAY_LEN(node_names); i++) {
struct node *n = alloc(sizeof(struct node));
for (unsigned i = 0; i < ARRAY_LEN(node_names); i++) {
struct node *n = new struct node;
n->name = node_names[i];
n->name = strdup(node_names[i]);
n->signals.state = STATE_DESTROYED;
list_init(&n->signals);
for (int j = 0; j < ARRAY_LEN(signal_names[i]); j++) {
for (unsigned j = 0; j < ARRAY_LEN(signal_names[i]); j++) {
struct signal *sig;
sig = signal_create(signal_names[i][j], NULL, SIGNAL_TYPE_AUTO);

View file

@ -40,21 +40,21 @@ extern void init_memory();
#define SIZE (1 << 10)
static struct queue q = { .state = STATE_DESTROYED };
static struct queue q = { .state = ATOMIC_VAR_INIT(STATE_DESTROYED) };
#if defined(_POSIX_BARRIERS) && _POSIX_BARRIERS > 0
static pthread_barrier_t barrier;
#endif
struct param {
int volatile start;
int thread_count;
int queue_size;
int iter_count;
int batch_size;
int queue_size;
int thread_count;
bool many;
struct queue queue;
int batch_size;
enum memory_type_flags memory_type;
volatile int start;
struct queue queue;
};
/** Get thread id as integer
@ -84,7 +84,7 @@ __attribute__((always_inline)) static inline void nop()
static void * producer(void *ctx)
{
int ret;
struct param *p = ctx;
struct param *p = (struct param *) ctx;
srand((unsigned) time(0) + thread_get_id());
size_t nops = rand() % 1000;
@ -108,7 +108,7 @@ static void * producer(void *ctx)
//cr_log_info("producer: start pushing");
/* Enqueue */
for (unsigned long count = 0; count < p->iter_count; count++) {
for (intptr_t count = 0; count < p->iter_count; count++) {
do {
ret = queue_push(&p->queue, (void *) count);
pthread_yield();
@ -123,7 +123,7 @@ static void * producer(void *ctx)
static void * consumer(void *ctx)
{
int ret;
struct param *p = ctx;
struct param *p = (struct param *) ctx;
srand((unsigned) time(0) + thread_get_id());
size_t nops = rand() % 1000;
@ -143,11 +143,11 @@ static void * consumer(void *ctx)
//cr_log_info("consumer: start pulling");
/* Dequeue */
for (unsigned long count = 0; count < p->iter_count; count++) {
void *ptr;
for (intptr_t count = 0; count < p->iter_count; count++) {
intptr_t ptr;
do {
ret = queue_pull(&p->queue, &ptr);
ret = queue_pull(&p->queue, (void **) &ptr);
} while (ret != 1);
//cr_log_info("consumer: %lu\n", count);
@ -163,7 +163,7 @@ static void * consumer(void *ctx)
#if defined(_POSIX_BARRIERS) && _POSIX_BARRIERS > 0
void * producer_consumer(void *ctx)
{
struct param *p = ctx;
struct param *p = (struct param *) ctx;
srand((unsigned) time(0) + thread_get_id());
size_t nops = rand() % 1000;
@ -179,13 +179,13 @@ void * producer_consumer(void *ctx)
for (int iter = 0; iter < p->iter_count; ++iter) {
pthread_barrier_wait(&barrier);
for (size_t i = 0; i < p->batch_size; i++) {
for (intptr_t i = 0; i < p->batch_size; i++) {
void *ptr = (void *) (iter * p->batch_size + i);
while (!queue_push(&p->queue, ptr))
pthread_yield(); /* queue full, let other threads proceed */
}
for (size_t i = 0; i < p->batch_size; i++) {
for (intptr_t i = 0; i < p->batch_size; i++) {
void *ptr;
while (!queue_pull(&p->queue, &ptr))
pthread_yield(); /* queue empty, let other threads proceed */
@ -197,7 +197,7 @@ void * producer_consumer(void *ctx)
void * producer_consumer_many(void *ctx)
{
struct param *p = ctx;
struct param *p = (struct param *) ctx;
srand((unsigned) time(0) + thread_get_id());
size_t nops = rand() % 1000;
@ -213,7 +213,7 @@ void * producer_consumer_many(void *ctx)
void *ptrs[p->batch_size];
for (int iter = 0; iter < p->iter_count; ++iter) {
for (size_t i = 0; i < p->batch_size; i++)
for (intptr_t i = 0; i < p->batch_size; i++)
ptrs[i] = (void *) (iter * p->batch_size + i);
pthread_barrier_wait(&barrier);
@ -358,7 +358,7 @@ ParameterizedTest(struct param *p, queue, multi_threaded, .timeout = 20, .init =
Test(queue, init_destroy, .init = init_memory)
{
int ret;
struct queue q = { .state = STATE_DESTROYED };
struct queue q = { .state = ATOMIC_VAR_INIT(STATE_DESTROYED) };
ret = queue_init(&q, 1024, &memory_heap);
cr_assert_eq(ret, 0); /* Should succeed */

View file

@ -42,7 +42,7 @@ struct param {
static void * producer(void * ctx)
{
int ret;
struct queue_signalled *q = ctx;
struct queue_signalled *q = (struct queue_signalled *) ctx;
for (intptr_t i = 0; i < NUM_ELEM; i++) {
ret = queue_signalled_push(q, (void *) i);
@ -58,7 +58,7 @@ static void * producer(void * ctx)
static void * consumer(void * ctx)
{
int ret;
struct queue_signalled *q = ctx;
struct queue_signalled *q = (struct queue_signalled *) ctx;
void *data[NUM_ELEM];
@ -79,7 +79,7 @@ static void * consumer(void * ctx)
void * polled_consumer(void *ctx)
{
int ret, fd;
struct queue_signalled *q = ctx;
struct queue_signalled *q = (struct queue_signalled *) ctx;
fd = queue_signalled_fd(q);
cr_assert_geq(fd, 0);
@ -130,7 +130,8 @@ ParameterizedTest(struct param *param, queue_signalled, simple, .timeout = 5, .i
{
int ret;
void *r1, *r2;
struct queue_signalled q = { .queue.state = STATE_DESTROYED };
struct queue_signalled q;
q.queue.state = STATE_DESTROYED;
pthread_t t1, t2;