KPN/tests/test_node.cpp
2026-05-08 17:48:16 +02:00

50 lines
1.5 KiB
C++

#include <catch2/catch_test_macros.hpp>
#include <kpn/node.hpp>
#include <chrono>
#include <thread>
using namespace kpn;
static int double_it(int x) { return x * 2; }
static std::tuple<int, float> split_it(int x) { return {x, float(x) * 0.5f}; }
static void consume_it(int x) { (void)x; }
TEST_CASE("node input/output counts", "[node]") {
STATIC_REQUIRE(Node<double_it>::input_count == 1);
STATIC_REQUIRE(Node<double_it>::output_count == 1);
STATIC_REQUIRE(Node<split_it>::output_count == 2);
STATIC_REQUIRE(Node<consume_it>::output_count == 0);
}
TEST_CASE("node named port index resolution", "[node]") {
using N = Node<double_it, in<"value">, out<"result">>;
STATIC_REQUIRE(index_of<fixed_string("value"), fixed_string("value")>() == 0);
STATIC_REQUIRE(index_of<fixed_string("result"), fixed_string("result")>() == 0);
}
TEST_CASE("node processes a single item end-to-end", "[node]") {
Node<double_it> src_node(5); // used only as input source placeholder
Node<double_it> node(5);
// Manually wire: push to input channel, connect a downstream channel, run one item
auto& in_ch = node.input_channel<0>();
Channel<int> out_ch(5);
node.set_output_channel<0>(&out_ch);
node.start();
in_ch.push(21);
int result = out_ch.pop();
node.stop();
REQUIRE(result == 42);
}
TEST_CASE("node stop unblocks cleanly", "[node]") {
Node<double_it> node(5);
node.start();
// Node is blocked waiting for input — stop() must return without deadlock
node.stop();
REQUIRE_FALSE(node.running());
}