Skip to content

Commit

Permalink
Add attribute tests
Browse files Browse the repository at this point in the history
  • Loading branch information
trishaange01 committed May 7, 2024
1 parent 302cf15 commit 64d0212
Show file tree
Hide file tree
Showing 8 changed files with 457 additions and 52 deletions.
246 changes: 246 additions & 0 deletions test/attr_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from decimal import Decimal
from test.common import (
dev_interface,
dev_interface_device_name_channel,
dev_interface_sub_channel,
pytest_collection_modifyitems,
pytest_configure,
Expand Down Expand Up @@ -379,3 +380,248 @@ def attribute_check_range_readonly_with_depends(
except Exception as e:
del sdr
raise Exception(e)


def attribute_single_value_device_name_channel_readonly(
uri, classname, device_name, channel, attr
):
"""attribute_single_value:
Read only class property with device name and channel parameters
parameters:
uri: type=string
URI of IIO context of target board/system
classname: type=string
Name of pyadi interface class which contain attribute
device_name: type=string
Device name of target board/system
channel: type=string
Channel name of the target board/system
attr: type=string
Attribute name to be written. Must be property of classname
"""
sdr = eval(
classname
+ "(uri='"
+ uri
+ "', device_name='"
+ device_name
+ "').channel['"
+ channel
+ "']"
)
try:
if not hasattr(sdr, attr):
raise AttributeError(attr + " not defined in " + classname)
rval = getattr(sdr, attr)
assert type(rval) != None
del sdr
except Exception as e:
del sdr
raise Exception(e)


def attribute_write_only_str_device_channel(
uri, classname, device_name, channel, attr, value
):
"""attribute_write_only_str_device_channel: Write only string class property
with device name and channel parameters
parameters:
uri: type=string
URI of IIO context of target board/system
classname: type=string
Name of pyadi interface class which contain attribute
device_name: type=string
Device name of target board/system
channel: type=string
Channel name of the target board/system
attr: type=string
Attribute name to be written. Must be property of classname
value: type=string
Value to write into attr property
"""
sdr = eval(
classname
+ "(uri='"
+ uri
+ "', device_name='"
+ device_name
+ "').channel['"
+ channel
+ "']"
)

if not hasattr(sdr, attr):
raise AttributeError(f"no attribute named: {attr}")

try:
setattr(sdr, attr, value)
del sdr
except Exception as e:
del sdr
raise Exception(e)


def attribute_single_value_range_channel(
uri,
classname,
device_name,
channel,
attr,
start,
stop,
step,
tol,
repeats=1,
sub_channel=None,
):
"""attribute_single_value_range_channel:
Write and read back integer class property
This is performed a defined number of times and the value written
is randomly determined based in input parameters
parameters:
uri: type=string
URI of IIO context of target board/system
classname: type=string
Name of pyadi interface class which contain attribute
device_name: type=string
Device name of target board/system
channel: type=string
Channel name of the attribute
attr: type=string
Attribute name to be written. Must be property of classname
start: type=integer
Lower bound of possible values attribute can be
stop: type=integer
Upper bound of possible values attribute can be
step: type=integer
Difference between successive values attribute can be
tol: type=integer
Allowable error of written value compared to read back value
repeats: type=integer
Number of random values to tests. Generated from uniform distribution
sub_channel: type=string
Name of sub channel (nested class) to be tested
"""
# Pick random number in operational range
numints = (stop - start) / step
for _ in range(repeats):
ind = np.random.uniform(0, numints)
val = start + step * ind
if isinstance(val, float):
val = floor_step_size(val, str(step))
# Check hardware
if sub_channel:
assert dev_interface_sub_channel(
uri, classname, sub_channel, val, attr, tol
)
else:
assert dev_interface_device_name_channel(
uri, classname, device_name, channel, val, attr, tol
)


def attribute_multiple_values_device_channel(
uri,
classname,
device_name,
channel,
attr,
values,
tol,
repeats=1,
sleep=0,
sub_channel=None,
):
"""attribute_multiple_values_device_channel: Write and read back multiple class properties
in a loop where all values are pre-defined and device name and channel are specified.
This is performed a defined number of times.
parameters:
uri: type=string
URI of IIO context of target board/system
classname: type=string
Name of pyadi interface class which contain attribute
device_name: type=string
Device name of target board/system
channel: type=string
Channel name of the attribute
attr: type=string
Attribute name to be written. Must be property of classname
values: type=list
A list of values to write and check as attributes
tol: type=integer
Allowable error of written value compared to read back value
repeats: type=integer
Number of times to repeatedly write values
sleep: type=integer
Seconds to sleep between writing to attribute and reading it back
sub_channel: type=string
Name of sub channel (nested class) to be tested
"""
for _ in range(repeats):
for val in values:
if isinstance(val, str):
tol = 0
if sub_channel:
assert dev_interface_sub_channel(
uri, classname, sub_channel, val, attr, tol, sleep=sleep
)
else:
assert dev_interface_device_name_channel(
uri, classname, device_name, channel, val, attr, tol
)


def attribute_multiple_values_available_readonly(uri, classname, attr):
"""attribute_multiple_values_available_readonly:
Read only class property where the available attribute values are returned.
parameters:
uri: type=string
URI of IIO context of target board/system
classname: type=string
Name of pyadi interface class which contain attribute
attr: type=string
Attribute name to be written. Must be property of classname
"""
sdr = eval(classname + "(uri='" + uri + "')")
try:
if not hasattr(sdr, attr):
raise AttributeError(attr + " not defined in " + classname)
rval = getattr(sdr, attr)
assert type(rval) != None
del sdr
except Exception as e:
del sdr
raise Exception(e)


def attribute_single_value_channel_readonly(uri, classname, channel, attr):
"""attribute_single_value:
Read only class property where the channel name is specified.
parameters:
uri: type=string
URI of IIO context of target board/system
classname: type=string
Name of pyadi interface class which contain attribute
channel: type=string
Channel name of the target board/system
attr: type=string
Attribute name to be written. Must be property of classname
"""
sdr = eval(classname + "(uri='" + uri + "')." + channel + "")
try:
if not hasattr(sdr, attr):
raise AttributeError(attr + " not defined in " + classname)
rval = getattr(sdr, attr)
assert type(rval) != None
del sdr
except Exception as e:
del sdr
raise Exception(e)
77 changes: 77 additions & 0 deletions test/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,80 @@ def dev_interface_sub_channel(
print(f"Got: {str(rval)}")
return abs_val <= tol
return val == str(rval)


def dev_interface_device_name_channel(
uri,
classname,
device_name,
channel,
val,
attr,
tol,
sub_channel=None,
sleep=0.3,
readonly=False,
):
"""dev_interface_device_name_channel:
Includes device name and channel in the source to be evaluated
"""

sdr = eval(
classname
+ "(uri='"
+ uri
+ "', device_name='"
+ device_name
+ "').channel['"
+ channel
+ "']"
)
# Check hardware
if not hasattr(sdr, attr):
raise AttributeError(attr + " not defined in " + classname)

rval = getattr(sdr, attr)
is_list = isinstance(rval, list)
if is_list:
l = len(rval)
val = [val] * l

setattr(sdr, attr, val)
if sleep > 0:
time.sleep(sleep)
rval = getattr(sdr, attr)

if not isinstance(rval, str) and not is_list:
rval = float(rval)
for _ in range(5):
setattr(sdr, attr, val)
time.sleep(0.3)
rval = float(getattr(sdr, attr))
if rval == val:
break
else:
for _ in range(2):
setattr(sdr, attr, val)
time.sleep(0.3)
rval = str(getattr(sdr, attr))
if rval == val:
break

del sdr

if is_list and isinstance(rval[0], str):
return val == rval

if not isinstance(val, str):
abs_val = np.max(abs(np.array(val) - np.array(rval)))
if abs_val > tol:
print(f"Failed to set1: {attr}")
print(f"Set: {str(val)}")
print(f"Got: {str(rval)}")
return abs_val <= tol
else:
if val != str(rval):
print(f"Failed to set: {attr}")
print(f"Set: {val}")
print(f"Got: {rval}")
return val == str(rval)
30 changes: 30 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,33 @@ def test_verify_overflow(request):
@pytest.fixture()
def test_verify_underflow(request):
yield verify_underflow


@pytest.fixture
def test_attribute_single_value_device_name_channel_readonly(request):
yield attribute_single_value_device_name_channel_readonly


@pytest.fixture
def test_attribute_write_only_str_device_channel(request):
yield attribute_write_only_str_device_channel


@pytest.fixture
def test_attribute_single_value_range_channel(request):
yield attribute_single_value_range_channel


@pytest.fixture
def test_attribute_multiple_values_device_channel(request):
yield attribute_multiple_values_device_channel


@pytest.fixture
def test_attribute_multiple_values_available_readonly(request):
yield attribute_multiple_values_available_readonly


@pytest.fixture
def test_attribute_single_value_channel_readonly(request):
yield attribute_single_value_channel_readonly
1 change: 1 addition & 0 deletions test/emu/devices/ad7746.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE context [<!ELEMENT context (device | context-attribute)*><!ELEMENT context-attribute EMPTY><!ELEMENT device (channel | attribute | debug-attribute | buffer-attribute)*><!ELEMENT channel (scan-element?, attribute*)><!ELEMENT attribute EMPTY><!ELEMENT scan-element EMPTY><!ELEMENT debug-attribute EMPTY><!ELEMENT buffer-attribute EMPTY><!ATTLIST context name CDATA #REQUIRED description CDATA #IMPLIED><!ATTLIST context-attribute name CDATA #REQUIRED value CDATA #REQUIRED><!ATTLIST device id CDATA #REQUIRED name CDATA #IMPLIED><!ATTLIST channel id CDATA #REQUIRED type (input|output) #REQUIRED name CDATA #IMPLIED><!ATTLIST scan-element index CDATA #REQUIRED format CDATA #REQUIRED scale CDATA #IMPLIED><!ATTLIST attribute name CDATA #REQUIRED filename CDATA #IMPLIED value CDATA #IMPLIED><!ATTLIST debug-attribute name CDATA #REQUIRED value CDATA #IMPLIED><!ATTLIST buffer-attribute name CDATA #REQUIRED value CDATA #IMPLIED>]><context name="network" description="192.168.10.151 Linux analog 5.15.92-v7l+ #1 SMP Tue Apr 2 06:14:52 UTC 2024 armv7l" ><context-attribute name="hw_carrier" value="Raspberry Pi 4 Model B Rev 1.1" /><context-attribute name="dtoverlay" value="vc4-kms-v3d,rpi-cn0552" /><context-attribute name="local,kernel" value="5.15.92-v7l+" /><context-attribute name="uri" value="ip:192.168.10.151" /><context-attribute name="ip,ip-addr" value="192.168.10.151" /><device id="hwmon0" name="rpi_volt" ><channel id="in0" type="input" ><attribute name="lcrit_alarm" filename="in0_lcrit_alarm" value="0" /></channel></device><device id="iio:device0" name="ad7746" ><channel id="voltage1" name="supply" type="input" ><attribute name="label" filename="in_voltage1_supply_label" value="supply" /><attribute name="raw" filename="in_voltage1_supply_raw" value="23759046" /><attribute name="sampling_frequency" filename="in_voltage_sampling_frequency" value="50" /><attribute name="sampling_frequency_available" filename="in_voltage_sampling_frequency_available" value="50 31 16 8" /><attribute name="scale" filename="in_voltage_scale" value="0.000139474" /></channel><channel id="capacitance1" type="input" ><attribute name="calibbias" filename="in_capacitance_calibbias" value="32768" /><attribute name="calibbias_calibration" filename="in_capacitance1_calibbias_calibration" value="ERROR" /><attribute name="calibscale" filename="in_capacitance1_calibscale" value="1.376861" /><attribute name="calibscale_calibration" filename="in_capacitance1_calibscale_calibration" value="ERROR" /><attribute name="offset" filename="in_capacitance1_offset" value="0" /><attribute name="raw" filename="in_capacitance1_raw" value="-8388608" /><attribute name="sampling_frequency" filename="in_capacitance_sampling_frequency" value="91" /><attribute name="sampling_frequency_available" filename="in_capacitance_sampling_frequency_available" value="91 84 50 26 16 13 11 9" /><attribute name="scale" filename="in_capacitance_scale" value="0.000000488" /></channel><channel id="capacitance0" type="input" ><attribute name="calibbias" filename="in_capacitance_calibbias" value="32768" /><attribute name="calibbias_calibration" filename="in_capacitance0_calibbias_calibration" value="ERROR" /><attribute name="calibscale" filename="in_capacitance0_calibscale" value="1.376861" /><attribute name="calibscale_calibration" filename="in_capacitance0_calibscale_calibration" value="ERROR" /><attribute name="offset" filename="in_capacitance0_offset" value="0" /><attribute name="raw" filename="in_capacitance0_raw" value="8388607" /><attribute name="sampling_frequency" filename="in_capacitance_sampling_frequency" value="91" /><attribute name="sampling_frequency_available" filename="in_capacitance_sampling_frequency_available" value="91 84 50 26 16 13 11 9" /><attribute name="scale" filename="in_capacitance_scale" value="0.000000488" /></channel><channel id="capacitance0-capacitance2" type="input" ><attribute name="calibbias" filename="in_capacitance_calibbias" value="32768" /><attribute name="calibbias" filename="in_capacitance_calibbias" value="32768" /><attribute name="calibscale" filename="in_capacitance0-capacitance2_calibscale" value="1.376861" /><attribute name="offset" filename="in_capacitance0-capacitance2_offset" value="0" /><attribute name="raw" filename="in_capacitance0-capacitance2_raw" value="8388607" /><attribute name="sampling_frequency" filename="in_capacitance-capacitance_sampling_frequency" value="91" /><attribute name="sampling_frequency" filename="in_capacitance-capacitance_sampling_frequency" value="91" /><attribute name="sampling_frequency_available" filename="in_capacitance_sampling_frequency_available" value="91 84 50 26 16 13 11 9" /><attribute name="scale" filename="in_capacitance-capacitance_scale" value="0.000000488" /><attribute name="scale" filename="in_capacitance-capacitance_scale" value="0.000000488" /></channel><channel id="temp0" type="input" ><attribute name="input" filename="in_temp0_input" value="34211" /></channel><channel id="capacitance1-capacitance3" type="input" ><attribute name="calibbias" filename="in_capacitance_calibbias" value="32768" /><attribute name="calibbias" filename="in_capacitance_calibbias" value="32768" /><attribute name="calibscale" filename="in_capacitance1-capacitance3_calibscale" value="1.376861" /><attribute name="offset" filename="in_capacitance1-capacitance3_offset" value="0" /><attribute name="raw" filename="in_capacitance1-capacitance3_raw" value="-8388608" /><attribute name="sampling_frequency" filename="in_capacitance-capacitance_sampling_frequency" value="91" /><attribute name="sampling_frequency" filename="in_capacitance-capacitance_sampling_frequency" value="91" /><attribute name="sampling_frequency_available" filename="in_capacitance_sampling_frequency_available" value="91 84 50 26 16 13 11 9" /><attribute name="scale" filename="in_capacitance-capacitance_scale" value="0.000000488" /><attribute name="scale" filename="in_capacitance-capacitance_scale" value="0.000000488" /></channel><channel id="voltage0" type="input" ><attribute name="calibscale_calibration" filename="in_voltage0_calibscale_calibration" value="ERROR" /><attribute name="raw" filename="in_voltage0_raw" value="10759" /><attribute name="sampling_frequency" filename="in_voltage_sampling_frequency" value="50" /><attribute name="sampling_frequency_available" filename="in_voltage_sampling_frequency_available" value="50 31 16 8" /><attribute name="scale" filename="in_voltage_scale" value="0.000139474" /></channel><channel id="temp1" type="input" ><attribute name="input" filename="in_temp1_input" value="-248295" /></channel></device><device id="iio_sysfs_trigger" ><attribute name="add_trigger" value="ERROR" /><attribute name="remove_trigger" value="ERROR" /></device></context>
Loading

0 comments on commit 64d0212

Please sign in to comment.