-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtask.cpp
More file actions
71 lines (49 loc) · 1.54 KB
/
Copy pathtask.cpp
File metadata and controls
71 lines (49 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* Unit tests for periodic tasks.
*
* 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 <villas/task.hpp>
#include <villas/timing.hpp>
using namespace villas;
// cppcheck-suppress unknownMacro
TestSuite(task, .description = "Periodic timer tasks");
Test(task, rate, .timeout = 10) {
int runs = 10;
double rate = 5, waited;
struct timespec start, end;
Task task(CLOCK_MONOTONIC);
task.setRate(rate);
int i;
for (i = 0; i < runs; i++) {
clock_gettime(CLOCK_MONOTONIC, &start);
task.wait();
clock_gettime(CLOCK_MONOTONIC, &end);
waited = time_delta(&start, &end);
if (fabs(waited - 1.0 / rate) > 10e-3)
break;
}
if (i < runs)
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);
}
Test(task, wait_until, .timeout = 5) {
int ret;
struct timespec start, end, diff, future;
Task task(CLOCK_REALTIME);
double waitfor = 3.423456789;
start = time_now();
diff = time_from_double(waitfor);
future = time_add(&start, &diff);
task.setNext(&future);
ret = task.wait();
end = time_now();
cr_assert_eq(ret, 1);
double waited = time_delta(&start, &end);
cr_assert_float_eq(waited, waitfor, 1e-2,
"We slept for %f instead of %f secs", waited, waitfor);
}