From 0781307577f39d58d9d013c8831946421c97c148 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Mon, 4 Sep 2017 16:19:43 +0200 Subject: [PATCH] added sample_cmp() --- include/villas/sample.h | 3 +++ lib/sample.c | 43 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/include/villas/sample.h b/include/villas/sample.h index 662d07704..26353f75e 100644 --- a/include/villas/sample.h +++ b/include/villas/sample.h @@ -116,6 +116,9 @@ int sample_put(struct sample *s); int sample_copy(struct sample *dst, struct sample *src); +/** Compare two samples */ +int sample_cmp(struct sample *a, struct sample *b, double epsilon, int flags); + int sample_copy_many(struct sample *dsts[], struct sample *srcs[], int cnt); int sample_get_many(struct sample *smps[], int cnt); int sample_put_many(struct sample *smps[], int cnt); diff --git a/lib/sample.c b/lib/sample.c index eda960c2b..45d492daf 100644 --- a/lib/sample.c +++ b/lib/sample.c @@ -21,10 +21,12 @@ *********************************************************************************/ #include +#include #include "pool.h" #include "sample.h" #include "utils.h" +#include "timing.h" int sample_alloc(struct pool *p, struct sample *smps[], int cnt) { @@ -110,6 +112,47 @@ int sample_copy_many(struct sample *dsts[], struct sample *srcs[], int cnt) return cnt; } +int sample_cmp(struct sample *a, struct sample *b, double epsilon, int flags) +{ + if ((a->has & b->has & flags) != flags) { + printf("missing components: a=%#x, b=%#x, wanted=%#x\n", a->has, b->has, flags); + return -1; + } + + /* Compare sequence no */ + if (flags & SAMPLE_SEQUENCE) { + if (a->sequence != b->sequence) { + printf("sequence no: %d != %d\n", a->sequence, b->sequence); + return -2; + } + } + + /* Compare timestamp */ + if (flags & SAMPLE_ORIGIN) { + if (time_delta(&a->ts.origin, &b->ts.origin) > epsilon) { + printf("ts.origin: %f != %f\n", time_to_double(&a->ts.origin), time_to_double(&b->ts.origin)); + return -3; + } + } + + /* Compare data */ + if (flags & SAMPLE_VALUES) { + if (a->length != b->length) { + printf("length: %d != %d\n", a->length, b->length); + return -4; + } + + for (int i = 0; i < a->length; i++) { + if (fabs(a->data[i].f - b->data[i].f) > epsilon) { + printf("data[%d]: %f != %f\n", i, a->data[i].f, b->data[i].f); + return -5; + } + } + } + + return 0; +} + int sample_set_data_format(struct sample *s, int idx, enum sample_data_format fmt) { if (idx >= sizeof(s->format) * 8)