#include #include #include #include "time/Timeline.h" using namespace testing; using boost::none; using boost::optional; using std::initializer_list; using std::vector; TEST(Timeline, constructors_initializeState) { auto args = { Timed(-10_cs, 30_cs, 1), Timed(10_cs, 40_cs, 2), Timed(50_cs, 60_cs, 3) }; auto expected = { 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()), ElementsAreArray(expected)); EXPECT_THAT(Timeline(vector>(args)), ElementsAreArray(expected)); EXPECT_THAT(Timeline(args), ElementsAreArray(expected)); } TEST(Timeline, empty) { Timeline empty0; EXPECT_TRUE(empty0.empty()); EXPECT_THAT(empty0, IsEmpty()); Timeline empty1{}; EXPECT_TRUE(empty1.empty()); EXPECT_THAT(empty1, IsEmpty()); Timeline empty2{Timed(1_cs, 1_cs, 1)}; EXPECT_TRUE(empty2.empty()); EXPECT_THAT(empty2, IsEmpty()); Timeline nonEmpty{Timed(1_cs, 2_cs, 1)}; EXPECT_FALSE(nonEmpty.empty()); EXPECT_THAT(nonEmpty, Not(IsEmpty())); } TEST(Timeline, size) { Timeline empty0; EXPECT_EQ(0, empty0.size()); EXPECT_THAT(empty0, SizeIs(0)); Timeline empty1{}; EXPECT_EQ(0, empty1.size()); EXPECT_THAT(empty1, SizeIs(0)); Timeline empty2{Timed(1_cs, 1_cs, 1)}; EXPECT_EQ(0, empty2.size()); EXPECT_THAT(empty2, SizeIs(0)); Timeline size1{Timed(1_cs, 10_cs, 1)}; EXPECT_EQ(1, size1.size()); EXPECT_THAT(size1, SizeIs(1)); 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(0_cs, 0_cs), empty0.getRange()); Timeline empty1{}; EXPECT_EQ(TimeRange(0_cs, 0_cs), empty1.getRange()); Timeline empty2{Timed(1_cs, 1_cs, 1)}; EXPECT_EQ(TimeRange(0_cs, 0_cs), empty2.getRange()); Timeline nonEmpty1{Timed(1_cs, 10_cs, 1)}; EXPECT_EQ(TimeRange(1_cs, 10_cs), nonEmpty1.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(-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; copy(timeline.rbegin(), timeline.rend(), back_inserter(reversedActual)); vector> reversedExpected; reverse_copy(expected.begin(), expected.end(), back_inserter(reversedExpected)); EXPECT_THAT(reversedActual, ElementsAreArray(reversedExpected)); } void testFind( const Timeline& timeline, FindMode findMode, const initializer_list*> expectedResults ) { int i = -1; for (Timed* expectedResult : expectedResults) { auto it = timeline.find(centiseconds(++i), findMode); if (expectedResult != nullptr) { EXPECT_NE(it, timeline.end()) << "Timeline: " << timeline << "; findMode: " << static_cast(findMode) << "; i: " << i; if (it != timeline.end()) { EXPECT_EQ(*expectedResult, *it) << "Timeline: " << timeline << "; findMode: " << static_cast(findMode) << "; i: " << i; } } else { EXPECT_EQ(timeline.end(), it) << "Timeline: " << timeline << "; findMode: " << static_cast(findMode) << "; i: " << i; } } } TEST(Timeline, find) { Timed a = Timed(1_cs, 2_cs, 1); Timed b = Timed(2_cs, 5_cs, 2); Timed c = Timed(7_cs, 9_cs, 3); const Timeline timeline{a, b, c}; testFind( timeline, FindMode::SampleLeft, {nullptr, nullptr, &a, &b, &b, &b, nullptr, nullptr, &c, &c, nullptr} ); testFind( timeline, FindMode::SampleRight, {nullptr, &a, &b, &b, &b, nullptr, nullptr, &c, &c, nullptr, nullptr} ); testFind( timeline, FindMode::SearchLeft, {nullptr, nullptr, &a, &b, &b, &b, &b, &b, &c, &c, &c} ); testFind( timeline, FindMode::SearchRight, {&a, &a, &b, &b, &b, &c, &c, &c, &c, nullptr, nullptr} ); } TEST(Timeline, get) { 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 }; int i = -1; for (Timed* expectedResult : expectedResults) { optional&> value = timeline.get(centiseconds(++i)); if (expectedResult != nullptr) { EXPECT_TRUE(value) << "i: " << i; if (value) { EXPECT_EQ(*expectedResult, *value) << "i: " << i; } } else { EXPECT_FALSE(value) << "i: " << i; } } } TEST(Timeline, clear) { const Timeline original{{1_cs, 2_cs, 1}, {2_cs, 5_cs, 2}, {7_cs, 9_cs, 3}}; { auto timeline = original; timeline.clear(-10_cs, 10_cs); EXPECT_THAT(timeline, IsEmpty()); } { auto timeline = original; 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(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(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(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); } } void testSetter(const std::function&, Timeline&)>& set) { Timeline timeline; vector> expectedValues(20, none); auto newElements = { 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) { ++newElementIndex; // Set element in timeline set(newElement, timeline); // Update expected value for every index const 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 = 0_cs; t < 10_cs; ++t) { optional actual = timeline[t]; EXPECT_EQ(expectedValues[t.count()], actual ? optional(*actual) : none); } // Check timeline via iterators for (const auto& element : timeline) { // No element should have zero-length EXPECT_LT(0_cs, element.getDuration()); // Element should match expected values for (centiseconds t = std::max(centiseconds::zero(), element.getStart()); t < element.getEnd(); ++t) { optional expectedValue = expectedValues[t.count()]; EXPECT_TRUE(expectedValue) << "Index " << t.count() << " should not have a value, but is within element " << element << ". " << "newElementIndex: " << newElementIndex; if (expectedValue) { EXPECT_EQ(*expectedValue, element.getValue()); } } } } } TEST(Timeline, set) { testSetter([](const Timed& element, Timeline& timeline) { timeline.set(element); }); testSetter([](const Timed& element, Timeline& timeline) { timeline.set(element.getTimeRange(), element.getValue()); }); testSetter([](const Timed& element, Timeline& timeline) { timeline.set(element.getStart(), element.getEnd(), element.getValue()); }); } TEST(Timeline, indexer_get) { 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 = 0_cs; t < 9_cs; ++t) { { optional actual = timeline[t]; EXPECT_EQ(expectedValues[t.count()], actual ? optional(*actual) : none); } { optional actual = timeline[t]; EXPECT_EQ(expectedValues[t.count()], actual ? optional(*actual) : none); } if (expectedValues[t.count()]) { { const int& actual = timeline[t]; EXPECT_EQ(*expectedValues[t.count()], actual); } { int actual = timeline[t]; EXPECT_EQ(*expectedValues[t.count()], actual); } } } } TEST(Timeline, indexer_set) { testSetter([](const Timed& element, Timeline& timeline) { for (centiseconds t = element.getStart(); t < element.getEnd(); ++t) { timeline[t] = element.getValue(); } }); } TEST(Timeline, joinAdjacent) { Timeline timeline{ {1_cs, 2_cs, 1}, {2_cs, 4_cs, 2}, {3_cs, 6_cs, 2}, {6_cs, 7_cs, 2}, // Gap {8_cs, 10_cs, 2}, {11_cs, 12_cs, 3} }; EXPECT_EQ(6, timeline.size()); timeline.joinAdjacent(); EXPECT_EQ(4, timeline.size()); Timed expectedJoined[] = { {1_cs, 2_cs, 1}, {2_cs, 7_cs, 2}, // Gap {8_cs, 10_cs, 2}, {11_cs, 12_cs, 3} }; EXPECT_THAT(timeline, ElementsAreArray(expectedJoined)); } TEST(Timeline, autoJoin) { JoiningTimeline timeline{ {1_cs, 2_cs, 1}, {2_cs, 4_cs, 2}, {3_cs, 6_cs, 2}, {6_cs, 7_cs, 2}, // Gap {8_cs, 10_cs, 2}, {11_cs, 12_cs, 3} }; Timed expectedJoined[] = { {1_cs, 2_cs, 1}, {2_cs, 7_cs, 2}, // Gap {8_cs, 10_cs, 2}, {11_cs, 12_cs, 3} }; EXPECT_EQ(4, timeline.size()); EXPECT_THAT(timeline, ElementsAreArray(expectedJoined)); } TEST(Timeline, shift) { 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{{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) { for (size_t j = 0; j < timelines.size(); ++j) { if (i == j) { EXPECT_EQ(timelines[i], Timeline(timelines[j])) << "i: " << i << ", j: " << j; } else { EXPECT_NE(timelines[i], timelines[j]) << "i: " << i << ", j: " << j; } } } }