Changeset - 2a4985e6a454
[Not reviewed]
stream
0 4 0
Hasan Yavuz ÖZDERYA - 7 years ago 2018-04-08 13:10:37
hy@ozderya.net
basic tests for AsciiReader
4 files changed with 78 insertions and 40 deletions:
0 comments (0 inline, 0 general)
src/asciireader.cpp
Show inline comments
 
/*
 
  Copyright © 2017 Hasan Yavuz Özderya
 
  Copyright © 2018 Hasan Yavuz Özderya
 

	
 
  This file is part of serialplot.
 

	
 
  serialplot is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  serialplot is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU General Public License for more details.
 

	
 
  You should have received a copy of the GNU General Public License
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#include <QtDebug>
 

	
 
#include "asciireader.h"
 

	
 
/// If set to this value number of channels is determined from input
 
#define NUMOFCHANNELS_AUTO   (0)
 

	
 
AsciiReader::AsciiReader(QIODevice* device, ChannelManager* channelMan,
 
                         DataRecorder* recorder, QObject* parent) :
 
    AbstractReader(device, channelMan, recorder, parent)
 
AsciiReader::AsciiReader(QIODevice* device, QObject* parent) :
 
    AbstractReader(device, parent)
 
{
 
    paused = false;
 
    discardFirstLine = true;
 

	
 
    _numOfChannels = _settingsWidget.numOfChannels();
 
    autoNumOfChannels = (_numOfChannels == NUMOFCHANNELS_AUTO);
 
    _numChannels = _settingsWidget.numOfChannels();
 
    autoNumOfChannels = (_numChannels == NUMOFCHANNELS_AUTO);
 
    delimiter = _settingsWidget.delimiter();
 

	
 
    connect(&_settingsWidget, &AsciiReaderSettings::numOfChannelsChanged,
 
            [this](unsigned value)
 
            {
 
                _numOfChannels = value;
 
                autoNumOfChannels = (_numOfChannels == NUMOFCHANNELS_AUTO);
 
                _numChannels = value;
 
                updateNumChannels(); // TODO: setting numchannels = 0, should remove all buffers
 
                                     // do we want this?
 
                autoNumOfChannels = (_numChannels == NUMOFCHANNELS_AUTO);
 
                if (!autoNumOfChannels)
 
                {
 
                    emit numOfChannelsChanged(value);
 
                }
 
            });
 

	
 
    connect(&_settingsWidget, &AsciiReaderSettings::delimiterChanged,
 
            [this](QChar d)
 
            {
 
                delimiter = d;
 
            });
 

	
 
    connect(device, &QIODevice::aboutToClose, [this](){discardFirstLine=true;});
 
}
 

	
 
QWidget* AsciiReader::settingsWidget()
 
{
 
    return &_settingsWidget;
 
}
 

	
 
unsigned AsciiReader::numOfChannels()
 
unsigned AsciiReader::numChannels() const
 
{
 
    // TODO: an alternative is to never set _numChannels to '0'
 
    // do not allow '0'
 
    if (_numOfChannels == 0)
 
    {
 
        return 1;
 
    }
 
    else
 
    {
 
        return _numOfChannels;
 
    }
 
    return _numChannels == 0 ? 1 : _numChannels;
 
}
 

	
 
// TODO: this could be a part of AbstractReader
 
void AsciiReader::enable(bool enabled)
 
{
 
    if (enabled)
 
    {
 
        discardFirstLine = true;
 
        QObject::connect(_device, &QIODevice::readyRead,
 
                         this, &AsciiReader::onDataReady);
 
    }
 
    else
 
    {
 
        QObject::disconnect(_device, 0, this, 0);
 
        disconnectSinks();
 
    }
 
}
 

	
 
void AsciiReader::pause(bool enabled)
 
{
 
    paused = enabled;
 
}
 

	
 
void AsciiReader::onDataReady()
 
{
 
    while(_device->canReadLine())
 
    {
 
@@ -122,65 +118,64 @@ void AsciiReader::onDataReady()
 
        {
 
            continue;
 
        }
 

	
 
        auto separatedValues = line.split(delimiter, QString::SkipEmptyParts);
 

	
 
        unsigned numReadChannels; // effective number of channels to read
 
        unsigned numComingChannels = separatedValues.length();
 

	
 
        if (autoNumOfChannels)
 
        {
 
            // did number of channels changed?
 
            if (numComingChannels != _numOfChannels)
 
            if (numComingChannels != _numChannels)
 
            {
 
                _numOfChannels = numComingChannels;
 
                _numChannels = numComingChannels;
 
                updateNumChannels();
 
                emit numOfChannelsChanged(numComingChannels);
 
            }
 
            numReadChannels = numComingChannels;
 
        }
 
        else if (numComingChannels >= _numOfChannels)
 
        else if (numComingChannels >= _numChannels)
 
        {
 
            numReadChannels = _numOfChannels;
 
            numReadChannels = _numChannels;
 
        }
 
        else // there is missing channel data
 
        {
 
            numReadChannels = separatedValues.length();
 
            qWarning() << "Incoming data is missing data for some channels!";
 
            qWarning() << "Read line: " << line;
 
        }
 

	
 
        // parse read line
 
        unsigned numDataBroken = 0;
 
        double* channelSamples = new double[_numOfChannels]();
 
        SamplePack samples(1, _numChannels);
 
        for (unsigned ci = 0; ci < numReadChannels; ci++)
 
        {
 
            bool ok;
 
            channelSamples[ci] = separatedValues[ci].toDouble(&ok);
 
            samples.data(ci)[0] = separatedValues[ci].toDouble(&ok);
 
            if (!ok)
 
            {
 
                qWarning() << "Data parsing error for channel: " << ci;
 
                qWarning() << "Read line: " << line;
 
                channelSamples[ci] = 0;
 
                samples.data(ci)[0] = 0;
 
                numDataBroken++;
 
            }
 
        }
 

	
 
        if (numReadChannels > numDataBroken)
 
        {
 
            // commit data
 
            addData(channelSamples, _numOfChannels);
 
            feedOut(samples);
 
        }
 

	
 
        delete[] channelSamples;
 
    }
 
}
 

	
 
void AsciiReader::saveSettings(QSettings* settings)
 
{
 
    _settingsWidget.saveSettings(settings);
 
}
 

	
 
void AsciiReader::loadSettings(QSettings* settings)
 
{
 
    _settingsWidget.loadSettings(settings);
 
}
src/asciireader.h
Show inline comments
 
/*
 
  Copyright © 2017 Hasan Yavuz Özderya
 
  Copyright © 2018 Hasan Yavuz Özderya
 

	
 
  This file is part of serialplot.
 

	
 
  serialplot is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  serialplot is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU General Public License for more details.
 
@@ -21,40 +21,39 @@
 
#define ASCIIREADER_H
 

	
 
#include <QSettings>
 

	
 
#include "abstractreader.h"
 
#include "asciireadersettings.h"
 

	
 
class AsciiReader : public AbstractReader
 
{
 
    Q_OBJECT
 

	
 
public:
 
    explicit AsciiReader(QIODevice* device, ChannelManager* channelMan,
 
                         DataRecorder* recorder, QObject *parent = 0);
 
    explicit AsciiReader(QIODevice* device, QObject *parent = 0);
 
    QWidget* settingsWidget();
 
    unsigned numOfChannels();
 
    unsigned numChannels() const;
 
    void enable(bool enabled = true);
 
    /// Stores settings into a `QSettings`
 
    void saveSettings(QSettings* settings);
 
    /// Loads settings from a `QSettings`.
 
    void loadSettings(QSettings* settings);
 

	
 
public slots:
 
    void pause(bool);
 

	
 
private:
 
    AsciiReaderSettings _settingsWidget;
 
    unsigned _numOfChannels;
 
    unsigned _numChannels;
 
    /// number of channels will be determined from incoming data
 
    unsigned autoNumOfChannels;
 
    QChar delimiter; ///< selected column delimiter
 
    bool paused;
 

	
 
    // We may have (usually true) started reading in the middle of a
 
    // line, so its a better idea to just discard first line.
 
    bool discardFirstLine;
 

	
 
private slots:
 
    void onDataReady();
 
};
tests/CMakeLists.txt
Show inline comments
 
@@ -33,36 +33,39 @@ add_executable(Test EXCLUDE_FROM_ALL
 
  ../src/linindexbuffer.cpp
 
  ../src/ringbuffer.cpp
 
  ../src/readonlybuffer.cpp
 
  ../src/stream.cpp
 
  ../src/streamchannel.cpp
 
  ../src/channelinfomodel.cpp
 
  )
 
add_test(NAME test1 COMMAND Test)
 
qt5_use_modules(Test Widgets)
 

	
 
qt5_wrap_ui(UI_FILES_T
 
  ../src/binarystreamreadersettings.ui
 
  ../src/asciireadersettings.ui
 
  ../src/numberformatbox.ui
 
  ../src/endiannessbox.ui
 
  )
 

	
 
# test for readers
 
add_executable(TestReaders EXCLUDE_FROM_ALL
 
  test_readers.cpp
 
  ../src/samplepack.cpp
 
  ../src/sink.cpp
 
  ../src/source.cpp
 
  ../src/abstractreader.cpp
 
  ../src/binarystreamreader.cpp
 
  ../src/abstractreader.cpp
 
  ../src/binarystreamreadersettings.cpp
 
  ../src/asciireader.cpp
 
  ../src/asciireadersettings.cpp
 
  ../src/endiannessbox.cpp
 
  ../src/numberformatbox.cpp
 
  ../src/numberformat.cpp
 
  ${UI_FILES_T}
 
  )
 
qt5_use_modules(TestReaders Widgets Test)
 
add_test(NAME test_readers COMMAND TestReaders)
 

	
 
set(CMAKE_CTEST_COMMAND ctest -V)
 
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND})
 
add_dependencies(check Test TestReaders)
tests/test_readers.cpp
Show inline comments
 
@@ -15,36 +15,30 @@
 

	
 
  You should have received a copy of the GNU General Public License
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
// This tells Catch to provide a main() - only do this in one cpp file per executable
 
#define CATCH_CONFIG_RUNNER
 
#include "catch.hpp"
 

	
 
#include <QSignalSpy>
 
#include <QBuffer>
 
#include "binarystreamreader.h"
 
#include "asciireader.h"
 

	
 
#include "test_helpers.h"
 

	
 
static const int READYREAD_TIMEOUT = 10; // milliseconds
 

	
 
TEST_CASE("creating a BinaryStreamReader", "[reader]")
 
{
 
    QBuffer buffer;
 

	
 
    BinaryStreamReader bs(&buffer);
 
}
 

	
 
TEST_CASE("reading data with BinaryStreamReader", "[reader]")
 
{
 
    QBuffer bufferDev;
 
    BinaryStreamReader bs(&bufferDev);
 
    bs.enable(true);
 

	
 
    TestSink sink;
 
    bs.connectSink(&sink);
 

	
 
    REQUIRE(sink._numChannels == 1);
 
    REQUIRE(sink._hasX == false);
 

	
 
@@ -71,22 +65,69 @@ TEST_CASE("disabled BinaryStreamReader s
 

	
 
    bufferDev.open(QIODevice::ReadWrite);
 
    const char data[] = {0x01, 0x02, 0x03, 0x04};
 
    bufferDev.write(data, 4);
 
    bufferDev.seek(0);
 

	
 
    QSignalSpy spy(&bufferDev, SIGNAL(readyRead()));
 
    // readyRead isn't signaled because there are no connections to it
 
    REQUIRE_FALSE(spy.wait(READYREAD_TIMEOUT));
 
    REQUIRE(sink.totalFed == 0);
 
}
 

	
 
TEST_CASE("reading data with AsciiReader", "[reader, ascii]")
 
{
 
    QBuffer bufferDev;
 
    AsciiReader reader(&bufferDev);
 
    reader.enable(true);
 

	
 
    TestSink sink;
 
    reader.connectSink(&sink);
 

	
 
    REQUIRE(sink._numChannels == 1);
 
    REQUIRE(sink._hasX == false);
 

	
 
    // inject data to the buffer
 
    bufferDev.open(QIODevice::ReadWrite);
 
    bufferDev.write("0,1,3\n0,1,3\n0,1,3\n0,1,3\n");
 
    bufferDev.seek(0);
 

	
 
    QSignalSpy spy(&bufferDev, SIGNAL(readyRead()));
 
    REQUIRE(spy.wait(READYREAD_TIMEOUT));
 
    REQUIRE(sink._numChannels == 3);
 
    REQUIRE(sink._hasX == false);
 
    REQUIRE(sink.totalFed == 3);
 
}
 

	
 
TEST_CASE("AsciiReader shouldn't read when disabled", "[reader, ascii]")
 
{
 
    QBuffer bufferDev;
 
    AsciiReader reader(&bufferDev); // disabled by default
 

	
 
    TestSink sink;
 
    reader.connectSink(&sink);
 

	
 
    REQUIRE(sink._numChannels == 1);
 
    REQUIRE(sink._hasX == false);
 

	
 
    // inject data to the buffer
 
    bufferDev.open(QIODevice::ReadWrite);
 
    bufferDev.write("0,1,3\n0,1,3\n0,1,3\n0,1,3\n");
 
    bufferDev.seek(0);
 

	
 
    QSignalSpy spy(&bufferDev, SIGNAL(readyRead()));
 
    REQUIRE_FALSE(spy.wait(READYREAD_TIMEOUT));
 
    REQUIRE(sink._numChannels == 1);
 
    REQUIRE(sink._hasX == false);
 
    REQUIRE(sink.totalFed == 0);
 
}
 

	
 
// Note: this is added because `QApplication` must be created for widgets
 
#include <QApplication>
 
int main(int argc, char* argv[])
 
{
 
    QApplication a(argc, argv);
 

	
 
    int result = Catch::Session().run( argc, argv );
 

	
 
    return result;
 
}
0 comments (0 inline, 0 general)