1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00
VILLASnode/common/tests/unit/timing.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

76 lines
2 KiB
C++
Raw Permalink Normal View History

/* Unit tests for time related utilities.
*
* Author: Steffen Vogel <post@steffenvogel.de>
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
* SPDX-License-Identifier: Apache-2.0
*/
#include <criterion/criterion.h>
#include <math.h>
#include <unistd.h>
2021-08-11 12:40:19 -04:00
#include <villas/timing.hpp>
2020-09-11 15:16:53 +02:00
// cppcheck-suppress unknownMacro
2018-10-19 14:33:10 +02:00
TestSuite(timing, .description = "Time measurements");
2018-08-23 13:14:39 +02:00
Test(timing, time_now) {
struct timespec now1 = time_now();
struct timespec now2 = time_now();
cr_assert(time_cmp(&now1, &now2) <= 0, "time_now() was reordered!");
}
Test(timing, time_diff) {
struct timespec ts1 = {.tv_sec = 0, .tv_nsec = 1}; // Value doesnt matter
struct timespec ts2 = {.tv_sec = 1,
.tv_nsec = 0}; // Overflow in nano seconds!
struct timespec ts3 = time_diff(&ts1, &ts2);
// ts4 == ts2?
cr_assert_eq(ts3.tv_sec, 0);
cr_assert_eq(ts3.tv_nsec, 999999999);
}
Test(timing, time_add) {
struct timespec ts1 = {.tv_sec = 1,
.tv_nsec = 999999999}; // Value doesnt matter
struct timespec ts2 = {.tv_sec = 1,
.tv_nsec = 1}; // Overflow in nano seconds!
struct timespec ts3 = time_add(&ts1, &ts2);
// ts4 == ts2?
cr_assert_eq(ts3.tv_sec, 3);
cr_assert_eq(ts3.tv_nsec, 0);
}
Test(timing, time_delta) {
struct timespec ts1 = {.tv_sec = 1, .tv_nsec = 123}; // Value doesnt matter
struct timespec ts2 = {.tv_sec = 5,
.tv_nsec = 246}; // Overflow in nano seconds!
double delta = time_delta(&ts1, &ts2);
cr_assert_float_eq(delta, 4 + 123e-9, 1e-9);
}
Test(timing, time_from_double) {
double ref = 1234.56789;
struct timespec ts = time_from_double(ref);
cr_assert_eq(ts.tv_sec, 1234);
cr_assert_eq(ts.tv_nsec, 567890000);
}
Test(timing, time_to_from_double) {
double ref = 1234.56789;
struct timespec ts = time_from_double(ref);
double dbl = time_to_double(&ts);
cr_assert_float_eq(dbl, ref, 1e-9);
}