Skip to content

Commit

Permalink
Introduce InspectSink
Browse files Browse the repository at this point in the history
This helps with writing unit tests.
  • Loading branch information
vimpostor committed Feb 15, 2024
1 parent f7e9adf commit 883019b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
16 changes: 16 additions & 0 deletions blocks/basic/include/gnuradio-4.0/basic/common_blocks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@
#include <gnuradio-4.0/Graph.hpp>
#include <gnuradio-4.0/reflection.hpp>

// A convenience class to make writing unit tests easier.
// This sink allows to inspect the input port values as a class member.
template<typename T>
class InspectSink : public gr::Block<InspectSink<T>> {
public:
T value{};
gr::PortIn<T> in;

constexpr void
processOne(T val) {
value = val;
}
};

ENABLE_REFLECTION_FOR_TEMPLATE_FULL((typename T), (InspectSink<T>), value, in);

template<typename T>
class builtin_multiply : public gr::Block<builtin_multiply<T>> {
public:
Expand Down
6 changes: 3 additions & 3 deletions core/test/qa_DynamicBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const boost::ut::suite DynamicBlocktests = [] {
// sources_count / 2 inputs on construction, and change the number
// via settings
auto &adder = testGraph.addBlock(std::make_unique<multi_adder<double>>(sources_count / 2));
auto &sink = testGraph.emplaceBlock<DebugSink<double>>({});
auto &sink = testGraph.emplaceBlock<InspectSink<double>>({});

// Function that adds a new source node to the graph, and connects
// it to one of adder's ports
Expand All @@ -80,7 +80,7 @@ const boost::ut::suite DynamicBlocktests = [] {
const auto work = sink.work(1UZ);
expect(eq(work.performed_work, 1UZ));

expect(eq(sink.lastValue, static_cast<double>((i + 1) * sources.size())));
expect(eq(sink.value, static_cast<double>((i + 1) * sources.size())));
}

// add yet another sources_count number of ports
Expand All @@ -104,7 +104,7 @@ const boost::ut::suite DynamicBlocktests = [] {
const auto sink_work = sink.work(1UZ);
expect(eq(sink_work.performed_work, 1UZ));

expect(eq(sink.lastValue, static_cast<double>((i + 1) * sources_count + (i - events_count + 1) * sources_count)));
expect(eq(sink.value, static_cast<double>((i + 1) * sources_count + (i - events_count + 1) * sources_count)));
}
};
};
Expand Down

0 comments on commit 883019b

Please sign in to comment.