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

187 lines
5.5 KiB
C
Raw Normal View History

2017-03-25 21:23:48 +01:00
/** Unit tests for sample value mapping.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
2017-04-27 12:56:43 +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.
*
2017-04-27 12:56:43 +02:00
* 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.
*
2017-04-27 12:56:43 +02:00
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2017-03-25 21:23:48 +01:00
*********************************************************************************/
#include <criterion/criterion.h>
2018-03-26 12:50:15 +02:00
#include <villas/mapping.h>
#include <villas/node.h>
#include <villas/list.h>
2018-03-26 14:09:20 +02:00
#include <villas/utils.h>
#include <villas/signal.h>
Test(mapping, parse_nodes)
{
int ret;
struct mapping_entry m;
2018-03-26 14:09:20 +02:00
struct list nodes = { .state = STATE_DESTROYED };
2018-03-26 14:09:20 +02:00
char *node_names[3] = { "apple", "cherry", "carrot" };
char *signal_names[3][4] = {
{ "abra", "kadabra", "simsala", "bimm" },
{ "this", "is", "a", "test" },
{ "o", "sole", "mio", "italia" }
};
2018-03-26 14:09:20 +02:00
list_init(&nodes);
2018-03-26 14:09:20 +02:00
for (int i = 0; i < ARRAY_LEN(node_names); i++) {
struct node *n = alloc(sizeof(struct node));
2017-09-02 22:01:52 +02:00
2018-03-26 14:09:20 +02:00
n->name = node_names[i];
2018-08-20 18:32:10 +02:00
n->signals.state = STATE_DESTROYED;
2018-03-26 14:09:20 +02:00
2018-08-20 18:32:10 +02:00
list_init(&n->signals);
2018-03-26 14:09:20 +02:00
for (int j = 0; j < ARRAY_LEN(signal_names[i]); j++) {
2018-08-20 18:32:10 +02:00
struct signal *sig;
2018-03-26 14:09:20 +02:00
2018-08-20 18:32:10 +02:00
sig = signal_create(signal_names[i][j], NULL, SIGNAL_TYPE_AUTO);
cr_assert_not_null(sig);
2018-03-26 14:09:20 +02:00
2018-08-20 18:32:10 +02:00
list_push(&n->signals, sig);
2018-03-26 14:09:20 +02:00
}
list_push(&nodes, n);
}
ret = mapping_parse_str(&m, "apple.ts.origin", &nodes);
cr_assert_eq(ret, 0);
2018-03-26 14:09:20 +02:00
cr_assert_eq(m.node, list_lookup(&nodes, "apple"));
2018-08-06 10:10:17 +02:00
cr_assert_eq(m.type, MAPPING_TYPE_TIMESTAMP);
2018-08-20 18:32:10 +02:00
cr_assert_eq(m.timestamp.type, MAPPING_TIMESTAMP_TYPE_ORIGIN);
2018-03-26 14:09:20 +02:00
ret = mapping_parse_str(&m, "cherry.stats.owd.mean", &nodes);
cr_assert_eq(ret, 0);
2018-03-26 14:09:20 +02:00
cr_assert_eq(m.node, list_lookup(&nodes, "cherry"));
cr_assert_eq(m.type, MAPPING_TYPE_STATS);
cr_assert_eq(m.stats.id, STATS_OWD);
cr_assert_eq(m.stats.type, MAPPING_STATS_TYPE_MEAN);
2018-03-26 14:09:20 +02:00
ret = mapping_parse_str(&m, "carrot.data[1-2]", &nodes);
cr_assert_eq(ret, 0);
2018-03-26 14:09:20 +02:00
cr_assert_eq(m.node, list_lookup(&nodes, "carrot"));
cr_assert_eq(m.type, MAPPING_TYPE_DATA);
cr_assert_eq(m.data.offset, 1);
cr_assert_eq(m.length, 2);
2017-09-02 22:01:52 +02:00
2018-03-26 14:09:20 +02:00
ret = mapping_parse_str(&m, "carrot", &nodes);
cr_assert_eq(ret, 0);
2018-03-26 14:09:20 +02:00
cr_assert_eq(m.node, list_lookup(&nodes, "carrot"));
cr_assert_eq(m.type, MAPPING_TYPE_DATA);
cr_assert_eq(m.data.offset, 0);
2018-08-20 18:32:10 +02:00
cr_assert_eq(m.length, list_length(&m.node->signals));
2017-09-02 22:01:52 +02:00
2018-03-26 14:09:20 +02:00
ret = mapping_parse_str(&m, "carrot.data[sole]", &nodes);
cr_assert_eq(ret, 0);
cr_assert_eq(m.node, list_lookup(&nodes, "carrot"));
cr_assert_eq(m.type, MAPPING_TYPE_DATA);
cr_assert_eq(m.data.offset, 1);
cr_assert_eq(m.length, 1);
ret = mapping_parse_str(&m, "carrot.data[sole-mio]", &nodes);
cr_assert_eq(ret, 0);
cr_assert_eq(m.node, list_lookup(&nodes, "carrot"));
cr_assert_eq(m.type, MAPPING_TYPE_DATA);
cr_assert_eq(m.data.offset, 1);
cr_assert_eq(m.length, 2);
ret = list_destroy(&nodes, NULL, true);
cr_assert_eq(ret, 0);
}
2017-03-25 21:23:48 +01:00
Test(mapping, parse)
{
int ret;
struct mapping_entry m;
2017-08-28 17:18:06 +02:00
ret = mapping_parse_str(&m, "ts.origin", NULL);
2017-03-25 21:23:48 +01:00
cr_assert_eq(ret, 0);
2018-08-06 10:10:17 +02:00
cr_assert_eq(m.type, MAPPING_TYPE_TIMESTAMP);
2018-08-20 18:32:10 +02:00
cr_assert_eq(m.timestamp.type, MAPPING_TIMESTAMP_TYPE_ORIGIN);
2017-08-28 17:18:06 +02:00
ret = mapping_parse_str(&m, "hdr.sequence", NULL);
2017-03-25 21:23:48 +01:00
cr_assert_eq(ret, 0);
2018-08-06 10:10:17 +02:00
cr_assert_eq(m.type, MAPPING_TYPE_HEADER);
2018-08-20 18:32:10 +02:00
cr_assert_eq(m.header.type, MAPPING_HEADER_TYPE_SEQUENCE);
2017-03-25 21:23:48 +01:00
2017-08-28 17:18:06 +02:00
ret = mapping_parse_str(&m, "stats.owd.mean", NULL);
2017-03-25 21:23:48 +01:00
cr_assert_eq(ret, 0);
cr_assert_eq(m.type, MAPPING_TYPE_STATS);
cr_assert_eq(m.stats.id, STATS_OWD);
cr_assert_eq(m.stats.type, MAPPING_STATS_TYPE_MEAN);
2017-08-28 17:18:06 +02:00
ret = mapping_parse_str(&m, "data[1-2]", NULL);
2017-03-25 21:23:48 +01:00
cr_assert_eq(ret, 0);
cr_assert_eq(m.type, MAPPING_TYPE_DATA);
cr_assert_eq(m.data.offset, 1);
cr_assert_eq(m.length, 2);
2017-08-28 17:18:06 +02:00
ret = mapping_parse_str(&m, "data[5-5]", NULL);
2017-03-25 21:23:48 +01:00
cr_assert_eq(ret, 0);
cr_assert_eq(m.type, MAPPING_TYPE_DATA);
cr_assert_eq(m.data.offset, 5);
cr_assert_eq(m.length, 1);
2017-08-28 17:18:06 +02:00
ret = mapping_parse_str(&m, "data[22]", NULL);
2017-03-25 21:23:48 +01:00
cr_assert_eq(ret, 0);
cr_assert_eq(m.type, MAPPING_TYPE_DATA);
cr_assert_eq(m.data.offset, 22);
cr_assert_eq(m.length, 1);
2017-09-02 22:01:52 +02:00
2017-08-28 17:18:06 +02:00
ret = mapping_parse_str(&m, "data", NULL);
cr_assert_eq(ret, 0);
cr_assert_eq(m.type, MAPPING_TYPE_DATA);
cr_assert_eq(m.data.offset, 0);
cr_assert_eq(m.length, 0);
2017-09-02 22:01:52 +02:00
2017-08-28 17:18:06 +02:00
ret = mapping_parse_str(&m, "data[]", NULL);
cr_assert_eq(ret, 0);
cr_assert_eq(m.type, MAPPING_TYPE_DATA);
cr_assert_eq(m.data.offset, 0);
cr_assert_eq(m.length, 0);
2017-08-28 17:18:06 +02:00
ret = mapping_parse_str(&m, "data[1.1-2f]", NULL);
2017-03-25 21:23:48 +01:00
cr_assert_neq(ret, 0);
/* Missing parts */
2017-08-28 17:18:06 +02:00
ret = mapping_parse_str(&m, "stats.owd", NULL);
2017-03-25 21:23:48 +01:00
cr_assert_neq(ret, 0);
/* This a type */
2017-08-28 17:18:06 +02:00
ret = mapping_parse_str(&m, "hdr.sequences", NULL);
2017-03-25 21:23:48 +01:00
cr_assert_neq(ret, 0);
/* Check for superfluous chars at the end */
2017-08-28 17:18:06 +02:00
ret = mapping_parse_str(&m, "stats.ts.origin.bla", NULL);
2017-03-25 21:23:48 +01:00
cr_assert_neq(ret, 0);
2017-08-28 17:18:06 +02:00
ret = mapping_parse_str(&m, "stats.ts.origin.", NULL);
2017-03-25 21:23:48 +01:00
cr_assert_neq(ret, 0);
2017-08-28 17:18:06 +02:00
ret = mapping_parse_str(&m, "data[1-2]bla", NULL);
2017-03-25 21:23:48 +01:00
cr_assert_neq(ret, 0);
/* Negative length of chunk */
2017-08-28 17:18:06 +02:00
ret = mapping_parse_str(&m, "data[5-3]", NULL);
2017-03-25 21:23:48 +01:00
cr_assert_eq(ret, -1);
2017-09-02 22:01:52 +02:00
}