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/tests/unit/timing.c

93 lines
2.6 KiB
C
Raw Permalink Normal View History

2016-10-06 17:56:55 -04:00
/** Unit tests for time related utlities
*
* @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/>.
2016-10-06 17:56:55 -04:00
*********************************************************************************/
2016-10-13 22:12:06 -04:00
#include <unistd.h>
#include <math.h>
2016-10-06 17:56:55 -04:00
#include <criterion/criterion.h>
2018-03-26 12:50:15 +02:00
#include <villas/timing.h>
2016-10-06 17:56:55 -04:00
2016-10-13 22:11:40 -04:00
Test(timing, time_now)
{
2016-10-06 17:56:55 -04:00
struct timespec now1 = time_now();
struct timespec now2 = time_now();
2016-10-06 17:56:55 -04:00
double delta = time_delta(&now1, &now2);
2016-10-06 17:56:55 -04:00
cr_assert_float_eq(delta, 0, 1e-5, "time_now() shows large variance!");
cr_assert_gt(delta, 0, "time_now() was reordered!");
}
2016-10-13 22:11:40 -04:00
Test(timing, time_diff)
{
2016-10-13 19:48:18 -04:00
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! */
2016-10-06 17:56:55 -04:00
struct timespec ts3 = time_diff(&ts1, &ts2);
2016-10-06 17:56:55 -04:00
/* ts4 == ts2? */
cr_assert_eq(ts3.tv_sec, 0);
cr_assert_eq(ts3.tv_nsec, 999999999);
}
2016-10-13 22:11:40 -04:00
Test(timing, time_add)
{
2016-10-13 19:48:18 -04:00
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! */
2016-10-06 17:56:55 -04:00
struct timespec ts3 = time_add(&ts1, &ts2);
2016-10-06 17:56:55 -04:00
/* ts4 == ts2? */
2016-10-13 19:48:18 -04:00
cr_assert_eq(ts3.tv_sec, 3);
2016-10-06 17:56:55 -04:00
cr_assert_eq(ts3.tv_nsec, 0);
}
2016-10-13 22:11:40 -04:00
Test(timing, time_delta)
{
2016-10-13 19:48:18 -04:00
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! */
2016-10-13 19:48:18 -04:00
double delta = time_delta(&ts1, &ts2);
2016-10-13 19:48:18 -04:00
cr_assert_float_eq(delta, 4 + 123e-9, 1e-9);
2016-10-06 17:56:55 -04:00
}
2016-10-13 22:11:40 -04:00
Test(timing, time_from_double)
{
2016-10-06 17:56:55 -04:00
double ref = 1234.56789;
2016-10-06 17:56:55 -04:00
struct timespec ts = time_from_double(ref);
cr_assert_eq(ts.tv_sec, 1234);
cr_assert_eq(ts.tv_nsec, 567890000);
}
2016-10-13 22:11:40 -04:00
Test(timing, time_to_from_double)
{
2016-10-06 17:56:55 -04:00
double ref = 1234.56789;
2016-10-06 17:56:55 -04:00
struct timespec ts = time_from_double(ref);
double dbl = time_to_double(&ts);
cr_assert_float_eq(dbl, ref, 1e-9);
2018-03-26 12:50:15 +02:00
}