diff options
author | Samuel O'Brien <sam.obrien@ni.com> | 2020-06-25 15:43:32 -0500 |
---|---|---|
committer | Aaron Rossetto <aaron.rossetto@ni.com> | 2020-07-16 09:59:25 -0500 |
commit | 919a147afcbecace5107a4d0a4da556cfd56df92 (patch) | |
tree | c7b5faea5b89069232e7fb9f7cfebe1e495f4bca /host/tests/pychdr_parse_test.py | |
parent | be6491428db599867129733f73e7ce0ce23e05a7 (diff) | |
download | uhd-919a147afcbecace5107a4d0a4da556cfd56df92.tar.gz uhd-919a147afcbecace5107a4d0a4da556cfd56df92.tar.bz2 uhd-919a147afcbecace5107a4d0a4da556cfd56df92.zip |
python: Add bindings for C++ CHDR Parser
This commit adds pybind11 glue code for the userland chdr parsing code
introduced in the uhd::utils::chdr namespace. Additionally, it moves
some pybind11 adapter code to a common pybind_adaptors.hpp file which
originally existed in the cal_python.hpp file.
This commit also adds unit tests for the python bindings using a
captured wireshark trace which is located in rfnoc_packets_*.py and some
handwritten packets in hardcoded_packets.py
Signed-off-by: Samuel O'Brien <sam.obrien@ni.com>
Diffstat (limited to 'host/tests/pychdr_parse_test.py')
-rw-r--r-- | host/tests/pychdr_parse_test.py | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/host/tests/pychdr_parse_test.py b/host/tests/pychdr_parse_test.py new file mode 100644 index 000000000..34842ad28 --- /dev/null +++ b/host/tests/pychdr_parse_test.py @@ -0,0 +1,68 @@ +# +# Copyright 2020 Ettus Research, a National Instruments Brand +# +# SPDX-License-Identifier: GPL-3.0-or-later +# +""" +Unit test for libpyuhd.chdr (CHDR Parsing API) +""" + +import unittest +from uhd.libpyuhd import chdr +from chdr_resource import hardcoded_packets +from chdr_resource import rfnoc_packets_data +from chdr_resource import rfnoc_packets_ctrl_mgmt + +# unittest doesn't support parameterized tests natively, +# rather than add another dependency just for testing, we use this +class parameterize: + """Decorate a class with this. It deletes the method named by + func_name and adds methods for every test case, appending _{name} + to the name of the function + """ + def __init__(self, func_name, names, cases): + self.func_name = func_name + self.cases = cases + self.names = names + + def __call__(self, cls): + func = getattr(cls, self.func_name) + # First remove the function + delattr(cls, self.func_name) + for case, name in zip(self.cases, self.names): + # Add a new test function for every case + def new_func(self, this_case=case): + return func(self, *this_case) + setattr(cls, self.func_name + "_" + name, new_func) + return cls + + +@parameterize("test_serialize_deserialize_eq", hardcoded_packets.names, hardcoded_packets.packets) +class CHDRParseTest(unittest.TestCase): + """ Test Python-wrapped CHDR Parser classes """ + + def test_parse_no_errors(self): + """Parse every packet in the trace we have. + This test is just looking for errors + """ + packets = [packet_data for peer in [ + rfnoc_packets_ctrl_mgmt.peer0, + rfnoc_packets_ctrl_mgmt.peer1, + rfnoc_packets_data.peer0, + rfnoc_packets_data.peer1 + ] for packet_data in peer] + for packet_data in packets: + _packet = chdr.ChdrPacket.deserialize( + chdr.ChdrWidth.W64, packet_data) + + def test_serialize_deserialize_eq(self, packet, data): + """This test serializes and then deserializes a few packets to + make sure that they survive a round trip without changing + """ + generated_data = bytes(packet.serialize()) + self.assertEqual(generated_data, data) + + generated_packet = chdr.ChdrPacket.deserialize( + chdr.ChdrWidth.W64, data) + generated_data = bytes(generated_packet.serialize()) + self.assertEqual(generated_data, data) |