Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1a54fd3e6fb65dcad05ddfe4ac46a294359f8cc9 (plain) (blame)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*******************************************************************************
 * Copyright (c) 2013 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * CONTRIBUTORS:
 * 		Thomas Schuetz (initial contribution)
 *
 *******************************************************************************/


#include "TestEtTimer.h"

#include "etUnit/etUnit.h"

#include "etDatatypes.h"
#include "osal/etTimer.h"
#include "osal/etThread.h"
#include "osal/etSema.h"

#include "helpers/etTimeHelpers.h"

etSema GlobalSema;
etInt32 counter;

etTimerFunction_RETURN_VALUE TestEtTimer_TimerCallback1(etTimerFunction_ARGUMENT_LIST){
	etSema_wakeup(&GlobalSema);
}

etTimerFunction_RETURN_VALUE TestEtTimer_TimerCallback2(etTimerFunction_ARGUMENT_LIST){
	counter++;
}

static void TestEtTimer_lifecycle (etInt16 id) {
	etTimer timer1;
	etTime interval;
	etTime startTime;
	etTime endTime;

	/* create semaphore */
	etSema_construct(&GlobalSema);
	/* create and start timer */
	interval.sec = 1; /* = 1000 milliseconds */
	interval.nSec = 500000000; /* = 50 milliseconds */
	etTimer_construct(&timer1, &interval, TestEtTimer_TimerCallback1);

	getTimeFromTarget(&startTime);
	etTimer_start(&timer1);
	etSema_waitForWakeup(&GlobalSema); /* wait until callback function releases timer the first time (fires immediately) */
	etSema_waitForWakeup(&GlobalSema); /* wait until callback function releases timer the second time (fires after first interval)*/
	etTimer_stop(&timer1);
	getTimeFromTarget(&endTime);

	etInt32 elapsed = etTimeHelpers_convertToMSec(&endTime) - etTimeHelpers_convertToMSec(&startTime);
	EXPECT_TRUE(id, "elapsed time wrong", (elapsed > 1400) && (elapsed < 1600));

	/* release resources */
	etTimer_destruct(&timer1);
	etSema_destruct(&GlobalSema);
}

static void TestEtTimer_multiTimer (etInt16 id) {
	etTimer timer1;
	etTimer timer2;
	etTime interval;
	counter = 0;


	/* create semaphore */
	etSema_construct(&GlobalSema);

	interval.sec = 1; /* = 1000 milliseconds */
	interval.nSec = 0; /* = 0 milliseconds */
	etTimer_construct(&timer1, &interval, TestEtTimer_TimerCallback1);
	interval.sec = 0; /* = 1000 milliseconds */
	interval.nSec = 1000000; /* = 1 millisecond */
	etTimer_construct(&timer2, &interval, TestEtTimer_TimerCallback2);

	etTimer_start(&timer1);
	etTimer_start(&timer2);

	etSema_waitForWakeup(&GlobalSema); /* wait until callback function releases timer the first time (fires immediately) */
	etSema_waitForWakeup(&GlobalSema); /* wait until callback function releases timer the second time (fires after first interval)*/


	etTimer_stop(&timer2);
	etTimer_stop(&timer1);

	EXPECT_TRUE(id, "counter wrong", (counter > 950) && (counter < 1050));
	etTimer_destruct(&timer2);
	etTimer_destruct(&timer1);

	etSema_destruct(&GlobalSema);

}

void TestEtTimer_runSuite(void){
	etUnit_openTestSuite("TestEtTimer");
	ADD_TESTCASE(TestEtTimer_lifecycle);
	ADD_TESTCASE(TestEtTimer_multiTimer);
	etUnit_closeTestSuite();
}

Back to the top