diff --git a/base/base_tests/stl_helpers_test.cpp b/base/base_tests/stl_helpers_test.cpp index f898d69e5b..dddb028508 100644 --- a/base/base_tests/stl_helpers_test.cpp +++ b/base/base_tests/stl_helpers_test.cpp @@ -21,9 +21,9 @@ private: UNIT_TEST(LessBy) { - using TValue = pair; - { + using TValue = pair; + vector v = {{2, 2}, {0, 4}, {3, 1}, {4, 0}, {1, 3}}; sort(v.begin(), v.end(), my::LessBy(&TValue::first)); for (size_t i = 0; i < v.size(); ++i) @@ -51,13 +51,27 @@ UNIT_TEST(LessBy) UNIT_TEST(EqualsBy) { - using TValue = pair; - vector 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()); + { + using TValue = pair; + vector 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 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 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 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 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(), ()); + } } } // namespace diff --git a/base/stl_helpers.hpp b/base/stl_helpers.hpp index c01fbc5b99..832f53b829 100644 --- a/base/stl_helpers.hpp +++ b/base/stl_helpers.hpp @@ -16,31 +16,31 @@ struct Less; template struct Less { - Less(T(C::*p)) : p_(p) {} + Less(T(C::*p)) : m_p(p) {} - inline bool operator()(C const & lhs, C const & rhs) const { return lhs.*p_ < rhs.*p_; } + inline bool operator()(C const & lhs, C const & rhs) const { return lhs.*m_p < rhs.*m_p; } inline bool operator()(C const * const lhs, C const * const rhs) const { - return lhs->*p_ < rhs->*p_; + return lhs->*m_p < rhs->*m_p; } - T(C::*p_); + T(C::*m_p); }; template struct Less { - Less(T (C::*p)() const) : p_(p) {} + Less(T (C::*p)() const) : m_p(p) {} - inline bool operator()(C const & lhs, C const & rhs) const { return (lhs.*p_)() < (rhs.*p_)(); } + inline bool operator()(C const & lhs, C const & rhs) const { return (lhs.*m_p)() < (rhs.*m_p)(); } inline bool operator()(C const * const lhs, C const * const rhs) const { - return (lhs->*p_)() < (rhs->*p_)(); + return (lhs->*m_p)() < (rhs->*m_p)(); } - T (C::*p_)() const; + T (C::*m_p)() const; }; template @@ -49,31 +49,31 @@ struct Equals; template struct Equals { - Equals(T(C::*p)) : p_(p) {} + Equals(T(C::*p)) : m_p(p) {} - inline bool operator()(C const & lhs, C const & rhs) const { return lhs.*p_ == rhs.*p_; } + inline bool operator()(C const & lhs, C const & rhs) const { return lhs.*m_p == rhs.*m_p; } inline bool operator()(C const * const lhs, C const * const rhs) const { - return lhs->*p_ == rhs->*p_; + return lhs->*m_p == rhs->*m_p; } - T(C::*p_); + T(C::*m_p); }; template struct Equals { - Equals(T (C::*p)() const) : p_(p) {} + Equals(T (C::*p)() const) : m_p(p) {} - inline bool operator()(C const & lhs, C const & rhs) const { return (lhs.*p_)() == (rhs.*p_)(); } + inline bool operator()(C const & lhs, C const & rhs) const { return (lhs.*m_p)() == (rhs.*m_p)(); } inline bool operator()(C const * const lhs, C const * const rhs) const { - return (lhs->*p_)() == (rhs->*p_)(); + return (lhs->*m_p)() == (rhs->*m_p)(); } - T (C::*p_)() const; + T (C::*m_p)() const; }; } // namespace impl