Debian Codegraph @

tests/instruments/hp/test_hp3478a.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
27 from pymeasure.test import expected_protocol
28 from pymeasure.instruments.hp import HP3478A
29
30 VALID_CAL_DATA = [
31 0, 0, 0, 0, 3, 0, 8, 2, 15, 4, 4, 0, 13, 11, 0, 0,
32 0, 0, 3, 3, 2, 15, 5, 3, 0, 14, 0, 0, 0, 0, 0, 0,
33 3, 2, 15, 4, 0, 0, 14, 7, 9, 9, 9, 9, 9, 7, 2, 0,
34 15, 3, 12, 10, 11, 9, 9, 9, 9, 9, 9, 2, 0, 14, 15, 14,
35 9, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 15, 9,
36 9, 8, 6, 0, 9, 2, 14, 14, 0, 12, 10, 12, 9, 9, 9, 9,
37 9, 5, 1, 12, 0, 5, 14, 10, 13, 9, 9, 9, 9, 9, 8, 1,
38 12, 1, 15, 1, 10, 12, 0, 0, 0, 0, 0, 0, 1, 12, 15, 3,
39 0, 14, 0, 9, 9, 9, 9, 9, 9, 1, 12, 13, 2, 14, 9, 15,
40 9, 9, 9, 9, 9, 9, 1, 12, 13, 4, 2, 10, 9, 9, 9, 9,
41 9, 9, 9, 1, 12, 14, 12, 13, 9, 5, 9, 9, 9, 9, 9, 9,
42 1, 12, 2, 1, 15, 10, 10, 0, 0, 0, 0, 4, 2, 3, 0, 0,
43 3, 12, 14, 7, 0, 0, 0, 0, 0, 4, 3, 0, 1, 12, 3, 14,
44 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 15, 9, 9,
45 8, 6, 0, 9, 3, 14, 3, 1, 1, 12, 0, 0, 0, 0, 0, 0,
46 0, 0, 0, 0, 0, 0, 15, 15, 0, 0, 0, 0, 0, 0, 0, 0
47 ]
48
49
50 def convert_cal_data_to_cal_read_xfers(cal_data):
51 # Convert cal_data into 256 single byte 'W' read operations.
52 cal_read_xfers = []
53 for addr, value in enumerate(cal_data):
54 cal_read_xfers.append([bytes([ord('W'), addr]), bytes([value+64])])
55
56 return cal_read_xfers
57
58
59 def convert_cal_data_to_cal_write_xfers(cal_data):
60 # Convert cal_data into 256 single byte 'X' write operations.
61 cal_write_xfers = []
62 for addr, value in enumerate(cal_data):
63 cal_write_xfers.append([bytes([ord('X'), addr, value]), None])
64
65 return cal_write_xfers
66
67 # ============================================================
68 # TESTS
69 # ============================================================
70
71
72 def test_calibration_enabled():
73 with expected_protocol(
74 HP3478A,
75 [
76 (b"B", b'\x00\x20\x00\x00\x00'), # cal_enable bit is set
77 (b"B", b'\x00\x00\x00\x00\x00')
78 ],
79 ) as instr:
80 assert instr.calibration_enabled
81 assert not instr.calibration_enabled
82
83
84 def test_calibration_data_getter():
85 cal_read_xfers = convert_cal_data_to_cal_read_xfers(VALID_CAL_DATA)
86
87 with expected_protocol(
88 HP3478A,
89 cal_read_xfers
90 ) as instr:
91 cal_data = instr.calibration_data
92 assert instr.verify_calibration_data(cal_data)
93
94
95 def test_calibration_data_setter_cal_disabled():
96 with pytest.raises(Exception, match="CAL ENABLE switch not set to ON"):
97 with expected_protocol(
98 HP3478A,
99 # cal_enable cleared. As a result there won't be calibration
100 # write transactions.
101 [
102 (b"B", b'\x00\x00\x00\x00\x00'),
103 ]
104 ) as instr:
105 instr.calibration_data = VALID_CAL_DATA
106
107
108 def test_calibration_data_setter_pass():
109 valid_cal_write_xfers = convert_cal_data_to_cal_write_xfers(VALID_CAL_DATA)
110
111 with expected_protocol(
112 HP3478A,
113 # setter pass
114 [
115 (b"B", b'\x00\x20\x00\x00\x00'),
116 ] + valid_cal_write_xfers
117 ) as instr:
118 # Writing correct data
119 instr.calibration_data = VALID_CAL_DATA
120
121
122 def test_calibration_data_setter_invalid_data():
123 with pytest.raises(ValueError, match="cal_data verification fail"):
124 with expected_protocol(
125 HP3478A,
126 # setter fail due to invalid data
127 [
128 (b"B", b'\x00\x20\x00\x00\x00'),
129 ]
130 ) as instr:
131 # Assigning invalid data results in an Exception without data writes
132 valid_cal_data_corrupt = VALID_CAL_DATA.copy()
133 valid_cal_data_corrupt[1] = 1
134 instr.calibration_data = valid_cal_data_corrupt
135
136
137 def test_write_calibration_data():
138 invalid_cal_data = VALID_CAL_DATA.copy()
139 invalid_cal_data[1] = 1
140 invalid_cal_write_xfers = convert_cal_data_to_cal_write_xfers(invalid_cal_data)
141
142 with expected_protocol(
143 HP3478A,
144 invalid_cal_write_xfers
145 ) as instr:
146 # Writing invalid data with verification bypass
147 instr.write_calibration_data(invalid_cal_data, verify_calibration_data=False)