From 16892ae991dd50d0082641eb37c7325a1434ad40 Mon Sep 17 00:00:00 2001 From: Daniel Wolf Date: Tue, 9 Aug 2016 22:31:16 +0200 Subject: [PATCH] Fixed OS X build --- CMakeLists.txt | 10 ++- src/Exporter.cpp | 2 +- src/ProgressBar.h | 1 + src/Viseme.cpp | 6 +- src/audio/AudioClip.cpp | 2 +- src/audio/DCOffset.cpp | 1 + src/audio/WaveFileReader.cpp | 5 +- src/audio/voiceActivityDetection.cpp | 10 +-- src/centiseconds.h | 6 +- src/logging.cpp | 25 +++++- src/mouthAnimation.cpp | 22 ++--- src/platformTools.cpp | 22 +++++ src/platformTools.h | 5 ++ src/tokenization.h | 1 + src/tools.cpp | 4 +- tests/BoundedTimelineTests.cpp | 68 +++++++------- tests/ContinuousTimelineTests.cpp | 72 +++++++-------- tests/TimelineTests.cpp | 130 +++++++++++++-------------- 18 files changed, 220 insertions(+), 172 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bcbf470..e6acaec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,11 +35,11 @@ if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" STREQ elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") set(enableWarningsFlags "/W4") set(disableWarningsFlags "/W0") - + # Disable warning C4456: declaration of '...' hides previous local declaration # I'm doing that on purpose. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4458") - + # Assume UTF-8 encoding for source files and encode string constants in UTF-8 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8") endif() @@ -116,6 +116,9 @@ set(webRTCFiles ) add_library(webRTC ${webRTCFiles}) target_compile_options(webRTC PRIVATE ${disableWarningsFlags}) +if (NOT WIN32) + target_compile_definitions(webRTC PRIVATE WEBRTC_POSIX) +endif() set_target_properties(webRTC PROPERTIES FOLDER lib) # ... Flite @@ -242,6 +245,7 @@ set(TEST_FILES tests/g2pTests.cpp tests/LazyTests.cpp src/stringTools.cpp src/stringTools.h + src/platformTools.cpp src/platformTools.h src/Timeline.h src/TimeRange.cpp src/TimeRange.h src/centiseconds.cpp src/centiseconds.h @@ -254,7 +258,7 @@ set(TEST_FILES src/Lazy.h ) add_executable(runTests ${TEST_FILES}) -target_link_libraries(runTests gtest gmock gmock_main flite cppFormat) +target_link_libraries(runTests ${Boost_LIBRARIES} gtest gmock gmock_main flite cppFormat) set(CPACK_PACKAGE_NAME ${appName}) string(REPLACE " " "-" CPACK_PACKAGE_NAME "${CPACK_PACKAGE_NAME}") diff --git a/src/Exporter.cpp b/src/Exporter.cpp index 1b9f301..61deae5 100644 --- a/src/Exporter.cpp +++ b/src/Exporter.cpp @@ -37,7 +37,7 @@ std::vector> dummyShapeIfEmpty(const Timeline& shapes) { std::copy(shapes.begin(), shapes.end(), std::back_inserter(result)); if (result.empty()) { // Add zero-length empty mouth - result.push_back(Timed(0cs, 0cs, Shape::A)); + result.push_back(Timed(0_cs, 0_cs, Shape::A)); } return result; } diff --git a/src/ProgressBar.h b/src/ProgressBar.h index ba697fc..93332df 100644 --- a/src/ProgressBar.h +++ b/src/ProgressBar.h @@ -6,6 +6,7 @@ #include #include #include +#include class ProgressSink { public: diff --git a/src/Viseme.cpp b/src/Viseme.cpp index 3acc9a1..b1dcb2b 100644 --- a/src/Viseme.cpp +++ b/src/Viseme.cpp @@ -27,15 +27,15 @@ bool VisemeOption::operator!=(const VisemeOption& rhs) const { } Viseme::Viseme(const VisemeOption& option) : - options{ { 0cs, option } } + options{ { 0_cs, option } } {} Viseme::Viseme(const VisemeOption& option1, centiseconds threshold, const VisemeOption& option2) : - options{ { 0cs, option1 }, { threshold, option2 } } + options{ { 0_cs, option1 }, { threshold, option2 } } {} Viseme::Viseme(const VisemeOption& option1, centiseconds threshold1, const VisemeOption& option2, centiseconds threshold2, const VisemeOption& option3) : - options{ { 0cs, option1 },{ threshold1, option2 }, { threshold2, option3 } } + options{ { 0_cs, option1 },{ threshold1, option2 }, { threshold2, option3 } } {} Shape Viseme::getShape(centiseconds duration, Shape context) const { diff --git a/src/audio/AudioClip.cpp b/src/audio/AudioClip.cpp index 6c83ff3..6e0bc48 100644 --- a/src/audio/AudioClip.cpp +++ b/src/audio/AudioClip.cpp @@ -4,7 +4,7 @@ using std::invalid_argument; TimeRange AudioClip::getTruncatedRange() const { - return TimeRange(0cs, centiseconds(100 * size() / getSampleRate())); + return TimeRange(0_cs, centiseconds(100 * size() / getSampleRate())); } class SafeSampleReader { diff --git a/src/audio/DCOffset.cpp b/src/audio/DCOffset.cpp index 6e5dc76..4af36ca 100644 --- a/src/audio/DCOffset.cpp +++ b/src/audio/DCOffset.cpp @@ -1,4 +1,5 @@ #include "DCOffset.h" +#include using std::unique_ptr; using std::make_unique; diff --git a/src/audio/WaveFileReader.cpp b/src/audio/WaveFileReader.cpp index f8f31ce..c192875 100644 --- a/src/audio/WaveFileReader.cpp +++ b/src/audio/WaveFileReader.cpp @@ -2,6 +2,7 @@ #include #include "WaveFileReader.h" #include "ioTools.h" +#include "platformTools.h" using std::runtime_error; using fmt::format; @@ -46,9 +47,7 @@ std::ifstream openFile(path filePath) { return std::move(file); } catch (const std::ifstream::failure&) { - char message[256]; - strerror_s(message, sizeof message, errno); - throw runtime_error(message); + throw runtime_error(errorNumberToString(errno)); } } diff --git a/src/audio/voiceActivityDetection.cpp b/src/audio/voiceActivityDetection.cpp index e3970ee..ec7abc2 100644 --- a/src/audio/voiceActivityDetection.cpp +++ b/src/audio/voiceActivityDetection.cpp @@ -35,7 +35,7 @@ BoundedTimeline webRtcDetectVoiceActivity(const AudioClip& audioClip, Prog // Detect activity BoundedTimeline activity(audioClip.getTruncatedRange()); - centiseconds time = 0cs; + centiseconds time = 0_cs; const size_t bufferCapacity = audioClip.getSampleRate() / 100; auto processBuffer = [&](const vector& buffer) { // WebRTC is picky regarding buffer size @@ -46,9 +46,9 @@ BoundedTimeline webRtcDetectVoiceActivity(const AudioClip& audioClip, Prog bool isActive = result != 0; if (isActive) { - activity.set(time, time + 1cs); + activity.set(time, time + 1_cs); } - time += 1cs; + time += 1_cs; }; process16bitAudioClip(audioClip, processBuffer, bufferCapacity, pass1ProgressSink); @@ -58,8 +58,8 @@ BoundedTimeline webRtcDetectVoiceActivity(const AudioClip& audioClip, Prog if (!activity.empty()) { TimeRange firstActivity = activity.begin()->getTimeRange(); activity.clear(firstActivity); - unique_ptr streamStart = audioClip.clone() | segment(TimeRange(0cs, firstActivity.getEnd())); - time = 0cs; + unique_ptr streamStart = audioClip.clone() | segment(TimeRange(0_cs, firstActivity.getEnd())); + time = 0_cs; process16bitAudioClip(*streamStart, processBuffer, bufferCapacity, pass2ProgressSink); } diff --git a/src/centiseconds.h b/src/centiseconds.h index a5b1a4a..2f05c46 100644 --- a/src/centiseconds.h +++ b/src/centiseconds.h @@ -1,17 +1,15 @@ #pragma once #include +#include typedef std::chrono::duration centiseconds; std::ostream& operator <<(std::ostream& stream, const centiseconds cs); -// I know user-defined literals should start with an underscore. -// But chances are slim the standard will introduce a "cs" literal -// with a different meaning than "centiseconds". #pragma warning(push) #pragma warning(disable: 4455) -inline constexpr centiseconds operator ""cs(unsigned long long cs) { +inline constexpr centiseconds operator "" _cs(unsigned long long cs) { return centiseconds(cs); } #pragma warning(pop) diff --git a/src/logging.cpp b/src/logging.cpp index cba8547..f5b95fd 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -2,12 +2,15 @@ #include #include #include +#include +#include using namespace logging; using std::string; using std::vector; using std::shared_ptr; using std::lock_guard; +using std::unordered_map; LevelConverter& LevelConverter::get() { static LevelConverter converter; @@ -37,15 +40,29 @@ std::istream& logging::operator>>(std::istream& stream, Level& value) { return LevelConverter::get().read(stream, value); } +// Returns an int representing the current thread. +// This used to be a simple thread_local variable, but Xcode doesn't support that yet +int getThreadCounter() { + using thread_id = std::thread::id; + + static std::mutex counterMutex; + lock_guard lock(counterMutex); + + static unordered_map threadCounters; + static int lastThreadId = 0; + thread_id threadId = std::this_thread::get_id(); + if (threadCounters.find(threadId) == threadCounters.end()) { + threadCounters.insert({threadId, ++lastThreadId}); + } + return threadCounters.find(threadId)->second; +} + Entry::Entry(Level level, const string& message) : level(level), message(message) { time(×tamp); - - static std::atomic lastThreadId = 0; - thread_local int threadCounter = ++lastThreadId; - this->threadCounter = threadCounter; + this->threadCounter = getThreadCounter(); } string SimpleConsoleFormatter::format(const Entry& entry) { diff --git a/src/mouthAnimation.cpp b/src/mouthAnimation.cpp index f5c1b4e..06e4d82 100644 --- a/src/mouthAnimation.cpp +++ b/src/mouthAnimation.cpp @@ -28,21 +28,21 @@ constexpr Shape X = Shape::X; Timeline animate(optional phone, centiseconds duration, centiseconds previousPhoneDuration) { auto single = [&](Viseme viseme) { return Timeline{ - { 0cs, duration, viseme } + { 0_cs, duration, viseme } }; }; auto diphtong = [&](Viseme first, Viseme second) { centiseconds firstDuration = duration_cast(duration * 0.6); return Timeline{ - { 0cs, firstDuration, first }, + { 0_cs, firstDuration, first }, { firstDuration, duration, second } }; }; auto bilabialStop = [&]() { - centiseconds maxDuration = 12cs; - centiseconds leftOverlap = clamp(previousPhoneDuration / 2, 4cs, maxDuration); + centiseconds maxDuration = 12_cs; + centiseconds leftOverlap = clamp(previousPhoneDuration / 2, 4_cs, maxDuration); centiseconds rightOverlap = min(duration, maxDuration - leftOverlap); return Timeline{ { -leftOverlap, rightOverlap, { A } }, @@ -61,7 +61,7 @@ Timeline animate(optional phone, centiseconds duration, centiseco case Phone::EH: return single({ C }); case Phone::IH: return single({ B }); case Phone::UH: return single({ E }); - case Phone::AH: return single({ { B, C, D, E, F }, 6cs, { C } }); // Heuristic: < 6cs is schwa + case Phone::AH: return single({ { B, C, D, E, F }, 6_cs, { C } }); // Heuristic: < 6_cs is schwa case Phone::AE: return single({ D }); case Phone::EY: return diphtong({ C }, { B }); case Phone::AY: return diphtong({ D }, { B }); @@ -119,8 +119,8 @@ optional> getTween(Shape first, Shape second) { } Timeline createTweens(ContinuousTimeline shapes) { - centiseconds minTweenDuration = 4cs; - centiseconds maxTweenDuration = 10cs; + centiseconds minTweenDuration = 4_cs; + centiseconds maxTweenDuration = 10_cs; Timeline tweens; @@ -171,7 +171,7 @@ Timeline animatePauses(const ContinuousTimeline& shapes) { for (const auto& timedShape : shapes) { if (timedShape.getValue() != X) continue; - const centiseconds maxPausedOpenMouthDuration = 35cs; + const centiseconds maxPausedOpenMouthDuration = 35_cs; const TimeRange timeRange = timedShape.getTimeRange(); if (timeRange.getLength() <= maxPausedOpenMouthDuration) { result.set(timeRange, B); @@ -183,9 +183,9 @@ Timeline animatePauses(const ContinuousTimeline& shapes) { if (pause.getValue() != X) return; centiseconds lastLength = last.getTimeRange().getLength(); - const centiseconds minOpenDuration = 20cs; + const centiseconds minOpenDuration = 20_cs; if (isClosed(secondLast.getValue()) && !isClosed(last.getValue()) && lastLength < minOpenDuration) { - const centiseconds minSpillDuration = 20cs; + const centiseconds minSpillDuration = 20_cs; centiseconds spillDuration = std::min(minSpillDuration, pause.getTimeRange().getLength()); result.set(pause.getStart(), pause.getStart() + spillDuration, B); } @@ -203,7 +203,7 @@ ContinuousTimeline animate(const BoundedTimeline &phones) { // Create timeline of visemes ContinuousTimeline visemes(phones.getRange(), { X }); - centiseconds previousPhoneDuration = 0cs; + centiseconds previousPhoneDuration = 0_cs; for (const auto& timedPhone : continuousPhones) { // Animate one phone optional phone = timedPhone.getValue(); diff --git a/src/platformTools.cpp b/src/platformTools.cpp index e7fd948..36e0339 100644 --- a/src/platformTools.cpp +++ b/src/platformTools.cpp @@ -140,3 +140,25 @@ path getTempFilePath() { string fileName = to_string(generateUuid()); return tempDirectory / fileName; } + +std::tm getLocalTime(const time_t& time) { + // Xcode doesn't support localtime_s. + tm timeInfo; +#if (BOOST_OS_MACOS) + localtime_r(&time, &timeInfo); +#else + localtime_s(&timeInfo, &time); +#endif + return timeInfo; +} + +std::string errorNumberToString(int errorNumber) { + // Xcode doesn't support strerror_s. + char message[256]; +#if (BOOST_OS_MACOS) + strerror_r(errorNumber, message, sizeof message); +#else + strerror_s(message, sizeof message, errorNumber); +#endif + return message; +} diff --git a/src/platformTools.h b/src/platformTools.h index d9053a2..d5b3529 100644 --- a/src/platformTools.h +++ b/src/platformTools.h @@ -1,7 +1,12 @@ #pragma once #include +#include +#include boost::filesystem::path getBinPath(); boost::filesystem::path getBinDirectory(); boost::filesystem::path getTempFilePath(); + +std::tm getLocalTime(const time_t& time); +std::string errorNumberToString(int errorNumber); diff --git a/src/tokenization.h b/src/tokenization.h index 7b34961..16e48fc 100644 --- a/src/tokenization.h +++ b/src/tokenization.h @@ -2,5 +2,6 @@ #include #include +#include std::vector tokenizeText(const std::u32string& text, std::function dictionaryContains); diff --git a/src/tools.cpp b/src/tools.cpp index 5ad0242..6932eb3 100644 --- a/src/tools.cpp +++ b/src/tools.cpp @@ -1,4 +1,5 @@ #include "tools.h" +#include "platformTools.h" #include #include #include @@ -11,8 +12,7 @@ string formatDuration(duration seconds) { } string formatTime(time_t time, const string& format) { - tm timeInfo; - localtime_s(&timeInfo, &time); + tm timeInfo = getLocalTime(time); std::vector buffer(20); bool success = false; while (!success) { diff --git a/tests/BoundedTimelineTests.cpp b/tests/BoundedTimelineTests.cpp index 66bde70..b449895 100644 --- a/tests/BoundedTimelineTests.cpp +++ b/tests/BoundedTimelineTests.cpp @@ -7,16 +7,16 @@ using boost::optional; using std::initializer_list; TEST(BoundedTimeline, constructors_initializeState) { - TimeRange range(-5cs, 55cs); + TimeRange range(-5_cs, 55_cs); auto args = { - Timed(-10cs, 30cs, 1), - Timed(10cs, 40cs, 2), - Timed(50cs, 60cs, 3) + Timed(-10_cs, 30_cs, 1), + Timed(10_cs, 40_cs, 2), + Timed(50_cs, 60_cs, 3) }; auto expected = { - Timed(-5cs, 10cs, 1), - Timed(10cs, 40cs, 2), - Timed(50cs, 55cs, 3) + Timed(-5_cs, 10_cs, 1), + Timed(10_cs, 40_cs, 2), + Timed(50_cs, 55_cs, 3) }; EXPECT_THAT( BoundedTimeline(range, args.begin(), args.end()), @@ -29,67 +29,67 @@ TEST(BoundedTimeline, constructors_initializeState) { } TEST(BoundedTimeline, empty) { - BoundedTimeline empty(TimeRange(0cs, 10cs)); + BoundedTimeline empty(TimeRange(0_cs, 10_cs)); EXPECT_TRUE(empty.empty()); EXPECT_THAT(empty, IsEmpty()); - BoundedTimeline nonEmpty(TimeRange(0cs, 10cs), { Timed(1cs, 2cs, 1) }); + BoundedTimeline nonEmpty(TimeRange(0_cs, 10_cs), { Timed(1_cs, 2_cs, 1) }); EXPECT_FALSE(nonEmpty.empty()); EXPECT_THAT(nonEmpty, Not(IsEmpty())); } TEST(BoundedTimeline, getRange) { - TimeRange range(0cs, 10cs); + TimeRange range(0_cs, 10_cs); BoundedTimeline empty(range); EXPECT_EQ(range, empty.getRange()); - BoundedTimeline nonEmpty(range, { Timed(1cs, 2cs, 1) }); + BoundedTimeline nonEmpty(range, { Timed(1_cs, 2_cs, 1) }); EXPECT_EQ(range, nonEmpty.getRange()); } TEST(BoundedTimeline, setAndClear) { - TimeRange range(0cs, 10cs); + TimeRange range(0_cs, 10_cs); BoundedTimeline timeline(range); // Out of range - timeline.set(-10cs, -1cs, 1); - timeline.set(TimeRange(-5cs, -1cs), 2); - timeline.set(Timed(10cs, 15cs, 3)); + timeline.set(-10_cs, -1_cs, 1); + timeline.set(TimeRange(-5_cs, -1_cs), 2); + timeline.set(Timed(10_cs, 15_cs, 3)); // Overlapping - timeline.set(-2cs, 5cs, 4); - timeline.set(TimeRange(-1cs, 1cs), 5); - timeline.set(Timed(8cs, 12cs, 6)); + timeline.set(-2_cs, 5_cs, 4); + timeline.set(TimeRange(-1_cs, 1_cs), 5); + timeline.set(Timed(8_cs, 12_cs, 6)); // Within - timeline.set(5cs, 9cs, 7); - timeline.set(TimeRange(6cs, 7cs), 8); - timeline.set(Timed(7cs, 8cs, 9)); + timeline.set(5_cs, 9_cs, 7); + timeline.set(TimeRange(6_cs, 7_cs), 8); + timeline.set(Timed(7_cs, 8_cs, 9)); auto expected = { - Timed(0cs, 1cs, 5), - Timed(1cs, 5cs, 4), - Timed(5cs, 6cs, 7), - Timed(6cs, 7cs, 8), - Timed(7cs, 8cs, 9), - Timed(8cs, 9cs, 7), - Timed(9cs, 10cs, 6) + Timed(0_cs, 1_cs, 5), + Timed(1_cs, 5_cs, 4), + Timed(5_cs, 6_cs, 7), + Timed(6_cs, 7_cs, 8), + Timed(7_cs, 8_cs, 9), + Timed(8_cs, 9_cs, 7), + Timed(9_cs, 10_cs, 6) }; EXPECT_THAT(timeline, ElementsAreArray(expected)); } TEST(BoundedTimeline, shift) { - BoundedTimeline timeline(TimeRange(0cs, 10cs), { { 1cs, 2cs, 1 }, { 2cs, 5cs, 2 }, { 7cs, 9cs, 3 } }); - BoundedTimeline expected(TimeRange(2cs, 12cs), { { 3cs, 4cs, 1 }, { 4cs, 7cs, 2 }, { 9cs, 11cs, 3 } }); - timeline.shift(2cs); + BoundedTimeline timeline(TimeRange(0_cs, 10_cs), { { 1_cs, 2_cs, 1 }, { 2_cs, 5_cs, 2 }, { 7_cs, 9_cs, 3 } }); + BoundedTimeline expected(TimeRange(2_cs, 12_cs), { { 3_cs, 4_cs, 1 }, { 4_cs, 7_cs, 2 }, { 9_cs, 11_cs, 3 } }); + timeline.shift(2_cs); EXPECT_EQ(expected, timeline); } TEST(BoundedTimeline, equality) { vector> timelines = { - BoundedTimeline(TimeRange(0cs, 10cs)), - BoundedTimeline(TimeRange(0cs, 10cs), { { 1cs, 2cs, 1 } }), - BoundedTimeline(TimeRange(1cs, 10cs), { { 1cs, 2cs, 1 } }) + BoundedTimeline(TimeRange(0_cs, 10_cs)), + BoundedTimeline(TimeRange(0_cs, 10_cs), { { 1_cs, 2_cs, 1 } }), + BoundedTimeline(TimeRange(1_cs, 10_cs), { { 1_cs, 2_cs, 1 } }) }; for (size_t i = 0; i < timelines.size(); ++i) { diff --git a/tests/ContinuousTimelineTests.cpp b/tests/ContinuousTimelineTests.cpp index 4f7cc3f..83b736e 100644 --- a/tests/ContinuousTimelineTests.cpp +++ b/tests/ContinuousTimelineTests.cpp @@ -7,18 +7,18 @@ using boost::optional; using std::initializer_list; TEST(ContinuousTimeline, constructors_initializeState) { - TimeRange range(-5cs, 55cs); + TimeRange range(-5_cs, 55_cs); int defaultValue = -1; auto args = { - Timed(-10cs, 30cs, 1), - Timed(10cs, 40cs, 2), - Timed(50cs, 60cs, 3) + Timed(-10_cs, 30_cs, 1), + Timed(10_cs, 40_cs, 2), + Timed(50_cs, 60_cs, 3) }; auto expected = { - Timed(-5cs, 10cs, 1), - Timed(10cs, 40cs, 2), - Timed(40cs, 50cs, defaultValue), - Timed(50cs, 55cs, 3) + Timed(-5_cs, 10_cs, 1), + Timed(10_cs, 40_cs, 2), + Timed(40_cs, 50_cs, defaultValue), + Timed(50_cs, 55_cs, 3) }; EXPECT_THAT( ContinuousTimeline(range, defaultValue, args.begin(), args.end()), @@ -31,65 +31,65 @@ TEST(ContinuousTimeline, constructors_initializeState) { } TEST(ContinuousTimeline, empty) { - ContinuousTimeline empty(TimeRange(10cs, 10cs), -1); + ContinuousTimeline empty(TimeRange(10_cs, 10_cs), -1); EXPECT_TRUE(empty.empty()); EXPECT_THAT(empty, IsEmpty()); - ContinuousTimeline nonEmpty1(TimeRange(0cs, 10cs), -1); + ContinuousTimeline nonEmpty1(TimeRange(0_cs, 10_cs), -1); EXPECT_FALSE(nonEmpty1.empty()); EXPECT_THAT(nonEmpty1, Not(IsEmpty())); - ContinuousTimeline nonEmpty2(TimeRange(0cs, 10cs), -1, { Timed(1cs, 2cs, 1) }); + ContinuousTimeline nonEmpty2(TimeRange(0_cs, 10_cs), -1, { Timed(1_cs, 2_cs, 1) }); EXPECT_FALSE(nonEmpty2.empty()); EXPECT_THAT(nonEmpty2, Not(IsEmpty())); } TEST(ContinuousTimeline, setAndClear) { - TimeRange range(0cs, 10cs); + TimeRange range(0_cs, 10_cs); int defaultValue = -1; ContinuousTimeline timeline(range, defaultValue); // Out of range - timeline.set(-10cs, -1cs, 1); - timeline.set(TimeRange(-5cs, -1cs), 2); - timeline.set(Timed(10cs, 15cs, 3)); + timeline.set(-10_cs, -1_cs, 1); + timeline.set(TimeRange(-5_cs, -1_cs), 2); + timeline.set(Timed(10_cs, 15_cs, 3)); // Overlapping - timeline.set(-2cs, 3cs, 4); - timeline.set(TimeRange(-1cs, 1cs), 5); - timeline.set(Timed(8cs, 12cs, 6)); + timeline.set(-2_cs, 3_cs, 4); + timeline.set(TimeRange(-1_cs, 1_cs), 5); + timeline.set(Timed(8_cs, 12_cs, 6)); // Within - timeline.set(5cs, 9cs, 7); - timeline.set(TimeRange(6cs, 7cs), 8); - timeline.set(Timed(7cs, 8cs, 9)); + timeline.set(5_cs, 9_cs, 7); + timeline.set(TimeRange(6_cs, 7_cs), 8); + timeline.set(Timed(7_cs, 8_cs, 9)); auto expected = { - Timed(0cs, 1cs, 5), - Timed(1cs, 3cs, 4), - Timed(3cs, 5cs, defaultValue), - Timed(5cs, 6cs, 7), - Timed(6cs, 7cs, 8), - Timed(7cs, 8cs, 9), - Timed(8cs, 9cs, 7), - Timed(9cs, 10cs, 6) + Timed(0_cs, 1_cs, 5), + Timed(1_cs, 3_cs, 4), + Timed(3_cs, 5_cs, defaultValue), + Timed(5_cs, 6_cs, 7), + Timed(6_cs, 7_cs, 8), + Timed(7_cs, 8_cs, 9), + Timed(8_cs, 9_cs, 7), + Timed(9_cs, 10_cs, 6) }; EXPECT_THAT(timeline, ElementsAreArray(expected)); } TEST(ContinuousTimeline, shift) { - ContinuousTimeline timeline(TimeRange(0cs, 10cs), -1, { { 1cs, 2cs, 1 },{ 2cs, 5cs, 2 },{ 7cs, 9cs, 3 } }); - ContinuousTimeline expected(TimeRange(2cs, 12cs), -1, { { 3cs, 4cs, 1 },{ 4cs, 7cs, 2 },{ 9cs, 11cs, 3 } }); - timeline.shift(2cs); + ContinuousTimeline timeline(TimeRange(0_cs, 10_cs), -1, { { 1_cs, 2_cs, 1 },{ 2_cs, 5_cs, 2 },{ 7_cs, 9_cs, 3 } }); + ContinuousTimeline expected(TimeRange(2_cs, 12_cs), -1, { { 3_cs, 4_cs, 1 },{ 4_cs, 7_cs, 2 },{ 9_cs, 11_cs, 3 } }); + timeline.shift(2_cs); EXPECT_EQ(expected, timeline); } TEST(ContinuousTimeline, equality) { vector> timelines = { - ContinuousTimeline(TimeRange(0cs, 10cs), -1), - ContinuousTimeline(TimeRange(0cs, 10cs), 1), - ContinuousTimeline(TimeRange(0cs, 10cs), -1, { { 1cs, 2cs, 1 } }), - ContinuousTimeline(TimeRange(1cs, 10cs), -1, { { 1cs, 2cs, 1 } }) + ContinuousTimeline(TimeRange(0_cs, 10_cs), -1), + ContinuousTimeline(TimeRange(0_cs, 10_cs), 1), + ContinuousTimeline(TimeRange(0_cs, 10_cs), -1, { { 1_cs, 2_cs, 1 } }), + ContinuousTimeline(TimeRange(1_cs, 10_cs), -1, { { 1_cs, 2_cs, 1 } }) }; for (size_t i = 0; i < timelines.size(); ++i) { diff --git a/tests/TimelineTests.cpp b/tests/TimelineTests.cpp index 7dd1b6b..095d526 100644 --- a/tests/TimelineTests.cpp +++ b/tests/TimelineTests.cpp @@ -11,14 +11,14 @@ using boost::none; TEST(Timeline, constructors_initializeState) { auto args = { - Timed(-10cs, 30cs, 1), - Timed(10cs, 40cs, 2), - Timed(50cs, 60cs, 3) + Timed(-10_cs, 30_cs, 1), + Timed(10_cs, 40_cs, 2), + Timed(50_cs, 60_cs, 3) }; auto expected = { - Timed(-10cs, 10cs, 1), - Timed(10cs, 40cs, 2), - Timed(50cs, 60cs, 3) + Timed(-10_cs, 10_cs, 1), + Timed(10_cs, 40_cs, 2), + Timed(50_cs, 60_cs, 3) }; EXPECT_THAT( Timeline(args.begin(), args.end()), @@ -39,11 +39,11 @@ TEST(Timeline, empty) { EXPECT_TRUE(empty1.empty()); EXPECT_THAT(empty1, IsEmpty()); - Timeline empty2{ Timed(1cs, 1cs, 1) }; + Timeline empty2{ Timed(1_cs, 1_cs, 1) }; EXPECT_TRUE(empty2.empty()); EXPECT_THAT(empty2, IsEmpty()); - Timeline nonEmpty{ Timed(1cs, 2cs, 1) }; + Timeline nonEmpty{ Timed(1_cs, 2_cs, 1) }; EXPECT_FALSE(nonEmpty.empty()); EXPECT_THAT(nonEmpty, Not(IsEmpty())); } @@ -57,39 +57,39 @@ TEST(Timeline, size) { EXPECT_EQ(0, empty1.size()); EXPECT_THAT(empty1, SizeIs(0)); - Timeline empty2{ Timed(1cs, 1cs, 1) }; + Timeline empty2{ Timed(1_cs, 1_cs, 1) }; EXPECT_EQ(0, empty2.size()); EXPECT_THAT(empty2, SizeIs(0)); - Timeline size1{ Timed(1cs, 10cs, 1) }; + Timeline size1{ Timed(1_cs, 10_cs, 1) }; EXPECT_EQ(1, size1.size()); EXPECT_THAT(size1, SizeIs(1)); - Timeline size2{ Timed(-10cs, 10cs, 1), Timed(10cs, 11cs, 5) }; + Timeline size2{ Timed(-10_cs, 10_cs, 1), Timed(10_cs, 11_cs, 5) }; EXPECT_EQ(2, size2.size()); EXPECT_THAT(size2, SizeIs(2)); } TEST(Timeline, getRange) { Timeline empty0; - EXPECT_EQ(TimeRange(0cs, 0cs), empty0.getRange()); + EXPECT_EQ(TimeRange(0_cs, 0_cs), empty0.getRange()); Timeline empty1{}; - EXPECT_EQ(TimeRange(0cs, 0cs), empty1.getRange()); + EXPECT_EQ(TimeRange(0_cs, 0_cs), empty1.getRange()); - Timeline empty2{ Timed(1cs, 1cs, 1) }; - EXPECT_EQ(TimeRange(0cs, 0cs), empty2.getRange()); + Timeline empty2{ Timed(1_cs, 1_cs, 1) }; + EXPECT_EQ(TimeRange(0_cs, 0_cs), empty2.getRange()); - Timeline nonEmpty1{ Timed(1cs, 10cs, 1) }; - EXPECT_EQ(TimeRange(1cs, 10cs), nonEmpty1.getRange()); + Timeline nonEmpty1{ Timed(1_cs, 10_cs, 1) }; + EXPECT_EQ(TimeRange(1_cs, 10_cs), nonEmpty1.getRange()); - Timeline nonEmpty2{ Timed(-10cs, 5cs, 1), Timed(10cs, 11cs, 5) }; - EXPECT_EQ(TimeRange(-10cs, 11cs), nonEmpty2.getRange()); + Timeline nonEmpty2{ Timed(-10_cs, 5_cs, 1), Timed(10_cs, 11_cs, 5) }; + EXPECT_EQ(TimeRange(-10_cs, 11_cs), nonEmpty2.getRange()); } TEST(Timeline, iterators) { - Timeline timeline{ Timed(-5cs, 0cs, 10), Timed(5cs, 15cs, 9) }; - auto expected = { Timed(-5cs, 0cs, 10), Timed(5cs, 15cs, 9) }; + Timeline timeline{ Timed(-5_cs, 0_cs, 10), Timed(5_cs, 15_cs, 9) }; + auto expected = { Timed(-5_cs, 0_cs, 10), Timed(5_cs, 15_cs, 9) }; EXPECT_THAT(timeline, ElementsAreArray(expected)); vector> reversedActual; @@ -115,9 +115,9 @@ void testFind(const Timeline& timeline, FindMode findMode, const initialize } TEST(Timeline, find) { - Timed a = Timed(1cs, 2cs, 1); - Timed b = Timed(2cs, 5cs, 2); - Timed c = Timed(7cs, 9cs, 3); + Timed a = Timed(1_cs, 2_cs, 1); + Timed b = Timed(2_cs, 5_cs, 2); + Timed c = Timed(7_cs, 9_cs, 3); Timeline timeline{ a, b, c }; testFind(timeline, FindMode::SampleLeft, { nullptr, nullptr, &a, &b, &b, &b, nullptr, nullptr, &c, &c, nullptr }); @@ -127,9 +127,9 @@ TEST(Timeline, find) { } TEST(Timeline, get) { - Timed a = Timed(1cs, 2cs, 1); - Timed b = Timed(2cs, 5cs, 2); - Timed c = Timed(7cs, 9cs, 3); + Timed a = Timed(1_cs, 2_cs, 1); + Timed b = Timed(2_cs, 5_cs, 2); + Timed c = Timed(7_cs, 9_cs, 3); Timeline timeline{ a, b, c }; initializer_list*> expectedResults = { nullptr, &a, &b, &b, &b, nullptr, nullptr, &c, &c, nullptr, nullptr }; @@ -148,39 +148,39 @@ TEST(Timeline, get) { } TEST(Timeline, clear) { - Timeline original{ { 1cs, 2cs, 1 }, { 2cs, 5cs, 2 }, { 7cs, 9cs, 3 } }; + Timeline original{ { 1_cs, 2_cs, 1 }, { 2_cs, 5_cs, 2 }, { 7_cs, 9_cs, 3 } }; { auto timeline = original; - timeline.clear(-10cs, 10cs); + timeline.clear(-10_cs, 10_cs); EXPECT_THAT(timeline, IsEmpty()); } { auto timeline = original; - timeline.clear(1cs, 2cs); - Timeline expected{ { 2cs, 5cs, 2 }, { 7cs, 9cs, 3 } }; + timeline.clear(1_cs, 2_cs); + Timeline expected{ { 2_cs, 5_cs, 2 }, { 7_cs, 9_cs, 3 } }; EXPECT_EQ(expected, timeline); } { auto timeline = original; - timeline.clear(3cs, 4cs); - Timeline expected{ { 1cs, 2cs, 1 }, { 2cs, 3cs, 2 }, { 4cs, 5cs, 2}, { 7cs, 9cs, 3} }; + timeline.clear(3_cs, 4_cs); + Timeline expected{ { 1_cs, 2_cs, 1 }, { 2_cs, 3_cs, 2 }, { 4_cs, 5_cs, 2}, { 7_cs, 9_cs, 3} }; EXPECT_EQ(expected, timeline); } { auto timeline = original; - timeline.clear(6cs, 8cs); - Timeline expected{ { 1cs, 2cs, 1 }, { 2cs, 5cs, 2 }, { 8cs, 9cs, 3 } }; + timeline.clear(6_cs, 8_cs); + Timeline expected{ { 1_cs, 2_cs, 1 }, { 2_cs, 5_cs, 2 }, { 8_cs, 9_cs, 3 } }; EXPECT_EQ(expected, timeline); } { auto timeline = original; - timeline.clear(8cs, 10cs); - Timeline expected{ { 1cs, 2cs, 1 }, { 2cs, 5cs, 2 }, { 7cs, 8cs, 3 } }; + timeline.clear(8_cs, 10_cs); + Timeline expected{ { 1_cs, 2_cs, 1 }, { 2_cs, 5_cs, 2 }, { 7_cs, 8_cs, 3 } }; EXPECT_EQ(expected, timeline); } } @@ -189,23 +189,23 @@ void testSetter(std::function&, Timeline&)> set) { Timeline timeline; vector> expectedValues(20, none); auto newElements = { - Timed(1cs, 2cs, 4), - Timed(3cs, 6cs, 4), - Timed(7cs, 9cs, 5), - Timed(9cs, 10cs, 6), - Timed(2cs, 3cs, 4), - Timed(0cs, 1cs, 7), - Timed(-10cs, 1cs, 8), - Timed(-10cs, 0cs, 9), - Timed(-10cs, -1cs, 10), - Timed(9cs, 20cs, 11), - Timed(10cs, 20cs, 12), - Timed(11cs, 20cs, 13), - Timed(4cs, 6cs, 14), - Timed(4cs, 6cs, 15), - Timed(8cs, 10cs, 15), - Timed(6cs, 8cs, 15), - Timed(6cs, 8cs, 16) + Timed(1_cs, 2_cs, 4), + Timed(3_cs, 6_cs, 4), + Timed(7_cs, 9_cs, 5), + Timed(9_cs, 10_cs, 6), + Timed(2_cs, 3_cs, 4), + Timed(0_cs, 1_cs, 7), + Timed(-10_cs, 1_cs, 8), + Timed(-10_cs, 0_cs, 9), + Timed(-10_cs, -1_cs, 10), + Timed(9_cs, 20_cs, 11), + Timed(10_cs, 20_cs, 12), + Timed(11_cs, 20_cs, 13), + Timed(4_cs, 6_cs, 14), + Timed(4_cs, 6_cs, 15), + Timed(8_cs, 10_cs, 15), + Timed(6_cs, 8_cs, 15), + Timed(6_cs, 8_cs, 16) }; int newElementIndex = -1; for (const auto& newElement : newElements) { @@ -214,14 +214,14 @@ void testSetter(std::function&, Timeline&)> set) { set(newElement, timeline); // Update expected value for every index - centiseconds elementStart = max(newElement.getStart(), 0cs); + centiseconds elementStart = max(newElement.getStart(), 0_cs); centiseconds elementEnd = newElement.getEnd(); for (centiseconds t = elementStart; t < elementEnd; ++t) { expectedValues[t.count()] = newElement.getValue(); } // Check timeline via indexer - for (centiseconds t = 0cs; t < 10cs; ++t) { + for (centiseconds t = 0_cs; t < 10_cs; ++t) { optional actual = timeline[t]; EXPECT_EQ(expectedValues[t.count()], actual ? optional(*actual) : none); } @@ -230,7 +230,7 @@ void testSetter(std::function&, Timeline&)> set) { Timed lastElement(centiseconds::min(), centiseconds::min(), std::numeric_limits::min()); for (const auto& element : timeline) { // No element shound have zero-length - EXPECT_LT(0cs, element.getTimeRange().getLength()); + EXPECT_LT(0_cs, element.getTimeRange().getLength()); // No two adjacent elements should have the same value; they should have been merged if (element.getStart() == lastElement.getEnd()) { @@ -264,9 +264,9 @@ TEST(Timeline, set) { } TEST(Timeline, indexer_get) { - Timeline timeline{ { 1cs, 2cs, 1 }, { 2cs, 4cs, 2 }, { 6cs, 9cs, 3 } }; + Timeline timeline{ { 1_cs, 2_cs, 1 }, { 2_cs, 4_cs, 2 }, { 6_cs, 9_cs, 3 } }; vector> expectedValues{ none, 1, 2, 2, none, none, 3, 3, 3 }; - for (centiseconds t = 0cs; t < 9cs; ++t) { + for (centiseconds t = 0_cs; t < 9_cs; ++t) { { optional actual = timeline[t]; EXPECT_EQ(expectedValues[t.count()], actual ? optional(*actual) : none); @@ -297,18 +297,18 @@ TEST(Timeline, indexer_set) { } TEST(Timeline, shift) { - Timeline timeline{ { 1cs, 2cs, 1 },{ 2cs, 5cs, 2 },{ 7cs, 9cs, 3 } }; - Timeline expected{ { 3cs, 4cs, 1 },{ 4cs, 7cs, 2 },{ 9cs, 11cs, 3 } }; - timeline.shift(2cs); + Timeline timeline{ { 1_cs, 2_cs, 1 },{ 2_cs, 5_cs, 2 },{ 7_cs, 9_cs, 3 } }; + Timeline expected{ { 3_cs, 4_cs, 1 },{ 4_cs, 7_cs, 2 },{ 9_cs, 11_cs, 3 } }; + timeline.shift(2_cs); EXPECT_EQ(expected, timeline); } TEST(Timeline, equality) { vector> timelines = { Timeline{}, - Timeline{ { 1cs, 2cs, 0 } }, - Timeline{ { 1cs, 2cs, 1 } }, - Timeline{ { -10cs, 0cs, 0 } } + Timeline{ { 1_cs, 2_cs, 0 } }, + Timeline{ { 1_cs, 2_cs, 1 } }, + Timeline{ { -10_cs, 0_cs, 0 } } }; for (size_t i = 0; i < timelines.size(); ++i) {