Debian Codegraph @

tests/instruments/test_validators.py

raw
1 #
2 # This file is part of the PyMeasure package.
3 #
4 # Copyright (c) 2013-2024 PyMeasure Developers
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 # THE SOFTWARE.
23 #
24
25 import pytest
26 from pymeasure.instruments.validators import (
27 strict_range, strict_discrete_range, strict_discrete_set,
28 truncated_range, truncated_discrete_set,
29 modular_range, modular_range_bidirectional,
30 joined_validators
31 )
32
33
34 def test_strict_range():
35 assert strict_range(5, range(10)) == 5
36 assert strict_range(5.1, range(10)) == 5.1
37 with pytest.raises(ValueError):
38 strict_range(20, range(10))
39
40
41 def test_strict_discrete_range():
42 assert strict_discrete_range(0.1, [0, 0.2], 0.001) == 0.1
43 assert strict_discrete_range(5, range(10), 0.1) == 5
44 assert strict_discrete_range(5.1, range(10), 0.1) == 5.1
45 assert strict_discrete_range(5.1, range(10), 0.001) == 5.1
46 assert strict_discrete_range(-5.1, [-20, 20], 0.001) == -5.1
47 with pytest.raises(ValueError):
48 strict_discrete_range(5.1, range(5), 0.001)
49 with pytest.raises(ValueError):
50 strict_discrete_range(5.01, range(5), 0.1)
51 with pytest.raises(ValueError):
52 strict_discrete_range(0.003, [0, 0.2], 0.002)
53
54
55 def test_strict_discrete_set():
56 assert strict_discrete_set(5, range(10)) == 5
57 with pytest.raises(ValueError):
58 strict_discrete_set(5.1, range(10))
59 with pytest.raises(ValueError):
60 strict_discrete_set(20, range(10))
61
62
63 def test_truncated_range():
64 assert truncated_range(5, range(10)) == 5
65 assert truncated_range(5.1, range(10)) == 5.1
66 assert truncated_range(-10, range(10)) == 0
67 assert truncated_range(20, range(10)) == 9
68
69
70 def test_truncated_discrete_set():
71 assert truncated_discrete_set(5, range(10)) == 5
72 assert truncated_discrete_set(5.1, range(10)) == 6
73 assert truncated_discrete_set(11, range(10)) == 9
74 assert truncated_discrete_set(-10, range(10)) == 0
75
76
77 def test_modular_range():
78 assert modular_range(5, range(10)) == 5
79 assert abs(modular_range(5.1, range(10)) - 5.1) < 1e-6
80 assert modular_range(11, range(10)) == 2
81 assert abs(modular_range(11.3, range(10)) - 2.3) < 1e-6
82 assert abs(modular_range(-7.1, range(10)) - 1.9) < 1e-6
83 assert abs(modular_range(-13.2, range(10)) - 4.8) < 1e-6
84
85
86 def test_modular_range_bidirectional():
87 assert modular_range_bidirectional(5, range(10)) == 5
88 assert abs(modular_range_bidirectional(5.1, range(10)) - 5.1) < 1e-6
89 assert modular_range_bidirectional(11, range(10)) == 2
90 assert abs(modular_range_bidirectional(11.3, range(10)) - 2.3) < 1e-6
91 assert modular_range_bidirectional(-7, range(10)) == -7
92 assert abs(modular_range_bidirectional(-7.1, range(10)) - (-7.1)) < 1e-6
93 assert abs(modular_range_bidirectional(-13.2, range(10)) - (-4.2)) < 1e-6
94
95
96 def test_joined_validators():
97 tst_validator = joined_validators(strict_discrete_set, strict_range)
98 values = [["ON", "OFF"], range(10)]
99 assert tst_validator(5, values) == 5
100 assert tst_validator(5.1, values) == 5.1
101 assert tst_validator("ON", values) == "ON"
102 with pytest.raises(ValueError):
103 tst_validator("OUT", values)
104 with pytest.raises(ValueError):
105 tst_validator(20, values)