4
4
from nose .tools import eq_
5
5
from pysimavr .avr import Avr
6
6
from pysimavr .swig .simavr import cpu_Running
7
-
7
+ from hamcrest import assert_that , close_to , greater_than , equal_to , none , is_
8
+ import weakref
9
+ import gc
8
10
9
11
def test_timer_simple ():
10
12
avr = Avr (mcu = 'atmega88' , f_cpu = 8000000 )
@@ -14,10 +16,9 @@ def test_timer_simple():
14
16
#Schedule callback at 20uSec.
15
17
# cycles = avr->frequency * (avr_cycle_count_t)usec / 1000000;
16
18
timer = avr .timer (callbackMock , uSec = 20 )
17
- expectedCycle = 8000000 * 20 / 1000000
18
-
19
- # Check uSec got converted to cycles correctly.
20
- assert abs (expectedCycle - timer .status ()) < 10
19
+
20
+ assert_that (timer .status (), close_to (8000000 * 20 / 1000000 , 10 ),
21
+ "uSec to cycles convertion" )
21
22
22
23
avr .step (1000 )
23
24
eq_ (avr .state , cpu_Running , "mcu is not running" )
@@ -44,10 +45,9 @@ def test_timer_reoccuring():
44
45
eq_ (avr .state , cpu_Running , "mcu is not running" )
45
46
eq_ (callbackMock .call_count , 2 , "number of calback invocations" )
46
47
47
- lastCallFirstArg = callbackMock .call_args [0 ][0 ]
48
-
49
- #Check the last cycle number received +- matches the requested one
50
- assert abs (lastCallFirstArg - 200 ) < 10
48
+ lastCallFirstArg = callbackMock .call_args [0 ][0 ]
49
+ assert_that (lastCallFirstArg , close_to (200 , 10 ),
50
+ "The last cycle number received in the callback doesn't match the requested one" )
51
51
avr .terminate ()
52
52
53
53
def test_timer_cancel ():
@@ -61,4 +61,14 @@ def test_timer_cancel():
61
61
avr .step (1000 )
62
62
callbackMock .assert_not_called ()
63
63
64
+ avr .terminate ()
65
+
66
+ def test_timer_GC ():
67
+ avr = Avr (mcu = 'atmega88' , f_cpu = 1000000 )
68
+ callbackMock = Mock (return_value = 0 )
69
+ t = weakref .ref (avr .timer (callbackMock , cycle = 10 ))
70
+ gc .collect ()
71
+ assert_that (t (), is_ (none ()), "Orphan Timer didn't get garbage collected." )
72
+ avr .step (100 )
73
+ assert_that (callbackMock .call_count , equal_to (0 ), "Number of IRQ callback invocations." )
64
74
avr .terminate ()
0 commit comments