1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-16 00:00:02 +01:00
VILLASnode/src/villas-test-cmp.cpp

220 lines
5.4 KiB
C++
Raw Normal View History

/** Compare two data files.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2018-08-20 18:39:04 +02:00
* @copyright 2017-2018, 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/>.
*********************************************************************************/
2018-07-03 21:04:28 +02:00
#include <iostream>
#include <stdbool.h>
#include <getopt.h>
#include <jansson.h>
#include <villas/sample.h>
2017-08-05 21:02:09 +02:00
#include <villas/io.h>
2018-05-12 13:56:12 +02:00
#include <villas/format_type.h>
#include <villas/utils.h>
#include <villas/pool.h>
#include <villas/config.h>
struct side {
char *path;
2018-07-03 20:42:37 +02:00
const char *format;
struct sample *sample;
struct io io;
2018-05-12 13:56:12 +02:00
struct format_type *fmt;
};
void usage()
{
2018-07-03 21:04:28 +02:00
std::cout << "Usage: villas-test-cmp [OPTIONS] FILE1 FILE2 ... FILEn" << std::endl;
std::cout << " FILE a list of files to compare" << std::endl;
std::cout << " OPTIONS is one or more of the following options:" << std::endl;
std::cout << " -d LVL adjust the debug level" << std::endl;
std::cout << " -e EPS set epsilon for floating point comparisons to EPS" << std::endl;
std::cout << " -v ignore data values" << std::endl;
std::cout << " -t ignore timestamp" << std::endl;
std::cout << " -s ignore sequence no" << std::endl;
std::cout << " -f FMT file format for all files" << std::endl;
std::cout << " -h show this usage information" << std::endl;
std::cout << " -V show the version of the tool" << std::endl << std::endl;
std::cout << "Return codes:" << std::endl;
std::cout << " 0 files are equal" << std::endl;
std::cout << " 1 file length not equal" << std::endl;
std::cout << " 2 sequence no not equal" << std::endl;
std::cout << " 3 timestamp not equal" << std::endl;
std::cout << " 4 number of values is not equal" << std::endl;
std::cout << " 5 data is not equal" << std::endl << std::endl;
print_copyright();
}
int main(int argc, char *argv[])
{
2018-08-20 18:31:27 +02:00
int ret, rc = 0;
/* Default values */
double epsilon = 1e-9;
2018-07-03 20:42:37 +02:00
const char *format = "villas.human";
2018-08-20 18:31:27 +02:00
int flags = SAMPLE_HAS_SEQUENCE | SAMPLE_HAS_DATA | SAMPLE_HAS_TS_ORIGIN;
2017-03-29 20:14:35 +02:00
struct pool pool = { .state = STATE_DESTROYED };
/* Parse Arguments */
char c, *endptr;
2018-05-08 11:43:16 +02:00
while ((c = getopt (argc, argv, "he:vtsf:V")) != -1) {
switch (c) {
case 'e':
epsilon = strtod(optarg, &endptr);
goto check;
case 'v':
2018-08-20 18:31:27 +02:00
flags &= ~SAMPLE_HAS_DATA;
break;
case 't':
2018-08-20 18:31:27 +02:00
flags &= ~SAMPLE_HAS_TS_ORIGIN;
break;
case 's':
flags &= ~SAMPLE_HAS_SEQUENCE;
break;
case 'f':
format = optarg;
break;
2018-05-08 11:43:16 +02:00
case 'V':
print_version();
exit(EXIT_SUCCESS);
case 'h':
case '?':
usage();
exit(c == '?' ? EXIT_FAILURE : EXIT_SUCCESS);
}
continue;
check: if (optarg == endptr)
error("Failed to parse parse option argument '-%c %s'", c, optarg);
}
if (argc - optind < 2) {
usage();
exit(EXIT_FAILURE);
}
int n = argc - optind; /* The number of files which we compare */
struct side s[n];
2018-08-17 12:41:10 +02:00
ret = memory_init(0);
if (ret)
error("Failed to initialize memory system");
ret = pool_init(&pool, n, SAMPLE_LENGTH(DEFAULT_SAMPLE_LENGTH), &memory_heap);
2017-08-05 21:02:09 +02:00
if (ret)
error("Failed to initialize pool");
/* Open files */
for (int i = 0; i < n; i++) {
s[i].format = format;
s[i].path = argv[optind + i];
s[i].io.state = STATE_DESTROYED;
2018-05-12 13:56:12 +02:00
s[i].fmt = format_type_lookup(s[i].format);
if (!s[i].fmt)
error("Invalid IO format: %s", s[i].format);
2018-08-20 18:31:27 +02:00
ret = io_init_auto(&s[i].io, s[i].fmt, DEFAULT_SAMPLE_LENGTH, 0);
if (ret)
error("Failed to initialize IO");
2018-08-20 18:31:27 +02:00
ret = io_check(&s[i].io);
if (ret)
error("Failed to validate IO configuration");
ret = io_open(&s[i].io, s[i].path);
if (ret)
error("Failed to open file: %s", s[i].path);
s[i].sample = sample_alloc(&pool);
if (!s[i].sample)
error("Failed to allocate samples");
}
2017-09-04 23:03:00 +02:00
int eofs, line, failed;
line = 0;
for (;;) {
/* Read next sample from all files */
2017-09-04 23:03:00 +02:00
retry: eofs = 0;
for (int i = 0; i < n; i++) {
ret = io_eof(&s[i].io);
2017-09-04 23:03:00 +02:00
if (ret)
eofs++;
}
2017-09-04 23:03:00 +02:00
if (eofs) {
if (eofs == n)
ret = 0;
else {
2018-07-03 21:04:28 +02:00
std::cout << "length unequal" << std::endl;
2018-08-20 18:31:27 +02:00
rc = 1;
}
2017-09-04 23:03:00 +02:00
goto out;
}
2017-09-04 23:03:00 +02:00
failed = 0;
for (int i = 0; i < n; i++) {
ret = io_scan(&s[i].io, &s[i].sample, 1);
if (ret <= 0)
failed++;
}
2017-09-04 23:03:00 +02:00
if (failed)
goto retry;
/* We compare all files against the first one */
for (int i = 1; i < n; i++) {
ret = sample_cmp(s[0].sample, s[i].sample, epsilon, flags);
2018-08-20 18:31:27 +02:00
if (ret) {
rc = ret;
2017-09-04 23:03:00 +02:00
goto out;
2018-08-20 18:31:27 +02:00
}
}
line++;
}
2017-09-04 23:03:00 +02:00
out: for (int i = 0; i < n; i++) {
2018-05-12 15:25:29 +02:00
ret = io_close(&s[i].io);
if (ret)
error("Failed to close IO");
ret = io_destroy(&s[i].io);
if (ret)
error("Failed to destroy IO");
sample_decref(s[i].sample);
}
2018-05-12 15:25:29 +02:00
ret = pool_destroy(&pool);
if (ret)
error("Failed to destroy pool");
2018-08-20 18:31:27 +02:00
return rc;
2017-07-24 19:33:35 +02:00
}