Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/algorithm/sorted_ranges.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <random> | |
| 9 | |
| 10 #include "base/containers/container_test_utils.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 | |
| 13 namespace base { | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 template <typename T> | |
| 18 void UniquePickLastTest(const std::vector<T>& in, | |
| 19 const std::vector<T>& expected) { | |
|
dyaroshev
2017/04/08 17:03:12
Unused
| |
| 20 std::vector<T> copy; | |
| 21 UniqueCopyPickLast(in.begin(), in.end(), std::back_inserter(copy)); | |
| 22 EXPECT_EQ(copy, expected); | |
| 23 auto in_place = in; | |
| 24 in_place.erase(UniquePickLast(in_place.begin(), in_place.end()), | |
| 25 in_place.end()); | |
| 26 EXPECT_EQ(in_place, expected); | |
| 27 } | |
| 28 | |
| 29 } // namespace | |
| 30 | |
| 31 TEST(Algorithm, UniqueCopyPickLastTest) { | |
| 32 // Behavior should be identical to std::unique when used when algorithm is | |
| 33 // called with strict Equality. | |
| 34 { | |
| 35 auto CheckUniqueInts = [](const std::vector<int>& in) { | |
| 36 std::vector<int> expected; | |
| 37 std::unique_copy(in.begin(), in.end(), std::back_inserter(expected)); | |
| 38 | |
| 39 std::vector<int> copy; | |
| 40 UniqueCopyPickLast(in.begin(), in.end(), std::back_inserter(copy)); | |
| 41 EXPECT_EQ(expected, copy); | |
| 42 | |
| 43 auto in_place = in; | |
| 44 in_place.erase(UniquePickLast(in_place.begin(), in_place.end()), | |
| 45 in_place.end()); | |
| 46 EXPECT_EQ(expected, in_place); | |
| 47 }; | |
| 48 | |
| 49 CheckUniqueInts({}); | |
| 50 CheckUniqueInts({1}); | |
| 51 CheckUniqueInts({1, 2}); | |
| 52 CheckUniqueInts({1, 2, 3}); | |
| 53 CheckUniqueInts({1, 1}); | |
| 54 CheckUniqueInts({1, 2, 2, 1}); | |
| 55 CheckUniqueInts({1, 1, 1}); | |
| 56 CheckUniqueInts({1, 3, 1}); | |
| 57 CheckUniqueInts({1, 1, 2}); | |
| 58 CheckUniqueInts({1, 2, 2}); | |
| 59 CheckUniqueInts({1, 2, 2, 3, 3, 3}); | |
| 60 CheckUniqueInts({1, 1, 1, 2, 3, 4, 5, 5, 5}); | |
| 61 | |
| 62 // More random input. | |
| 63 std::mt19937 gen; | |
| 64 std::uniform_int_distribution<> dis(1, 10); | |
| 65 for (int i = 10; i < 100; ++i) { | |
| 66 std::vector<int> random_input(i); | |
| 67 std::generate(random_input.begin(), random_input.end(), | |
| 68 [&] { return dis(gen); }); | |
| 69 CheckUniqueInts(random_input); | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 // Check that algorithm works with move only types (compilation is enough). | |
| 74 { | |
| 75 int input_range[] = {1, 2, 2}; | |
| 76 std::vector<MoveOnlyInt> move_only(std::begin(input_range), | |
| 77 std::end(input_range)); | |
| 78 move_only.erase(UniquePickLast(move_only.begin(), move_only.end()), | |
| 79 move_only.end()); | |
| 80 | |
| 81 EXPECT_EQ(2U, move_only.size()); | |
| 82 EXPECT_EQ(MoveOnlyInt(1), move_only[0]); | |
| 83 EXPECT_EQ(MoveOnlyInt(2), move_only[1]); | |
| 84 } | |
| 85 | |
| 86 // Keep last. | |
| 87 { | |
| 88 using IntPair = std::pair<int, int>; | |
| 89 auto CheckTakeLastDuplicate = [](std::vector<IntPair> in, | |
| 90 const std::vector<int>& expected_second) { | |
| 91 in.erase(UniquePickLast(in.begin(), in.end(), | |
| 92 [](const IntPair& lhs, const IntPair& rhs) { | |
| 93 return lhs.first == rhs.first; | |
| 94 }), | |
| 95 in.end()); | |
| 96 | |
| 97 ASSERT_EQ(expected_second.size(), in.size()); | |
| 98 | |
| 99 for (size_t i = 0; i < in.size(); ++i) | |
| 100 EXPECT_EQ(expected_second[i], in[i].second) << "i= " << i; | |
| 101 }; | |
| 102 | |
| 103 CheckTakeLastDuplicate({{1, 0}, {1, 1}, {2, 0}}, {1, 0}); | |
| 104 CheckTakeLastDuplicate({{1, 0}, {1, 1}, {1, 2}}, {2}); | |
| 105 CheckTakeLastDuplicate({{1, 0}, {2, 0}, {2, 1}}, {0, 1}); | |
| 106 CheckTakeLastDuplicate({{1, 0}, {1, 1}, {2, 0}, {2, 1}}, {1, 1}); | |
| 107 CheckTakeLastDuplicate({{1, 0}, {2, 0}, {1, 0}, {1, 1}}, {0, 0, 1}); | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 } // namespace base | |
| OLD | NEW |