diff options
author | Martin Braun <martin.braun@ettus.com> | 2019-11-27 21:36:46 -0800 |
---|---|---|
committer | atrnati <54334261+atrnati@users.noreply.github.com> | 2020-01-29 08:57:25 -0600 |
commit | 0a49a8844a65698b11fe979441a97939dad80044 (patch) | |
tree | 30bbd6fb82c17abf7db4627250f3586a8d6fb175 /host/tests | |
parent | 25b434ae858dd476e13738e0acaa9edeb019cee4 (diff) | |
download | uhd-0a49a8844a65698b11fe979441a97939dad80044.tar.gz uhd-0a49a8844a65698b11fe979441a97939dad80044.tar.bz2 uhd-0a49a8844a65698b11fe979441a97939dad80044.zip |
rfnoc: Create mock factory
This is an API that allows creating mock block controllers, to write
unit tests for block controllers. See rfnoc_blocks_test for an example
how to use them.
Diffstat (limited to 'host/tests')
-rw-r--r-- | host/tests/CMakeLists.txt | 1 | ||||
-rw-r--r-- | host/tests/client_zero_test.cpp | 3 | ||||
-rw-r--r-- | host/tests/rfnoc_blocks_test.cpp | 70 | ||||
-rw-r--r-- | host/tests/rfnoc_mock_reg_iface.hpp | 138 |
4 files changed, 15 insertions, 197 deletions
diff --git a/host/tests/CMakeLists.txt b/host/tests/CMakeLists.txt index 017778fb8..bf47f6638 100644 --- a/host/tests/CMakeLists.txt +++ b/host/tests/CMakeLists.txt @@ -245,7 +245,6 @@ UHD_ADD_NONAPI_TEST( TARGET rfnoc_blocks_test.cpp EXTRA_SOURCES ${CMAKE_SOURCE_DIR}/lib/rfnoc/graph.cpp - ${CMAKE_SOURCE_DIR}/lib/rfnoc/registry_factory.cpp ) UHD_ADD_NONAPI_TEST( diff --git a/host/tests/client_zero_test.cpp b/host/tests/client_zero_test.cpp index e97dd83e2..927161418 100644 --- a/host/tests/client_zero_test.cpp +++ b/host/tests/client_zero_test.cpp @@ -5,6 +5,7 @@ // #include <uhd/rfnoc/register_iface.hpp> +#include <uhd/rfnoc/mock_block.hpp> #include <uhd/utils/log.hpp> #include <uhdlib/rfnoc/client_zero.hpp> #include <uhdlib/utils/narrow.hpp> @@ -14,8 +15,6 @@ #include <cstring> #include <memory> -#include "rfnoc_mock_reg_iface.hpp" - using namespace uhd; using namespace uhd::rfnoc; diff --git a/host/tests/rfnoc_blocks_test.cpp b/host/tests/rfnoc_blocks_test.cpp index ea1f91b6b..b9899177e 100644 --- a/host/tests/rfnoc_blocks_test.cpp +++ b/host/tests/rfnoc_blocks_test.cpp @@ -5,14 +5,12 @@ // #include "rfnoc_graph_mock_nodes.hpp" -#include "rfnoc_mock_reg_iface.hpp" +#include <uhd/rfnoc/mock_block.hpp> #include <uhd/rfnoc/actions.hpp> #include <uhd/rfnoc/ddc_block_control.hpp> #include <uhd/rfnoc/defaults.hpp> #include <uhd/rfnoc/duc_block_control.hpp> #include <uhd/rfnoc/null_block_control.hpp> -#include <uhdlib/rfnoc/clock_iface.hpp> -#include <uhdlib/rfnoc/factory.hpp> #include <uhdlib/rfnoc/graph.hpp> #include <uhdlib/rfnoc/node_accessor.hpp> #include <uhdlib/utils/narrow.hpp> @@ -21,47 +19,10 @@ using namespace uhd::rfnoc; -// Redeclare this here, since it's only defined outside of UHD_API -noc_block_base::make_args_t::~make_args_t() = default; - namespace { constexpr size_t DEFAULT_MTU = 8000; -noc_block_base::make_args_ptr make_make_args(noc_id_t noc_id, - const std::string& block_id, - const size_t n_inputs, - const size_t n_outputs, - const std::string& tb_clock_name = CLOCK_KEY_GRAPH, - const std::string& cp_clock_name = "MOCK_CLOCK") -{ - auto make_args = std::make_unique<noc_block_base::make_args_t>(); - make_args->noc_id = noc_id; - make_args->num_input_ports = n_inputs; - make_args->num_output_ports = n_outputs; - make_args->mtu = DEFAULT_MTU; - make_args->reg_iface = std::make_shared<mock_reg_iface_t>(); - make_args->block_id = block_id; - make_args->ctrlport_clk_iface = std::make_shared<clock_iface>(cp_clock_name); - make_args->tb_clk_iface = std::make_shared<clock_iface>(tb_clock_name); - make_args->tree = uhd::property_tree::make(); - return make_args; -} - -noc_block_base::sptr make_block(noc_block_base::make_args_ptr&& make_args) -{ - try { - auto block_factory_info = - factory::get_block_factory(make_args->noc_id, ANY_DEVICE); - return block_factory_info.factory_fn(std::move(make_args)); - } catch (std::out_of_range&) { - UHD_LOG_WARNING("TEST", - "Skipping tests due to Windows linker misconfiguration that needs to be " - "resolved."); - exit(0); - } -} - } // namespace BOOST_AUTO_TEST_CASE(test_null_block) @@ -72,17 +33,17 @@ BOOST_AUTO_TEST_CASE(test_null_block) constexpr uint32_t item_width = 32; constexpr noc_id_t noc_id = 0x00000001; - auto make_args = make_make_args(noc_id, "0/NullSrcSink#0", num_chans, num_chans); - auto reg_iface = std::dynamic_pointer_cast<mock_reg_iface_t>(make_args->reg_iface); - auto set_mem = [&](const uint32_t addr, const uint32_t data) { + auto block_container = get_mock_block(noc_id, num_chans, num_chans); + // Shorthand to save typing + auto& reg_iface = block_container.reg_iface; + auto set_mem = [&](const uint32_t addr, const uint32_t data) { reg_iface->read_memory[addr] = data; }; auto get_mem = [&](const uint32_t addr) { return reg_iface->write_memory[addr]; }; auto copy_mem = [&](const uint32_t addr) { set_mem(addr, get_mem(addr)); }; - set_mem(null_block_control::REG_CTRL_STATUS, (nipc << 24) | (item_width << 16)); - auto test_null = - std::dynamic_pointer_cast<null_block_control>(make_block(std::move(make_args))); + + auto test_null = block_container.get_block<null_block_control>(); BOOST_REQUIRE(test_null); using uhd::stream_cmd_t; @@ -163,15 +124,14 @@ BOOST_AUTO_TEST_CASE(test_ddc_block) constexpr noc_id_t noc_id = DDC_BLOCK; constexpr int TEST_DECIM = 20; - auto ddc_make_args = make_make_args(noc_id, "0/DDC#0", num_chans, num_chans); - ddc_make_args->args = uhd::device_addr_t("foo=bar"); - auto ddc_reg_iface = - std::dynamic_pointer_cast<mock_reg_iface_t>(ddc_make_args->reg_iface); + auto block_container = + get_mock_block(noc_id, num_chans, num_chans, uhd::device_addr_t("foo=bar")); + auto& ddc_reg_iface = block_container.reg_iface; ddc_reg_iface->read_memory[ddc_block_control::RB_COMPAT_NUM] = (ddc_block_control::MAJOR_COMPAT << 16) | ddc_block_control::MINOR_COMPAT; ddc_reg_iface->read_memory[ddc_block_control::RB_NUM_HB] = num_hb; ddc_reg_iface->read_memory[ddc_block_control::RB_CIC_MAX_DECIM] = max_cic; - auto test_ddc = make_block(std::move(ddc_make_args)); + auto test_ddc = block_container.get_block<ddc_block_control>(); BOOST_REQUIRE(test_ddc); BOOST_CHECK_EQUAL(test_ddc->get_block_args().get("foo"), "bar"); @@ -266,15 +226,13 @@ BOOST_AUTO_TEST_CASE(test_duc_block) constexpr noc_id_t noc_id = DUC_BLOCK; constexpr int TEST_INTERP = 20; // 2 halfbands, CIC==5 - auto duc_make_args = make_make_args(noc_id, "0/DUC#0", num_chans, num_chans); - duc_make_args->args = uhd::device_addr_t(); - auto duc_reg_iface = - std::dynamic_pointer_cast<mock_reg_iface_t>(duc_make_args->reg_iface); + auto block_container = get_mock_block(noc_id, num_chans, num_chans); + auto& duc_reg_iface = block_container.reg_iface; duc_reg_iface->read_memory[duc_block_control::RB_COMPAT_NUM] = (duc_block_control::MAJOR_COMPAT << 16) | duc_block_control::MINOR_COMPAT; duc_reg_iface->read_memory[duc_block_control::RB_NUM_HB] = num_hb; duc_reg_iface->read_memory[duc_block_control::RB_CIC_MAX_INTERP] = max_cic; - auto test_duc = make_block(std::move(duc_make_args)); + auto test_duc = block_container.get_block<duc_block_control>(); BOOST_REQUIRE(test_duc); node_accessor.init_props(test_duc.get()); diff --git a/host/tests/rfnoc_mock_reg_iface.hpp b/host/tests/rfnoc_mock_reg_iface.hpp deleted file mode 100644 index a6e85b790..000000000 --- a/host/tests/rfnoc_mock_reg_iface.hpp +++ /dev/null @@ -1,138 +0,0 @@ -// -// Copyright 2019 Ettus Research, a National Instruments Brand -// -// SPDX-License-Identifier: GPL-3.0-or-later -// - -#ifndef INCLUDED_LIBUHD_TESTS_MOCK_REG_IFACE_HPP -#define INCLUDED_LIBUHD_TESTS_MOCK_REG_IFACE_HPP - -#include <uhd/rfnoc/register_iface.hpp> -#include <uhd/utils/log.hpp> -#include <uhd/types/time_spec.hpp> -#include <boost/format.hpp> -#include <unordered_map> -#include <vector> - -class mock_reg_iface_t : public uhd::rfnoc::register_iface -{ -public: - mock_reg_iface_t() = default; - virtual ~mock_reg_iface_t() = default; - - /************************************************************************** - * API - *************************************************************************/ - void poke32(uint32_t addr, uint32_t data, uhd::time_spec_t time, bool ack) - { - write_memory[addr] = data; - _poke_cb(addr, data, time, ack); - } - - void multi_poke32(const std::vector<uint32_t> addrs, - const std::vector<uint32_t> data, - uhd::time_spec_t time, - bool ack) - { - if (addrs.size() != data.size()) { - throw uhd::value_error("addrs and data vectors must be of the same length"); - } - for (size_t i = 0; i < addrs.size(); i++) { - poke32(addrs[i], data[i], time, ack); - } - } - - void block_poke32(uint32_t first_addr, - const std::vector<uint32_t> data, - uhd::time_spec_t timestamp, - bool ack) - { - for (size_t i = 0; i < data.size(); i++) { - poke32(first_addr + 4 * i, data[i], timestamp, ack); - } - } - - uint32_t peek32(uint32_t addr, uhd::time_spec_t time) - { - _peek_cb(addr, time); - try { - return read_memory.at(addr); - } catch (const std::out_of_range&) { - throw uhd::runtime_error(str(boost::format("No data defined for address: 0x%04X") % addr)); - } - } - - std::vector<uint32_t> block_peek32( - uint32_t first_addr, size_t length, uhd::time_spec_t time) - { - std::vector<uint32_t> result(length, 0); - for (size_t i = 0; i < length; ++i) { - result[i] = peek32(first_addr + i * 4, time); - } - return result; - } - - void poll32(uint32_t addr, - uint32_t data, - uint32_t mask, - uhd::time_spec_t /* timeout */, - uhd::time_spec_t time = uhd::time_spec_t::ASAP, - bool /* ack */ = false) - { - if (force_timeout) { - throw uhd::op_timeout("timeout"); - } - - if ((peek32(addr, time) & mask) == data) { - UHD_LOG_INFO("MOCK_REG_IFACE", "poll32() successful at addr " << addr); - } else { - UHD_LOG_INFO("MOCK_REG_IFACE", "poll32() not successful at addr " << addr); - } - } - - void sleep(uhd::time_spec_t /*duration*/, bool /*ack*/) - { - // nop - } - - void register_async_msg_validator(async_msg_validator_t /*callback_f*/) - { - // nop - } - - void register_async_msg_handler(async_msg_callback_t /*callback_f*/) - { - // nop - } - - void set_policy(const std::string& name, const uhd::device_addr_t& args) - { - UHD_LOG_INFO("MOCK_REG_IFACE", - "Requested to set policy for " << name << " to " << args.to_string()); - } - - uint16_t get_src_epid() const - { - return 0; - } - - uint16_t get_port_num() const - { - return 0; - } - - bool force_timeout = false; - - std::unordered_map<uint32_t, uint32_t> read_memory; - std::unordered_map<uint32_t, uint32_t> write_memory; - -protected: - virtual void _poke_cb( - uint32_t /*addr*/, uint32_t /*data*/, uhd::time_spec_t /*time*/, bool /*ack*/) - { - } - virtual void _peek_cb(uint32_t /*addr*/, uhd::time_spec_t /*time*/) {} -}; - - -#endif /* INCLUDED_LIBUHD_TESTS_MOCK_REG_IFACE_HPP */ |