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

410 lines
11 KiB
C++
Raw Permalink Normal View History

2017-08-05 21:02:09 +02:00
/** Unit tests for IO formats.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2020-01-20 17:17:00 +01:00
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
2017-08-05 21:02:09 +02:00
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#include <stdio.h>
#include <float.h>
2018-10-20 14:24:51 +02:00
#include <complex>
2017-08-05 21:02:09 +02:00
#include <criterion/criterion.h>
#include <criterion/parameterized.h>
#include <villas/utils.hpp>
2018-03-26 12:50:15 +02:00
#include <villas/timing.h>
#include <villas/sample.h>
2018-08-20 18:32:10 +02:00
#include <villas/signal.h>
2018-03-26 12:50:15 +02:00
#include <villas/plugin.h>
#include <villas/pool.h>
#include <villas/io.h>
2018-10-20 14:24:51 +02:00
#include <villas/log.hpp>
2017-08-05 21:02:09 +02:00
2020-01-26 16:17:59 +01:00
#include "helpers.hpp"
2018-10-20 14:24:51 +02:00
using namespace villas;
2018-08-20 18:32:10 +02:00
extern void init_memory();
2017-08-05 21:02:09 +02:00
#define NUM_VALUES 10
2020-06-16 01:00:57 +02:00
using string = std::basic_string<char, std::char_traits<char>, criterion::allocator<char>>;
2020-01-26 16:17:59 +01:00
2020-06-16 01:00:57 +02:00
struct Param {
2020-01-26 16:17:59 +01:00
public:
2020-06-16 01:00:57 +02:00
Param(const char *f, int c, int b) :
fmt(f), cnt(c), bits(b)
{}
string fmt;
2018-08-20 18:32:10 +02:00
int cnt;
int bits;
};
2019-01-07 10:28:55 +01:00
void fill_sample_data(struct vlist *signals, struct sample *smps[], unsigned cnt)
2017-08-05 21:02:09 +02:00
{
2018-03-26 12:50:30 +02:00
struct timespec delta, now;
2017-08-23 15:45:28 +02:00
2018-03-26 12:50:30 +02:00
now = time_now();
delta = time_from_double(50e-6);
2018-08-23 13:32:44 +02:00
for (unsigned i = 0; i < cnt; i++) {
struct sample *smp = smps[i];
2019-06-23 16:13:23 +02:00
smps[i]->flags = (int) SampleFlags::HAS_SEQUENCE | (int) SampleFlags::HAS_DATA | (int) SampleFlags::HAS_TS_ORIGIN;
2019-01-07 10:28:55 +01:00
smps[i]->length = vlist_length(signals);
2017-08-23 15:45:28 +02:00
smps[i]->sequence = 235 + i;
2018-03-26 12:50:30 +02:00
smps[i]->ts.origin = now;
2018-08-20 18:32:10 +02:00
smps[i]->signals = signals;
2017-08-23 15:45:28 +02:00
2019-01-07 10:28:55 +01:00
for (size_t j = 0; j < vlist_length(signals); j++) {
struct signal *sig = (struct signal *) vlist_at(signals, j);
2018-08-23 13:32:44 +02:00
union signal_data *data = &smp->data[j];
2018-08-20 18:32:10 +02:00
switch (sig->type) {
2019-06-23 16:13:23 +02:00
case SignalType::BOOLEAN:
2018-08-23 13:32:44 +02:00
data->b = j * 0.1 + i * 100;
2018-08-20 18:32:10 +02:00
break;
2019-06-23 16:13:23 +02:00
case SignalType::COMPLEX: {
2018-08-23 13:32:44 +02:00
/** @todo: Port to proper C++ */
std::complex<float> z = { j * 0.1f, i * 100.0f };
memcpy(&data->z, &z, sizeof(data->z));
2018-08-20 18:32:10 +02:00
break;
2018-08-23 13:32:44 +02:00
}
2018-08-20 18:32:10 +02:00
2019-06-23 16:13:23 +02:00
case SignalType::FLOAT:
2018-08-23 13:32:44 +02:00
data->f = j * 0.1 + i * 100;
2018-08-20 18:32:10 +02:00
break;
2019-06-23 16:13:23 +02:00
case SignalType::INTEGER:
2018-08-23 13:32:44 +02:00
data->i = j + i * 1000;
2018-08-20 18:32:10 +02:00
break;
default: { }
}
2018-03-26 12:50:30 +02:00
}
now = time_add(&now, &delta);
}
}
2018-08-20 18:32:10 +02:00
void cr_assert_eq_sample(struct sample *a, struct sample *b, int flags)
2018-03-26 12:50:30 +02:00
{
cr_assert_eq(a->length, b->length);
2019-06-23 16:13:23 +02:00
if (flags & (int) SampleFlags::HAS_SEQUENCE)
2018-08-20 18:32:10 +02:00
cr_assert_eq(a->sequence, b->sequence);
2018-03-26 12:50:30 +02:00
2019-06-23 16:13:23 +02:00
if (flags & (int) SampleFlags::HAS_TS_ORIGIN) {
2018-08-20 18:32:10 +02:00
cr_assert_eq(a->ts.origin.tv_sec, b->ts.origin.tv_sec);
cr_assert_eq(a->ts.origin.tv_nsec, b->ts.origin.tv_nsec);
}
2019-06-23 16:13:23 +02:00
if (flags & (int) SampleFlags::HAS_DATA) {
for (unsigned j = 0; j < MIN(a->length, b->length); j++) {
2018-08-20 18:32:10 +02:00
cr_assert_eq(sample_format(a, j), sample_format(b, j));
switch (sample_format(b, j)) {
2019-06-23 16:13:23 +02:00
case SignalType::FLOAT:
2018-08-20 18:32:10 +02:00
cr_assert_float_eq(a->data[j].f, b->data[j].f, 1e-3, "Sample data mismatch at index %d: %f != %f", j, a->data[j].f, b->data[j].f);
break;
2019-06-23 16:13:23 +02:00
case SignalType::INTEGER:
2018-08-20 18:32:10 +02:00
cr_assert_eq(a->data[j].i, b->data[j].i, "Sample data mismatch at index %d: %lld != %lld", j, a->data[j].i, b->data[j].i);
break;
2018-03-26 12:50:30 +02:00
2019-06-23 16:13:23 +02:00
case SignalType::BOOLEAN:
2018-08-20 18:32:10 +02:00
cr_assert_eq(a->data[j].b, b->data[j].b, "Sample data mismatch at index %d: %s != %s", j, a->data[j].b ? "true" : "false", b->data[j].b ? "true" : "false");
break;
2019-06-23 16:13:23 +02:00
case SignalType::COMPLEX: {
2018-10-20 14:24:51 +02:00
auto ca = * (std::complex<float> *) &a->data[j].z;
auto cb = * (std::complex<float> *) &b->data[j].z;
cr_assert_float_eq(std::abs(ca - cb), 0, 1e-6, "Sample data mismatch at index %d: %f+%fi != %f+%fi", j, ca.real(), ca.imag(), cb.real(), cb.imag());
2018-08-20 18:32:10 +02:00
break;
2018-10-20 14:24:51 +02:00
}
2018-08-20 18:32:10 +02:00
default: { }
}
2018-03-26 12:50:30 +02:00
}
2017-08-23 15:45:28 +02:00
}
}
2018-08-20 18:32:10 +02:00
void cr_assert_eq_sample_raw(struct sample *a, struct sample *b, int flags, int bits)
2017-08-23 15:45:28 +02:00
{
2018-08-20 18:32:10 +02:00
cr_assert_eq(a->length, b->length);
2017-08-23 15:45:28 +02:00
2019-06-23 16:13:23 +02:00
if (flags & (int) SampleFlags::HAS_SEQUENCE)
2018-08-20 18:32:10 +02:00
cr_assert_eq(a->sequence, b->sequence);
2017-08-23 15:45:28 +02:00
2019-06-23 16:13:23 +02:00
if (flags & (int) SampleFlags::HAS_TS_ORIGIN) {
2018-08-20 18:32:10 +02:00
cr_assert_eq(a->ts.origin.tv_sec, b->ts.origin.tv_sec);
cr_assert_eq(a->ts.origin.tv_nsec, b->ts.origin.tv_nsec);
}
2019-06-23 16:13:23 +02:00
if (flags & (int) SampleFlags::HAS_DATA) {
for (unsigned j = 0; j < MIN(a->length, b->length); j++) {
2018-08-20 18:32:10 +02:00
cr_assert_eq(sample_format(a, j), sample_format(b, j));
switch (sample_format(b, j)) {
2019-06-23 16:13:23 +02:00
case SignalType::FLOAT:
2018-08-20 18:32:10 +02:00
if (bits != 8 && bits != 16)
cr_assert_float_eq(a->data[j].f, b->data[j].f, 1e-3, "Sample data mismatch at index %d: %f != %f", j, a->data[j].f, b->data[j].f);
break;
2019-06-23 16:13:23 +02:00
case SignalType::INTEGER:
2018-08-20 18:32:10 +02:00
cr_assert_eq(a->data[j].i, b->data[j].i, "Sample data mismatch at index %d: %lld != %lld", j, a->data[j].i, b->data[j].i);
break;
2019-06-23 16:13:23 +02:00
case SignalType::BOOLEAN:
2018-08-20 18:32:10 +02:00
cr_assert_eq(a->data[j].b, b->data[j].b, "Sample data mismatch at index %d: %s != %s", j, a->data[j].b ? "true" : "false", b->data[j].b ? "true" : "false");
break;
2019-06-23 16:13:23 +02:00
case SignalType::COMPLEX:
2018-10-20 14:24:51 +02:00
if (bits != 8 && bits != 16) {
auto ca = * (std::complex<float> *) &a->data[j].z;
auto cb = * (std::complex<float> *) &b->data[j].z;
cr_assert_float_eq(std::abs(ca - cb), 0, 1e-6, "Sample data mismatch at index %d: %f+%fi != %f+%fi", j, ca.real(), ca.imag(), cb.real(), cb.imag());
}
2018-08-20 18:32:10 +02:00
break;
default: { }
2017-08-23 15:45:28 +02:00
}
}
}
2017-08-05 21:02:09 +02:00
}
2017-08-23 15:45:28 +02:00
ParameterizedTestParameters(io, lowlevel)
2017-08-05 21:02:09 +02:00
{
2020-06-16 01:00:57 +02:00
static criterion::parameters<Param> params;
params.emplace_back("gtnet", 1, 32);
params.emplace_back("gtnet.fake", 1, 32);
params.emplace_back("raw.8", 1, 8);
params.emplace_back("raw.16.be", 1, 16);
params.emplace_back("raw.16.le", 1, 16);
params.emplace_back("raw.32.be", 1, 32);
params.emplace_back("raw.32.le", 1, 32);
params.emplace_back("raw.64.be", 1, 64);
params.emplace_back("raw.64.le", 1, 64);
params.emplace_back("villas.human", 10, 0);
params.emplace_back("villas.binary", 10, 0);
params.emplace_back("csv", 10, 0);
params.emplace_back("json", 10, 0);
#ifdef PROTOBUF_FOUND
params.emplace_back("protobuf", 10, 0 );
#endif
2020-01-26 16:17:59 +01:00
return params;
2017-08-23 15:45:28 +02:00
}
2017-08-05 21:02:09 +02:00
// cppcheck-suppress unknownMacro
2020-01-26 16:17:59 +01:00
ParameterizedTest(Param *p, io, lowlevel, .init = init_memory)
2017-08-23 15:45:28 +02:00
{
2018-08-23 13:32:44 +02:00
int ret;
unsigned cnt;
2017-08-23 15:45:28 +02:00
char buf[8192];
size_t wbytes, rbytes;
2017-09-04 14:30:07 +02:00
2018-10-20 14:24:51 +02:00
Logger logger = logging.get("test:io:lowlevel");
2020-06-16 01:00:57 +02:00
logger->info("Running test for format={}, cnt={}", p->fmt.c_str(), p->cnt);
2018-10-20 14:24:51 +02:00
2018-05-12 13:56:12 +02:00
struct format_type *f;
2017-08-23 15:45:28 +02:00
2020-06-16 02:35:34 +02:00
struct pool pool;
struct io io;
struct vlist signals;
2018-08-20 18:32:10 +02:00
struct sample *smps[p->cnt];
struct sample *smpt[p->cnt];
2017-08-23 15:45:28 +02:00
ret = pool_init(&pool, 2 * p->cnt, SAMPLE_LENGTH(NUM_VALUES));
2017-08-23 15:45:28 +02:00
cr_assert_eq(ret, 0);
ret = vlist_init(&signals);
cr_assert_eq(ret, 0);
2019-06-23 16:13:23 +02:00
signal_list_generate(&signals, NUM_VALUES, SignalType::FLOAT);
2018-08-20 18:32:10 +02:00
ret = sample_alloc_many(&pool, smps, p->cnt);
cr_assert_eq(ret, p->cnt);
2018-03-26 12:50:30 +02:00
2018-08-20 18:32:10 +02:00
ret = sample_alloc_many(&pool, smpt, p->cnt);
cr_assert_eq(ret, p->cnt);
2017-09-04 14:30:07 +02:00
2018-08-20 18:32:10 +02:00
fill_sample_data(&signals, smps, p->cnt);
2017-09-04 14:30:07 +02:00
2020-06-16 01:00:57 +02:00
f = format_type_lookup(p->fmt.c_str());
cr_assert_not_null(f, "Format '%s' does not exist", p->fmt.c_str());
2018-08-20 18:32:10 +02:00
2019-06-23 16:13:23 +02:00
ret = io_init(&io, f, &signals, (int) SampleFlags::HAS_ALL);
2018-05-12 15:25:47 +02:00
cr_assert_eq(ret, 0);
2018-08-20 18:32:10 +02:00
cnt = io_sprint(&io, buf, sizeof(buf), &wbytes, smps, p->cnt);
cr_assert_eq(cnt, p->cnt, "Written only %d of %d samples for format %s", cnt, p->cnt, format_type_name(f));
2017-09-04 14:30:07 +02:00
2018-08-20 18:32:10 +02:00
cnt = io_sscan(&io, buf, wbytes, &rbytes, smpt, p->cnt);
cr_assert_eq(cnt, p->cnt, "Read only %d of %d samples back for format %s", cnt, p->cnt, format_type_name(f));
2017-09-04 14:30:07 +02:00
2018-08-20 18:32:10 +02:00
cr_assert_eq(rbytes, wbytes, "rbytes != wbytes: %#zx != %#zx", rbytes, wbytes);
2017-08-23 15:45:28 +02:00
2018-08-23 13:32:44 +02:00
for (unsigned i = 0; i < cnt; i++) {
2018-08-20 18:32:10 +02:00
if (p->bits)
cr_assert_eq_sample_raw(smps[i], smpt[i], f->flags, p->bits);
else
cr_assert_eq_sample(smps[i], smpt[i], f->flags);
}
sample_free_many(smps, p->cnt);
sample_free_many(smpt, p->cnt);
ret = pool_destroy(&pool);
2017-08-23 15:45:28 +02:00
cr_assert_eq(ret, 0);
}
ParameterizedTestParameters(io, highlevel)
{
2020-06-16 01:00:57 +02:00
static criterion::parameters<Param> params;
params.emplace_back("gtnet", 1, 32);
params.emplace_back("gtnet.fake", 1, 32);
params.emplace_back("raw.8", 1, 8);
params.emplace_back("raw.16.be", 1, 16);
params.emplace_back("raw.16.le", 1, 16);
params.emplace_back("raw.32.be", 1, 32);
params.emplace_back("raw.32.le", 1, 32);
params.emplace_back("raw.64.be", 1, 64);
params.emplace_back("raw.64.le", 1, 64);
params.emplace_back("villas.human", 10, 0);
params.emplace_back("villas.binary", 10, 0);
params.emplace_back("csv", 10, 0);
params.emplace_back("json", 10, 0);
#ifdef PROTOBUF_FOUND
params.emplace_back("protobuf", 10, 0 );
#endif
2020-01-26 16:17:59 +01:00
return params;
2017-08-05 21:02:09 +02:00
}
2020-01-26 16:17:59 +01:00
ParameterizedTest(Param *p, io, highlevel, .init = init_memory)
2017-08-05 21:02:09 +02:00
{
int ret, cnt;
char *retp;
2017-08-05 21:02:09 +02:00
2018-10-20 14:24:51 +02:00
Logger logger = logging.get("test:io:highlevel");
2020-06-16 01:00:57 +02:00
logger->info("Running test for format={}, cnt={}", p->fmt.c_str(), p->cnt);
2018-10-20 14:24:51 +02:00
return;
2018-05-12 13:56:12 +02:00
struct format_type *f;
2018-08-20 18:32:10 +02:00
struct sample *smps[p->cnt];
struct sample *smpt[p->cnt];
2018-05-12 13:56:12 +02:00
2020-06-16 02:35:34 +02:00
struct pool pool;
struct vlist signals;
struct io io;
ret = pool_init(&pool, 2 * p->cnt, SAMPLE_LENGTH(NUM_VALUES));
2017-08-05 21:02:09 +02:00
cr_assert_eq(ret, 0);
2018-08-20 18:32:10 +02:00
ret = sample_alloc_many(&pool, smps, p->cnt);
cr_assert_eq(ret, p->cnt);
ret = sample_alloc_many(&pool, smpt, p->cnt);
cr_assert_eq(ret, p->cnt);
ret = vlist_init(&signals);
cr_assert_eq(ret, 0);
2019-06-23 16:13:23 +02:00
signal_list_generate(&signals, NUM_VALUES, SignalType::FLOAT);
2018-08-20 18:32:10 +02:00
fill_sample_data(&signals, smps, p->cnt);
2017-08-05 21:02:09 +02:00
/* Open a file for IO */
char *fn, dir[64];
strncpy(dir, "/tmp/villas.XXXXXX", sizeof(dir));
2017-09-04 14:30:07 +02:00
retp = mkdtemp(dir);
cr_assert_not_null(retp);
ret = asprintf(&fn, "%s/file", dir);
cr_assert_gt(ret, 0);
2017-08-05 21:02:09 +02:00
2020-06-16 01:00:57 +02:00
f = format_type_lookup(p->fmt.c_str());
cr_assert_not_null(f, "Format '%s' does not exist", p->fmt.c_str());
2018-08-20 18:32:10 +02:00
2019-06-23 16:13:23 +02:00
ret = io_init(&io, f, &signals, (int) SampleFlags::HAS_ALL);
2018-08-20 18:32:10 +02:00
cr_assert_eq(ret, 0);
2017-08-05 21:02:09 +02:00
ret = io_open(&io, fn);
2017-08-05 21:02:09 +02:00
cr_assert_eq(ret, 0);
2018-08-20 18:32:10 +02:00
cnt = io_print(&io, smps, p->cnt);
cr_assert_eq(cnt, p->cnt, "Written only %d of %d samples for format %s", cnt, p->cnt, format_type_name(f));
2017-09-04 14:30:07 +02:00
ret = io_flush(&io);
2017-09-04 14:30:07 +02:00
cr_assert_eq(ret, 0);
2017-08-05 21:02:09 +02:00
#if 0 /* Show the file contents */
2017-08-05 21:02:09 +02:00
char cmd[128];
2020-06-16 01:00:57 +02:00
if (p->fmt == "csv" || p->fmt == "json" || p->fmt == "villas.human")
snprintf(cmd, sizeof(cmd), "cat %s", fn);
else
snprintf(cmd, sizeof(cmd), "hexdump -C %s", fn);
2017-08-05 21:02:09 +02:00
system(cmd);
#endif
2017-09-04 14:30:07 +02:00
2018-05-12 13:56:12 +02:00
io_rewind(&io);
2018-08-20 18:32:10 +02:00
cnt = io_scan(&io, smpt, p->cnt);
2018-05-12 13:56:12 +02:00
cr_assert_gt(cnt, 0, "Failed to read samples back: cnt=%d", cnt);
2018-08-20 18:32:10 +02:00
cr_assert_eq(cnt, p->cnt, "Read only %d of %d samples back", cnt, p->cnt);
for (int i = 0; i < cnt; i++) {
if (p->bits)
cr_assert_eq_sample_raw(smps[i], smpt[i], f->flags, p->bits);
else
cr_assert_eq_sample(smps[i], smpt[i], f->flags);
}
2017-08-05 21:02:09 +02:00
ret = io_close(&io);
cr_assert_eq(ret, 0);
ret = io_destroy(&io);
cr_assert_eq(ret, 0);
ret = unlink(fn);
cr_assert_eq(ret, 0);
2017-09-04 14:30:07 +02:00
ret = rmdir(dir);
2017-08-05 21:02:09 +02:00
cr_assert_eq(ret, 0);
2017-09-04 14:30:07 +02:00
free(fn);
2017-08-05 21:02:09 +02:00
2018-08-20 18:32:10 +02:00
sample_free_many(smps, p->cnt);
sample_free_many(smpt, p->cnt);
2017-08-05 21:02:09 +02:00
2018-08-20 18:32:10 +02:00
ret = pool_destroy(&pool);
2017-08-05 21:02:09 +02:00
cr_assert_eq(ret, 0);
}