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

146 lines
3.6 KiB
C
Raw 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>
#include "timing.h"
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);
2016-10-06 17:56:55 -04:00
}
2016-10-19 01:35:08 -04:00
Test(timing, timerfd_create_rate, .timeout = 20)
2016-10-13 22:12:06 -04:00
{
struct timespec start, end;
2016-10-13 22:12:06 -04:00
double rate = 5, waited;
2016-10-13 22:12:06 -04:00
int tfd = timerfd_create_rate(rate);
2016-10-13 22:12:06 -04:00
cr_assert(tfd > 0);
2016-10-30 21:14:48 -04:00
timerfd_wait(tfd);
int i;
for (i = 0; i < 10; i++) {
2016-10-13 22:12:06 -04:00
start = time_now();
timerfd_wait(tfd);
2016-10-13 22:12:06 -04:00
end = time_now();
waited = time_delta(&start, &end);
if (fabs(waited - 1.0 / rate) > 10e-3)
break;
2016-10-13 22:12:06 -04:00
}
if (i < 10)
2017-05-14 11:10:27 +02:00
cr_assert_float_eq(waited, 1.0 / rate, 1e-2, "We slept for %f instead of %f secs in round %d", waited, 1.0 / rate, i);
2016-10-13 22:12:06 -04:00
close(tfd);
}
2017-03-31 21:20:36 +02:00
Test(timing, timerfd_wait_until, .timeout = 10)
2016-10-13 22:12:06 -04:00
{
2016-10-13 19:48:18 -04:00
int tfd = timerfd_create(CLOCK_REALTIME, 0);
2016-10-06 17:56:55 -04:00
cr_assert(tfd > 0);
2017-03-31 21:20:36 +02:00
double waitfor = 2.423456789;
2016-10-06 17:56:55 -04:00
struct timespec start = time_now();
struct timespec diff = time_from_double(waitfor);
2016-10-13 19:48:18 -04:00
struct timespec future = time_add(&start, &diff);
2016-10-13 19:48:18 -04:00
timerfd_wait_until(tfd, &future);
2016-10-06 17:56:55 -04:00
struct timespec end = time_now();
2016-10-13 19:48:18 -04:00
double waited = time_delta(&start, &end);
2017-03-31 21:20:36 +02:00
cr_assert_float_eq(waited, waitfor, 1e-2, "We slept for %f instead of %f secs", waited, waitfor);
2016-10-13 22:12:06 -04:00
close(tfd);
2016-10-06 17:56:55 -04:00
}