File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change
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 ()
You can’t perform that action at this time.
0 commit comments