-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_tasktimer.py
136 lines (107 loc) · 3.81 KB
/
test_tasktimer.py
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
"""
Copyright 2018 Sigvald Marholm <[email protected]>
This file is part of TaskTimer.
TaskTimer is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
TaskTimer 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with TaskTimer. If not, see <http://www.gnu.org/licenses/>.
"""
from tasktimer import *
import pytest
from pytest import approx
from math import sqrt, isnan
@pytest.fixture
def time():
class Time(object):
def __init__(self):
self.t = 0
def set(self, t):
self.t = t
def __call__(self):
return self.t
return Time()
def prefill(timer):
timer.last = 2
timer.total = 4.5
timer.mean = 1.5
timer.sum_sq_diff = 0.5
timer.laps = 3
return timer
def test_Timer(time):
timer = Timer(time_function=time)
assert timer.last == approx(0)
assert timer.total == approx(0)
assert timer.mean == approx(0)
assert timer.sum_sq_diff == approx(0)
assert timer.laps == approx(0)
assert isnan(timer.population_variance())
assert isnan(timer.population_stdev())
assert isnan(timer.sample_variance())
assert isnan(timer.sample_stdev())
time.set(4)
timer.start()
time.set(5.5)
timer.stop() # 1.5
assert timer.last == approx(1.5)
assert timer.total == approx(1.5)
assert timer.mean == approx(1.5)
assert timer.sum_sq_diff == approx(0)
assert timer.laps == approx(1)
assert timer.population_variance() == approx(0)
assert timer.population_stdev() == approx(0)
assert isnan(timer.sample_variance())
assert isnan(timer.sample_stdev())
time.set(7)
timer.start()
time.set(8)
timer.stop() # 1
assert timer.last == approx(1)
assert timer.total == approx(2.5)
assert timer.mean == approx(1.25)
assert timer.sum_sq_diff == approx(0.125)
assert timer.laps == approx(2)
assert timer.population_variance() == approx(0.0625)
assert timer.population_stdev() == approx(sqrt(0.0625))
assert timer.sample_variance() == approx(0.125)
assert timer.sample_stdev() == approx(sqrt(0.125))
time.set(11)
timer.start()
time.set(13)
assert timer.laps == approx(2)
timer.stop() # 2
assert timer.last == approx(2)
assert timer.total == approx(4.5)
assert timer.mean == approx(1.5)
assert timer.sum_sq_diff == approx(0.5)
assert timer.laps == approx(3)
assert timer.population_variance() == approx(0.5/3)
assert timer.sample_variance() == approx(0.5/2)
assert timer.population_stdev() == approx(sqrt(0.5/3))
assert timer.sample_stdev() == approx(sqrt(0.5/2))
timer.reset()
assert timer.last == approx(0)
assert timer.total == approx(0)
assert timer.mean == approx(0)
assert timer.sum_sq_diff == approx(0)
assert timer.laps == approx(0)
assert isnan(timer.population_variance())
assert isnan(timer.population_stdev())
assert isnan(timer.sample_variance())
assert isnan(timer.sample_stdev())
def test_Timer_statistics(caplog):
timer = Timer(statistics='population')
prefill(timer)
assert timer.variance() == timer.population_variance()
assert timer.stdev() == timer.population_stdev()
timer = Timer(statistics='sample')
prefill(timer)
assert timer.variance() == timer.sample_variance()
assert timer.stdev() == timer.sample_stdev()
with pytest.raises(TypeError):
timer = Timer(statistics='bullshit')