-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_eecalpy.py
182 lines (149 loc) · 5.54 KB
/
test_eecalpy.py
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# -*- coding: UTF-8 -*-
import pytest
import random
from hypothesis import given, example, assume, note, settings, Verbosity
import hypothesis.strategies as st
from eecalpy.electrical_units import *
hyp_random_unit = st.sampled_from((U, R, I, P, Usq, Isq, Factor))
hyp_float = st.floats(min_value=1e-14, max_value=1e100, allow_nan=False, allow_infinity=False)
@given(unit=hyp_random_unit, _min=hyp_float, _max=hyp_float)
def test_classmethod_fmm(unit, _min, _max):
assume(_max >= _min)
eu = unit.from_min_max(_min, _max)
assert isinstance(eu, unit)
assert eu.min <= eu.value <= eu.max
@pytest.mark.parametrize("exponnent", list(range(-14, 15)))
def test_factors(exponnent):
assert U(10**exponnent, 0.01).pretty(vrange=False).find('1000.0') == -1
def test_R_temp_coeff():
r = R(1e3)
assert r.alpha_ppm == None
r = R(1e3, 0.01, 200)
assert r.alpha_ppm == 200
@given(unit=hyp_random_unit, val=hyp_float, tol=hyp_float)
# @settings(max_examples=500, verbosity=Verbosity.verbose)
def test_tolerance(unit, val, tol):
if unit == R:
assume(val > 0.0)
eu = unit(val, tol)
assert eu.pretty() != ''
assert eu.min <= eu.value <= eu.max
@given(unit=hyp_random_unit, a=hyp_float, b=hyp_float)
def test_tolerance_sign(unit, a, b):
eu = unit.from_min_max(a, b)
assert eu.tolerance >= 0
@given(unit=st.sampled_from((U, I, P, Usq, Isq)), a=hyp_float, b=hyp_float)
def test_operators(unit, a, b):
x = unit(a)
y = unit(b)
w = x + y
assert a + b == w.value
z = x - y
assert a - b == z.value
def test_pretty():
i1 = I(12e-6, 0.05)
assert str(i1) == i1.pretty()
assert i1.pretty() == '12.0µA ± 5.0% (± 600.0nA) [11.4000 .. 12.6000]µA'
assert i1.pretty(variation=False) == '12.0µA ± 5.0% [11.4000 .. 12.6000]µA'
assert i1.pretty(vrange=False) == '12.0µA ± 5.0% (± 600.0nA)'
assert i1.pretty(variation=False, vrange=False) == '12.0µA ± 5.0%'
assert i1.pretty(tolerance=False, variation=False, vrange=False) == '12.0µA'
assert i1.pretty(value=False, tolerance=False, variation=False, vrange=False) == ''
assert i1.pretty(value=False, variation=False, vrange=False) == '± 5.0%'
assert i1.pretty(value=False, variation=False) == '± 5.0% [11.4000 .. 12.6000]µA'
assert i1.pretty(value=False) == '± 5.0% (± 600.0nA) [11.4000 .. 12.6000]µA'
r1 = R(8.5e3, 0.01, 200)
assert r1.pretty() == '8.5kΩ ± 1.0% (± 85.0Ω) [8.4150 .. 8.5850]kΩ @ 20°C α=200ppm'
assert r1.pretty(temperature=False) == '8.5kΩ ± 1.0% (± 85.0Ω) [8.4150 .. 8.5850]kΩ'
assert r1.pretty(tolerance=False, temperature=False) == '8.5kΩ'
assert r1.pretty(value=False, temperature=False) == '± 1.0% (± 85.0Ω) [8.4150 .. 8.5850]kΩ'
@pytest.mark.parametrize(
"eu,expected",
[
(U(3), '3.0V'),
(U(-12.3), '-12.3V'),
(U(818e3), '818.0kV'),
(U(200e-3, 0.1), '200.0mV ± 10.0% (± 20.0mV) [180.0000 .. 220.0000]mV'),
(R(1e3, 0.01, 200).at_T(100) + R(2e3, 0.01, 150), '3.02kΩ ± 1.0% (± 30.16Ω) [2.9858 .. 3.0462]kΩ @ mixed temp.'),
(Factor(1.0, 0.01), '1.0 ± 1.0% [0.9900 .. 1.0100]'),
(Factor.from_min_max(2, 3), '2.5 ± 20.0% [2.0000 .. 3.0000]')
]
)
def test_repr(eu, expected):
assert str(eu) == expected
def test_same_temperatures():
r1 = R(1e3, 0.01, 200)
r2 = R(2e3, 0.01, 150)
r = r1 + r2
assert r.temperature == 20.0
r = r1 | r2
assert r.temperature == 20.0
r = r1.at_T(100) + r2.at_T(100)
assert r.temperature == 100.0
r = r1.at_T(100) | r2.at_T(100)
assert r.temperature == 100.0
def test_temp_and_factors():
r1 = R(1e3, 0.01, 200)
r2 = R(2e3, 0.01, 150)
r = r1.at_T(100) * Factor(2.0)
assert r.temperature == 100.0
r = Factor(2.0) * r1.at_T(100)
assert r.temperature == 100.0
r = r1 * Factor(2.0)
assert r.temperature == 20.0
r = Factor(2.0) * r1
assert r.temperature == 20.0
r = r1.at_T(100) * Factor(2.0)
assert r.temperature == 100.0
r = Factor(2.0) * r1.at_T(100)
assert r.temperature == 100.0
def test_different_temperatures():
r1 = R(1e3, 0.01, 200)
r2 = R(2e3, 0.01, 150)
r = r1 + r2.at_T(100)
assert r.temperature is None
r = r1 | r2.at_T(100)
assert r.temperature is None
r = r1.at_T(100) + r2.at_T(60)
assert r.temperature is None
r = r1.at_T(60) | r2.at_T(100)
assert r.temperature is None
@pytest.mark.parametrize(
"operation,expected_type",
[
# U = R * I
(R(1) * I(1), U),
(I(1) * R(1), U),
# P = U * I
(U(1) * I(1), P),
(I(1) * U(1), P),
# factors
(Factor(1) * Factor(1), Factor),
(U(1) * Factor(1), U),
(R(1) * Factor(1), R),
(I(1) * Factor(1), I),
(P(1) * Factor(1), P),
(Factor(1) * U(1), U),
(Factor(1) * R(1), R),
(Factor(1) * I(1), I),
(Factor(1) * P(1), P)
]
)
def test_operations_mul(operation, expected_type):
assert isinstance(operation, expected_type)
@pytest.mark.parametrize(
"operation,expected_type",
[
(U(1) / R(1), I), # U / R = I
(U(1) / I(1), R), # U / I = R
(P(1) / U(1), I), # P / U = I
(P(1) / I(1), U), # P / I = U
(R(1) / R(1), Factor), # R / R = f
(U(1) / U(1), Factor), # U / U = f
(I(1) / I(1), Factor), # I / I = f
(P(1) / P(1), Factor), # P / P = f
(Factor(1) / Factor(1), Factor), # f / f = f
]
)
def test_operations_div(operation, expected_type):
assert isinstance(operation, expected_type)