112 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// mapped_type& at(const key_type& k);14// const mapped_type& at(const key_type& k) const;15 16#include <cassert>17#include <deque>18#include <flat_map>19#include <functional>20#include <stdexcept>21#include <type_traits>22#include <vector>23 24#include "MinSequenceContainer.h"25#include "min_allocator.h"26#include "test_macros.h"27 28template <class KeyContainer, class ValueContainer>29constexpr void test() {30 using P = std::pair<int, double>;31 P ar[] = {32 P(1, 1.5),33 P(2, 2.5),34 P(3, 3.5),35 P(4, 4.5),36 P(5, 5.5),37 P(7, 7.5),38 P(8, 8.5),39 };40 const int one = 1;41 {42 std::flat_map<int, double, std::less<int>, KeyContainer, ValueContainer> m(ar, ar + sizeof(ar) / sizeof(ar[0]));43 ASSERT_SAME_TYPE(decltype(m.at(one)), double&);44 assert(m.size() == 7);45 assert(m.at(one) == 1.5);46 m.at(1) = -1.5;47 assert(m.at(1) == -1.5);48 assert(m.at(2) == 2.5);49 assert(m.at(3) == 3.5);50 assert(m.at(4) == 4.5);51 assert(m.at(5) == 5.5);52#ifndef TEST_HAS_NO_EXCEPTIONS53 if (!TEST_IS_CONSTANT_EVALUATED) {54 try {55 TEST_IGNORE_NODISCARD m.at(6);56 assert(false);57 } catch (std::out_of_range&) {58 }59 }60#endif61 assert(m.at(7) == 7.5);62 assert(m.at(8) == 8.5);63 assert(m.size() == 7);64 }65 {66 const std::flat_map<int, double, std::less<int>, KeyContainer, ValueContainer> m(67 ar, ar + sizeof(ar) / sizeof(ar[0]));68 ASSERT_SAME_TYPE(decltype(m.at(one)), const double&);69 assert(m.size() == 7);70 assert(m.at(one) == 1.5);71 assert(m.at(2) == 2.5);72 assert(m.at(3) == 3.5);73 assert(m.at(4) == 4.5);74 assert(m.at(5) == 5.5);75#ifndef TEST_HAS_NO_EXCEPTIONS76 if (!TEST_IS_CONSTANT_EVALUATED) {77 try {78 TEST_IGNORE_NODISCARD m.at(6);79 assert(false);80 } catch (std::out_of_range&) {81 }82 }83#endif84 assert(m.at(7) == 7.5);85 assert(m.at(8) == 8.5);86 assert(m.size() == 7);87 }88}89 90constexpr bool test() {91 test<std::vector<int>, std::vector<double>>();92#ifndef __cpp_lib_constexpr_deque93 if (!TEST_IS_CONSTANT_EVALUATED)94#endif95 {96 test<std::deque<int>, std::vector<double>>();97 }98 test<MinSequenceContainer<int>, MinSequenceContainer<double>>();99 test<std::vector<int, min_allocator<int>>, std::vector<double, min_allocator<double>>>();100 101 return true;102}103 104int main(int, char**) {105 test();106#if TEST_STD_VER >= 26107 static_assert(test());108#endif109 110 return 0;111}112