2017-03-12 17:10:45 -03:00
|
|
|
/** 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-05-05 19:24:16 +00:00
|
|
|
*
|
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-05-05 19:24:16 +00:00
|
|
|
*
|
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-12 17:10:45 -03:00
|
|
|
*********************************************************************************/
|
|
|
|
|
2018-07-03 21:04:28 +02:00
|
|
|
#include <iostream>
|
2017-03-12 17:10:45 -03:00
|
|
|
#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>
|
2017-03-12 17:10:45 -03:00
|
|
|
#include <villas/utils.h>
|
|
|
|
#include <villas/pool.h>
|
2017-12-09 02:23:29 +08:00
|
|
|
#include <villas/config.h>
|
2017-03-29 06:01:50 +02:00
|
|
|
|
2017-09-04 18:04:48 +02:00
|
|
|
struct side {
|
|
|
|
char *path;
|
2018-07-03 20:42:37 +02:00
|
|
|
const char *format;
|
2017-09-04 18:04:48 +02:00
|
|
|
|
|
|
|
struct sample *sample;
|
|
|
|
|
|
|
|
struct io io;
|
2018-05-12 13:56:12 +02:00
|
|
|
struct format_type *fmt;
|
2017-09-04 18:04:48 +02:00
|
|
|
};
|
|
|
|
|
2017-03-12 17:10:45 -03:00
|
|
|
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;
|
2017-03-12 17:10:45 -03:00
|
|
|
|
2017-05-05 19:24:16 +00:00
|
|
|
print_copyright();
|
2017-03-12 17:10:45 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2018-08-20 18:31:27 +02:00
|
|
|
int ret, rc = 0;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2017-08-20 10:56:16 +02:00
|
|
|
/* Default values */
|
2017-03-12 17:10:45 -03:00
|
|
|
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
|
|
|
|
2017-03-12 17:10:45 -03: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) {
|
2017-03-12 17:10:45 -03:00
|
|
|
switch (c) {
|
|
|
|
case 'e':
|
|
|
|
epsilon = strtod(optarg, &endptr);
|
|
|
|
goto check;
|
2017-08-20 10:56:16 +02:00
|
|
|
case 'v':
|
2018-08-20 18:31:27 +02:00
|
|
|
flags &= ~SAMPLE_HAS_DATA;
|
2017-08-20 10:56:16 +02:00
|
|
|
break;
|
|
|
|
case 't':
|
2018-08-20 18:31:27 +02:00
|
|
|
flags &= ~SAMPLE_HAS_TS_ORIGIN;
|
2017-08-20 10:56:16 +02:00
|
|
|
break;
|
|
|
|
case 's':
|
2017-09-16 15:04:59 +02:00
|
|
|
flags &= ~SAMPLE_HAS_SEQUENCE;
|
2017-09-04 18:04:48 +02:00
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
format = optarg;
|
2017-08-20 10:56:16 +02:00
|
|
|
break;
|
2018-05-08 11:43:16 +02:00
|
|
|
case 'V':
|
|
|
|
print_version();
|
|
|
|
exit(EXIT_SUCCESS);
|
2017-03-12 17:10:45 -03:00
|
|
|
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);
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-09-04 18:04:48 +02:00
|
|
|
if (argc - optind < 2) {
|
2017-03-12 17:10:45 -03:00
|
|
|
usage();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-09-04 18:04:48 +02:00
|
|
|
int n = argc - optind; /* The number of files which we compare */
|
|
|
|
struct side s[n];
|
2017-03-12 17:10:45 -03:00
|
|
|
|
2018-08-17 12:41:10 +02:00
|
|
|
ret = memory_init(0);
|
|
|
|
if (ret)
|
|
|
|
error("Failed to initialize memory system");
|
|
|
|
|
2018-08-13 14:56:16 +02:00
|
|
|
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");
|
|
|
|
|
2017-09-04 18:04:48 +02:00
|
|
|
/* Open files */
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
s[i].format = format;
|
|
|
|
s[i].path = argv[optind + i];
|
2018-05-13 14:21:50 +02:00
|
|
|
s[i].io.state = STATE_DESTROYED;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-05-12 13:56:12 +02:00
|
|
|
s[i].fmt = format_type_lookup(s[i].format);
|
2017-09-04 18:04:48 +02:00
|
|
|
if (!s[i].fmt)
|
|
|
|
error("Invalid IO format: %s", s[i].format);
|
2017-03-12 17:10:45 -03:00
|
|
|
|
2018-08-20 18:31:27 +02:00
|
|
|
ret = io_init_auto(&s[i].io, s[i].fmt, DEFAULT_SAMPLE_LENGTH, 0);
|
2017-09-04 18:04:48 +02:00
|
|
|
if (ret)
|
|
|
|
error("Failed to initialize IO");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-08-20 18:31:27 +02:00
|
|
|
ret = io_check(&s[i].io);
|
|
|
|
if (ret)
|
|
|
|
error("Failed to validate IO configuration");
|
|
|
|
|
2017-09-04 18:04:48 +02:00
|
|
|
ret = io_open(&s[i].io, s[i].path);
|
|
|
|
if (ret)
|
|
|
|
error("Failed to open file: %s", s[i].path);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-10-16 08:08:17 +02:00
|
|
|
s[i].sample = sample_alloc(&pool);
|
|
|
|
if (!s[i].sample)
|
2017-09-04 18:04:48 +02:00
|
|
|
error("Failed to allocate samples");
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-09-04 23:03:00 +02:00
|
|
|
int eofs, line, failed;
|
|
|
|
|
|
|
|
line = 0;
|
2017-09-04 18:04:48 +02:00
|
|
|
for (;;) {
|
|
|
|
/* Read next sample from all files */
|
2017-09-04 23:03:00 +02:00
|
|
|
retry: eofs = 0;
|
2017-09-04 18:04:48 +02:00
|
|
|
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-03-12 17:10:45 -03:00
|
|
|
|
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-03-12 17:10:45 -03:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-09-04 23:03:00 +02:00
|
|
|
goto out;
|
2017-03-12 17:10:45 -03:00
|
|
|
}
|
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-03-12 17:10:45 -03:00
|
|
|
}
|
2017-09-04 23:03:00 +02:00
|
|
|
if (failed)
|
|
|
|
goto retry;
|
|
|
|
|
2017-09-04 18:04:48 +02:00
|
|
|
/* 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
|
|
|
}
|
2017-09-04 18:04:48 +02:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-09-04 18:04:48 +02:00
|
|
|
line++;
|
|
|
|
}
|
2017-03-12 17:10:45 -03:00
|
|
|
|
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");
|
|
|
|
|
2018-08-07 09:22:26 +02:00
|
|
|
sample_decref(s[i].sample);
|
2017-09-04 18:04:48 +02:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-05-12 15:25:29 +02:00
|
|
|
ret = pool_destroy(&pool);
|
|
|
|
if (ret)
|
|
|
|
error("Failed to destroy pool");
|
2017-03-12 17:10:45 -03:00
|
|
|
|
2018-08-20 18:31:27 +02:00
|
|
|
return rc;
|
2017-07-24 19:33:35 +02:00
|
|
|
}
|