From e5acf1279e617786442175a794d44ed66e7783a1 Mon Sep 17 00:00:00 2001 From: sbashford Date: Wed, 30 Oct 2019 14:00:25 -0500 Subject: [PATCH] Replay flagged targets 10 29 19 (#14) --- .../adaptive-track/src/AdaptiveTrack.cpp | 2 +- .../include/av-speech-in-noise/Model.hpp | 6 +- .../include/presentation/Presenter.hpp | 1 - .../presentation/src/Presenter.cpp | 39 ++- .../include/recognition-test/Model.hpp | 1 + .../recognition-test/src/AdaptiveMethod.cpp | 2 +- .../recognition-test/src/FixedLevelMethod.cpp | 4 +- .../recognition-test/src/OutputFile.cpp | 4 +- .../src/ResponseEvaluator.cpp | 14 +- .../src/TrackSettingsInterpreter.cpp | 4 +- .../stimulus-players/src/AudioReaderImpl.cpp | 4 +- .../stimulus-players/src/MaskerPlayerImpl.cpp | 14 +- .../target-list/RandomizedTargetList.hpp | 3 +- .../target-list/src/RandomizedTargetList.cpp | 6 + .../src/SubdirectoryTargetListReader.cpp | 6 +- tests/AdaptiveMethodTests.cpp | 6 +- tests/AdaptiveTrackTests.cpp | 28 +- tests/AudioReaderStub.h | 11 +- tests/AudioReaderTests.cpp | 10 +- tests/DirectoryReaderStub.h | 6 +- tests/FixedLevelMethodTests.cpp | 293 +++++++++++------- tests/LogString.h | 20 +- tests/MaskerPlayerStub.h | 3 +- tests/MaskerPlayerTests.cpp | 42 +-- tests/ModelEventListenerStub.h | 6 +- tests/ModelTests.cpp | 8 +- tests/OutputFileStub.h | 3 +- tests/OutputFileTests.cpp | 34 +- tests/PresenterTests.h | 55 ++-- tests/RecognitionTestModelTests.cpp | 49 +-- tests/ResponseEvaluatorStub.h | 30 +- tests/ResponseEvaluatorTests.cpp | 12 +- tests/SubdirectoryTargetListReaderTests.cpp | 2 +- tests/TargetListSetReaderStub.h | 8 +- tests/TargetListStub.h | 14 +- tests/TargetListTests.cpp | 90 ++++-- tests/TargetPlayerStub.h | 10 +- tests/TargetPlayerTests.cpp | 24 +- tests/TestConcluderStub.h | 4 +- tests/TrackSettingsInterpreterTests.cpp | 34 +- tests/TrackSettingsReaderTests.cpp | 8 +- tests/TrackStub.h | 14 +- tests/assert-utility.h | 9 +- 43 files changed, 539 insertions(+), 404 deletions(-) diff --git a/av-speech-in-noise/adaptive-track/src/AdaptiveTrack.cpp b/av-speech-in-noise/adaptive-track/src/AdaptiveTrack.cpp index 86b215bd7..100b9ec17 100644 --- a/av-speech-in-noise/adaptive-track/src/AdaptiveTrack.cpp +++ b/av-speech-in-noise/adaptive-track/src/AdaptiveTrack.cpp @@ -5,7 +5,7 @@ AdaptiveTrack::AdaptiveTrack(const Settings &p) : x_{p.startingX}, ceiling_{p.ceiling}, floor_{p.floor}, bumpLimit_{p.bumpLimit}, bumpCount_{0} { for (const auto &sequence : *p.rule) - if (sequence.runCount) { + if (sequence.runCount != 0) { stepSizes.push_back(sequence.stepSize); runCounts.push_back(sequence.runCount); up_.push_back(sequence.up); diff --git a/av-speech-in-noise/av-speech-in-noise/include/av-speech-in-noise/Model.hpp b/av-speech-in-noise/av-speech-in-noise/include/av-speech-in-noise/Model.hpp index 4dbcea328..65d188dcd 100644 --- a/av-speech-in-noise/av-speech-in-noise/include/av-speech-in-noise/Model.hpp +++ b/av-speech-in-noise/av-speech-in-noise/include/av-speech-in-noise/Model.hpp @@ -24,8 +24,8 @@ struct Trial { }; struct AdaptiveTrial : Trial { - int SNR_dB; - int reversals; + int SNR_dB{}; + int reversals{}; }; struct FixedLevelTrial : Trial {}; @@ -82,7 +82,7 @@ struct AdaptiveTest : Test { }; struct FixedLevelTest : Test { - int snr_dB; + int snr_dB{}; int trials{30}; }; diff --git a/av-speech-in-noise/presentation/include/presentation/Presenter.hpp b/av-speech-in-noise/presentation/include/presentation/Presenter.hpp index a72488c7b..a42168d83 100644 --- a/av-speech-in-noise/presentation/include/presentation/Presenter.hpp +++ b/av-speech-in-noise/presentation/include/presentation/Presenter.hpp @@ -197,7 +197,6 @@ class Presenter : public Model::EventListener { bool method(Method m); int readMaskerLevel(); int readCalibrationLevel(); - int readInteger(std::string x, std::string identifier); bool auditoryOnly(); View::TestSetup *view; diff --git a/av-speech-in-noise/presentation/src/Presenter.cpp b/av-speech-in-noise/presentation/src/Presenter.cpp index 5fae0f352..cd34a66b8 100644 --- a/av-speech-in-noise/presentation/src/Presenter.cpp +++ b/av-speech-in-noise/presentation/src/Presenter.cpp @@ -110,8 +110,7 @@ auto Presenter::trialCompletionHandler() -> TrialCompletionHandler * { return &adaptiveOpenSetTrialCompletionHandler; if (fixedLevelClosedSet()) return &fixedLevelClosedSetTrialCompletionHandler; - else - return &fixedLevelOpenSetTrialCompletionHandler; + return &fixedLevelOpenSetTrialCompletionHandler; } void Presenter::showErrorMessage(std::string e) { @@ -243,6 +242,19 @@ void Presenter::TestSetup::show() { view->show(); } void Presenter::TestSetup::hide() { view->hide(); } +static int readInteger(const std::string &x, const std::string &identifier) { + try { + return std::stoi(x); + } catch (const std::invalid_argument &) { + std::stringstream stream; + stream << '\'' << x << '\''; + stream << " is not a valid "; + stream << identifier; + stream << '.'; + throw BadInput{stream.str()}; + } +} + FixedLevelTest Presenter::TestSetup::fixedLevelTest() { FixedLevelTest p; commonTest(p); @@ -283,19 +295,6 @@ Condition Presenter::TestSetup::readCondition() { return auditoryOnly() ? Condition::auditoryOnly : Condition::audioVisual; } -int Presenter::TestSetup::readInteger(std::string x, std::string identifier) { - try { - return std::stoi(x); - } catch (const std::invalid_argument &) { - std::stringstream stream; - stream << '\'' << std::move(x) << '\''; - stream << " is not a valid "; - stream << std::move(identifier); - stream << '.'; - throw BadInput{stream.str()}; - } -} - bool Presenter::TestSetup::auditoryOnly() { return view->condition() == conditionName(Condition::auditoryOnly); } @@ -410,7 +409,7 @@ void Presenter::Subject::hideResponseButtons() { view->hideResponseButtons(); } void Presenter::Subject::showResponseButtons() { view->showResponseButtons(); } coordinate_response_measure::Response Presenter::Subject::subjectResponse() { - coordinate_response_measure::Response p; + coordinate_response_measure::Response p{}; p.color = colorResponse(); p.number = std::stoi(view->numberResponse()); return p; @@ -419,12 +418,12 @@ coordinate_response_measure::Response Presenter::Subject::subjectResponse() { coordinate_response_measure::Color Presenter::Subject::colorResponse() { if (view->greenResponse()) return coordinate_response_measure::Color::green; - else if (view->blueResponse()) + if (view->blueResponse()) return coordinate_response_measure::Color::blue; - else if (view->whiteResponse()) + if (view->whiteResponse()) return coordinate_response_measure::Color::white; - else - return coordinate_response_measure::Color::red; + + return coordinate_response_measure::Color::red; } Presenter::Testing::Testing(View::Testing *view) : view{view} { diff --git a/av-speech-in-noise/recognition-test/include/recognition-test/Model.hpp b/av-speech-in-noise/recognition-test/include/recognition-test/Model.hpp index 6715ef80a..bed7ef85e 100644 --- a/av-speech-in-noise/recognition-test/include/recognition-test/Model.hpp +++ b/av-speech-in-noise/recognition-test/include/recognition-test/Model.hpp @@ -13,6 +13,7 @@ class TargetList { virtual std::string next() = 0; virtual std::string current() = 0; virtual bool empty() = 0; + virtual void reinsertCurrent() = 0; }; class ResponseEvaluator { diff --git a/av-speech-in-noise/recognition-test/src/AdaptiveMethod.cpp b/av-speech-in-noise/recognition-test/src/AdaptiveMethod.cpp index 2bb0be639..08f394f99 100644 --- a/av-speech-in-noise/recognition-test/src/AdaptiveMethod.cpp +++ b/av-speech-in-noise/recognition-test/src/AdaptiveMethod.cpp @@ -29,7 +29,7 @@ void AdaptiveMethodImpl::selectNextListAfter(void (AdaptiveMethodImpl::*f)()) { void AdaptiveMethodImpl::makeSnrTracks() { targetListsWithTracks.clear(); - for (auto list : lists) + for (const auto& list : lists) makeTrackWithList(list.get()); } diff --git a/av-speech-in-noise/recognition-test/src/FixedLevelMethod.cpp b/av-speech-in-noise/recognition-test/src/FixedLevelMethod.cpp index 1a39c5bee..2588ae24d 100644 --- a/av-speech-in-noise/recognition-test/src/FixedLevelMethod.cpp +++ b/av-speech-in-noise/recognition-test/src/FixedLevelMethod.cpp @@ -48,7 +48,9 @@ void FixedLevelMethodImpl::submitIncorrectResponse() {} void FixedLevelMethodImpl::submitCorrectResponse() {} -void FixedLevelMethodImpl::submitResponse(const FreeResponse &) { +void FixedLevelMethodImpl::submitResponse(const FreeResponse &response) { + if (response.flagged) + targetList->reinsertCurrent(); complete_ = concluder->complete(targetList); } } diff --git a/av-speech-in-noise/recognition-test/src/OutputFile.cpp b/av-speech-in-noise/recognition-test/src/OutputFile.cpp index be7995c40..675302d55 100644 --- a/av-speech-in-noise/recognition-test/src/OutputFile.cpp +++ b/av-speech-in-noise/recognition-test/src/OutputFile.cpp @@ -7,8 +7,8 @@ class FormattedStream { std::stringstream stream; public: - template void writeLabeledLine(std::string label, T thing) { - stream << std::move(label); + template void writeLabeledLine(const std::string& label, T thing) { + stream << label; stream << ": "; stream << thing; stream << '\n'; diff --git a/av-speech-in-noise/recognition-test/src/ResponseEvaluator.cpp b/av-speech-in-noise/recognition-test/src/ResponseEvaluator.cpp index afe784d3a..f1a176217 100644 --- a/av-speech-in-noise/recognition-test/src/ResponseEvaluator.cpp +++ b/av-speech-in-noise/recognition-test/src/ResponseEvaluator.cpp @@ -30,14 +30,14 @@ int ResponseEvaluatorImpl::correctNumber_(const std::string &filePath) { unsigned long ResponseEvaluatorImpl::leadingPathLength( const std::string &filePath) { - return filePath.find_last_of("/") + 1; + return filePath.find_last_of('/') + 1; } long ResponseEvaluatorImpl::colorNameLength( const std::string &filePath, unsigned long leadingPathLength_) { auto fileNameBeginning = filePath.begin() + leadingPathLength_; auto notAlpha = std::find_if(fileNameBeginning, filePath.end(), - [](unsigned char c) { return !std::isalpha(c); }); + [](unsigned char c) { return std::isalpha(c) == 0; }); return std::distance(fileNameBeginning, notAlpha); } @@ -54,14 +54,14 @@ coordinate_response_measure::Color ResponseEvaluatorImpl::color( using coordinate_response_measure::Color; if (colorName == "green") return Color::green; - else if (colorName == "blue") + if (colorName == "blue") return Color::blue; - else if (colorName == "red") + if (colorName == "red") return Color::red; - else if (colorName == "white") + if (colorName == "white") return Color::white; - else - return Color::notAColor; + + return Color::notAColor; } std::string ResponseEvaluatorImpl::fileName(const std::string &filePath) { diff --git a/av-speech-in-noise/recognition-test/src/TrackSettingsInterpreter.cpp b/av-speech-in-noise/recognition-test/src/TrackSettingsInterpreter.cpp index 9b7d807c0..2cefaac8e 100644 --- a/av-speech-in-noise/recognition-test/src/TrackSettingsInterpreter.cpp +++ b/av-speech-in-noise/recognition-test/src/TrackSettingsInterpreter.cpp @@ -10,7 +10,7 @@ class Stream { bool failed_{}; public: - explicit Stream(std::string s) : stream{std::move(s)} {} + explicit Stream(const std::string& s) : stream{s} {} bool nextLine() { if (!std::getline(stream, lastLine_)) @@ -78,7 +78,7 @@ static void (*propertyApplication(const std::string &s))( const TrackingRule *TrackSettingsInterpreterImpl::trackingRule(std::string s) { rule_.clear(); - auto stream = Stream{std::move(s)}; + auto stream = Stream{s}; while (stream.nextLine()) { auto sequenceCount{0U}; auto f = propertyApplication(stream.propertyName()); diff --git a/av-speech-in-noise/stimulus-players/src/AudioReaderImpl.cpp b/av-speech-in-noise/stimulus-players/src/AudioReaderImpl.cpp index 484e52ffd..c0c0f6e4f 100644 --- a/av-speech-in-noise/stimulus-players/src/AudioReaderImpl.cpp +++ b/av-speech-in-noise/stimulus-players/src/AudioReaderImpl.cpp @@ -7,13 +7,13 @@ AudioReaderImpl::AudioReaderImpl(BufferedAudioReader *reader) std::vector> AudioReaderImpl::read(std::string filePath) { loadFile(std::move(filePath)); std::vector> audio{}; - float minimumSample = reader->minimumPossibleSample(); + auto minimumSample = reader->minimumPossibleSample(); for (auto buffer = reader->readNextBuffer(); !buffer->empty(); buffer = reader->readNextBuffer()) { audio.resize(buffer->channels()); for (int channel = 0; channel < buffer->channels(); ++channel) for (auto x : buffer->channel(channel)) - audio.at(channel).push_back(x / minimumSample); + audio.at(channel).push_back(1.F * x / minimumSample); } return audio; } diff --git a/av-speech-in-noise/stimulus-players/src/MaskerPlayerImpl.cpp b/av-speech-in-noise/stimulus-players/src/MaskerPlayerImpl.cpp index 6b9b431a9..1bb447df5 100644 --- a/av-speech-in-noise/stimulus-players/src/MaskerPlayerImpl.cpp +++ b/av-speech-in-noise/stimulus-players/src/MaskerPlayerImpl.cpp @@ -1,5 +1,6 @@ #include "MaskerPlayerImpl.hpp" #include +#include namespace stimulus_players { MaskerPlayerImpl::MaskerPlayerImpl( @@ -68,7 +69,7 @@ void MaskerPlayerImpl::setFadeInOutSeconds(double x) { } void MaskerPlayerImpl::setAudioDevice(std::string device) { - player->setDevice(findDeviceIndex(std::move(device))); + player->setDevice(findDeviceIndex(device)); } int MaskerPlayerImpl::findDeviceIndex(const std::string &device) { @@ -198,12 +199,13 @@ void MaskerPlayerImpl::AudioThread::prepareToFadeOut() { } int MaskerPlayerImpl::AudioThread::levelTransitionSamples() { - return sharedAtomics->fadeInOutSeconds.load() * player->sampleRateHz(); + return gsl::narrow_cast( + sharedAtomics->fadeInOutSeconds.load() * player->sampleRateHz()); } void MaskerPlayerImpl::AudioThread::scaleAudio( const std::vector> &audio) { - if (audio.size() == 0) + if (audio.empty()) return; auto firstChannel = audio.front(); @@ -211,15 +213,15 @@ void MaskerPlayerImpl::AudioThread::scaleAudio( for (int i = 0; i < firstChannel.size(); ++i) { auto fadeScalar_ = fadeScalar(); updateFadeState(); - for (size_t channel = 0; channel < audio.size(); ++channel) - audio.at(channel).at(i) *= fadeScalar_ * levelScalar_; + for (auto channel : audio) + channel.at(i) *= fadeScalar_ * levelScalar_; } } static const auto pi = std::acos(-1); double MaskerPlayerImpl::AudioThread::fadeScalar() { - const auto squareRoot = halfWindowLength + const auto squareRoot = halfWindowLength != 0 ? std::sin((pi * hannCounter) / (2 * halfWindowLength)) : 1; return squareRoot * squareRoot; diff --git a/av-speech-in-noise/target-list/include/target-list/RandomizedTargetList.hpp b/av-speech-in-noise/target-list/include/target-list/RandomizedTargetList.hpp index 9f8b82290..a47507259 100644 --- a/av-speech-in-noise/target-list/include/target-list/RandomizedTargetList.hpp +++ b/av-speech-in-noise/target-list/include/target-list/RandomizedTargetList.hpp @@ -32,7 +32,7 @@ class RandomizedTargetList : public av_speech_in_noise::TargetList { std::string next() override; std::string current() override; bool empty() override; - + void reinsertCurrent() override; private: std::string fullPath(std::string file); void shuffle(); @@ -66,6 +66,7 @@ class RandomizedFiniteTargetList : public av_speech_in_noise::TargetList { void loadFromDirectory(std::string directory) override; std::string next() override; std::string current() override; + void reinsertCurrent() override; private: bool empty_(); diff --git a/av-speech-in-noise/target-list/src/RandomizedTargetList.cpp b/av-speech-in-noise/target-list/src/RandomizedTargetList.cpp index 93664fe74..f1b4f362a 100644 --- a/av-speech-in-noise/target-list/src/RandomizedTargetList.cpp +++ b/av-speech-in-noise/target-list/src/RandomizedTargetList.cpp @@ -42,6 +42,8 @@ std::string RandomizedTargetList::fullPath(std::string file) { std::string RandomizedTargetList::current() { return fullPath(currentFile_); } +void RandomizedTargetList::reinsertCurrent() {} + RandomizedFiniteTargetList::RandomizedFiniteTargetList( DirectoryReader *reader, Randomizer *randomizer) : reader{reader}, randomizer{randomizer} {} @@ -72,4 +74,8 @@ std::string RandomizedFiniteTargetList::fullPath(std::string file) { std::string RandomizedFiniteTargetList::current() { return fullPath(currentFile_); } + +void RandomizedFiniteTargetList::reinsertCurrent() { + files.push_back(currentFile_); +} } diff --git a/av-speech-in-noise/target-list/src/SubdirectoryTargetListReader.cpp b/av-speech-in-noise/target-list/src/SubdirectoryTargetListReader.cpp index 096d54fd7..07bc7b3b9 100644 --- a/av-speech-in-noise/target-list/src/SubdirectoryTargetListReader.cpp +++ b/av-speech-in-noise/target-list/src/SubdirectoryTargetListReader.cpp @@ -8,9 +8,11 @@ SubdirectoryTargetListReader::SubdirectoryTargetListReader( auto SubdirectoryTargetListReader::read(std::string directory) -> lists_type { lists_type lists{}; auto subDirectories_ = subDirectories(directory); - for (auto d : subDirectories_) { + for (const auto &subDirectory : subDirectories_) { lists.push_back(targetListFactory->make()); - lists.back()->loadFromDirectory(directory + "/" + d); + auto fullPath = directory; + fullPath.append("/" + subDirectory); + lists.back()->loadFromDirectory(std::move(fullPath)); } if (subDirectories_.empty()) { lists.push_back(targetListFactory->make()); diff --git a/tests/AdaptiveMethodTests.cpp b/tests/AdaptiveMethodTests.cpp index 685b34314..b45ad04eb 100644 --- a/tests/AdaptiveMethodTests.cpp +++ b/tests/AdaptiveMethodTests.cpp @@ -15,7 +15,7 @@ class TrackSettingsReaderStub : public TrackSettingsReader { std::string filePath_{}; public: - auto filePath() const { return filePath_; } + [[nodiscard]] auto filePath() const { return filePath_; } const TrackingRule *read(std::string s) override { filePath_ = std::move(s); @@ -195,7 +195,7 @@ class AdaptiveMethodTests : public ::testing::Test { WritingCorrectResponse writingCorrectResponse{outputFile}; WritingIncorrectResponse writingIncorrectResponse{outputFile}; AdaptiveTest test; - coordinate_response_measure::Response coordinateResponse; + coordinate_response_measure::Response coordinateResponse{}; TrackingRule targetLevelRule_; std::vector> lists; std::vector> tracks; @@ -326,7 +326,7 @@ class AdaptiveMethodTests : public ::testing::Test { assertEqual(4, useCase.writtenSnr(outputFile)); } - auto blueColor() { return coordinate_response_measure::Color::blue; } + static auto blueColor() { return coordinate_response_measure::Color::blue; } bool writtenCoordinateResponseTrialCorrect() { return writtenCoordinateResponseTrial().correct; diff --git a/tests/AdaptiveTrackTests.cpp b/tests/AdaptiveTrackTests.cpp index cba453f29..539e20aa3 100644 --- a/tests/AdaptiveTrackTests.cpp +++ b/tests/AdaptiveTrackTests.cpp @@ -75,54 +75,58 @@ class AdaptiveTrackTests : public ::testing::Test { void setFirstSequenceDown(int x) { firstSequence().down = x; } - void down(AdaptiveTrackFacade &track) { track.down(); } + static void down(AdaptiveTrackFacade &track) { track.down(); } - void up(AdaptiveTrackFacade &track) { track.up(); } + static void up(AdaptiveTrackFacade &track) { track.up(); } template void assertXEquals(AdaptiveTrackFacade &track, T x) { track.assertXEquals(x); } - void assertIncomplete(AdaptiveTrackFacade &track) { + static void assertIncomplete(AdaptiveTrackFacade &track) { track.assertIncomplete(); } - void assertComplete(AdaptiveTrackFacade &track) { track.assertComplete(); } + static void assertComplete(AdaptiveTrackFacade &track) { + track.assertComplete(); + } - void update(AdaptiveTrackFacade &track, const std::string &s) { + static void update(AdaptiveTrackFacade &track, const std::string &s) { track.update(s); } - void assertReversalsEquals(AdaptiveTrackFacade &track, int n) { + static void assertReversalsEquals(AdaptiveTrackFacade &track, int n) { track.assertReversalsEquals(n); } - void assertReversalsEqualsAfterDown(AdaptiveTrackFacade &track, int n) { + static void assertReversalsEqualsAfterDown( + AdaptiveTrackFacade &track, int n) { down(track); assertReversalsEquals(track, n); } - void assertReversalsEqualsAfterUp(AdaptiveTrackFacade &track, int n) { + static void assertReversalsEqualsAfterUp( + AdaptiveTrackFacade &track, int n) { up(track); assertReversalsEquals(track, n); } - void assertIncompleteAfterDown(AdaptiveTrackFacade &track) { + static void assertIncompleteAfterDown(AdaptiveTrackFacade &track) { down(track); assertIncomplete(track); } - void assertIncompleteAfterUp(AdaptiveTrackFacade &track) { + static void assertIncompleteAfterUp(AdaptiveTrackFacade &track) { up(track); assertIncomplete(track); } - void assertCompleteAfterUp(AdaptiveTrackFacade &track) { + static void assertCompleteAfterUp(AdaptiveTrackFacade &track) { up(track); assertComplete(track); } - void assertCompleteAfterDown(AdaptiveTrackFacade &track) { + static void assertCompleteAfterDown(AdaptiveTrackFacade &track) { down(track); assertComplete(track); } diff --git a/tests/AudioReaderStub.h b/tests/AudioReaderStub.h index eb9f0968b..4b202d2b4 100644 --- a/tests/AudioReaderStub.h +++ b/tests/AudioReaderStub.h @@ -1,8 +1,7 @@ -#ifndef AudioReaderStub_h -#define AudioReaderStub_h +#ifndef TESTS_AUDIOREADERSTUB_H_ +#define TESTS_AUDIOREADERSTUB_H_ #include -#include namespace stimulus_players::tests { class AudioReaderStub : public AudioReader { @@ -12,14 +11,18 @@ class AudioReaderStub : public AudioReader { public: void set(std::vector> x) { toRead_ = std::move(x); } + std::vector> read(std::string filePath) override { filePath_ = std::move(filePath); if (throwOnRead_) throw InvalidFile{}; return toRead_; } - auto filePath() const { return filePath_; } + + [[nodiscard]] auto filePath() const { return filePath_; } + void throwOnRead() { throwOnRead_ = true; } }; } + #endif diff --git a/tests/AudioReaderTests.cpp b/tests/AudioReaderTests.cpp index 6466fb268..bf60753c4 100644 --- a/tests/AudioReaderTests.cpp +++ b/tests/AudioReaderTests.cpp @@ -29,10 +29,10 @@ class BufferedAudioReaderStub : public stimulus_players::BufferedAudioReader { public: void failOnLoad() { failOnLoad_ = true; } - auto file() const { return file_; } + [[nodiscard]] auto file() const { return file_; } std::shared_ptr readNextBuffer() override { - if (buffers_.size()) { + if (!buffers_.empty()) { buffer->setAudio(buffers_.front()); buffers_.erase(buffers_.begin()); } else @@ -89,9 +89,9 @@ TEST_F(AudioReaderTests, readConcatenatesNormalizedBuffers) { {{10, 11, 12}, {13, 14, 15}, {16, 17, 18}}, {{19, 20, 21}, {22, 23, 24}, {25, 26, 27}}}); bufferedReader.setMinimumPossibleSample(-3); - assertEqual({dividedBy({1, 2, 3, 10, 11, 12, 19, 20, 21}, -3.f), - dividedBy({4, 5, 6, 13, 14, 15, 22, 23, 24}, -3.f), - dividedBy({7, 8, 9, 16, 17, 18, 25, 26, 27}, -3.f)}, + assertEqual({dividedBy({1, 2, 3, 10, 11, 12, 19, 20, 21}, -3.F), + dividedBy({4, 5, 6, 13, 14, 15, 22, 23, 24}, -3.F), + dividedBy({7, 8, 9, 16, 17, 18, 25, 26, 27}, -3.F)}, read()); } } diff --git a/tests/DirectoryReaderStub.h b/tests/DirectoryReaderStub.h index a5cbccb4d..23179a07d 100644 --- a/tests/DirectoryReaderStub.h +++ b/tests/DirectoryReaderStub.h @@ -1,5 +1,5 @@ -#ifndef DirectoryReaderStub_h -#define DirectoryReaderStub_h +#ifndef TESTS_DIRECTORYREADERSTUB_H_ +#define TESTS_DIRECTORYREADERSTUB_H_ #include @@ -23,7 +23,7 @@ class DirectoryReaderStub : public target_list::DirectoryReader { return subDirectories_; } - auto directory() const { return directory_; } + [[nodiscard]] auto directory() const { return directory_; } void setSubDirectories(std::vector v) { subDirectories_ = std::move(v); diff --git a/tests/FixedLevelMethodTests.cpp b/tests/FixedLevelMethodTests.cpp index 2a67e337e..fe959ff7f 100644 --- a/tests/FixedLevelMethodTests.cpp +++ b/tests/FixedLevelMethodTests.cpp @@ -1,12 +1,15 @@ +#include "LogString.h" #include "OutputFileStub.h" #include "ResponseEvaluatorStub.h" #include "TargetListStub.h" #include "TestConcluderStub.h" #include "assert-utility.h" +#include "av-speech-in-noise/Model.hpp" #include #include namespace av_speech_in_noise::tests { +namespace { class UseCase { public: virtual ~UseCase() = default; @@ -14,7 +17,7 @@ class UseCase { }; class InitializingMethod : public UseCase { - FixedLevelTest test; + FixedLevelTest test_; TargetList &list; TestConcluder &concluder; @@ -23,15 +26,29 @@ class InitializingMethod : public UseCase { : list{list}, concluder{concluder} {} void run(FixedLevelMethod &m) override { - m.initialize(test, &list, &concluder); + m.initialize(test_, &list, &concluder); } + + [[nodiscard]] auto test() const -> auto & { return test_; } + + void setTargetListDirectory(std::string s) { + test_.targetListDirectory = std::move(s); + } + + void setSnr(int x) { test_.snr_dB = x; } }; class SubmittingCoordinateResponse : public UseCase { - coordinate_response_measure::Response response; + coordinate_response_measure::Response response_{}; public: - void run(FixedLevelMethod &m) override { m.submitResponse(response); } + void run(FixedLevelMethod &m) override { m.submitResponse(response_); } + + void setColor(coordinate_response_measure::Color c) { response_.color = c; } + + void setNumber(int n) { response_.number = n; } + + [[nodiscard]] auto response() const -> auto & { return response_; } }; class SubmittingFreeResponse : public UseCase { @@ -39,9 +56,11 @@ class SubmittingFreeResponse : public UseCase { public: void run(FixedLevelMethod &m) override { m.submitResponse(response); } + void setFlagged() { response.flagged = true; } }; -namespace { +auto blueColor() { return coordinate_response_measure::Color::blue; } + class FixedLevelMethodTests : public ::testing::Test { protected: ResponseEvaluatorStub evaluator; @@ -49,38 +68,21 @@ class FixedLevelMethodTests : public ::testing::Test { TestConcluderStub testConcluder; OutputFileStub outputFile; FixedLevelMethodImpl method{&evaluator}; - FixedLevelTest test; - coordinate_response_measure::Response coordinateResponse; - FreeResponse freeResponse; SubmittingCoordinateResponse submittingCoordinateResponse; SubmittingFreeResponse submittingFreeResponse; InitializingMethod initializingMethod{targetList, testConcluder}; - void initialize() { method.initialize(test, &targetList, &testConcluder); } - - void writeCoordinateResponse() { - initialize(); - submitCoordinateResponse(); - writeLastCoordinateResponse(); - } - - void submitCoordinateResponse() { - method.submitResponse(coordinateResponse); - } - - void submitFreeResponse() { method.submitResponse(freeResponse); } + FixedLevelMethodTests() { run(initializingMethod); } void writeLastCoordinateResponse() { method.writeLastCoordinateResponse(&outputFile); } - auto blueColor() { return coordinate_response_measure::Color::blue; } - auto writtenFixedLevelTrial() { return outputFile.writtenFixedLevelTrial(); } - bool writtenFixedLevelTrialCorrect() { + auto writtenFixedLevelTrialCorrect() -> bool { return writtenFixedLevelTrial().correct; } @@ -88,19 +90,9 @@ class FixedLevelMethodTests : public ::testing::Test { targetList.setCurrent(std::move(s)); } - void assertTestIncompleteAfter(UseCase &useCase) { - run(useCase); - assertTestIncomplete(); - } - - void assertTestCompleteAfter(UseCase &useCase) { - run(useCase); - assertTestComplete(); - } - void assertTestIncomplete() { assertFalse(testComplete()); } - bool testComplete() { return method.complete(); } + auto testComplete() -> bool { return method.complete(); } void assertTestComplete() { assertTrue(testComplete()); } @@ -110,26 +102,26 @@ class FixedLevelMethodTests : public ::testing::Test { void run(UseCase &useCase) { useCase.run(method); } - void assertTargetListPassedToConcluder(UseCase &useCase) { - initialize(); - assertTargetListPassedToConcluder_(useCase); + void assertTargetListPassedToConcluderAfter(UseCase &useCase) { + run(useCase); + assertTestConcluderPassedTargetList(); } - void assertTargetListPassedToConcluder_(UseCase &useCase) { - run(useCase); + void assertTestConcluderPassedTargetList() { assertEqual( static_cast(&targetList), testConcluder.targetList()); } - void assertTestCompleteWhenComplete(UseCase &useCase) { - initialize(); - assertTestCompleteWhenComplete_(useCase); + void assertTestCompleteOnlyWhenComplete(UseCase &useCase) { + run(useCase); + assertTestIncomplete(); + assertTestCompleteWhenComplete(useCase); } - void assertTestCompleteWhenComplete_(UseCase &useCase) { - assertTestIncompleteAfter(useCase); + void assertTestCompleteWhenComplete(UseCase &useCase) { setTestComplete(); - assertTestCompleteAfter(useCase); + run(useCase); + assertTestComplete(); } void assertTestConcluderLogContains(const std::string &s) { @@ -141,145 +133,210 @@ class FixedLevelMethodTests : public ::testing::Test { run(useCase); assertTestConcluderLogContains(s); } + + auto reinsertCurrentCalled() -> bool { + return targetList.reinsertCurrentCalled(); + } + + void assertCurrentTargetNotReinserted() { + assertFalse(reinsertCurrentCalled()); + } + + void assertCurrentTargetReinserted() { + assertTrue(reinsertCurrentCalled()); + } }; -TEST_F(FixedLevelMethodTests, initializePassesTestParametersToConcluder) { - initialize(); - assertEqual(&std::as_const(test), testConcluder.test()); -} +#define FIXED_LEVEL_METHOD_TEST(a) TEST_F(FixedLevelMethodTests, a) -TEST_F(FixedLevelMethodTests, initializePassesTargetListDirectory) { - test.targetListDirectory = "a"; - initialize(); - assertEqual("a", targetList.directory()); +FIXED_LEVEL_METHOD_TEST(passesTestParametersToConcluder) { + assertEqual(&initializingMethod.test(), testConcluder.test()); } -TEST_F(FixedLevelMethodTests, nextReturnsNextTarget) { - initialize(); +FIXED_LEVEL_METHOD_TEST(nextReturnsNextTarget) { targetList.setNext("a"); assertEqual("a", method.next()); } -TEST_F(FixedLevelMethodTests, snrReturnsInitializedSnr) { - test.snr_dB = 1; - initialize(); - assertEqual(1, method.snr_dB()); -} - -TEST_F(FixedLevelMethodTests, writeCoordinateResponsePassesSubjectColor) { - coordinateResponse.color = blueColor(); - writeCoordinateResponse(); +FIXED_LEVEL_METHOD_TEST(writeCoordinateResponsePassesSubjectColor) { + submittingCoordinateResponse.setColor(blueColor()); + run(submittingCoordinateResponse); + writeLastCoordinateResponse(); assertEqual(blueColor(), writtenFixedLevelTrial().subjectColor); } -TEST_F(FixedLevelMethodTests, writeCoordinateResponsePassesSubjectNumber) { - coordinateResponse.number = 1; - writeCoordinateResponse(); +FIXED_LEVEL_METHOD_TEST(writeCoordinateResponsePassesSubjectNumber) { + submittingCoordinateResponse.setNumber(1); + run(submittingCoordinateResponse); + writeLastCoordinateResponse(); assertEqual(1, writtenFixedLevelTrial().subjectNumber); } -TEST_F(FixedLevelMethodTests, writeCoordinateResponsePassesCorrectColor) { +FIXED_LEVEL_METHOD_TEST(writeCoordinateResponsePassesCorrectColor) { evaluator.setCorrectColor(blueColor()); - writeCoordinateResponse(); + run(submittingCoordinateResponse); + writeLastCoordinateResponse(); assertEqual(blueColor(), writtenFixedLevelTrial().correctColor); } -TEST_F(FixedLevelMethodTests, writeCoordinateResponsePassesCorrectNumber) { +FIXED_LEVEL_METHOD_TEST(writeCoordinateResponsePassesCorrectNumber) { evaluator.setCorrectNumber(1); - writeCoordinateResponse(); + run(submittingCoordinateResponse); + writeLastCoordinateResponse(); assertEqual(1, writtenFixedLevelTrial().correctNumber); } -TEST_F(FixedLevelMethodTests, writeCoordinateResponsePassesStimulus) { +FIXED_LEVEL_METHOD_TEST(writeCoordinateResponsePassesStimulus) { setCurrentTarget("a"); - writeCoordinateResponse(); + run(submittingCoordinateResponse); + writeLastCoordinateResponse(); assertEqual("a", writtenFixedLevelTrial().target); } -TEST_F(FixedLevelMethodTests, writeCorrectCoordinateResponse) { +FIXED_LEVEL_METHOD_TEST(writeCorrectCoordinateResponse) { evaluator.setCorrect(); - writeCoordinateResponse(); + run(submittingCoordinateResponse); + writeLastCoordinateResponse(); assertTrue(writtenFixedLevelTrialCorrect()); } -TEST_F(FixedLevelMethodTests, writeIncorrectCoordinateResponse) { +FIXED_LEVEL_METHOD_TEST(writeIncorrectCoordinateResponse) { evaluator.setIncorrect(); - writeCoordinateResponse(); + run(submittingCoordinateResponse); + writeLastCoordinateResponse(); assertFalse(writtenFixedLevelTrialCorrect()); } -TEST_F(FixedLevelMethodTests, writeTestPassesSettings) { - initialize(); +FIXED_LEVEL_METHOD_TEST(writeTestPassesSettings) { method.writeTestingParameters(&outputFile); - assertEqual(&std::as_const(test), outputFile.fixedLevelTest()); + assertEqual(&initializingMethod.test(), outputFile.fixedLevelTest()); } -TEST_F(FixedLevelMethodTests, submitCoordinateResponsePassesResponse) { - initialize(); - submitCoordinateResponse(); - assertEqual(&std::as_const(coordinateResponse), evaluator.response()); +FIXED_LEVEL_METHOD_TEST(submitCoordinateResponsePassesResponse) { + run(submittingCoordinateResponse); + assertEqual(&submittingCoordinateResponse.response(), evaluator.response()); } -TEST_F( - FixedLevelMethodTests, submitCoordinateResponseSubmitsResponseToConcluder) { - initialize(); - submitCoordinateResponse(); +FIXED_LEVEL_METHOD_TEST(submitCoordinateResponseSubmitsResponseToConcluder) { + run(submittingCoordinateResponse); assertTrue(testConcluder.responseSubmitted()); } -TEST_F( - FixedLevelMethodTests, submitCoordinateResponsePassesCurrentToEvaluator) { - initialize(); +FIXED_LEVEL_METHOD_TEST(submitCoordinateResponsePassesCurrentToEvaluator) { setCurrentTarget("a"); - submitCoordinateResponse(); + run(submittingCoordinateResponse); assertEqual("a", evaluator.correctColorFilePath()); assertEqual("a", evaluator.correctNumberFilePath()); } -TEST_F(FixedLevelMethodTests, +FIXED_LEVEL_METHOD_TEST( submitCoordinateResponsePassesCorrectTargetToEvaluator) { - initialize(); setCurrentTarget("a"); - submitCoordinateResponse(); + run(submittingCoordinateResponse); assertEqual("a", evaluator.correctFilePath()); } -TEST_F(FixedLevelMethodTests, completeWhenTestCompleteAfterCoordinateResponse) { - assertTestCompleteWhenComplete(submittingCoordinateResponse); +FIXED_LEVEL_METHOD_TEST(completeWhenTestCompleteAfterCoordinateResponse) { + assertTestCompleteOnlyWhenComplete(submittingCoordinateResponse); } -TEST_F(FixedLevelMethodTests, completeWhenTestCompleteAfterFreeResponse) { - assertTestCompleteWhenComplete(submittingFreeResponse); +FIXED_LEVEL_METHOD_TEST(completeWhenTestCompleteAfterFreeResponse) { + assertTestCompleteOnlyWhenComplete(submittingFreeResponse); } -TEST_F(FixedLevelMethodTests, completeWhenTestCompleteAfterInitializing) { - assertTestCompleteWhenComplete_(initializingMethod); +FIXED_LEVEL_METHOD_TEST(submitCoordinateResponsePassesTargetListToConcluder) { + assertTargetListPassedToConcluderAfter(submittingCoordinateResponse); } -TEST_F(FixedLevelMethodTests, - submitCoordinateResponsePassesTargetListToConcluder) { - assertTargetListPassedToConcluder(submittingCoordinateResponse); +FIXED_LEVEL_METHOD_TEST(submitFreeResponsePassesTargetListToConcluder) { + assertTargetListPassedToConcluderAfter(submittingFreeResponse); } -TEST_F(FixedLevelMethodTests, submitFreeResponsePassesTargetListToConcluder) { - assertTargetListPassedToConcluder(submittingFreeResponse); +FIXED_LEVEL_METHOD_TEST( + submitCoordinateResponseSubmitsResponsePriorToQueryingCompletion) { + assertTestConcluderLogContainsAfter( + "submitResponse complete", submittingCoordinateResponse); } -TEST_F(FixedLevelMethodTests, initializePassesTargetListToConcluder) { - assertTargetListPassedToConcluder_(initializingMethod); +FIXED_LEVEL_METHOD_TEST(submitFreeResponseDoesNotReinsertCurrentTarget) { + run(submittingFreeResponse); + assertCurrentTargetNotReinserted(); } -TEST_F(FixedLevelMethodTests, - submitCoordinateResponseSubmitsResponsePriorToQueryingCompletion) { - initialize(); - assertTestConcluderLogContainsAfter( - "submitResponse complete", submittingCoordinateResponse); +FIXED_LEVEL_METHOD_TEST(submitFreeResponseReinsertsCurrentTargetIfFlagged) { + submittingFreeResponse.setFlagged(); + run(submittingFreeResponse); + assertCurrentTargetReinserted(); } -TEST_F(FixedLevelMethodTests, - initializeInitializesConcluderBeforeQueryingCompletion) { - assertTestConcluderLogContainsAfter( - "initialize complete", initializingMethod); +FIXED_LEVEL_METHOD_TEST(initializesConcluderBeforeQueryingCompletion) { + assertTestConcluderLogContains("initialize complete"); +} + +FIXED_LEVEL_METHOD_TEST(initializePassesTargetListToConcluder) { + assertTestConcluderPassedTargetList(); +} + +FIXED_LEVEL_METHOD_TEST(completeWhenTestCompleteAfterInitializing) { + assertTestIncomplete(); + assertTestCompleteWhenComplete(initializingMethod); +} + +class PreInitializedFixedLevelMethodTests : public ::testing::Test { + protected: + ResponseEvaluatorStub evaluator; + TargetListStub targetList; + TestConcluderStub testConcluder; + FixedLevelMethodImpl method{&evaluator}; + InitializingMethod initializingMethod{targetList, testConcluder}; + + void run(UseCase &useCase) { useCase.run(method); } +}; + +TEST_F(PreInitializedFixedLevelMethodTests, snrReturnsInitializedSnr) { + initializingMethod.setSnr(1); + run(initializingMethod); + assertEqual(1, method.snr_dB()); +} + +TEST_F( + PreInitializedFixedLevelMethodTests, initializePassesTargetListDirectory) { + initializingMethod.setTargetListDirectory("a"); + run(initializingMethod); + assertEqual("a", targetList.directory()); +} + +class TargetListTestConcluderComboStub : public TargetList, + public TestConcluder { + public: + void loadFromDirectory(std::string) override {} + auto next() -> std::string override { return {}; } + auto current() -> std::string override { return {}; } + auto empty() -> bool override { return {}; } + void reinsertCurrent() override { log_.insert("reinsertCurrent "); } + auto complete(TargetList *) -> bool override { + log_.insert("complete "); + return {}; + } + void submitResponse() override {} + void initialize(const FixedLevelTest &) override {} + auto log() const -> auto & { return log_; } + + private: + LogString log_; +}; + +TEST(FixedLevelMethodTestsTBD, + submitFreeResponseReinsertsCurrentTargetIfFlaggedBeforeQueryingCompletion) { + ResponseEvaluatorStub evaluator; + TargetListTestConcluderComboStub combo; + FixedLevelMethodImpl method{&evaluator}; + FixedLevelTest test; + method.initialize(test, &combo, &combo); + FreeResponse response; + response.flagged = true; + method.submitResponse(response); + assertEqual("complete reinsertCurrent complete ", combo.log()); } class FixedTrialTestConcluderTests : public ::testing::Test { diff --git a/tests/LogString.h b/tests/LogString.h index 6e7192c5d..d56ced37b 100644 --- a/tests/LogString.h +++ b/tests/LogString.h @@ -1,21 +1,23 @@ -#ifndef LogString_h -#define LogString_h +#ifndef TESTS_LOGSTRING_H_ +#define TESTS_LOGSTRING_H_ #include +#include +#include class LogString { std::stringstream log{}; public: - void insert(std::string s_) { log << std::move(s_); } + void insert(const std::string &s_) { log << s_; } bool isEmpty() const { return log.str().empty(); } bool beginsWith(std::string const &beginning) const { if (log.str().length() >= beginning.length()) return 0 == log.str().compare(0, beginning.length(), beginning); - else - return false; + + return false; } bool endsWith(std::string const &ending) const { @@ -23,12 +25,12 @@ class LogString { return 0 == log.str().compare(log.str().length() - ending.length(), ending.length(), ending); - else - return false; + + return false; } - bool contains(std::string s2) const { - return log.str().find(std::move(s2)) != std::string::npos; + bool contains(const std::string &s2) const { + return log.str().find(s2) != std::string::npos; } operator std::string() const { return log.str(); } diff --git a/tests/MaskerPlayerStub.h b/tests/MaskerPlayerStub.h index aadbe635b..f89361470 100644 --- a/tests/MaskerPlayerStub.h +++ b/tests/MaskerPlayerStub.h @@ -3,6 +3,7 @@ #include "LogString.h" #include +#include namespace av_speech_in_noise::tests { class MaskerPlayerStub : public MaskerPlayer { @@ -67,7 +68,7 @@ class MaskerPlayerStub : public MaskerPlayer { throw InvalidAudioFile{}; } - void addToLog(std::string s) { log_.insert(std::move(s)); } + void addToLog(const std::string &s) { log_.insert(s); } std::vector outputAudioDeviceDescriptions() override { return outputAudioDeviceDescriptions_; diff --git a/tests/MaskerPlayerTests.cpp b/tests/MaskerPlayerTests.cpp index 96778dd06..e8167487b 100644 --- a/tests/MaskerPlayerTests.cpp +++ b/tests/MaskerPlayerTests.cpp @@ -1,8 +1,8 @@ -#include "AudioReaderStub.h" #include "assert-utility.h" -#include -#include +#include "AudioReaderStub.h" #include +#include +#include namespace { class AudioPlayerStub : public stimulus_players::AudioPlayer { @@ -37,7 +37,7 @@ class AudioPlayerStub : public stimulus_players::AudioPlayer { void stop() override { stopped_ = true; } - auto stopped() const { return stopped_; } + [[nodiscard]] auto stopped() const { return stopped_; } double sampleRateHz() override { return sampleRateHz_; } @@ -66,13 +66,13 @@ class AudioPlayerStub : public stimulus_players::AudioPlayer { void subscribe(EventListener *listener) override { listener_ = listener; } - auto filePath() const { return filePath_; } + [[nodiscard]] auto filePath() const { return filePath_; } - auto secondsSeeked() const { return secondsSeeked_; } + [[nodiscard]] auto secondsSeeked() const { return secondsSeeked_; } - auto deviceIndex() const { return deviceIndex_; } + [[nodiscard]] auto deviceIndex() const { return deviceIndex_; } - auto played() const { return played_; } + [[nodiscard]] auto played() const { return played_; } void fillAudioBuffer(const std::vector> &audio) { listener_->fillAudioBuffer(audio); @@ -97,13 +97,15 @@ class MaskerPlayerListenerStub fadeOutCompleted_ = true; } - auto fadeInCompleted() const { return fadeInCompleted_; } + [[nodiscard]] auto fadeInCompleted() const { return fadeInCompleted_; } - auto fadeOutCompleted() const { return fadeOutCompleted_; } + [[nodiscard]] auto fadeOutCompleted() const { return fadeOutCompleted_; } - auto fadeInCompletions() const { return fadeInCompletions_; } + [[nodiscard]] auto fadeInCompletions() const { return fadeInCompletions_; } - auto fadeOutCompletions() const { return fadeOutCompletions_; } + [[nodiscard]] auto fadeOutCompletions() const { + return fadeOutCompletions_; + } }; template class VectorFacade { @@ -133,7 +135,7 @@ class TimerStub : public stimulus_players::Timer { callbackScheduled_ = true; } - auto callbackScheduled() const { return callbackScheduled_; } + [[nodiscard]] auto callbackScheduled() const { return callbackScheduled_; } void clearCallbackCount() { callbackScheduled_ = false; } @@ -165,7 +167,7 @@ class MaskerPlayerTests : public ::testing::Test { fillAudioBuffer({leftChannel, rightChannel}); } - std::vector halfHannWindow(int length) { + static std::vector halfHannWindow(int length) { auto N = 2 * length - 1; const auto pi = std::acos(-1); std::vector window; @@ -174,20 +176,20 @@ class MaskerPlayerTests : public ::testing::Test { return window; } - std::vector backHalfHannWindow(int length) { + static std::vector backHalfHannWindow(int length) { auto frontHalf = halfHannWindow(length); std::reverse(frontHalf.begin(), frontHalf.end()); return frontHalf; } - std::vector oneToN(int N) { + static std::vector oneToN(int N) { std::vector result; result.resize(N); std::iota(result.begin(), result.end(), 1); return result; } - std::vector NtoOne(int N) { + static std::vector NtoOne(int N) { auto result = oneToN(N); std::reverse(result.begin(), result.end()); return result; @@ -264,9 +266,9 @@ class MaskerPlayerTests : public ::testing::Test { assertChannelEqual(leftChannel, std::move(x)); } - void assertChannelEqual( + static void assertChannelEqual( const std::vector &channel, std::vector x) { - assertEqual(std::move(x), channel, 1e-6f); + assertEqual(std::move(x), channel, 1e-6F); } void assertFillingStereoChannelsMultipliesBy( @@ -492,7 +494,7 @@ TEST_F(MaskerPlayerTests, steadyLevelFollowingFadeOut) { leftChannel = {1, 2, 3}; fillAudioBufferMono(); - assertEqual({0, 0, 0}, leftChannel, 1e-15f); + assertEqual({0, 0, 0}, leftChannel, 1e-15F); } TEST_F(MaskerPlayerTests, fadeInCompleteOnlyAfterFadeTime) { diff --git a/tests/ModelEventListenerStub.h b/tests/ModelEventListenerStub.h index f021983fd..9e07b08d4 100644 --- a/tests/ModelEventListenerStub.h +++ b/tests/ModelEventListenerStub.h @@ -1,5 +1,5 @@ -#ifndef ModelEventListenerStub_h -#define ModelEventListenerStub_h +#ifndef TESTS_MODELEVENTLISTENERSTUB_H_ +#define TESTS_MODELEVENTLISTENERSTUB_H_ #include @@ -10,7 +10,7 @@ class ModelEventListenerStub : public Model::EventListener { public: void trialComplete() override { notified_ = true; } - auto notified() const { return notified_; } + [[nodiscard]] auto notified() const { return notified_; } }; } diff --git a/tests/ModelTests.cpp b/tests/ModelTests.cpp index 7cec33874..a6a1c574d 100644 --- a/tests/ModelTests.cpp +++ b/tests/ModelTests.cpp @@ -13,7 +13,7 @@ class AdaptiveMethodStub : public AdaptiveMethod { public: void initialize(const AdaptiveTest &t) override { test_ = &t; } - auto test() const { return test_; } + [[nodiscard]] auto test() const { return test_; } bool complete() override { return {}; } std::string next() override { return {}; } @@ -43,11 +43,11 @@ class FixedLevelMethodStub : public FixedLevelMethod { test_ = &t; } - auto testConcluder() const { return testConcluder_; } + [[nodiscard]] auto testConcluder() const { return testConcluder_; } - auto targetList() const { return targetList_; } + [[nodiscard]] auto targetList() const { return targetList_; } - auto test() const { return test_; } + [[nodiscard]] auto test() const { return test_; } bool complete() override { return {}; } std::string next() override { return {}; } diff --git a/tests/OutputFileStub.h b/tests/OutputFileStub.h index 5f188450a..af3ee5471 100644 --- a/tests/OutputFileStub.h +++ b/tests/OutputFileStub.h @@ -3,6 +3,7 @@ #include "LogString.h" #include +#include namespace av_speech_in_noise::tests { class OutputFileStub : public OutputFile { @@ -63,7 +64,7 @@ class OutputFileStub : public OutputFile { auto &writtenFixedLevelTrial() const { return writtenFixedLevelTrial_; } - void addToLog(std::string s) { log_.insert(std::move(s)); } + void addToLog(const std::string &s) { log_.insert(s); } void throwOnOpen() { throwOnOpen_ = true; } diff --git a/tests/OutputFileTests.cpp b/tests/OutputFileTests.cpp index 67568e2a0..4a7f7713c 100644 --- a/tests/OutputFileTests.cpp +++ b/tests/OutputFileTests.cpp @@ -26,7 +26,7 @@ class WriterStub : public Writer { auto &written() const { return written_; } - void write(std::string s) override { written_.insert(std::move(s)); } + void write(std::string s) override { written_.insert(s); } bool failed() override { return {}; } }; @@ -193,12 +193,12 @@ class OutputFileTests : public ::testing::Test { assertColonDelimitedEntryWritten("condition", conditionName(c)); } - void assertWriterContains(std::string s) { - assertTrue(written().contains(std::move(s))); + void assertWriterContains(const std::string &s) { + assertTrue(written().contains(s)); } - void assertWrittenLast(std::string s) { - assertTrue(written().endsWith(std::move(s))); + void assertWrittenLast(const std::string &s) { + assertTrue(written().endsWith(s)); } std::string nthCommaDelimitedEntryOfLine(int n, int line) { @@ -211,7 +211,7 @@ class OutputFileTests : public ::testing::Test { return upUntilFirstOfAny(line_.substr(entryBeginning), {',', '\n'}); } - std::string::size_type find_nth_element( + static std::string::size_type find_nth_element( const std::string &content, int n, char what) { auto found = std::string::npos; for (int i = 0; i < n; ++i) @@ -219,7 +219,7 @@ class OutputFileTests : public ::testing::Test { return found; } - std::string upUntilFirstOfAny( + static std::string upUntilFirstOfAny( const std::string &content, std::vector v) { return content.substr(0, content.find_first_of({v.begin(), v.end()})); } @@ -255,12 +255,12 @@ class OutputFileTests : public ::testing::Test { assertWrittenLast("\n\n"); } - void assertNthEntryOfFirstLine(std::string what, int n) { - assertNthCommaDelimitedEntryOfLine(std::move(what), n, 1); + void assertNthEntryOfFirstLine(const std::string &what, int n) { + assertNthCommaDelimitedEntryOfLine(what, n, 1); } - void assertNthEntryOfSecondLine(std::string what, int n) { - assertNthCommaDelimitedEntryOfLine(std::move(what), n, 2); + void assertNthEntryOfSecondLine(const std::string &what, int n) { + assertNthCommaDelimitedEntryOfLine(what, n, 2); } void assertEntriesOfSecondLine(int n) { @@ -272,12 +272,13 @@ class OutputFileTests : public ::testing::Test { std::count(line_.begin(), line_.end(), ',')); } - void assertNthEntryOfThirdLine(std::string what, int n) { - assertNthCommaDelimitedEntryOfLine(std::move(what), n, 3); + void assertNthEntryOfThirdLine(const std::string &what, int n) { + assertNthCommaDelimitedEntryOfLine(what, n, 3); } - void assertNthCommaDelimitedEntryOfLine(std::string what, int n, int line) { - assertEqual(std::move(what), nthCommaDelimitedEntryOfLine(n, line)); + void assertNthCommaDelimitedEntryOfLine( + const std::string &what, int n, int line) { + assertEqual(what, nthCommaDelimitedEntryOfLine(n, line)); } void assertNthCommaDelimitedEntryOfLine(HeadingItem item, int n, int line) { @@ -394,7 +395,8 @@ class OutputFileTests : public ::testing::Test { assertNthCommaDelimitedEntryOfLine("22", 4, n); } - void assertColonDelimitedEntryWritten(std::string label, std::string what) { + void assertColonDelimitedEntryWritten( + const std::string &label, const std::string &what) { assertWriterContains(label + ": " + what + "\n"); } }; diff --git a/tests/PresenterTests.h b/tests/PresenterTests.h index b8c22f664..4177a1d2c 100644 --- a/tests/PresenterTests.h +++ b/tests/PresenterTests.h @@ -4,6 +4,8 @@ #include "assert-utility.h" #include #include +#include +#include namespace av_speech_in_noise::tests::presentation { template class Collection { @@ -12,7 +14,7 @@ template class Collection { public: explicit Collection(std::vector items = {}) : items{std::move(items)} {} - bool contains(const T &item) const { + [[nodiscard]] bool contains(const T &item) const { return std::find(items.begin(), items.end(), item) != items.end(); } }; @@ -539,11 +541,11 @@ class ConfirmingAdaptiveTest : public ConfirmingAdaptiveTest_ { explicit ConfirmingAdaptiveTest(ViewStub::TestSetupViewStub *view) : view{view} {} - auto adaptiveTest(ModelStub &m) { return m.adaptiveTest(); } + static auto adaptiveTest(ModelStub &m) { return m.adaptiveTest(); } - Test common(ModelStub &m) { return adaptiveTest(m); } + static Test common(ModelStub &m) { return adaptiveTest(m); } - auto information(ModelStub &m) { return adaptiveTest(m).identity; } + static auto information(ModelStub &m) { return adaptiveTest(m).identity; } void run() override { view->confirmTestSetup(); } @@ -596,7 +598,7 @@ class ConfirmingAdaptiveTest : public ConfirmingAdaptiveTest_ { } }; -void setMethod(ViewStub::TestSetupViewStub *view, Method m) { +inline void setMethod(ViewStub::TestSetupViewStub *view, Method m) { view->setMethod(methodName(m)); } @@ -741,11 +743,11 @@ class ConfirmingFixedLevelTest : public ConfirmingTestSetup { void run() override { view->confirmTestSetup(); } - auto fixedLevelTest(ModelStub &m) { return m.fixedLevelTest(); } + static auto fixedLevelTest(ModelStub &m) { return m.fixedLevelTest(); } - auto common(ModelStub &m) { return fixedLevelTest(m); } + static auto common(ModelStub &m) { return fixedLevelTest(m); } - auto information(ModelStub &m) { return fixedLevelTest(m).identity; } + static auto information(ModelStub &m) { return fixedLevelTest(m).identity; } int snr_dB(ModelStub &m) override { return fixedLevelTest(m).snr_dB; } @@ -1164,11 +1166,11 @@ class PresenterTests : public ::testing::Test { SubmittingFailedTrial submittingFailedTrial{&testingView}; ExitingTest exitingTest{&experimenterView}; - std::string auditoryOnlyConditionName() { + static std::string auditoryOnlyConditionName() { return conditionName(Condition::auditoryOnly); } - std::string audioVisualConditionName() { + static std::string audioVisualConditionName() { return conditionName(Condition::audioVisual); } @@ -1234,11 +1236,12 @@ class PresenterTests : public ::testing::Test { useCase.setResult(view, std::move(s)); } - void assertEntryEquals(BrowsingEnteredPathUseCase &useCase, std::string s) { - assertEqual(std::move(s), entry(useCase)); + static void assertEntryEquals( + BrowsingEnteredPathUseCase &useCase, const std::string &s) { + assertEqual(s, entry(useCase)); } - std::string entry(BrowsingEnteredPathUseCase &useCase) { + static std::string entry(BrowsingEnteredPathUseCase &useCase) { return useCase.entry(); } @@ -1253,12 +1256,12 @@ class PresenterTests : public ::testing::Test { void completeTrial() { model.completeTrial(); } - void assertSetupViewConditionsContains(std::string s) { - assertTrue(setupView.conditions().contains(std::move(s))); + void assertSetupViewConditionsContains(const std::string &s) { + assertTrue(setupView.conditions().contains(s)); } - void assertSetupViewMethodsContains(std::string s) { - assertTrue(setupView.methods().contains(std::move(s))); + void assertSetupViewMethodsContains(const std::string &s) { + assertTrue(setupView.methods().contains(s)); } void assertSetupViewMethodsContains(Method m) { @@ -1283,8 +1286,8 @@ class PresenterTests : public ::testing::Test { assertErrorMessageEquals("'a' is not a valid calibration level."); } - void assertErrorMessageEquals(std::string s) { - assertEqual(std::move(s), errorMessage()); + void assertErrorMessageEquals(const std::string &s) { + assertEqual(s, errorMessage()); } void assertInvalidMaskerLevelShowsErrorMessage(UseCase &useCase) { @@ -1335,7 +1338,7 @@ class PresenterTests : public ::testing::Test { assertEqual("a", model.trialParameters().audioDevice); } - void run(UseCase &useCase) { useCase.run(); } + static void run(UseCase &useCase) { useCase.run(); } void assertPlaysTrial(UseCase &useCase) { run(useCase); @@ -1344,7 +1347,7 @@ class PresenterTests : public ::testing::Test { bool trialPlayed() { return model.trialPlayed(); } - void assertHidesPlayTrialButton(PlayingTrial &useCase) { + static void assertHidesPlayTrialButton(PlayingTrial &useCase) { run(useCase); assertTrue(useCase.nextTrialButtonHidden()); } @@ -1362,7 +1365,7 @@ class PresenterTests : public ::testing::Test { return experimenterView.exitTestButtonShown(); } - void assertConfirmTestSetupShowsNextTrialButton( + static void assertConfirmTestSetupShowsNextTrialButton( ConfirmingTestSetup &confirmingTest, PlayingTrial &playingTrial) { run(confirmingTest); assertTrue(playingTrial.nextTrialButtonShown()); @@ -1415,7 +1418,7 @@ class PresenterTests : public ::testing::Test { assertTestingViewNotHidden(); } - void assertShowsNextTrialButton(TrialSubmission &useCase) { + static void assertShowsNextTrialButton(TrialSubmission &useCase) { run(useCase); assertTrue(useCase.nextTrialButtonShown()); } @@ -1550,7 +1553,7 @@ class PresenterTests : public ::testing::Test { assertEqual(s, experimenterView.displayed()); } - void assertResponseViewHidden(TrialSubmission &useCase) { + static void assertResponseViewHidden(TrialSubmission &useCase) { run(useCase); assertTrue(useCase.responseViewHidden()); } @@ -1636,9 +1639,9 @@ class PresenterFailureTests : public ::testing::Test { setupView.confirmTestSetup(); } - void assertConfirmTestSetupShowsErrorMessage(std::string s) { + void assertConfirmTestSetupShowsErrorMessage(const std::string &s) { confirmTestSetup(); - assertEqual(std::move(s), view.errorMessage()); + assertEqual(s, view.errorMessage()); } void assertConfirmTestSetupDoesNotHideSetupView() { diff --git a/tests/RecognitionTestModelTests.cpp b/tests/RecognitionTestModelTests.cpp index d6a0b227a..6c91270c5 100644 --- a/tests/RecognitionTestModelTests.cpp +++ b/tests/RecognitionTestModelTests.cpp @@ -34,51 +34,52 @@ class TestMethodStub : public TestMethod { void setNextTarget(std::string s) { next_ = std::move(s); } - bool complete() { return complete_; } + bool complete() override { return complete_; } - std::string next() { + std::string next() override { log_.insert("next "); current_ = currentWhenNext_; return next_; } - std::string current() { return current_; } + std::string current() override { return current_; } void setCurrent(std::string s) { current_ = std::move(s); } void setCurrentWhenNext(std::string s) { currentWhenNext_ = std::move(s); } - int snr_dB() { return snr_dB_; } + int snr_dB() override { return snr_dB_; } - void submitCorrectResponse() { + void submitCorrectResponse() override { log_.insert("submitCorrectResponse "); submittedCorrectResponse_ = true; } - void submitIncorrectResponse() { + void submitIncorrectResponse() override { log_.insert("submitIncorrectResponse "); submittedIncorrectResponse_ = true; } - void submitResponse(const FreeResponse &) {} + void submitResponse(const FreeResponse &) override {} - void writeTestingParameters(OutputFile *file) { + void writeTestingParameters(OutputFile *file) override { file->writeTest(AdaptiveTest{}); } - void writeLastCoordinateResponse(OutputFile *) { + void writeLastCoordinateResponse(OutputFile *) override { log_.insert("writeLastCoordinateResponse "); } - void writeLastCorrectResponse(OutputFile *) { + void writeLastCorrectResponse(OutputFile *) override { log_.insert("writeLastCorrectResponse "); } - void writeLastIncorrectResponse(OutputFile *) { + void writeLastIncorrectResponse(OutputFile *) override { log_.insert("writeLastIncorrectResponse "); } - void submitResponse(const coordinate_response_measure::Response &) { + void submitResponse( + const coordinate_response_measure::Response &) override { log_.insert("submitResponse "); } @@ -94,7 +95,6 @@ class UseCase { class TargetWritingUseCase : public virtual UseCase { public: - virtual ~TargetWritingUseCase() = default; virtual std::string writtenTarget(OutputFileStub &) = 0; }; @@ -210,7 +210,7 @@ class SubmittingCoordinateResponse : public SubmittingResponse { void setColor(coordinate_response_measure::Color c) { response_.color = c; } - auto &response() const { return response_; } + [[nodiscard]] auto response() const -> auto & { return response_; } }; class SubmittingCorrectResponse : public TargetWritingUseCase { @@ -263,11 +263,13 @@ class RecognitionTestModelTests : public ::testing::Test { assertTargetVideoNotShown(); } - bool targetPlayerVideoHidden() { return targetPlayer.videoHidden(); } + auto targetPlayerVideoHidden() -> bool { + return targetPlayer.videoHidden(); + } void assertTargetVideoNotShown() { assertFalse(targetPlayerVideoShown()); } - bool targetPlayerVideoShown() { return targetPlayer.videoShown(); } + auto targetPlayerVideoShown() -> bool { return targetPlayer.videoShown(); } void assertTargetVideoHiddenWhenAuditoryOnly(ConditionUseCase &useCase) { useCase.setAuditoryOnly(); @@ -297,8 +299,8 @@ class RecognitionTestModelTests : public ::testing::Test { auto &outputFileLog() { return outputFile.log(); } - void assertOutputFileLog(std::string s) { - assertEqual(std::move(s), outputFileLog()); + void assertOutputFileLog(const std::string &s) { + assertEqual(s, outputFileLog()); } template @@ -325,8 +327,8 @@ class RecognitionTestModelTests : public ::testing::Test { auto targetFilePath() { return targetPlayer.filePath(); } - void assertTargetFilePathEquals(std::string what) { - assertEqual(std::move(what), targetFilePath()); + void assertTargetFilePathEquals(const std::string &what) { + assertEqual(what, targetFilePath()); } void assertPassesNextTargetToPlayer(UseCase &useCase) { @@ -362,7 +364,7 @@ class RecognitionTestModelTests : public ::testing::Test { assertEqual(1., maskerPlayerSecondsSeeked()); } - double dB(double x) { return 20 * std::log10(x); } + static double dB(double x) { return 20 * std::log10(x); } auto targetPlayerLevel_dB() { return targetPlayer.level_dB(); } @@ -389,14 +391,15 @@ class RecognitionTestModelTests : public ::testing::Test { assertTrue(outputFileLog().endsWith("save ")); } - void assertCallThrowsRequestFailure(UseCase &useCase, std::string what) { + void assertCallThrowsRequestFailure( + UseCase &useCase, const std::string &what) { try { run(useCase); FAIL() << "Expected recognition_test::" "ModelImpl::" "RequestFailure"; } catch (const ModelImpl::RequestFailure &e) { - assertEqual(std::move(what), e.what()); + assertEqual(what, e.what()); } } diff --git a/tests/ResponseEvaluatorStub.h b/tests/ResponseEvaluatorStub.h index f34facb19..b865b1793 100644 --- a/tests/ResponseEvaluatorStub.h +++ b/tests/ResponseEvaluatorStub.h @@ -2,6 +2,7 @@ #define TESTS_RESPONSEEVALUATORSTUB_H_ #include +#include namespace av_speech_in_noise::tests { class ResponseEvaluatorStub : public ResponseEvaluator { @@ -24,42 +25,49 @@ class ResponseEvaluatorStub : public ResponseEvaluator { correctColor_ = c; } - auto correctNumberFilePath() const { return correctNumberFilePath_; } + [[nodiscard]] auto correctNumberFilePath() const { + return correctNumberFilePath_; + } - auto correctColorFilePath() const { return correctColorFilePath_; } + [[nodiscard]] auto correctColorFilePath() const { + return correctColorFilePath_; + } - auto correctFilePath() const { return correctTarget_; } + [[nodiscard]] auto correctFilePath() const { return correctTarget_; } - auto response() const { return response_; } + [[nodiscard]] auto response() const { return response_; } void setCorrect() { correct_ = true; } void setIncorrect() { correct_ = false; } - bool correct(const std::string &target, - const coordinate_response_measure::Response &p) override { + auto correct(const std::string &target, + const coordinate_response_measure::Response &p) -> bool override { correctTarget_ = target; response_ = &p; return correct_; } - coordinate_response_measure::Color correctColor( - const std::string &filePath) override { + auto correctColor(const std::string &filePath) + -> coordinate_response_measure::Color override { correctColorFilePath_ = filePath; return correctColor_; } - int correctNumber(const std::string &filePath) override { + auto correctNumber(const std::string &filePath) -> int override { correctNumberFilePath_ = filePath; return correctNumber_; } - std::string fileName(const std::string &filePath) override { + auto fileName(const std::string &filePath) -> std::string override { filePathForFileName_ = filePath; return fileName_; } - auto filePathForFileName() const { return filePathForFileName_; } + [[nodiscard]] auto filePathForFileName() const { + return filePathForFileName_; + } }; } + #endif diff --git a/tests/ResponseEvaluatorTests.cpp b/tests/ResponseEvaluatorTests.cpp index b0b9938ee..56feac3f8 100644 --- a/tests/ResponseEvaluatorTests.cpp +++ b/tests/ResponseEvaluatorTests.cpp @@ -10,16 +10,16 @@ class ResponseEvaluatorTests : public ::testing::Test { protected: ResponseEvaluatorImpl evaluator{}; - bool correct(std::string s, Response r) { - return evaluator.correct(std::move(s), r); + auto correct(const std::string &s, Response r) -> bool { + return evaluator.correct(s, r); } - void assertCorrect(std::string s, Response r) { - EXPECT_TRUE(correct(std::move(s), r)); + void assertCorrect(const std::string &s, Response r) { + EXPECT_TRUE(correct(s, r)); } - void assertIncorrect(std::string s, Response r) { - EXPECT_FALSE(correct(std::move(s), r)); + void assertIncorrect(const std::string &s, Response r) { + EXPECT_FALSE(correct(s, r)); } }; diff --git a/tests/SubdirectoryTargetListReaderTests.cpp b/tests/SubdirectoryTargetListReaderTests.cpp index c4a7b2e62..2ec2c472a 100644 --- a/tests/SubdirectoryTargetListReaderTests.cpp +++ b/tests/SubdirectoryTargetListReaderTests.cpp @@ -10,7 +10,7 @@ class TargetListFactoryStub : public target_list::TargetListFactory { std::vector> lists_{}; public: - std::shared_ptr make() override { + auto make() -> std::shared_ptr override { auto list = lists_.front(); lists_.erase(lists_.begin()); return list; diff --git a/tests/TargetListSetReaderStub.h b/tests/TargetListSetReaderStub.h index e8e6c0883..aaec1a34e 100644 --- a/tests/TargetListSetReaderStub.h +++ b/tests/TargetListSetReaderStub.h @@ -1,7 +1,8 @@ -#ifndef TargetListSetReaderStub_h -#define TargetListSetReaderStub_h +#ifndef TESTS_TARGETLISTSETREADERSTUB_H_ +#define TESTS_TARGETLISTSETREADERSTUB_H_ #include +#include namespace av_speech_in_noise::tests { class TargetListSetReaderStub : public TargetListReader { @@ -16,7 +17,8 @@ class TargetListSetReaderStub : public TargetListReader { return targetLists_; } - auto directory() const { return directory_; } + [[nodiscard]] auto directory() const { return directory_; } }; } + #endif diff --git a/tests/TargetListStub.h b/tests/TargetListStub.h index a081ac575..a779a13ed 100644 --- a/tests/TargetListStub.h +++ b/tests/TargetListStub.h @@ -3,6 +3,7 @@ #include "LogString.h" #include +#include namespace av_speech_in_noise::tests { class TargetListStub : public TargetList { @@ -13,13 +14,18 @@ class TargetListStub : public TargetList { std::string current_{}; bool nextCalled_{}; bool empty_{}; + bool reinsertCurrentCalled_{}; public: + void reinsertCurrent() override { reinsertCurrentCalled_ = true; } + + auto reinsertCurrentCalled() const { return reinsertCurrentCalled_; } + void setEmpty() { empty_ = true; } - bool empty() override { return empty_; } + auto empty() -> bool override { return empty_; } - std::string current() override { return current_; } + auto current() -> std::string override { return current_; } void setCurrent(std::string s) { current_ = std::move(s); } @@ -32,7 +38,7 @@ class TargetListStub : public TargetList { directory_ = std::move(directory); } - std::string next() override { + auto next() -> std::string override { log_.insert("next "); nextCalled_ = true; current_ = currentWhenNext_; @@ -43,7 +49,7 @@ class TargetListStub : public TargetList { auto directory() { return directory_; } - auto &log() const { return log_; } + auto log() const -> auto & { return log_; } }; } diff --git a/tests/TargetListTests.cpp b/tests/TargetListTests.cpp index 09d1bd9e2..90d885ee1 100644 --- a/tests/TargetListTests.cpp +++ b/tests/TargetListTests.cpp @@ -13,9 +13,9 @@ class RandomizerStub : public Randomizer { int rotateToTheLeft_{}; public: - auto toShuffle() const { return toShuffle_; } + [[nodiscard]] auto toShuffle() const { return toShuffle_; } - auto shuffledInts() const { return shuffledInts_; } + [[nodiscard]] auto shuffledInts() const { return shuffledInts_; } void shuffle(shuffle_iterator begin, shuffle_iterator end) override { toShuffle_ = {begin, end}; @@ -54,9 +54,13 @@ class RandomizedTargetListTests : public ::testing::Test { void assertEmpty() { assertTrue(empty()); } - bool empty() { return list.empty(); } + auto empty() -> bool { return list.empty(); } void assertNotEmpty() { assertFalse(empty()); } + + void reinsertCurrent() { list.reinsertCurrent(); } + + void assertNextEquals(const std::string &s) { assertEqual(s, next()); } }; TEST_F(RandomizedTargetListTests, emptyOnlyWhenNoFilesLoaded) { @@ -100,9 +104,9 @@ TEST_F(RandomizedTargetListTests, nextReplacesSecondToLastTarget) { TEST_F(RandomizedTargetListTests, nextReturnsFullPathToFile) { setFileNames({"a", "b", "c"}); loadFromDirectory("C:"); - assertEqual("C:/a", next()); - assertEqual("C:/b", next()); - assertEqual("C:/c", next()); + assertNextEquals("C:/a"); + assertNextEquals("C:/b"); + assertNextEquals("C:/c"); } TEST_F(RandomizedTargetListTests, currentReturnsFullPathToFile) { @@ -115,7 +119,7 @@ TEST_F(RandomizedTargetListTests, currentReturnsFullPathToFile) { TEST_F(RandomizedTargetListTests, nextReturnsEmptyIfNoFiles) { setFileNames({}); loadFromDirectory(); - assertEqual("", next()); + assertNextEquals(""); } class FiniteRandomizedTargetListTests : public ::testing::Test { @@ -139,15 +143,22 @@ class FiniteRandomizedTargetListTests : public ::testing::Test { void assertNotEmpty() { EXPECT_FALSE(empty()); } void assertEmpty() { EXPECT_TRUE(empty()); } + + void reinsertCurrent() { list.reinsertCurrent(); } + + void assertNextEquals(const std::string &s) { assertEqual(s, next()); } }; -TEST_F(FiniteRandomizedTargetListTests, +#define FINITE_RANDOMIZED_TARGET_LIST_TEST(a) \ + TEST_F(FiniteRandomizedTargetListTests, a) + +FINITE_RANDOMIZED_TARGET_LIST_TEST( loadFromDirectoryPassesDirectoryToDirectoryReader) { loadFromDirectory("a"); assertEqual("a", reader.directory()); } -TEST_F(FiniteRandomizedTargetListTests, emptyWhenStimulusFilesExhausted) { +FINITE_RANDOMIZED_TARGET_LIST_TEST(emptyWhenStimulusFilesExhausted) { setFileNames({"a", "b", "c"}); loadFromDirectory(); assertNotEmpty(); @@ -159,49 +170,59 @@ TEST_F(FiniteRandomizedTargetListTests, emptyWhenStimulusFilesExhausted) { assertEmpty(); } -TEST_F(FiniteRandomizedTargetListTests, nextReturnsFullPathToFileAtFront) { +FINITE_RANDOMIZED_TARGET_LIST_TEST(nextReturnsFullPathToFileAtFront) { setFileNames({"a", "b", "c"}); loadFromDirectory("C:"); - assertEqual("C:/a", next()); - assertEqual("C:/b", next()); - assertEqual("C:/c", next()); + assertNextEquals("C:/a"); + assertNextEquals("C:/b"); + assertNextEquals("C:/c"); } -TEST_F(FiniteRandomizedTargetListTests, loadFromDirectoryShufflesFileNames) { +FINITE_RANDOMIZED_TARGET_LIST_TEST(loadFromDirectoryShufflesFileNames) { setFileNames({"a", "b", "c"}); loadFromDirectory(); assertEqual({"a", "b", "c"}, randomizer.toShuffle()); } -TEST_F(FiniteRandomizedTargetListTests, currentReturnsFullPathToFile) { +FINITE_RANDOMIZED_TARGET_LIST_TEST(currentReturnsFullPathToFile) { setFileNames({"a", "b", "c"}); loadFromDirectory("C:"); next(); assertEqual("C:/a", list.current()); } -TEST_F(FiniteRandomizedTargetListTests, nextReturnsEmptyIfNoFiles) { +FINITE_RANDOMIZED_TARGET_LIST_TEST(nextReturnsEmptyIfNoFiles) { setFileNames({}); loadFromDirectory(); - assertEqual("", next()); + assertNextEquals(""); +} + +FINITE_RANDOMIZED_TARGET_LIST_TEST(reinsertCurrent) { + setFileNames({"a", "b", "c"}); + loadFromDirectory("C:"); + assertNextEquals("C:/a"); + assertNextEquals("C:/b"); + reinsertCurrent(); + assertNextEquals("C:/c"); + assertNextEquals("C:/b"); } -std::vector filesIn( - DirectoryReader &reader, std::string directory = {}) { +auto filesIn(DirectoryReader &reader, std::string directory = {}) + -> std::vector { return reader.filesIn(std::move(directory)); } -std::vector filter( - FileFilter &filter_, std::vector files = {}) { +auto filter(FileFilter &filter_, std::vector files = {}) + -> std::vector { return filter_.filter(std::move(files)); } -std::vector subDirectories( - DirectoryReader &reader, std::string directory = {}) { +auto subDirectories(DirectoryReader &reader, std::string directory = {}) + -> std::vector { return reader.subDirectories(std::move(directory)); } -std::string directory(DirectoryReaderStub &reader) { +auto directory(DirectoryReaderStub &reader) -> std::string { return reader.directory(); } @@ -210,11 +231,12 @@ class FileFilterStub : public FileFilter { std::vector files_; public: - auto files() const { return files_; } + [[nodiscard]] auto files() const { return files_; } void setFiltered(std::vector f) { filtered_ = std::move(f); } - std::vector filter(std::vector f) { + auto filter(std::vector f) + -> std::vector override { files_ = std::move(f); return filtered_; } @@ -255,7 +277,8 @@ TEST_F(FileFilterDecoratorTests, returnsSubdirectories) { class FileExtensionFilterTests : public ::testing::Test { protected: - FileExtensionFilter construct(std::vector filters = {}) { + static auto construct(std::vector filters = {}) + -> FileExtensionFilter { return FileExtensionFilter{std::move(filters)}; } }; @@ -268,7 +291,8 @@ TEST_F(FileExtensionFilterTests, returnsFilteredFiles) { class FileIdentifierFilterTests : public ::testing::Test { protected: - FileIdentifierFilter construct(std::string indentifier = {}) { + static auto construct(std::string indentifier = {}) + -> FileIdentifierFilter { return FileIdentifierFilter{std::move(indentifier)}; } }; @@ -287,8 +311,8 @@ TEST_F(FileIdentifierFilterTests, returnsFilesThatEndWithIdentifier) { class FileIdentifierExcluderFilterTests : public ::testing::Test { protected: - FileIdentifierExcluderFilter construct( - std::vector indentifiers = {}) { + static auto construct(std::vector indentifiers = {}) + -> FileIdentifierExcluderFilter { return FileIdentifierExcluderFilter{std::move(indentifiers)}; } }; @@ -304,7 +328,7 @@ class RandomSubsetFilesTests : public ::testing::Test { protected: RandomizerStub randomizer; - RandomSubsetFiles construct(int N = {}) { return {&randomizer, N}; } + auto construct(int N = {}) -> RandomSubsetFiles { return {&randomizer, N}; } auto shuffled() { return randomizer.shuffledInts(); } @@ -334,7 +358,7 @@ class DirectoryReaderCompositeTests : public ::testing::Test { protected: std::vector decorated; - DirectoryReaderComposite construct() { + auto construct() -> DirectoryReaderComposite { std::vector ptrs; for (auto &d : decorated) ptrs.push_back(&d); @@ -348,7 +372,7 @@ class DirectoryReaderCompositeTests : public ::testing::Test { assertEqual(expected, decorated.at(i).directory()); } - auto &decoratedAt(int n) { return decorated.at(n); } + auto decoratedAt(int n) -> auto & { return decorated.at(n); } void setFileNamesForDecorated(std::vector v, int n) { decoratedAt(n).setFileNames(std::move(v)); diff --git a/tests/TargetPlayerStub.h b/tests/TargetPlayerStub.h index 96b869128..9aac4e0ea 100644 --- a/tests/TargetPlayerStub.h +++ b/tests/TargetPlayerStub.h @@ -29,9 +29,9 @@ class TargetPlayerStub : public TargetPlayer { playbackCompletionSubscribedTo_ = true; } - bool playing() override { return playing_; } + auto playing() -> bool override { return playing_; } - auto &log() const { return log_; } + auto log() const -> auto & { return log_; } auto setDeviceCalled() const { return setDeviceCalled_; } @@ -71,9 +71,9 @@ class TargetPlayerStub : public TargetPlayer { filePath_ = std::move(filePath); } - void addToLog(std::string s) { log_.insert(std::move(s)); } + void addToLog(const std::string &s) { log_.insert(s); } - double rms() override { + auto rms() -> double override { addToLog("rms "); if (throwInvalidAudioFileOnRms_) throw InvalidAudioFile{}; @@ -82,7 +82,7 @@ class TargetPlayerStub : public TargetPlayer { void setLevel_dB(double x) override { level_dB_ = x; } - double durationSeconds() override { return durationSeconds_; } + auto durationSeconds() -> double override { return durationSeconds_; } void playbackComplete() { listener_->playbackComplete(); } diff --git a/tests/TargetPlayerTests.cpp b/tests/TargetPlayerTests.cpp index dafa7f3e9..4e0daac29 100644 --- a/tests/TargetPlayerTests.cpp +++ b/tests/TargetPlayerTests.cpp @@ -20,23 +20,23 @@ class VideoPlayerStub : public stimulus_players::VideoPlayer { bool playbackCompletionSubscribedTo_{}; public: - double durationSeconds() override { return durationSeconds_; } + auto durationSeconds() -> double override { return durationSeconds_; } void subscribeToPlaybackCompletion() override { playbackCompletionSubscribedTo_ = true; } - bool playing() override { return playing_; } + auto playing() -> bool override { return playing_; } - auto playbackCompletionSubscribedTo() const { + [[nodiscard]] auto playbackCompletionSubscribedTo() const { return playbackCompletionSubscribedTo_; } - int deviceCount() override { + auto deviceCount() -> int override { return gsl::narrow(audioDeviceDescriptions_.size()); } - std::string deviceDescription(int index) override { + auto deviceDescription(int index) -> std::string override { return audioDeviceDescriptions_.at(index); } @@ -48,21 +48,21 @@ class VideoPlayerStub : public stimulus_players::VideoPlayer { void setDevice(int index) override { deviceIndex_ = index; } - auto deviceIndex() const { return deviceIndex_; } + [[nodiscard]] auto deviceIndex() const { return deviceIndex_; } void play() override { played_ = true; } - auto played() const { return played_; } + [[nodiscard]] auto played() const { return played_; } void loadFile(std::string f) override { filePath_ = std::move(f); } - auto filePath() const { return filePath_; } + [[nodiscard]] auto filePath() const { return filePath_; } void hide() override { hidden_ = true; } - auto hidden() const { return hidden_; } + [[nodiscard]] auto hidden() const { return hidden_; } - auto shown() const { return shown_; } + [[nodiscard]] auto shown() const { return shown_; } void setPlaying() { playing_ = true; } @@ -84,7 +84,7 @@ class TargetPlayerListenerStub public: void playbackComplete() override { notified_ = true; } - auto notified() const { return notified_; } + [[nodiscard]] auto notified() const { return notified_; } }; class TargetPlayerTests : public ::testing::Test { @@ -168,7 +168,7 @@ TEST_F(TargetPlayerTests, audioDevicesReturnsDescriptions) { TEST_F(TargetPlayerTests, rmsComputesFirstChannel) { audioReader.set({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}); - EXPECT_EQ(std::sqrt((1 * 1 + 2 * 2 + 3 * 3) / 3.f), player.rms()); + EXPECT_EQ(std::sqrt((1 * 1 + 2 * 2 + 3 * 3) / 3.F), player.rms()); } TEST_F(TargetPlayerTests, rmsPassesLoadedFileToVideoPlayer) { diff --git a/tests/TestConcluderStub.h b/tests/TestConcluderStub.h index 5bad9eec9..8d2abd89b 100644 --- a/tests/TestConcluderStub.h +++ b/tests/TestConcluderStub.h @@ -13,7 +13,7 @@ class TestConcluderStub : public TestConcluder { bool responseSubmitted_{}; public: - const auto &log() const { return log_; } + auto log() const -> const auto & { return log_; } auto targetList() const { return targetList_; } @@ -25,7 +25,7 @@ class TestConcluderStub : public TestConcluder { void setIncomplete() { complete_ = false; } - bool complete(TargetList *t) override { + auto complete(TargetList *t) -> bool override { log_.insert("complete "); targetList_ = t; return complete_; diff --git a/tests/TrackSettingsInterpreterTests.cpp b/tests/TrackSettingsInterpreterTests.cpp index b4fc8057a..9198070d4 100644 --- a/tests/TrackSettingsInterpreterTests.cpp +++ b/tests/TrackSettingsInterpreterTests.cpp @@ -11,32 +11,34 @@ class TrackSettingsInterpreterTests : public ::testing::Test { TrackSettingsInterpreterImpl interpreter; void assertFileContentsYield( - std::vector v, const TrackingRule &expected) { - assertEqual( - expected, *interpreter.trackingRule(concatenate(std::move(v)))); + const std::vector &v, const TrackingRule &expected) { + assertEqual(expected, *interpreter.trackingRule(concatenate(v))); } - std::string concatenate(std::vector v) { + static auto concatenate(const std::vector &v) -> std::string { std::string result; - for (auto v_ : std::move(v)) + for (const auto &v_ : v) result.append(v_); return result; } - std::string propertyEntry(Property p, std::string s) { + static auto propertyEntry(Property p, std::string s) -> std::string { return std::string{TrackSettingsInterpreterImpl::propertyName(p)} + ": " + std::move(s); } - std::string propertyEntryWithNewline(Property p, std::string s) { + static auto propertyEntryWithNewline(Property p, std::string s) + -> std::string { return withNewLine(propertyEntry(p, std::move(s))); } - std::string withNewLine(std::string s) { return std::move(s) + '\n'; } + static auto withNewLine(std::string s) -> std::string { + return std::move(s) + '\n'; + } }; TEST_F(TrackSettingsInterpreterTests, oneSequence) { - TrackingSequence sequence; + TrackingSequence sequence{}; sequence.up = 1; sequence.down = 2; sequence.runCount = 3; @@ -50,12 +52,12 @@ TEST_F(TrackSettingsInterpreterTests, oneSequence) { } TEST_F(TrackSettingsInterpreterTests, twoSequences) { - TrackingSequence first; + TrackingSequence first{}; first.up = 1; first.down = 3; first.runCount = 5; first.stepSize = 7; - TrackingSequence second; + TrackingSequence second{}; second.up = 2; second.down = 4; second.runCount = 6; @@ -69,7 +71,7 @@ TEST_F(TrackSettingsInterpreterTests, twoSequences) { } TEST_F(TrackSettingsInterpreterTests, differentPropertyOrder) { - TrackingSequence sequence; + TrackingSequence sequence{}; sequence.up = 1; sequence.down = 2; sequence.runCount = 3; @@ -83,7 +85,7 @@ TEST_F(TrackSettingsInterpreterTests, differentPropertyOrder) { } TEST_F(TrackSettingsInterpreterTests, ignoresAnyUninterpretableEntries) { - TrackingSequence sequence; + TrackingSequence sequence{}; sequence.up = 1; sequence.down = 2; sequence.runCount = 3; @@ -99,12 +101,12 @@ TEST_F(TrackSettingsInterpreterTests, ignoresAnyUninterpretableEntries) { } TEST_F(TrackSettingsInterpreterTests, canBeUsedToInterpretMoreThanOnce) { - TrackingSequence firstCallFirst; + TrackingSequence firstCallFirst{}; firstCallFirst.up = 1; firstCallFirst.down = 2; firstCallFirst.runCount = 3; firstCallFirst.stepSize = 4; - TrackingSequence firstCallSecond; + TrackingSequence firstCallSecond{}; firstCallSecond.up = 5; firstCallSecond.down = 6; firstCallSecond.runCount = 7; @@ -115,7 +117,7 @@ TEST_F(TrackSettingsInterpreterTests, canBeUsedToInterpretMoreThanOnce) { propertyEntryWithNewline(Property::reversalsPerStepSize, "3 7"), propertyEntry(Property::stepSizes, "4 8")}, {firstCallFirst, firstCallSecond}); - TrackingSequence secondCall; + TrackingSequence secondCall{}; secondCall.up = 9; secondCall.down = 10; secondCall.runCount = 11; diff --git a/tests/TrackSettingsReaderTests.cpp b/tests/TrackSettingsReaderTests.cpp index 0e75541ea..c7a33396a 100644 --- a/tests/TrackSettingsReaderTests.cpp +++ b/tests/TrackSettingsReaderTests.cpp @@ -9,9 +9,9 @@ class TextFileReaderStub : public TextFileReader { std::string contents_{}; public: - auto filePath() const { return filePath_; } + [[nodiscard]] auto filePath() const { return filePath_; } - std::string read(std::string s) override { + auto read(std::string s) -> std::string override { filePath_ = std::move(s); return contents_; } @@ -24,12 +24,12 @@ class TrackSettingsInterpreterStub : public TrackSettingsInterpreter { const TrackingRule *trackingRule_{}; public: - const TrackingRule *trackingRule(std::string s) override { + auto trackingRule(std::string s) -> const TrackingRule * override { content_ = std::move(s); return trackingRule_; } - auto content() const { return content_; } + [[nodiscard]] auto content() const { return content_; } void setTrackingRule(const TrackingRule *r) { trackingRule_ = r; } }; diff --git a/tests/TrackStub.h b/tests/TrackStub.h index 6620c1baf..cef0edc4e 100644 --- a/tests/TrackStub.h +++ b/tests/TrackStub.h @@ -23,9 +23,9 @@ class TrackStub : public Track { void setReversals(int x) { reversals_ = x; } - auto pushedUp() const { return pushedUp_; } + [[nodiscard]] auto pushedUp() const { return pushedUp_; } - auto pushedDown() const { return pushedDown_; } + [[nodiscard]] auto pushedDown() const { return pushedDown_; } void setX(int x) { x_ = x; } @@ -41,11 +41,11 @@ class TrackStub : public Track { x_ = xWhenUpdated_; } - int x() override { return x_; } + auto x() -> int override { return x_; } - bool complete() override { return complete_; } + auto complete() -> bool override { return complete_; } - int reversals() override { return reversals_; } + auto reversals() -> int override { return reversals_; } void setComplete() { complete_ = true; } }; @@ -55,9 +55,9 @@ class TrackFactoryStub : public Track::Factory { std::vector> tracks_; public: - auto ¶meters() const { return parameters_; } + auto parameters() const -> auto & { return parameters_; } - std::shared_ptr make(const Track::Settings &s) override { + auto make(const Track::Settings &s) -> std::shared_ptr override { parameters_.push_back(s); auto track = tracks_.front(); tracks_.erase(tracks_.begin()); diff --git a/tests/assert-utility.h b/tests/assert-utility.h index 0a0bd262c..431dbebfe 100644 --- a/tests/assert-utility.h +++ b/tests/assert-utility.h @@ -1,13 +1,16 @@ -#ifndef assert_utility_h -#define assert_utility_h +#ifndef TESTS_ASSERT_UTILITY_H_ +#define TESTS_ASSERT_UTILITY_H_ #include +#include +#include inline void assertTrue(bool c) { EXPECT_TRUE(c); } inline void assertFalse(bool c) { EXPECT_FALSE(c); } -inline void assertEqual(std::string expected, std::string actual) { +inline void assertEqual( + const std::string &expected, const std::string &actual) { EXPECT_EQ(expected, actual); }