From 23abfcd7e696fa622e11f3eb0a3af3f6b9adb5cd Mon Sep 17 00:00:00 2001
From: Steffen Vogel <post@steffenvogel.de>
Date: Thu, 23 Aug 2018 13:32:44 +0200
Subject: [PATCH] tests: ported unit tests to C++

---
 tests/unit/CMakeLists.txt                     | 18 +++++-----
 tests/unit/{config_json.c => config_json.cpp} |  0
 tests/unit/{io.c => io.cpp}                   | 25 ++++++++-----
 tests/unit/{json.c => json.cpp}               |  2 +-
 tests/unit/{main.c => main.cpp}               |  2 +-
 tests/unit/{mapping.c => mapping.cpp}         | 12 +++----
 tests/unit/{memory.c => memory.cpp}           |  0
 tests/unit/{pool.c => pool.cpp}               |  0
 tests/unit/{queue.c => queue.cpp}             | 36 +++++++++----------
 ...{queue_signalled.c => queue_signalled.cpp} |  9 ++---
 10 files changed, 56 insertions(+), 48 deletions(-)
 rename tests/unit/{config_json.c => config_json.cpp} (100%)
 rename tests/unit/{io.c => io.cpp} (95%)
 rename tests/unit/{json.c => json.cpp} (99%)
 rename tests/unit/{main.c => main.cpp} (97%)
 rename tests/unit/{mapping.c => mapping.cpp} (94%)
 rename tests/unit/{memory.c => memory.cpp} (100%)
 rename tests/unit/{pool.c => pool.cpp} (100%)
 rename tests/unit/{queue.c => queue.cpp} (92%)
 rename tests/unit/{queue_signalled.c => queue_signalled.cpp} (94%)

diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt
index f52ec9ea0..4dd3ed66a 100644
--- a/tests/unit/CMakeLists.txt
+++ b/tests/unit/CMakeLists.txt
@@ -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})
diff --git a/tests/unit/config_json.c b/tests/unit/config_json.cpp
similarity index 100%
rename from tests/unit/config_json.c
rename to tests/unit/config_json.cpp
diff --git a/tests/unit/io.c b/tests/unit/io.cpp
similarity index 95%
rename from tests/unit/io.c
rename to tests/unit/io.cpp
index 612bd7485..1d3081449 100644
--- a/tests/unit/io.c
+++ b/tests/unit/io.cpp
@@ -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
diff --git a/tests/unit/json.c b/tests/unit/json.cpp
similarity index 99%
rename from tests/unit/json.c
rename to tests/unit/json.cpp
index 3033cea76..3600ca26e 100644
--- a/tests/unit/json.c
+++ b/tests/unit/json.cpp
@@ -28,7 +28,7 @@
 #include <villas/config_helper.h>
 
 struct param {
-	char *argv[32];
+	const char *argv[32];
 	const char *json;
 };
 
diff --git a/tests/unit/main.c b/tests/unit/main.cpp
similarity index 97%
rename from tests/unit/main.c
rename to tests/unit/main.cpp
index c7240b2b7..8286fc877 100644
--- a/tests/unit/main.c
+++ b/tests/unit/main.cpp
@@ -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");
 
diff --git a/tests/unit/mapping.c b/tests/unit/mapping.cpp
similarity index 94%
rename from tests/unit/mapping.c
rename to tests/unit/mapping.cpp
index a981a6691..01d791fbd 100644
--- a/tests/unit/mapping.c
+++ b/tests/unit/mapping.cpp
@@ -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);
diff --git a/tests/unit/memory.c b/tests/unit/memory.cpp
similarity index 100%
rename from tests/unit/memory.c
rename to tests/unit/memory.cpp
diff --git a/tests/unit/pool.c b/tests/unit/pool.cpp
similarity index 100%
rename from tests/unit/pool.c
rename to tests/unit/pool.cpp
diff --git a/tests/unit/queue.c b/tests/unit/queue.cpp
similarity index 92%
rename from tests/unit/queue.c
rename to tests/unit/queue.cpp
index ba26b1b2a..d3f79b47b 100644
--- a/tests/unit/queue.c
+++ b/tests/unit/queue.cpp
@@ -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 */
diff --git a/tests/unit/queue_signalled.c b/tests/unit/queue_signalled.cpp
similarity index 94%
rename from tests/unit/queue_signalled.c
rename to tests/unit/queue_signalled.cpp
index 76b4d6b6e..1b47c9931 100644
--- a/tests/unit/queue_signalled.c
+++ b/tests/unit/queue_signalled.cpp
@@ -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;