File: test_vars.py

package info (click to toggle)
pypck 0.8.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 528 kB
  • sloc: python: 5,616; makefile: 15
file content (126 lines) | stat: -rw-r--r-- 2,974 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""Tests for variable value and unit handling."""

import math

import pytest

from pypck.lcn_defs import VarUnit, VarValue

VARIABLE_TEST_VALUES = (
    0,
    48,
    49,
    50,
    51,
    52,
    100,
    198,
    199,
    200,
    201,
    202,
    205,
    1000,
    1023,
    4095,
    65535,
)

UNITS_LOSSLESS_ROUNDTRIP = (
    VarUnit.NATIVE,
    VarUnit.CELSIUS,
    VarUnit.KELVIN,
    VarUnit.FAHRENHEIT,
    VarUnit.LUX_I,
    VarUnit.METERPERSECOND,
    VarUnit.PERCENT,
    VarUnit.VOLT,
    VarUnit.AMPERE,
    VarUnit.DEGREE,
    VarUnit.PPM,
    # VarUnit.LUX_T,
)

ROUNDTRIP_TEST_VECTORS = (
    *(
        (unit, value, value)
        for value in VARIABLE_TEST_VALUES
        for is_abs in (True, False)
        for unit in UNITS_LOSSLESS_ROUNDTRIP
    ),
)


CALIBRATION_TEST_VECTORS = (
    *(
        (VarUnit.CELSIUS, native, value)
        for native, value in ((0, -100), (1000, 0), (2000, 100))
    ),
    *(
        (VarUnit.KELVIN, native, value)
        for native, value in (
            (0, -100 + 273.15),
            (1000, 0 + 273.15),
            (2000, 100 + 273.15),
        )
    ),
    *(
        (VarUnit.FAHRENHEIT, native, value)
        for native, value in (
            (0, -100 * 1.8 + 32),
            (1000, 0 * 1.8 + 32),
            (2000, 100 * 1.8 + 32),
        )
    ),
    *(
        (VarUnit.LUX_I, native, value)
        for native, value in (
            (0, math.exp(0)),
            (10, math.exp(0.1)),
            (100, math.exp(1)),
            (1000, math.exp(10)),
        )
    ),
    *(
        (VarUnit.METERPERSECOND, native, value)
        for native, value in ((0, 0), (10, 1), (100, 10), (1000, 100))
    ),
    *(
        (VarUnit.PERCENT, native, value)
        for native, value in ((0, 0), (1, 1), (100, 100))
    ),
    *(
        (VarUnit.VOLT, native, value)
        for native, value in ((0, 0), (400, 1), (4000, 10))
    ),
    *(
        (VarUnit.AMPERE, native, value)
        for native, value in ((0, 0), (100, 0.001), (4000, 0.04))
    ),
    *(
        (VarUnit.DEGREE, native, value)
        for native, value in ((0, -100), (1000, 0), (2000, 100), (4000, 300))
    ),
    *(
        (VarUnit.PPM, native, value)
        for native, value in ((0, 0), (1, 1), (100, 100), (1000, 1000))
    ),
    # VarUnit.LUX_T,
)


@pytest.mark.parametrize("unit, native, expected", ROUNDTRIP_TEST_VECTORS)
def test_roundtrip(unit: VarUnit, native: int, expected: VarValue) -> None:
    """Test that variable conversion roundtrips."""
    assert (
        expected
        == VarValue.from_var_unit(
            VarValue.to_var_unit(VarValue.from_native(native), unit), unit, True
        ).to_native()
    )


@pytest.mark.parametrize("unit, native, value", CALIBRATION_TEST_VECTORS)
def test_calibration(unit: VarUnit, native: int, value: int | float) -> None:
    """Test proper calibration of variable conversion."""
    assert value == VarValue.to_var_unit(VarValue.from_native(native), unit)