Skip to content

Commit 4440f31

Browse files
committed
Create test_edgepi_dac.py
1 parent ebd6f92 commit 4440f31

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

tests/test_edgepi_dac.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import pytest
2+
from edgepi.dac.dac_constants import EDGEPI_DAC_CHANNEL as CH
3+
from edgepi.dac.dac_commands import DACCommands
4+
from edgepi.dac.dac_constants import EDGEPI_DAC_CALIBRATION_CONSTANTS as CALIB_CONSTS
5+
from edgepi.dac.dac_calibration import DACHwCalib_const, DACSwCalib_const
6+
7+
@pytest.fixture(name='dac_ops')
8+
def fixture_test_DAC_ops():
9+
dac_ops = DACCommands(DACHwCalib_const, [DACSwCalib_const]*8)
10+
return dac_ops
11+
12+
'''
13+
Combine command needs check for interger numbers for op-code, channel and value
14+
It also needs to check the range of each value.
15+
'''
16+
17+
@pytest.mark.parametrize("sample, result", [([1], True),
18+
([1,2,3,4, 5], True),
19+
([-1, 1, 2, 3], True),
20+
([444, 22, 3333, 5], True),
21+
([-111, -2222], True)])
22+
def test_check_for_int(sample, result, dac_ops):
23+
assert dac_ops.check_for_int(sample) == result
24+
25+
@pytest.mark.parametrize("sample, error", [([1,2.22,3,4, 0], ValueError),
26+
([None, 1, 2, 3], ValueError),
27+
([], ValueError),
28+
([-1, -2.22], ValueError)])
29+
def test_check_for_int_exception(sample, error, dac_ops):
30+
with pytest.raises(Exception) as e:
31+
dac_ops.check_for_int(sample)
32+
assert e.type is error
33+
34+
@pytest.mark.parametrize("min, target, max, result",[(0, 0, 10, True),
35+
(0, 10, 10, True),
36+
(0, 5, 10, True),
37+
(0.5, 1, 1.1, True)])
38+
def test_check_range(min, target, max, result, dac_ops):
39+
assert dac_ops.check_range(target, min, max) == result
40+
41+
@pytest.mark.parametrize("min, target, max, error",[(0, -1, len(CH), ValueError),
42+
(0, 11, len(CH), ValueError),
43+
(0, -5, CALIB_CONSTS.RANGE.value, ValueError),
44+
(0, 65536,CALIB_CONSTS.RANGE.value, ValueError)])
45+
def test_check_range(min, target, max, error, dac_ops):
46+
with pytest.raises(Exception) as e:
47+
dac_ops.check_range(target, min, max)
48+
assert e.type is error
49+
50+
@pytest.mark.parametrize("a, b, c, d",[(3, 1, 1000, [49, 3, 232]),
51+
(3, 0, 1000, [48, 3, 232]),
52+
(3, 3, 1000, [51, 3, 232])])
53+
def test_combine_command(a, b, c, d, dac_ops):
54+
assert dac_ops.combine_command(a, b, c) == d
55+
56+
57+
@pytest.mark.parametrize("a, b, c",[(1, 1000, [49, 3, 232]),
58+
(0, 1000, [48, 3, 232]),
59+
(3, 1000, [51, 3, 232])])
60+
def test_generate_write_and_update_command(a, b, c, dac_ops):
61+
assert dac_ops.generate_write_and_update_command(a, b) == c
62+
63+
'''
64+
voltage to code conversion
65+
voltage = positive floating number 0~5V ideally
66+
code = positive integer number 0~65535
67+
rounding up/down during conversion ?
68+
'''
69+
70+
@pytest.mark.parametrize("ch, expected, result",[(1, 2.345, 30030),
71+
(0, 2.345, 30030),
72+
(3, 2.345, 30030)])
73+
def test_voltage_to_code(ch, expected, result, dac_ops):
74+
assert dac_ops.voltage_to_code(ch, expected) == result

0 commit comments

Comments
 (0)