Skip to content

Commit b0875bf

Browse files
committed
Added Hamcrest lib more verbose assertions in the tests. Some more tests.
1 parent 527c1a4 commit b0875bf

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

requirements-test.txt

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ tox
22
nose
33
coverage
44
entrypoint2
5+
pyhamcrest

tests/test_firmware.py

+16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from pyavrutils import AvrGcc
33
from pysimavr.avr import Avr
44
from pysimavr.firmware import Firmware
5+
from pysimavr.swig.simavr import cpu_Running
6+
from time import sleep
57

68
mcu = 'atmega48'
79

@@ -13,7 +15,9 @@ def test_fw_1():
1315

1416
avr = Avr(mcu=mcu, firmware=fw, f_cpu=8000000)
1517
eq_(avr.f_cpu, 8000000)
18+
eq_(avr.frequency, avr.f_cpu)
1619
eq_(avr.mcu, 'atmega48')
20+
avr.terminate()
1721

1822

1923
def test_fw_3():
@@ -24,4 +28,16 @@ def test_fw_3():
2428
avr = Avr(mcu=mcu, f_cpu=8000000)
2529
avr.load_firmware(fw)
2630
eq_(avr.f_cpu, 8000000)
31+
eq_(avr.frequency, avr.f_cpu)
2732
eq_(avr.mcu, 'atmega48')
33+
avr.terminate()
34+
35+
def test_background_thread():
36+
cc = AvrGcc(mcu=mcu)
37+
cc.build('int main(){}')
38+
fw = Firmware(cc.output)
39+
avr = Avr(mcu=mcu, firmware=fw, f_cpu=8000000)
40+
eq_(avr.state, cpu_Running, "mcu is not running")
41+
avr.goto_cycle(100)
42+
eq_(avr.state, cpu_Running, "mcu is not running")
43+
avr.terminate()

tests/test_timer.py

+19-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
from nose.tools import eq_
55
from pysimavr.avr import Avr
66
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
810

911
def test_timer_simple():
1012
avr = Avr(mcu='atmega88', f_cpu=8000000)
@@ -14,10 +16,9 @@ def test_timer_simple():
1416
#Schedule callback at 20uSec.
1517
# cycles = avr->frequency * (avr_cycle_count_t)usec / 1000000;
1618
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")
2122

2223
avr.step(1000)
2324
eq_(avr.state, cpu_Running, "mcu is not running")
@@ -44,10 +45,9 @@ def test_timer_reoccuring():
4445
eq_(avr.state, cpu_Running, "mcu is not running")
4546
eq_(callbackMock.call_count, 2, "number of calback invocations")
4647

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")
5151
avr.terminate()
5252

5353
def test_timer_cancel():
@@ -61,4 +61,14 @@ def test_timer_cancel():
6161
avr.step(1000)
6262
callbackMock.assert_not_called()
6363

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.")
6474
avr.terminate()

0 commit comments

Comments
 (0)