Skip to content

Commit

Permalink
[WIP] Decimator test
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkretz committed Jul 17, 2023
1 parent 500c6e6 commit 8218734
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
96 changes: 96 additions & 0 deletions test/qa_decimator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include <boost/ut.hpp>

#include <buffer.hpp>
#include <graph.hpp>
#include <node.hpp>
#include <reflection.hpp>
#include <scheduler.hpp>

#include <fmt/format.h>

namespace fg = fair::graph;

using std::size_t;
using ssize_t = std::make_signed_t<size_t>;

template<typename T>
struct source : public fg::node<source<T>, fg::OUT<T, 0, 1024, "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<T> 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<typename T>
struct drop_odd_samples : public fg::node<drop_odd_samples<T>, fg::IN<T, 0, 15, "in">, fg::OUT<T, 0, 15, "out">> {
bool even = true;

constexpr fg::work_return_status_t
process_bulk(std::span<const T> 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<typename T, T increment>
struct sink : public fg::node<sink<T, increment>, fg::IN<T, 0, 1024, "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<source<int>>();
auto &n1 = flow_graph.make_node<drop_odd_samples<int>>();
auto &n2 = flow_graph.make_node<sink<int, 2>>();

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() {}

0 comments on commit 8218734

Please sign in to comment.