Tests on using deque by SortUnique and EqualsBy

This commit is contained in:
Vladimir Byko-Ianko 2016-09-09 13:56:57 +03:00
parent b8197602fa
commit ceeee1a8ec

View file

@ -3,6 +3,7 @@
#include "base/stl_helpers.hpp"
#include "std/algorithm.hpp"
#include "std/deque.hpp"
#include "std/utility.hpp"
#include "std/vector.hpp"
@ -19,6 +20,70 @@ private:
int m_v;
};
template<template<class, class> class TContainer>
void TestSortUnique()
{
{
TContainer<int, allocator<int>> actual = {1, 2, 1, 4, 3, 5, 2, 7, 1};
my::SortUnique(actual);
TContainer<int, allocator<int>> const expected = {1, 2, 3, 4, 5, 7};
TEST_EQUAL(actual, expected, ());
}
{
using TValue = int;
using TPair = pair<TValue, int>;
TContainer<TPair, allocator<TPair>> d =
{{1, 22}, {2, 33}, {1, 23}, {4, 54}, {3, 34}, {5, 23}, {2, 23}, {7, 32}, {1, 12}};
my::SortUnique<TPair>(d, my::LessBy(&TPair::first), my::EqualsBy(&TPair::first));
TContainer<TValue, allocator<TValue>> const expected = {1, 2, 3, 4, 5, 7};
TEST_EQUAL(d.size(), expected.size(), ());
for (int i = 0; i < d.size(); ++i)
TEST_EQUAL(d[i].first, expected[i], (i));
}
{
using TValue = double;
using TPair = pair<TValue, int>;
TContainer<TPair, allocator<TPair>> d =
{{0.5, 11}, {1000.99, 234}, {0.5, 23}, {1234.56789, 54}, {1000.99, 34}};
my::SortUnique<TPair>(d, my::LessBy(&TPair::first), my::EqualsBy(&TPair::first));
TContainer<TValue, allocator<TValue>> const expected = {0.5, 1000.99, 1234.56789};
TEST_EQUAL(d.size(), expected.size(), ());
for (int i = 0; i < d.size(); ++i)
TEST_EQUAL(d[i].first, expected[i], (i));
}
}
template<template<class, class> class TContainer>
void TestEqualsBy()
{
{
using TValue = pair<int, int>;
TContainer<TValue, allocator<TValue>> actual = {{1, 2}, {1, 3}, {2, 100}, {3, 7}, {3, 8}, {2, 500}};
actual.erase(unique(actual.begin(), actual.end(), my::EqualsBy(&TValue::first)), actual.end());
TContainer<int, allocator<int>> const expected = {{1, 2, 3, 2}};
TEST_EQUAL(expected.size(), actual.size(), ());
for (size_t i = 0; i < actual.size(); ++i)
TEST_EQUAL(expected[i], actual[i].first, ());
}
{
TContainer<Int, allocator<Int>> actual;
for (auto const v : {0, 0, 1, 2, 2, 0})
actual.emplace_back(v);
actual.erase(unique(actual.begin(), actual.end(), my::EqualsBy(&Int::Get)), actual.end());
TContainer<int, allocator<int>> const expected = {{0, 1, 2, 0}};
TEST_EQUAL(expected.size(), actual.size(), ());
for (size_t i = 0; i < actual.size(); ++i)
TEST_EQUAL(expected[i], actual[i].Get(), ());
}
}
UNIT_TEST(LessBy)
{
{
@ -49,65 +114,23 @@ UNIT_TEST(LessBy)
}
}
UNIT_TEST(EqualsBy)
UNIT_TEST(EqualsBy_VectorTest)
{
{
using TValue = pair<int, int>;
vector<TValue> actual = {{1, 2}, {1, 3}, {2, 100}, {3, 7}, {3, 8}, {2, 500}};
actual.erase(unique(actual.begin(), actual.end(), my::EqualsBy(&TValue::first)), actual.end());
vector<int> const expected = {{1, 2, 3, 2}};
TEST_EQUAL(expected.size(), actual.size(), ());
for (size_t i = 0; i < actual.size(); ++i)
TEST_EQUAL(expected[i], actual[i].first, ());
}
{
vector<Int> actual;
for (auto const v : {0, 0, 1, 2, 2, 0})
actual.emplace_back(v);
actual.erase(unique(actual.begin(), actual.end(), my::EqualsBy(&Int::Get)), actual.end());
vector<int> const expected = {{0, 1, 2, 0}};
TEST_EQUAL(expected.size(), actual.size(), ());
for (size_t i = 0; i < actual.size(); ++i)
TEST_EQUAL(expected[i], actual[i].Get(), ());
}
TestEqualsBy<vector>();
}
UNIT_TEST(SortUnique)
UNIT_TEST(EqualsBy_DequeTest)
{
{
vector<int> actual = {1, 2, 1, 4, 3, 5, 2, 7, 1};
my::SortUnique(actual);
vector<int> const expected = {1, 2, 3, 4, 5, 7};
TEST_EQUAL(actual, expected, ());
}
{
using TValue = int;
using TPair = pair<TValue, int>;
vector<TPair> v =
{{1, 22}, {2, 33}, {1, 23}, {4, 54}, {3, 34}, {5, 23}, {2, 23}, {7, 32}, {1, 12}};
TestEqualsBy<deque>();
}
my::SortUnique<TPair>(v, my::LessBy(&TPair::first), my::EqualsBy(&TPair::first));
UNIT_TEST(SortUnique_VectorTest)
{
TestSortUnique<vector>();
}
vector<TValue> const expected = {1, 2, 3, 4, 5, 7};
TEST_EQUAL(v.size(), expected.size(), ());
for (int i = 0; i < v.size(); ++i)
TEST_EQUAL(v[i].first, expected[i], (i));
}
{
using TValue = double;
using TPair = pair<TValue, int>;
vector<TPair> v =
{{0.5, 11}, {1000.99, 234}, {0.5, 23}, {1234.56789, 54}, {1000.99, 34}};
my::SortUnique<TPair>(v, my::LessBy(&TPair::first), my::EqualsBy(&TPair::first));
vector<TValue> const expected = {0.5, 1000.99, 1234.56789};
TEST_EQUAL(v.size(), expected.size(), ());
for (int i = 0; i < v.size(); ++i)
TEST_EQUAL(v[i].first, expected[i], (i));
}
UNIT_TEST(SortUnique_DequeTest)
{
TestSortUnique<deque>();
}
} // namespace