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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
#include <math.h>
#include "unit/TestRunner.h"
#include "time/TaskTimer.h"
#define SLEEP_DURATION_MS 10.0
// Timer testing is a bit unreliable, so we just check to see that each sleep
// call (see below) is recorded off no more than this amount of milliseconds.
#define MAX_TIMER_TOLERANCE_MS 1.5f
#define TEST_COMPONENT_NAME "component"
#define TEST_SUBCOMPONENT_NAME "subcomponent"
// Keep a static task timer which can easily be destroyed in the teardown. The
// reason for this is that when running the test suite in valgrind, the timing
// tests often fail, which will cuase the suite to leak. Having failed timing
// tests is probably ok when running in valgrind, but leaking memory isn't, as
// that's the entire point of running it there. :)
static TaskTimer _testTaskTimer;
static void _testTaskTimerSetup(void)
{
_testTaskTimer = newTaskTimerWithCString(TEST_COMPONENT_NAME, TEST_SUBCOMPONENT_NAME);
}
static void _testTaskTimerTeardown(void)
{
freeTaskTimer(_testTaskTimer);
}
static int _testNewTaskTimer(void)
{
CharString c = newCharStringWithCString(TEST_COMPONENT_NAME);
TaskTimer t = newTaskTimer(c, TEST_SUBCOMPONENT_NAME);
assert(t->enabled);
assertCharStringEquals(TEST_COMPONENT_NAME, t->component);
assertCharStringEquals(TEST_SUBCOMPONENT_NAME, t->subcomponent);
assertDoubleEquals(0.0, t->totalTaskTime, TEST_DEFAULT_TOLERANCE);
freeCharString(c);
freeTaskTimer(t);
return 0;
}
static int _testNewObjectWithEmptyComponent(void)
{
CharString c = newCharStringWithCString(EMPTY_STRING);
TaskTimer t = newTaskTimer(c, TEST_SUBCOMPONENT_NAME);
assertNotNull(t);
assertCharStringEquals(EMPTY_STRING, t->component);
assertCharStringEquals(TEST_SUBCOMPONENT_NAME, t->subcomponent);
freeCharString(c);
freeTaskTimer(t);
return 0;
}
static int _testNewObjectWithEmptySubcomponent(void)
{
CharString c = newCharStringWithCString(TEST_COMPONENT_NAME);
TaskTimer t = newTaskTimer(c, EMPTY_STRING);
assertNotNull(t);
assertCharStringEquals(TEST_COMPONENT_NAME, t->component);
assertCharStringEquals(EMPTY_STRING, t->subcomponent);
freeCharString(c);
freeTaskTimer(t);
return 0;
}
static int _testNewObjectWithNullComponent(void)
{
TaskTimer t = newTaskTimer(NULL, NULL);
assertNotNull(t);
assertCharStringEquals(EMPTY_STRING, t->component);
assertCharStringEquals(EMPTY_STRING, t->subcomponent);
freeTaskTimer(t);
return 0;
}
static int _testNewObjectWithNullComponentCString(void)
{
TaskTimer t = newTaskTimerWithCString(NULL, NULL);
assertNotNull(t);
assertCharStringEquals(EMPTY_STRING, t->component);
assertCharStringEquals(EMPTY_STRING, t->subcomponent);
freeTaskTimer(t);
return 0;
}
static int _testNewObjectWithNullSubcomponent(void)
{
CharString c = newCharStringWithCString(TEST_COMPONENT_NAME);
TaskTimer t = newTaskTimer(c, NULL);
assertNotNull(t);
assertCharStringEquals(TEST_COMPONENT_NAME, t->component);
assertCharStringEquals(EMPTY_STRING, t->subcomponent);
freeCharString(c);
freeTaskTimer(t);
return 0;
}
static int _testNewObjectWithCStrings(void)
{
TaskTimer t = newTaskTimerWithCString(TEST_COMPONENT_NAME, TEST_SUBCOMPONENT_NAME);
assert(t->enabled);
assertCharStringEquals(TEST_COMPONENT_NAME, t->component);
assertCharStringEquals(TEST_SUBCOMPONENT_NAME, t->subcomponent);
assertDoubleEquals(0.0, t->totalTaskTime, TEST_DEFAULT_TOLERANCE);
freeTaskTimer(t);
return 0;
}
static void _testSleep(void)
{
taskTimerSleep(SLEEP_DURATION_MS);
}
static int _testTaskTimerDuration(void)
{
double elapsedTime = 0.0;
taskTimerStart(_testTaskTimer);
_testSleep();
elapsedTime = taskTimerStop(_testTaskTimer);
assertTimeEquals(SLEEP_DURATION_MS, _testTaskTimer->totalTaskTime, MAX_TIMER_TOLERANCE_MS);
assertTimeEquals(SLEEP_DURATION_MS, elapsedTime, MAX_TIMER_TOLERANCE_MS);
return 0;
}
static int _testTaskTimerDurationMultipleTimes(void)
{
double elapsedTime = 0.0;
int i;
for (i = 0; i < 5; i++) {
taskTimerStart(_testTaskTimer);
_testSleep();
elapsedTime = taskTimerStop(_testTaskTimer);
assertTimeEquals(SLEEP_DURATION_MS, elapsedTime, MAX_TIMER_TOLERANCE_MS);
elapsedTime = 0.0;
}
assertTimeEquals(5.0 * SLEEP_DURATION_MS, _testTaskTimer->totalTaskTime, MAX_TIMER_TOLERANCE_MS * 5.0);
return 0;
}
static int _testTaskTimerCallStartTwice(void)
{
taskTimerStart(_testTaskTimer);
taskTimerStart(_testTaskTimer);
_testSleep();
taskTimerStop(_testTaskTimer);
assertTimeEquals(SLEEP_DURATION_MS, _testTaskTimer->totalTaskTime, MAX_TIMER_TOLERANCE_MS);
return 0;
}
static int _testTaskTimerCallStopTwice(void)
{
taskTimerStart(_testTaskTimer);
_testSleep();
taskTimerStop(_testTaskTimer);
taskTimerStop(_testTaskTimer);
assertTimeEquals(SLEEP_DURATION_MS, _testTaskTimer->totalTaskTime, MAX_TIMER_TOLERANCE_MS);
return 0;
}
static int _testCallStopBeforeStart(void)
{
taskTimerStop(_testTaskTimer);
taskTimerStart(_testTaskTimer);
_testSleep();
taskTimerStop(_testTaskTimer);
assertTimeEquals(SLEEP_DURATION_MS, _testTaskTimer->totalTaskTime, MAX_TIMER_TOLERANCE_MS);
return 0;
}
static int _testHumanReadableTimeMs(void)
{
CharString s;
_testTaskTimer->totalTaskTime = 230;
s = taskTimerHumanReadbleString(_testTaskTimer);
assertCharStringEquals("230ms", s);
freeCharString(s);
return 0;
}
static int _testHumanReadableTimeSec(void)
{
CharString s;
// 23 seconds
_testTaskTimer->totalTaskTime = 23000;
s = taskTimerHumanReadbleString(_testTaskTimer);
assertCharStringEquals("23sec", s);
freeCharString(s);
return 0;
}
static int _testHumanReadableTimeMinSec(void)
{
CharString s;
// 10 minutes, 23 seconds
_testTaskTimer->totalTaskTime = 600000 + 23000;
s = taskTimerHumanReadbleString(_testTaskTimer);
assertCharStringEquals("10:23sec", s);
freeCharString(s);
return 0;
}
static int _testHumanReadableTimeHoursMinSec(void)
{
CharString s;
// 2 hours, 10 minutes, 23 seconds
_testTaskTimer->totalTaskTime = (1000 * 60 * 60 * 2) + 600000 + 23000;
s = taskTimerHumanReadbleString(_testTaskTimer);
assertCharStringEquals("2:10:23sec", s);
freeCharString(s);
return 0;
}
static int _testHumanReadableTimeNotStarted(void)
{
CharString s = taskTimerHumanReadbleString(_testTaskTimer);
assertCharStringEquals("0ms", s);
freeCharString(s);
return 0;
}
static int _testSleepMilliseconds(void)
{
double elapsedTime;
TaskTimer t = newTaskTimerWithCString("test", "test");
taskTimerStart(t);
taskTimerSleep(12);
elapsedTime = taskTimerStop(t);
assertTimeEquals(12, elapsedTime, 0.1);
freeTaskTimer(t);
return 0;
}
TestSuite addTaskTimerTests(void);
TestSuite addTaskTimerTests(void)
{
TestSuite testSuite = newTestSuite("TaskTimer", _testTaskTimerSetup, _testTaskTimerTeardown);
addTest(testSuite, "NewObject", _testNewTaskTimer);
addTest(testSuite, "NewObjectWithEmptyComponent", _testNewObjectWithEmptyComponent);
addTest(testSuite, "NewObjectWithEmptySubcomponent", _testNewObjectWithEmptySubcomponent);
addTest(testSuite, "NewObjectWithNullComponent", _testNewObjectWithNullComponent);
addTest(testSuite, "NewObjectWithNullComponentCString", _testNewObjectWithNullComponentCString);
addTest(testSuite, "NewObjectWithNullSubcomponent", _testNewObjectWithNullSubcomponent);
addTest(testSuite, "NewObjectWithCStrings", _testNewObjectWithCStrings);
addTest(testSuite, "TaskDuration", _testTaskTimerDuration);
addTest(testSuite, "TaskDurationMultipleTimes", _testTaskTimerDurationMultipleTimes);
addTest(testSuite, "CallStartTwice", _testTaskTimerCallStartTwice);
addTest(testSuite, "CallStopTwice", _testTaskTimerCallStopTwice);
addTest(testSuite, "CallStartTwice", _testTaskTimerCallStartTwice);
addTest(testSuite, "CallStopBeforeStart", _testCallStopBeforeStart);
addTest(testSuite, "HumanReadableTimeMs", _testHumanReadableTimeMs);
addTest(testSuite, "HumanReadableTimeSec", _testHumanReadableTimeSec);
addTest(testSuite, "HumanReadableTimeMinSec", _testHumanReadableTimeMinSec);
addTest(testSuite, "HumanReadableTimeHoursMinSec", _testHumanReadableTimeHoursMinSec);
addTest(testSuite, "HumanReadableTimeNotStarted", _testHumanReadableTimeNotStarted);
addTest(testSuite, "SleepMilliseconds", _testSleepMilliseconds);
return testSuite;
}
|