# HG changeset patch # User Hasan Yavuz ÖZDERYA # Date 2018-04-08 13:10:37 # Node ID 2a4985e6a454e06ea7df9a30381eb918dc1ec1ed # Parent 6a8b86244dace50d85c5191d41cc168c84f10fda basic tests for AsciiReader diff --git a/src/asciireader.cpp b/src/asciireader.cpp --- a/src/asciireader.cpp +++ b/src/asciireader.cpp @@ -1,5 +1,5 @@ /* - Copyright © 2017 Hasan Yavuz Özderya + Copyright © 2018 Hasan Yavuz Özderya This file is part of serialplot. @@ -24,22 +24,23 @@ /// 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); @@ -60,17 +61,11 @@ 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 @@ -85,6 +80,7 @@ void AsciiReader::enable(bool enabled) else { QObject::disconnect(_device, 0, this, 0); + disconnectSinks(); } } @@ -131,16 +127,17 @@ void AsciiReader::onDataReady() 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 { @@ -151,16 +148,16 @@ void AsciiReader::onDataReady() // 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++; } } @@ -168,10 +165,8 @@ void AsciiReader::onDataReady() if (numReadChannels > numDataBroken) { // commit data - addData(channelSamples, _numOfChannels); + feedOut(samples); } - - delete[] channelSamples; } } diff --git a/src/asciireader.h b/src/asciireader.h --- a/src/asciireader.h +++ b/src/asciireader.h @@ -1,5 +1,5 @@ /* - Copyright © 2017 Hasan Yavuz Özderya + Copyright © 2018 Hasan Yavuz Özderya This file is part of serialplot. @@ -30,10 +30,9 @@ class AsciiReader : public AbstractReade 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); @@ -45,7 +44,7 @@ public slots: private: AsciiReaderSettings _settingsWidget; - unsigned _numOfChannels; + unsigned _numChannels; /// number of channels will be determined from incoming data unsigned autoNumOfChannels; QChar delimiter; ///< selected column delimiter diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -42,6 +42,7 @@ qt5_use_modules(Test Widgets) qt5_wrap_ui(UI_FILES_T ../src/binarystreamreadersettings.ui + ../src/asciireadersettings.ui ../src/numberformatbox.ui ../src/endiannessbox.ui ) @@ -52,9 +53,11 @@ add_executable(TestReaders EXCLUDE_FROM_ ../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 diff --git a/tests/test_readers.cpp b/tests/test_readers.cpp --- a/tests/test_readers.cpp +++ b/tests/test_readers.cpp @@ -24,18 +24,12 @@ #include #include #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; @@ -80,6 +74,53 @@ TEST_CASE("disabled BinaryStreamReader s 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 int main(int argc, char* argv[])