2016-04-04 18:10:28 +00:00
|
|
|
#include <gmock/gmock.h>
|
|
|
|
#include "Timeline.h"
|
|
|
|
#include <limits>
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
using namespace testing;
|
|
|
|
using std::vector;
|
2016-05-02 18:31:59 +00:00
|
|
|
using boost::optional;
|
|
|
|
using std::initializer_list;
|
|
|
|
using boost::none;
|
2016-04-04 18:10:28 +00:00
|
|
|
|
|
|
|
TEST(Timeline, constructors_initializeState) {
|
|
|
|
auto args = {
|
2016-08-09 20:31:16 +00:00
|
|
|
Timed<int>(-10_cs, 30_cs, 1),
|
|
|
|
Timed<int>(10_cs, 40_cs, 2),
|
|
|
|
Timed<int>(50_cs, 60_cs, 3)
|
2016-04-04 18:10:28 +00:00
|
|
|
};
|
|
|
|
auto expected = {
|
2016-08-09 20:31:16 +00:00
|
|
|
Timed<int>(-10_cs, 10_cs, 1),
|
|
|
|
Timed<int>(10_cs, 40_cs, 2),
|
|
|
|
Timed<int>(50_cs, 60_cs, 3)
|
2016-04-04 18:10:28 +00:00
|
|
|
};
|
|
|
|
EXPECT_THAT(
|
2016-05-02 18:31:59 +00:00
|
|
|
Timeline<int>(args.begin(), args.end()),
|
2016-04-04 18:10:28 +00:00
|
|
|
ElementsAreArray(expected)
|
|
|
|
);
|
|
|
|
EXPECT_THAT(
|
2016-05-02 18:31:59 +00:00
|
|
|
Timeline<int>(args),
|
2016-04-04 18:10:28 +00:00
|
|
|
ElementsAreArray(expected)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Timeline, empty) {
|
2016-05-02 18:31:59 +00:00
|
|
|
Timeline<int> empty0;
|
|
|
|
EXPECT_TRUE(empty0.empty());
|
|
|
|
EXPECT_THAT(empty0, IsEmpty());
|
|
|
|
|
2016-04-04 18:10:28 +00:00
|
|
|
Timeline<int> empty1{};
|
|
|
|
EXPECT_TRUE(empty1.empty());
|
|
|
|
EXPECT_THAT(empty1, IsEmpty());
|
|
|
|
|
2016-08-09 20:31:16 +00:00
|
|
|
Timeline<int> empty2{ Timed<int>(1_cs, 1_cs, 1) };
|
2016-04-04 18:10:28 +00:00
|
|
|
EXPECT_TRUE(empty2.empty());
|
|
|
|
EXPECT_THAT(empty2, IsEmpty());
|
|
|
|
|
2016-08-09 20:31:16 +00:00
|
|
|
Timeline<int> nonEmpty{ Timed<int>(1_cs, 2_cs, 1) };
|
2016-04-04 18:10:28 +00:00
|
|
|
EXPECT_FALSE(nonEmpty.empty());
|
|
|
|
EXPECT_THAT(nonEmpty, Not(IsEmpty()));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Timeline, size) {
|
2016-05-02 18:31:59 +00:00
|
|
|
Timeline<int> empty0;
|
|
|
|
EXPECT_EQ(0, empty0.size());
|
|
|
|
EXPECT_THAT(empty0, SizeIs(0));
|
|
|
|
|
2016-04-04 18:10:28 +00:00
|
|
|
Timeline<int> empty1{};
|
|
|
|
EXPECT_EQ(0, empty1.size());
|
|
|
|
EXPECT_THAT(empty1, SizeIs(0));
|
|
|
|
|
2016-08-09 20:31:16 +00:00
|
|
|
Timeline<int> empty2{ Timed<int>(1_cs, 1_cs, 1) };
|
2016-04-04 18:10:28 +00:00
|
|
|
EXPECT_EQ(0, empty2.size());
|
|
|
|
EXPECT_THAT(empty2, SizeIs(0));
|
|
|
|
|
2016-08-09 20:31:16 +00:00
|
|
|
Timeline<int> size1{ Timed<int>(1_cs, 10_cs, 1) };
|
2016-04-04 18:10:28 +00:00
|
|
|
EXPECT_EQ(1, size1.size());
|
|
|
|
EXPECT_THAT(size1, SizeIs(1));
|
|
|
|
|
2016-08-09 20:31:16 +00:00
|
|
|
Timeline<int> size2{ Timed<int>(-10_cs, 10_cs, 1), Timed<int>(10_cs, 11_cs, 5) };
|
2016-04-04 18:10:28 +00:00
|
|
|
EXPECT_EQ(2, size2.size());
|
|
|
|
EXPECT_THAT(size2, SizeIs(2));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Timeline, getRange) {
|
2016-05-02 18:31:59 +00:00
|
|
|
Timeline<int> empty0;
|
2016-08-09 20:31:16 +00:00
|
|
|
EXPECT_EQ(TimeRange(0_cs, 0_cs), empty0.getRange());
|
2016-05-02 18:31:59 +00:00
|
|
|
|
2016-04-04 18:10:28 +00:00
|
|
|
Timeline<int> empty1{};
|
2016-08-09 20:31:16 +00:00
|
|
|
EXPECT_EQ(TimeRange(0_cs, 0_cs), empty1.getRange());
|
2016-04-04 18:10:28 +00:00
|
|
|
|
2016-08-09 20:31:16 +00:00
|
|
|
Timeline<int> empty2{ Timed<int>(1_cs, 1_cs, 1) };
|
|
|
|
EXPECT_EQ(TimeRange(0_cs, 0_cs), empty2.getRange());
|
2016-04-04 18:10:28 +00:00
|
|
|
|
2016-08-09 20:31:16 +00:00
|
|
|
Timeline<int> nonEmpty1{ Timed<int>(1_cs, 10_cs, 1) };
|
|
|
|
EXPECT_EQ(TimeRange(1_cs, 10_cs), nonEmpty1.getRange());
|
2016-04-04 18:10:28 +00:00
|
|
|
|
2016-08-09 20:31:16 +00:00
|
|
|
Timeline<int> nonEmpty2{ Timed<int>(-10_cs, 5_cs, 1), Timed<int>(10_cs, 11_cs, 5) };
|
|
|
|
EXPECT_EQ(TimeRange(-10_cs, 11_cs), nonEmpty2.getRange());
|
2016-04-04 18:10:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Timeline, iterators) {
|
2016-08-09 20:31:16 +00:00
|
|
|
Timeline<int> timeline{ Timed<int>(-5_cs, 0_cs, 10), Timed<int>(5_cs, 15_cs, 9) };
|
|
|
|
auto expected = { Timed<int>(-5_cs, 0_cs, 10), Timed<int>(5_cs, 15_cs, 9) };
|
2016-04-04 18:10:28 +00:00
|
|
|
EXPECT_THAT(timeline, ElementsAreArray(expected));
|
|
|
|
|
|
|
|
vector<Timed<int>> reversedActual;
|
2016-05-02 18:31:59 +00:00
|
|
|
copy(timeline.rbegin(), timeline.rend(), back_inserter(reversedActual));
|
2016-04-04 18:10:28 +00:00
|
|
|
vector<Timed<int>> reversedExpected;
|
2016-05-02 18:31:59 +00:00
|
|
|
reverse_copy(expected.begin(), expected.end(), back_inserter(reversedExpected));
|
2016-04-04 18:10:28 +00:00
|
|
|
EXPECT_THAT(reversedActual, ElementsAreArray(reversedExpected));
|
|
|
|
}
|
|
|
|
|
2016-05-02 18:31:59 +00:00
|
|
|
void testFind(const Timeline<int>& timeline, FindMode findMode, const initializer_list<Timed<int>*> expectedResults) {
|
|
|
|
int i = -1;
|
|
|
|
for (Timed<int>* expectedResult : expectedResults) {
|
2016-07-05 19:17:51 +00:00
|
|
|
auto it = timeline.find(centiseconds(++i), findMode);
|
2016-05-02 18:31:59 +00:00
|
|
|
if (expectedResult != nullptr) {
|
|
|
|
EXPECT_NE(it, timeline.end()) << "Timeline: " << timeline << "; findMode: " << static_cast<int>(findMode) << "; i: " << i;
|
|
|
|
if (it != timeline.end()) {
|
|
|
|
EXPECT_EQ(*expectedResult, *it) << "Timeline: " << timeline << "; findMode: " << static_cast<int>(findMode) << "; i: " << i;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
EXPECT_EQ(timeline.end(), it) << "Timeline: " << timeline << "; findMode: " << static_cast<int>(findMode) << "; i: " << i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-04 18:10:28 +00:00
|
|
|
TEST(Timeline, find) {
|
2016-08-09 20:31:16 +00:00
|
|
|
Timed<int> a = Timed<int>(1_cs, 2_cs, 1);
|
|
|
|
Timed<int> b = Timed<int>(2_cs, 5_cs, 2);
|
|
|
|
Timed<int> c = Timed<int>(7_cs, 9_cs, 3);
|
2016-05-02 18:31:59 +00:00
|
|
|
Timeline<int> 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 });
|
2016-04-04 18:10:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Timeline, get) {
|
2016-08-09 20:31:16 +00:00
|
|
|
Timed<int> a = Timed<int>(1_cs, 2_cs, 1);
|
|
|
|
Timed<int> b = Timed<int>(2_cs, 5_cs, 2);
|
|
|
|
Timed<int> c = Timed<int>(7_cs, 9_cs, 3);
|
2016-05-02 18:31:59 +00:00
|
|
|
Timeline<int> timeline{ a, b, c };
|
|
|
|
|
|
|
|
initializer_list<Timed<int>*> expectedResults = { nullptr, &a, &b, &b, &b, nullptr, nullptr, &c, &c, nullptr, nullptr };
|
|
|
|
int i = -1;
|
|
|
|
for (Timed<int>* expectedResult : expectedResults) {
|
2016-07-05 19:17:51 +00:00
|
|
|
optional<const Timed<int>&> value = timeline.get(centiseconds(++i));
|
2016-05-02 18:31:59 +00:00
|
|
|
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) {
|
2016-08-09 20:31:16 +00:00
|
|
|
Timeline<int> original{ { 1_cs, 2_cs, 1 }, { 2_cs, 5_cs, 2 }, { 7_cs, 9_cs, 3 } };
|
2016-05-02 18:31:59 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
auto timeline = original;
|
2016-08-09 20:31:16 +00:00
|
|
|
timeline.clear(-10_cs, 10_cs);
|
2016-05-02 18:31:59 +00:00
|
|
|
EXPECT_THAT(timeline, IsEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto timeline = original;
|
2016-08-09 20:31:16 +00:00
|
|
|
timeline.clear(1_cs, 2_cs);
|
|
|
|
Timeline<int> expected{ { 2_cs, 5_cs, 2 }, { 7_cs, 9_cs, 3 } };
|
2016-05-02 18:31:59 +00:00
|
|
|
EXPECT_EQ(expected, timeline);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto timeline = original;
|
2016-08-09 20:31:16 +00:00
|
|
|
timeline.clear(3_cs, 4_cs);
|
|
|
|
Timeline<int> expected{ { 1_cs, 2_cs, 1 }, { 2_cs, 3_cs, 2 }, { 4_cs, 5_cs, 2}, { 7_cs, 9_cs, 3} };
|
2016-05-02 18:31:59 +00:00
|
|
|
EXPECT_EQ(expected, timeline);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto timeline = original;
|
2016-08-09 20:31:16 +00:00
|
|
|
timeline.clear(6_cs, 8_cs);
|
|
|
|
Timeline<int> expected{ { 1_cs, 2_cs, 1 }, { 2_cs, 5_cs, 2 }, { 8_cs, 9_cs, 3 } };
|
2016-05-02 18:31:59 +00:00
|
|
|
EXPECT_EQ(expected, timeline);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto timeline = original;
|
2016-08-09 20:31:16 +00:00
|
|
|
timeline.clear(8_cs, 10_cs);
|
|
|
|
Timeline<int> expected{ { 1_cs, 2_cs, 1 }, { 2_cs, 5_cs, 2 }, { 7_cs, 8_cs, 3 } };
|
2016-05-02 18:31:59 +00:00
|
|
|
EXPECT_EQ(expected, timeline);
|
|
|
|
}
|
2016-04-04 18:10:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void testSetter(std::function<void(const Timed<int>&, Timeline<int>&)> set) {
|
2016-05-02 18:31:59 +00:00
|
|
|
Timeline<int> timeline;
|
|
|
|
vector<optional<int>> expectedValues(20, none);
|
2016-04-04 18:10:28 +00:00
|
|
|
auto newElements = {
|
2016-08-09 20:31:16 +00:00
|
|
|
Timed<int>(1_cs, 2_cs, 4),
|
|
|
|
Timed<int>(3_cs, 6_cs, 4),
|
|
|
|
Timed<int>(7_cs, 9_cs, 5),
|
|
|
|
Timed<int>(9_cs, 10_cs, 6),
|
|
|
|
Timed<int>(2_cs, 3_cs, 4),
|
|
|
|
Timed<int>(0_cs, 1_cs, 7),
|
|
|
|
Timed<int>(-10_cs, 1_cs, 8),
|
|
|
|
Timed<int>(-10_cs, 0_cs, 9),
|
|
|
|
Timed<int>(-10_cs, -1_cs, 10),
|
|
|
|
Timed<int>(9_cs, 20_cs, 11),
|
|
|
|
Timed<int>(10_cs, 20_cs, 12),
|
|
|
|
Timed<int>(11_cs, 20_cs, 13),
|
|
|
|
Timed<int>(4_cs, 6_cs, 14),
|
|
|
|
Timed<int>(4_cs, 6_cs, 15),
|
|
|
|
Timed<int>(8_cs, 10_cs, 15),
|
|
|
|
Timed<int>(6_cs, 8_cs, 15),
|
|
|
|
Timed<int>(6_cs, 8_cs, 16)
|
2016-04-04 18:10:28 +00:00
|
|
|
};
|
2016-05-02 18:31:59 +00:00
|
|
|
int newElementIndex = -1;
|
2016-04-04 18:10:28 +00:00
|
|
|
for (const auto& newElement : newElements) {
|
2016-05-02 18:31:59 +00:00
|
|
|
++newElementIndex;
|
2016-04-04 18:10:28 +00:00
|
|
|
// Set element in timeline
|
|
|
|
set(newElement, timeline);
|
|
|
|
|
|
|
|
// Update expected value for every index
|
2016-08-09 20:31:16 +00:00
|
|
|
centiseconds elementStart = max(newElement.getStart(), 0_cs);
|
2016-07-05 19:17:51 +00:00
|
|
|
centiseconds elementEnd = newElement.getEnd();
|
|
|
|
for (centiseconds t = elementStart; t < elementEnd; ++t) {
|
2016-04-04 18:10:28 +00:00
|
|
|
expectedValues[t.count()] = newElement.getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check timeline via indexer
|
2016-08-09 20:31:16 +00:00
|
|
|
for (centiseconds t = 0_cs; t < 10_cs; ++t) {
|
2016-09-23 19:15:34 +00:00
|
|
|
optional<int> actual = timeline[t];
|
2016-05-02 18:31:59 +00:00
|
|
|
EXPECT_EQ(expectedValues[t.count()], actual ? optional<int>(*actual) : none);
|
2016-04-04 18:10:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check timeline via iterators
|
2016-07-05 19:17:51 +00:00
|
|
|
Timed<int> lastElement(centiseconds::min(), centiseconds::min(), std::numeric_limits<int>::min());
|
2016-04-04 18:10:28 +00:00
|
|
|
for (const auto& element : timeline) {
|
|
|
|
// No element shound have zero-length
|
2016-08-09 20:31:16 +00:00
|
|
|
EXPECT_LT(0_cs, element.getTimeRange().getLength());
|
2016-04-04 18:10:28 +00:00
|
|
|
|
|
|
|
// No two adjacent elements should have the same value; they should have been merged
|
2016-05-02 18:31:59 +00:00
|
|
|
if (element.getStart() == lastElement.getEnd()) {
|
|
|
|
EXPECT_NE(lastElement.getValue(), element.getValue());
|
|
|
|
}
|
|
|
|
lastElement = element;
|
2016-04-04 18:10:28 +00:00
|
|
|
|
|
|
|
// Element should match expected values
|
2016-07-05 19:17:51 +00:00
|
|
|
for (centiseconds t = std::max(centiseconds::zero(), element.getStart()); t < element.getEnd(); ++t) {
|
2016-05-02 18:31:59 +00:00
|
|
|
optional<int> 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());
|
|
|
|
}
|
2016-04-04 18:10:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Timeline, set) {
|
|
|
|
testSetter([](const Timed<int>& element, Timeline<int>& timeline) {
|
|
|
|
timeline.set(element);
|
|
|
|
});
|
|
|
|
testSetter([](const Timed<int>& element, Timeline<int>& timeline) {
|
2016-05-02 18:31:59 +00:00
|
|
|
timeline.set(element.getTimeRange(), element.getValue());
|
2016-04-04 18:10:28 +00:00
|
|
|
});
|
|
|
|
testSetter([](const Timed<int>& element, Timeline<int>& timeline) {
|
|
|
|
timeline.set(element.getStart(), element.getEnd(), element.getValue());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-02 18:31:59 +00:00
|
|
|
TEST(Timeline, indexer_get) {
|
2016-08-09 20:31:16 +00:00
|
|
|
Timeline<int> timeline{ { 1_cs, 2_cs, 1 }, { 2_cs, 4_cs, 2 }, { 6_cs, 9_cs, 3 } };
|
2016-05-02 18:31:59 +00:00
|
|
|
vector<optional<int>> expectedValues{ none, 1, 2, 2, none, none, 3, 3, 3 };
|
2016-08-09 20:31:16 +00:00
|
|
|
for (centiseconds t = 0_cs; t < 9_cs; ++t) {
|
2016-05-02 18:31:59 +00:00
|
|
|
{
|
2016-09-23 19:15:34 +00:00
|
|
|
optional<int> actual = timeline[t];
|
2016-05-02 18:31:59 +00:00
|
|
|
EXPECT_EQ(expectedValues[t.count()], actual ? optional<int>(*actual) : none);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
optional<int> actual = timeline[t];
|
|
|
|
EXPECT_EQ(expectedValues[t.count()], actual ? optional<int>(*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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-04 18:10:28 +00:00
|
|
|
TEST(Timeline, indexer_set) {
|
|
|
|
testSetter([](const Timed<int>& element, Timeline<int>& timeline) {
|
2016-07-05 19:17:51 +00:00
|
|
|
for (centiseconds t = element.getStart(); t < element.getEnd(); ++t) {
|
2016-05-02 18:31:59 +00:00
|
|
|
timeline[t] = element.getValue();
|
2016-04-04 18:10:28 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-02 18:31:59 +00:00
|
|
|
TEST(Timeline, shift) {
|
2016-08-09 20:31:16 +00:00
|
|
|
Timeline<int> timeline{ { 1_cs, 2_cs, 1 },{ 2_cs, 5_cs, 2 },{ 7_cs, 9_cs, 3 } };
|
|
|
|
Timeline<int> expected{ { 3_cs, 4_cs, 1 },{ 4_cs, 7_cs, 2 },{ 9_cs, 11_cs, 3 } };
|
|
|
|
timeline.shift(2_cs);
|
2016-05-02 18:31:59 +00:00
|
|
|
EXPECT_EQ(expected, timeline);
|
|
|
|
}
|
|
|
|
|
2016-04-04 18:10:28 +00:00
|
|
|
TEST(Timeline, equality) {
|
|
|
|
vector<Timeline<int>> timelines = {
|
|
|
|
Timeline<int>{},
|
2016-08-09 20:31:16 +00:00
|
|
|
Timeline<int>{ { 1_cs, 2_cs, 0 } },
|
|
|
|
Timeline<int>{ { 1_cs, 2_cs, 1 } },
|
|
|
|
Timeline<int>{ { -10_cs, 0_cs, 0 } }
|
2016-04-04 18:10:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for (size_t i = 0; i < timelines.size(); ++i) {
|
|
|
|
for (size_t j = 0; j < timelines.size(); ++j) {
|
|
|
|
if (i == j) {
|
2016-05-02 18:31:59 +00:00
|
|
|
EXPECT_EQ(timelines[i], Timeline<int>(timelines[j])) << "i: " << i << ", j: " << j;
|
2016-04-04 18:10:28 +00:00
|
|
|
} else {
|
2016-05-02 18:31:59 +00:00
|
|
|
EXPECT_NE(timelines[i], timelines[j]) << "i: " << i << ", j: " << j;
|
2016-04-04 18:10:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-02 18:31:59 +00:00
|
|
|
}
|