95 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();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 "min_allocator.h"24#include "test_allocator.h"25 26struct DefaultCtableComp {27 constexpr explicit DefaultCtableComp() { default_constructed_ = true; }28 constexpr bool operator()(int, int) const { return false; }29 bool default_constructed_ = false;30};31 32template <template <class...> class KeyContainer, template <class...> class ValueContainer>33constexpr void test() {34 {35 std::flat_map<int, char*, std::less<int>, KeyContainer<int>, ValueContainer<char*>> m;36 assert(m.empty());37 }38 {39 // explicit(false)40 std::flat_map<int, char*, std::less<int>, KeyContainer<int>, ValueContainer<char*>> m = {};41 assert(m.empty());42 }43 {44 std::flat_map<int, char*, DefaultCtableComp, KeyContainer<int, min_allocator<int>>> m;45 assert(m.empty());46 assert(m.begin() == m.end());47 assert(m.key_comp().default_constructed_);48 }49 {50 using A1 = explicit_allocator<int>;51 using A2 = explicit_allocator<char*>;52 {53 std::flat_map<int, char*, DefaultCtableComp, KeyContainer<int, A1>, ValueContainer<char*, A2>> m;54 assert(m.empty());55 assert(m.key_comp().default_constructed_);56 }57 {58 A1 a1;59 std::flat_map<int, int, DefaultCtableComp, KeyContainer<int, A1>, ValueContainer<int, A1>> m(a1);60 assert(m.empty());61 assert(m.key_comp().default_constructed_);62 }63 }64 {65 // If an allocator is given, it must be usable by both containers.66 using A = test_allocator<int>;67 using M = std::flat_map<int, int, std::less<>, KeyContainer<int>, ValueContainer<int, A>>;68 static_assert(std::is_constructible_v<M>);69 static_assert(!std::is_constructible_v<M, std::allocator<int>>);70 static_assert(!std::is_constructible_v<M, A>);71 }72}73 74constexpr bool test() {75 test<std::vector, std::vector>();76 77#ifndef __cpp_lib_constexpr_deque78 if (!TEST_IS_CONSTANT_EVALUATED)79#endif80 {81 test<std::deque, std::deque>();82 }83 84 return true;85}86 87int main(int, char**) {88 test();89#if TEST_STD_VER >= 2690 static_assert(test());91#endif92 93 return 0;94}95