Changeset - 0bcc3039cc7c
[Not reviewed]
stream
0 5 0
Hasan Yavuz Ă–ZDERYA - 8 years ago 2017-12-03 14:34:59
hy@ozderya.net
added tests for Sink class and some improvements
5 files changed with 84 insertions and 4 deletions:
0 comments (0 inline, 0 general)
src/sink.cpp
Show inline comments
 
@@ -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)
src/sink.h
Show inline comments
 
@@ -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;
 

	
src/source.cpp
Show inline comments
 
@@ -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)
tests/CMakeLists.txt
Show inline comments
 
@@ -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)
 

	
tests/test.cpp
Show inline comments
 
@@ -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);
 
}
0 comments (0 inline, 0 general)