Skip to content

Commit f83f38f

Browse files
committed
Add test for color sensor
1 parent ae19fc7 commit f83f38f

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

test/color.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""Test Color Sensor functionality"""
2+
import time
3+
import unittest
4+
5+
from buildhat import ColorSensor
6+
7+
8+
class TestColor(unittest.TestCase):
9+
"""Test color sensor functions"""
10+
11+
def test_color(self):
12+
"""Test color sensor interval"""
13+
color = ColorSensor('A')
14+
color.avg_reads = 1
15+
color.interval = 10
16+
count = 1000
17+
expecteddur = count * color.interval * 1e-3
18+
19+
start = time.time()
20+
for _ in range(count):
21+
color.get_ambient_light()
22+
end = time.time()
23+
diff = abs((end - start) - expecteddur)
24+
self.assertLess(diff, 0.25)
25+
26+
start = time.time()
27+
for _ in range(count):
28+
color.get_color_rgbi()
29+
end = time.time()
30+
diff = abs((end - start) - expecteddur)
31+
self.assertLess(diff, 0.25)
32+
33+
def test_caching(self):
34+
"""Test to make sure we're not reading cached data"""
35+
color = ColorSensor('A')
36+
color.avg_reads = 1
37+
color.interval = 1
38+
39+
for _ in range(100):
40+
color.mode(2)
41+
self.assertEqual(len(color.get()), 1)
42+
color.mode(5)
43+
self.assertEqual(len(color.get()), 4)
44+
45+
46+
if __name__ == '__main__':
47+
unittest.main()

0 commit comments

Comments
 (0)