//===----------------------------------------------------------------------===// // // The LLVM Compiler Infrastructure // // This file is dual licensed under the MIT and the University of Illinois Open // Source Licenses. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // UNSUPPORTED: c++98, c++03, c++11, c++14 // // class unordered_multiset // template // void merge(unordered_set& source); // template // void merge(unordered_set&& source); // template // void merge(unordered_multiset& source); // template // void merge(unordered_multiset&& source); #include #include #include "test_macros.h" #include "Counter.h" template bool set_equal(const Set& set, Set other) { return set == other; } #ifndef TEST_HAS_NO_EXCEPTIONS template struct throw_hasher { bool& should_throw_; throw_hasher(bool& should_throw) : should_throw_(should_throw) {} size_t operator()(const T& p) const { if (should_throw_) throw 0; return std::hash()(p); } }; #endif int main() { { std::unordered_multiset src{1, 3, 5}; std::unordered_multiset dst{2, 4, 5}; dst.merge(src); assert(set_equal(src, {})); assert(set_equal(dst, {1, 2, 3, 4, 5, 5})); } #ifndef TEST_HAS_NO_EXCEPTIONS { bool do_throw = false; typedef std::unordered_multiset, throw_hasher>> set_type; set_type src({1, 3, 5}, 0, throw_hasher>(do_throw)); set_type dst({2, 4, 5}, 0, throw_hasher>(do_throw)); assert(Counter_base::gConstructed == 6); do_throw = true; try { dst.merge(src); } catch (int) { do_throw = false; } assert(!do_throw); assert(set_equal(src, set_type({1, 3, 5}, 0, throw_hasher>(do_throw)))); assert(set_equal(dst, set_type({2, 4, 5}, 0, throw_hasher>(do_throw)))); } #endif assert(Counter_base::gConstructed == 0); struct equal { equal() = default; bool operator()(const Counter& lhs, const Counter& rhs) const { return lhs == rhs; } }; struct hasher { hasher() = default; size_t operator()(const Counter& p) const { return std::hash>()(p); } }; { typedef std::unordered_multiset, std::hash>, std::equal_to>> first_set_type; typedef std::unordered_multiset, hasher, equal> second_set_type; typedef std::unordered_set, hasher, equal> third_set_type; { first_set_type first{1, 2, 3}; second_set_type second{2, 3, 4}; third_set_type third{1, 3}; assert(Counter_base::gConstructed == 8); first.merge(second); first.merge(third); assert(set_equal(first, {1, 2, 3, 4, 2, 3, 1, 3})); assert(set_equal(second, {})); assert(set_equal(third, {})); assert(Counter_base::gConstructed == 8); } assert(Counter_base::gConstructed == 0); { first_set_type first{1, 2, 3}; second_set_type second{2, 3, 4}; third_set_type third{1, 3}; assert(Counter_base::gConstructed == 8); first.merge(std::move(second)); first.merge(std::move(third)); assert(set_equal(first, {1, 2, 3, 4, 2, 3, 1, 3})); assert(set_equal(second, {})); assert(set_equal(third, {})); assert(Counter_base::gConstructed == 8); } assert(Counter_base::gConstructed == 0); } { std::unordered_multiset first; { std::unordered_multiset second; first.merge(second); first.merge(std::move(second)); } { std::unordered_set second; first.merge(second); first.merge(std::move(second)); } } }