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/src/test-cmp.c

204 lines
4.6 KiB
C
Raw Normal View History

/** Compare two data files.
*
* @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/>.
*********************************************************************************/
#include <stdio.h>
#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;
char *format;
struct sample *sample;
struct io io;
2018-05-12 13:56:12 +02:00
struct format_type *fmt;
};
void usage()
{
printf("Usage: villas-test-cmp [OPTIONS] FILE1 FILE2 ... FILEn\n");
2017-09-04 23:03:00 +02:00
printf(" FILE a list of files to compare\n");
printf(" OPTIONS is one or more of the following options:\n");
printf(" -d LVL adjust the debug level\n");
printf(" -e EPS set epsilon for floating point comparisons to EPS\n");
printf(" -v ignore data values\n");
printf(" -t ignore timestamp\n");
printf(" -s ignore sequence no\n");
2017-09-04 23:03:00 +02:00
printf(" -f FMT file format for all files\n");
2018-05-08 11:43:16 +02:00
printf(" -h show this usage information\n");
printf(" -V show the version of the tool\n\n");
printf("Return codes:\n");
printf(" 0 files are equal\n");
printf(" 1 file length not equal\n");
printf(" 2 sequence no not equal\n");
printf(" 3 timestamp not equal\n");
printf(" 4 number of values is not equal\n");
printf(" 5 data is not equal\n");
printf("\n");
print_copyright();
}
int main(int argc, char *argv[])
{
int ret;
/* Default values */
double epsilon = 1e-9;
char *format = "villas.human";
int flags = SAMPLE_HAS_SEQUENCE | SAMPLE_HAS_VALUES | SAMPLE_HAS_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':
flags &= ~SAMPLE_HAS_VALUES;
break;
case 't':
flags &= ~SAMPLE_HAS_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];
ret = pool_init(&pool, n, SAMPLE_LEN(DEFAULT_SAMPLELEN), &memtype_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];
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);
2017-09-04 23:03:00 +02:00
ret = io_init(&s[i].io, s[i].fmt, 0);
if (ret)
error("Failed to initialize IO");
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 {
printf("length unequal\n");
ret = 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);
if (ret)
2017-09-04 23:03:00 +02:00
goto out;
}
line++;
}
2017-09-04 23:03:00 +02:00
out: for (int i = 0; i < n; i++) {
io_close(&s[i].io);
io_destroy(&s[i].io);
sample_put(s[i].sample);
}
pool_destroy(&pool);
return ret;
2017-07-24 19:33:35 +02:00
}