diff --git a/src/sink.cpp b/src/sink.cpp --- a/src/sink.cpp +++ b/src/sink.cpp @@ -25,6 +25,7 @@ void Sink::connectFollower(Sink* sink) Q_ASSERT(!followers.contains(sink)); followers.append(sink); + sink->setNumChannels(numChannels(), hasX()); } void Sink::disconnectFollower(Sink* sink) diff --git a/src/sink.h b/src/sink.h --- a/src/sink.h +++ b/src/sink.h @@ -38,10 +38,14 @@ public: protected: /// Entry point for incoming data. Re-implementations should /// call this function to feed followers. - void feedIn(const SamplePack& data); + virtual void feedIn(const SamplePack& data); /// Is set by connected source. Re-implementations should call /// this function to update followers. - void setNumChannels(unsigned nc, bool x); + virtual void setNumChannels(unsigned nc, bool x); + /// Returns true if sink has X data + virtual bool hasX() const = 0; + /// Returns number of channels + virtual unsigned numChannels() const = 0; friend Source; diff --git a/src/source.cpp b/src/source.cpp --- a/src/source.cpp +++ b/src/source.cpp @@ -43,7 +43,6 @@ void Source::feedOut(const SamplePack& d } } -/// Updates "number of channels" of connected sinks void Source::feedNumChannels(unsigned nc, bool x) const { for (auto sink : sinks) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -23,7 +23,11 @@ find_package(Qt5Widgets) include_directories("../src") add_executable(Test EXCLUDE_FROM_ALL - test.cpp ../src/samplepack.cpp) + test.cpp + ../src/samplepack.cpp + ../src/sink.cpp + ../src/source.cpp + ) add_test(NAME test1 COMMAND Test) qt5_use_modules(Test Widgets) diff --git a/tests/test.cpp b/tests/test.cpp --- a/tests/test.cpp +++ b/tests/test.cpp @@ -21,6 +21,7 @@ #include "catch.hpp" #include "samplepack.h" +#include "source.h" TEST_CASE("samplepack with no X", "[memory]") { @@ -47,3 +48,74 @@ TEST_CASE("samplepack with X", "[memory] REQUIRE(pack.numSamples() == 100); REQUIRE(pack.xData() != nullptr); } + +class TestSink : public Sink +{ +public: + int totalFed; + int _numChannels; + bool _hasX; + + TestSink() + { + totalFed = 0; + _numChannels = 0; + _hasX = false; + }; + +public: + void feedIn(const SamplePack& data) + { + REQUIRE(data.numChannels() == numChannels()); + + totalFed += data.numSamples(); + + Sink::feedIn(data); + }; + + void setNumChannels(unsigned nc, bool x) + { + _numChannels = nc; + _hasX = x; + + Sink::setNumChannels(nc, x); + }; + + virtual unsigned numChannels() const + { + return _numChannels; + }; + + virtual bool hasX() const + { + return _hasX; + }; +}; + +TEST_CASE("sink", "[memory, stream]") +{ + TestSink sink; + SamplePack pack(100, 3, false); + + sink.setNumChannels(3, false); + REQUIRE(sink.numChannels() == 3); + + sink.feedIn(pack); + REQUIRE(sink.totalFed == 100); + sink.feedIn(pack); + REQUIRE(sink.totalFed == 200); + + TestSink follower; + + sink.connectFollower(&follower); + REQUIRE(follower.numChannels() == 3); + REQUIRE(follower.hasX() == false); + + sink.feedIn(pack); + REQUIRE(sink.totalFed == 300); + REQUIRE(follower.totalFed == 100); + + sink.setNumChannels(2, true); + REQUIRE(follower.numChannels() == 2); + REQUIRE(follower.hasX() == true); +}