Skip to content

Commit 6e94616

Browse files
committed
Added test for one shot.
1 parent 97ba2c6 commit 6e94616

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

CppTimer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class CppTimer
4545
**/
4646
virtual void startns(long nanosecs, cppTimerType_t t = PERIODIC) {
4747
if (running) return;
48+
if (uthread.joinable()) uthread.join();
4849
timerType = t;
4950
if (fd < 0)
5051
throw("Could not start timer");
@@ -81,6 +82,7 @@ class CppTimer
8182
**/
8283
virtual void startms(long millisecs, cppTimerType_t t = PERIODIC) {
8384
if (running) return;
85+
if (uthread.joinable()) uthread.join();
8486
timerType = t;
8587
if (fd < 0)
8688
throw("Could not start timer");

test/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ add_test(TestStartStop_ms test_startstop_ms)
1919
add_executable (test_twotimers_ms twotimers_ms.cpp)
2020
TARGET_LINK_LIBRARIES(test_twotimers_ms cpptimer)
2121
add_test(TestTwotimers_ms test_twotimers_ms)
22+
23+
add_executable (test_oneshot oneshot.cpp)
24+
TARGET_LINK_LIBRARIES(test_oneshot cpptimer)
25+
add_test(TestOneShot test_oneshot)

test/oneshot.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <stdio.h>
2+
#include "CppTimer.h"
3+
#include <unistd.h>
4+
#include <time.h>
5+
#include <thread>
6+
7+
class DemoTimer1 : public CppTimer {
8+
9+
public:
10+
bool hasFired = false;
11+
void timerEvent() {
12+
fprintf(stdout,".");
13+
fflush(stdout);
14+
hasFired = true;
15+
}
16+
};
17+
18+
19+
int main( int, const char** ) {
20+
DemoTimer1 demoTimer1;
21+
demoTimer1.startns(0.5E9,ONESHOT);
22+
23+
std::this_thread::sleep_for(std::chrono::seconds(1));
24+
25+
if (!demoTimer1.hasFired) {
26+
char tmp[] = "nanosec Timer has not fired";
27+
printf("%s\n",tmp);
28+
fflush(stdout);
29+
throw tmp;
30+
}
31+
32+
demoTimer1.hasFired = false;
33+
34+
demoTimer1.startms(0.5E3,ONESHOT);
35+
36+
std::this_thread::sleep_for(std::chrono::seconds(1));
37+
38+
if (!demoTimer1.hasFired) {
39+
char tmp[] = "ms timer has not fired";
40+
printf("%s\n",tmp);
41+
fflush(stdout);
42+
throw tmp;
43+
}
44+
45+
return 0;
46+
}

0 commit comments

Comments
 (0)