diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 1b34313eb..9ece17cc6 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -29,6 +29,7 @@ endfunction() add_ut_test(qa_buffer) add_ut_test(qa_data_sink) +add_ut_test(qa_decimator) add_ut_test(qa_dynamic_node) add_ut_test(qa_dynamic_port) add_ut_test(qa_fft) diff --git a/test/qa_decimator.cpp b/test/qa_decimator.cpp new file mode 100644 index 000000000..51148ee7b --- /dev/null +++ b/test/qa_decimator.cpp @@ -0,0 +1,96 @@ +#include + +#include +#include +#include +#include +#include + +#include + +namespace fg = fair::graph; + +using std::size_t; +using ssize_t = std::make_signed_t; + +template +struct source : public fg::node, fg::OUT> { + ssize_t produced = 0; + T value = 1; + + constexpr ssize_t + available_samples(const source&) const noexcept { + return 64 - produced; + } + + constexpr fg::work_return_status_t + process_bulk(std::span out) noexcept { + if (available_samples(*this) <= 0) { + return fg::work_return_status_t::ERROR; + } + for (T &x : out) { + x = value++; + } + produced += ssize_t(out.size()); + const auto ret = (available_samples(*this) <= 0) ? fg::work_return_status_t::DONE : fg::work_return_status_t::OK; + fmt::println("source::process_bulk; produced = {}, returning {}", produced, int(ret)); + return ret; + } +}; + +template +struct drop_odd_samples : public fg::node, fg::IN, fg::OUT> { + bool even = true; + + constexpr fg::work_return_status_t + process_bulk(std::span in, fg::PublishableSpan auto& out) noexcept { + boost::ut::expect(in.size() <= 15); + boost::ut::expect(out.size() <= 15); + const size_t to_publish = (in.size() + even) / 2; + boost::ut::expect(to_publish <= out.size()); + for (size_t i = 0; i < in.size(); ++i) { + if (even) { + out[i / 2] = in[i]; + } + even = !even; + } + out.publish(to_publish); + fmt::println("drop_odd_samples::process_bulk received {} samples and published {} samples", in.size(), to_publish); + return fg::work_return_status_t::OK; + } +}; + +template +struct sink : public fg::node, fg::IN> { + T expect_next = 1; + + constexpr void + process_one(T x) noexcept { + using namespace boost::ut; + expect(eq(x, expect_next)); + expect_next += increment; + } +}; + +const boost::ut::suite simple_decimator = [] { + using namespace boost::ut; + + "drop odd"_test = [] { + fg::graph flow_graph; + + auto &n0 = flow_graph.make_node>(); + auto &n1 = flow_graph.make_node>(); + auto &n2 = flow_graph.make_node>(); + + expect(eq(flow_graph.connect<"out">(n0).to<"in">(n1), fg::connection_result_t::SUCCESS)); + expect(eq(flow_graph.connect<"out">(n1).to<"in">(n2), fg::connection_result_t::SUCCESS)); + + fg::scheduler::simple sched{ std::move(flow_graph) }; + fmt::println("drop odd starts"); + sched.start(); + fmt::println("drop odd done"); + }; +}; + +int +main() {}