2017-03-12 17:10:45 -03:00
|
|
|
/** 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-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
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
|
|
|
|
#include <jansson.h>
|
|
|
|
|
|
|
|
#include <villas/sample.h>
|
2017-08-05 21:02:09 +02:00
|
|
|
#include <villas/io.h>
|
2017-08-14 14:42:07 +02:00
|
|
|
#include <villas/io_format.h>
|
|
|
|
#include <villas/io/villas.h>
|
2017-03-12 17:10:45 -03:00
|
|
|
#include <villas/utils.h>
|
|
|
|
#include <villas/timing.h>
|
|
|
|
#include <villas/pool.h>
|
|
|
|
|
2017-03-29 06:01:50 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
2017-03-12 17:10:45 -03:00
|
|
|
void usage()
|
|
|
|
{
|
2017-05-03 19:26:58 +02:00
|
|
|
printf("Usage: villas-test-cmp [OPTIONS] FILE1 FILE2\n");
|
2017-03-12 17:10:45 -03:00
|
|
|
printf(" FILE1 first file to compare\n");
|
|
|
|
printf(" FILE2 second file to compare against\n");
|
2017-05-03 19:26:58 +02:00
|
|
|
printf(" OPTIONS is one or more of the following options:\n");
|
|
|
|
printf(" -h print this usage information\n");
|
|
|
|
printf(" -d LVL adjust the debug level\n");
|
|
|
|
printf(" -e EPS set epsilon for floating point comparisons to EPS\n");
|
2017-08-20 10:56:16 +02:00
|
|
|
printf(" -v ignore data values\n");
|
|
|
|
printf(" -t ignore timestamp\n");
|
|
|
|
printf(" -s ignore sequence no\n");
|
2017-05-03 19:26:58 +02:00
|
|
|
printf("\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");
|
2017-03-12 17:10:45 -03:00
|
|
|
printf("\n");
|
|
|
|
|
2017-05-05 19:24:16 +00:00
|
|
|
print_copyright();
|
2017-03-12 17:10:45 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2017-04-02 04:32:11 +02:00
|
|
|
int ret;
|
2017-08-20 10:56:16 +02:00
|
|
|
|
|
|
|
/* Default values */
|
2017-03-12 17:10:45 -03:00
|
|
|
double epsilon = 1e-9;
|
2017-08-20 10:56:16 +02:00
|
|
|
int timestamp = 1;
|
|
|
|
int sequence = 1;
|
|
|
|
int data = 1;
|
2017-03-29 20:14:35 +02:00
|
|
|
|
2017-03-12 17:10:45 -03:00
|
|
|
struct log log;
|
|
|
|
struct pool pool = { .state = STATE_DESTROYED };
|
|
|
|
struct sample *samples[2];
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-12 17:10:45 -03:00
|
|
|
struct {
|
|
|
|
int flags;
|
|
|
|
char *path;
|
|
|
|
FILE *handle;
|
|
|
|
struct sample *sample;
|
|
|
|
} f1, f2;
|
|
|
|
|
|
|
|
/* Parse Arguments */
|
|
|
|
char c, *endptr;
|
2017-08-20 10:56:16 +02:00
|
|
|
while ((c = getopt (argc, argv, "hjmd:e:l:H:r:vts")) != -1) {
|
2017-03-12 17:10:45 -03:00
|
|
|
switch (c) {
|
|
|
|
case 'd':
|
|
|
|
log.level = strtoul(optarg, &endptr, 10);
|
|
|
|
goto check;
|
|
|
|
case 'e':
|
|
|
|
epsilon = strtod(optarg, &endptr);
|
|
|
|
goto check;
|
2017-08-20 10:56:16 +02:00
|
|
|
case 'v':
|
|
|
|
data = 0;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
timestamp = 0;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
sequence = 0;
|
|
|
|
break;
|
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-05-03 19:26:58 +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-03-12 17:10:45 -03:00
|
|
|
f1.path = argv[optind];
|
|
|
|
f2.path = argv[optind + 1];
|
|
|
|
|
2017-08-05 21:02:09 +02:00
|
|
|
ret = log_init(&log, V, LOG_ALL);
|
|
|
|
if (ret)
|
|
|
|
error("Failed to initialize log");
|
2017-03-12 17:10:45 -03:00
|
|
|
|
2017-08-05 21:02:09 +02:00
|
|
|
ret = log_start(&log);
|
|
|
|
if (ret)
|
|
|
|
error("Failed to start log");
|
|
|
|
|
|
|
|
ret = pool_init(&pool, 1024, SAMPLE_LEN(DEFAULT_SAMPLELEN), &memtype_heap);
|
|
|
|
if (ret)
|
|
|
|
error("Failed to initialize pool");
|
|
|
|
|
|
|
|
ret = sample_alloc(&pool, samples, 2);
|
|
|
|
if (ret != 2)
|
|
|
|
error("Failed to allocate samples");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-12 17:10:45 -03:00
|
|
|
f1.sample = samples[0];
|
|
|
|
f2.sample = samples[1];
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-12 17:10:45 -03:00
|
|
|
f1.handle = fopen(f1.path, "r");
|
|
|
|
if (!f1.handle)
|
|
|
|
serror("Failed to open file: %s", f1.path);
|
|
|
|
|
|
|
|
f2.handle = fopen(f2.path, "r");
|
|
|
|
if (!f2.handle)
|
|
|
|
serror("Failed to open file: %s", f2.path);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-12 17:10:45 -03:00
|
|
|
while (!feof(f1.handle) && !feof(f2.handle)) {
|
2017-08-14 14:42:07 +02:00
|
|
|
ret = villas_fscan(f1.handle, &f1.sample, 1, &f1.flags);
|
2017-04-02 04:32:11 +02:00
|
|
|
if (ret < 0 && !feof(f1.handle))
|
2017-03-12 17:10:45 -03:00
|
|
|
goto out;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
ret = villas_fscan(f2.handle, &f2.sample, 1, &f2.flags);
|
2017-04-02 04:32:11 +02:00
|
|
|
if (ret < 0 && !feof(f2.handle))
|
2017-03-12 17:10:45 -03:00
|
|
|
goto out;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-02 04:32:11 +02:00
|
|
|
/* EOF is only okay if both files are at the end */
|
|
|
|
if (feof(f1.handle) || feof(f2.handle)) {
|
|
|
|
if (!(feof(f1.handle) && feof(f2.handle))) {
|
|
|
|
printf("file length not equal\n");
|
|
|
|
ret = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
2017-03-12 17:10:45 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Compare sequence no */
|
2017-08-20 10:56:16 +02:00
|
|
|
if (sequence && (f1.flags & IO_FORMAT_SEQUENCE) && (f2.flags & IO_FORMAT_SEQUENCE)) {
|
2017-03-12 17:10:45 -03:00
|
|
|
if (f1.sample->sequence != f2.sample->sequence) {
|
|
|
|
printf("sequence no: %d != %d\n", f1.sample->sequence, f2.sample->sequence);
|
2017-04-02 04:32:11 +02:00
|
|
|
ret = 2;
|
2017-03-12 17:10:45 -03:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-12 17:10:45 -03:00
|
|
|
/* Compare timestamp */
|
2017-08-20 10:56:16 +02:00
|
|
|
if (timestamp) {
|
|
|
|
if (time_delta(&f1.sample->ts.origin, &f2.sample->ts.origin) > epsilon) {
|
|
|
|
printf("ts.origin: %f != %f\n", time_to_double(&f1.sample->ts.origin), time_to_double(&f2.sample->ts.origin));
|
|
|
|
ret = 3;
|
|
|
|
goto out;
|
|
|
|
}
|
2017-03-12 17:10:45 -03:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-12 17:10:45 -03:00
|
|
|
/* Compare data */
|
2017-08-20 10:56:16 +02:00
|
|
|
if (data) {
|
|
|
|
if (f1.sample->length != f2.sample->length) {
|
|
|
|
printf("length: %d != %d\n", f1.sample->length, f2.sample->length);
|
|
|
|
ret = 4;
|
2017-03-12 17:10:45 -03:00
|
|
|
goto out;
|
|
|
|
}
|
2017-08-20 10:56:16 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < f1.sample->length; i++) {
|
|
|
|
if (fabs(f1.sample->data[i].f - f2.sample->data[i].f) > epsilon) {
|
|
|
|
printf("data[%d]: %f != %f\n", i, f1.sample->data[i].f, f2.sample->data[i].f);
|
|
|
|
ret = 5;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
2017-03-12 17:10:45 -03:00
|
|
|
}
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-02 04:32:11 +02:00
|
|
|
ret = 0;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-12 17:10:45 -03:00
|
|
|
out: sample_free(samples, 2);
|
|
|
|
|
|
|
|
fclose(f1.handle);
|
|
|
|
fclose(f2.handle);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-12 17:10:45 -03:00
|
|
|
pool_destroy(&pool);
|
|
|
|
|
|
|
|
return ret;
|
2017-07-24 19:33:35 +02:00
|
|
|
}
|