117 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++14, c++17, c++2010 11// <flat_map>12 13// flat_map& operator=(const flat_map& m);14 15#include <cassert>16#include <deque>17#include <flat_map>18#include <functional>19#include <type_traits>20#include <vector>21 22#include "test_macros.h"23#include "../../../test_compare.h"24#include "test_allocator.h"25 26template <template <class...> class KeyContainer, template <class...> class ValueContainer>27constexpr void test() {28 {29 // test_allocator is not propagated30 using C = test_less<int>;31 KeyContainer<int, test_allocator<int>> ks({1, 3, 5}, test_allocator<int>(6));32 ValueContainer<char, test_allocator<char>> vs({2, 2, 1}, test_allocator<char>(7));33 using M = std::flat_map<int, char, C, decltype(ks), decltype(vs)>;34 auto mo = M(ks, vs, C(5));35 auto m = M({{3, 3}, {4, 4}, {5, 5}}, C(3), test_allocator<int>(2));36 m = mo;37 38 assert(m.key_comp() == C(5));39 assert(m.keys() == ks);40 assert(m.values() == vs);41 assert(m.keys().get_allocator() == test_allocator<int>(2));42 assert(m.values().get_allocator() == test_allocator<char>(2));43 44 // mo is unchanged45 assert(mo.key_comp() == C(5));46 assert(mo.keys() == ks);47 assert(mo.values() == vs);48 assert(mo.keys().get_allocator() == test_allocator<int>(6));49 assert(mo.values().get_allocator() == test_allocator<char>(7));50 }51 {52 // other_allocator is propagated53 using C = test_less<int>;54 using Ks = KeyContainer<int, other_allocator<int>>;55 using Vs = ValueContainer<char, other_allocator<char>>;56 auto ks = Ks({1, 3, 5}, other_allocator<int>(6));57 auto vs = Vs({2, 2, 1}, other_allocator<char>(7));58 using M = std::flat_map<int, char, C, Ks, Vs>;59 auto mo = M(Ks(ks, other_allocator<int>(6)), Vs(vs, other_allocator<int>(7)), C(5));60 auto m = M({{3, 3}, {4, 4}, {5, 5}}, C(3), other_allocator<int>(2));61 m = mo;62 63 assert(m.key_comp() == C(5));64 assert(m.keys() == ks);65 assert(m.values() == vs);66 assert(m.keys().get_allocator() == other_allocator<int>(6));67 assert(m.values().get_allocator() == other_allocator<char>(7));68 69 // mo is unchanged70 assert(mo.key_comp() == C(5));71 assert(mo.keys() == ks);72 assert(mo.values() == vs);73 assert(mo.keys().get_allocator() == other_allocator<int>(6));74 assert(mo.values().get_allocator() == other_allocator<char>(7));75 }76 if (!TEST_IS_CONSTANT_EVALUATED) {77 // comparator is copied and invariant is preserved78 using M = std::flat_map<int, int, std::function<bool(int, int)>>;79 M mo = M({{1, 2}, {3, 4}}, std::less<int>());80 M m = M({{1, 2}, {3, 4}}, std::greater<int>());81 assert(m.key_comp()(2, 1) == true);82 assert(m != mo);83 m = mo;84 assert(m.key_comp()(2, 1) == false);85 assert(m == mo);86 }87 {88 // self-assignment89 using M = std::flat_map<int, int>;90 M m = {{1, 2}, {3, 4}};91 m = static_cast<const M&>(m);92 assert((m == M{{1, 2}, {3, 4}}));93 }94}95 96constexpr bool test() {97 test<std::vector, std::vector>();98 99#ifndef __cpp_lib_constexpr_deque100 if (!TEST_IS_CONSTANT_EVALUATED)101#endif102 {103 test<std::deque, std::deque>();104 }105 106 return true;107}108 109int main(int, char**) {110 test();111#if TEST_STD_VER >= 26112 static_assert(test());113#endif114 115 return 0;116}117