Chromium Code Reviews| Index: base/algorithm/sorted_ranges_unittest.cc |
| diff --git a/base/algorithm/sorted_ranges_unittest.cc b/base/algorithm/sorted_ranges_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5e759d3373c3e76c2e7a213f0c2eeb6b66fde9e8 |
| --- /dev/null |
| +++ b/base/algorithm/sorted_ranges_unittest.cc |
| @@ -0,0 +1,111 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/algorithm/sorted_ranges.h" |
| + |
| +#include <algorithm> |
| +#include <random> |
| + |
| +#include "base/containers/container_test_utils.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace base { |
| + |
| +namespace { |
| + |
| +template <typename T> |
| +void UniquePickLastTest(const std::vector<T>& in, |
| + const std::vector<T>& expected) { |
|
dyaroshev
2017/04/08 17:03:12
Unused
|
| + std::vector<T> copy; |
| + UniqueCopyPickLast(in.begin(), in.end(), std::back_inserter(copy)); |
| + EXPECT_EQ(copy, expected); |
| + auto in_place = in; |
| + in_place.erase(UniquePickLast(in_place.begin(), in_place.end()), |
| + in_place.end()); |
| + EXPECT_EQ(in_place, expected); |
| +} |
| + |
| +} // namespace |
| + |
| +TEST(Algorithm, UniqueCopyPickLastTest) { |
| + // Behavior should be identical to std::unique when used when algorithm is |
| + // called with strict Equality. |
| + { |
| + auto CheckUniqueInts = [](const std::vector<int>& in) { |
| + std::vector<int> expected; |
| + std::unique_copy(in.begin(), in.end(), std::back_inserter(expected)); |
| + |
| + std::vector<int> copy; |
| + UniqueCopyPickLast(in.begin(), in.end(), std::back_inserter(copy)); |
| + EXPECT_EQ(expected, copy); |
| + |
| + auto in_place = in; |
| + in_place.erase(UniquePickLast(in_place.begin(), in_place.end()), |
| + in_place.end()); |
| + EXPECT_EQ(expected, in_place); |
| + }; |
| + |
| + CheckUniqueInts({}); |
| + CheckUniqueInts({1}); |
| + CheckUniqueInts({1, 2}); |
| + CheckUniqueInts({1, 2, 3}); |
| + CheckUniqueInts({1, 1}); |
| + CheckUniqueInts({1, 2, 2, 1}); |
| + CheckUniqueInts({1, 1, 1}); |
| + CheckUniqueInts({1, 3, 1}); |
| + CheckUniqueInts({1, 1, 2}); |
| + CheckUniqueInts({1, 2, 2}); |
| + CheckUniqueInts({1, 2, 2, 3, 3, 3}); |
| + CheckUniqueInts({1, 1, 1, 2, 3, 4, 5, 5, 5}); |
| + |
| + // More random input. |
| + std::mt19937 gen; |
| + std::uniform_int_distribution<> dis(1, 10); |
| + for (int i = 10; i < 100; ++i) { |
| + std::vector<int> random_input(i); |
| + std::generate(random_input.begin(), random_input.end(), |
| + [&] { return dis(gen); }); |
| + CheckUniqueInts(random_input); |
| + } |
| + } |
| + |
| + // Check that algorithm works with move only types (compilation is enough). |
| + { |
| + int input_range[] = {1, 2, 2}; |
| + std::vector<MoveOnlyInt> move_only(std::begin(input_range), |
| + std::end(input_range)); |
| + move_only.erase(UniquePickLast(move_only.begin(), move_only.end()), |
| + move_only.end()); |
| + |
| + EXPECT_EQ(2U, move_only.size()); |
| + EXPECT_EQ(MoveOnlyInt(1), move_only[0]); |
| + EXPECT_EQ(MoveOnlyInt(2), move_only[1]); |
| + } |
| + |
| + // Keep last. |
| + { |
| + using IntPair = std::pair<int, int>; |
| + auto CheckTakeLastDuplicate = [](std::vector<IntPair> in, |
| + const std::vector<int>& expected_second) { |
| + in.erase(UniquePickLast(in.begin(), in.end(), |
| + [](const IntPair& lhs, const IntPair& rhs) { |
| + return lhs.first == rhs.first; |
| + }), |
| + in.end()); |
| + |
| + ASSERT_EQ(expected_second.size(), in.size()); |
| + |
| + for (size_t i = 0; i < in.size(); ++i) |
| + EXPECT_EQ(expected_second[i], in[i].second) << "i= " << i; |
| + }; |
| + |
| + CheckTakeLastDuplicate({{1, 0}, {1, 1}, {2, 0}}, {1, 0}); |
| + CheckTakeLastDuplicate({{1, 0}, {1, 1}, {1, 2}}, {2}); |
| + CheckTakeLastDuplicate({{1, 0}, {2, 0}, {2, 1}}, {0, 1}); |
| + CheckTakeLastDuplicate({{1, 0}, {1, 1}, {2, 0}, {2, 1}}, {1, 1}); |
| + CheckTakeLastDuplicate({{1, 0}, {2, 0}, {1, 0}, {1, 1}}, {0, 0, 1}); |
| + } |
| +} |
| + |
| +} // namespace base |