llvm-project
165 строк · 4.0 Кб
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-exception
6//
7//===----------------------------------------------------------------------===//
8
9// <map>
10
11// class 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 <map>18#include <stdexcept>19
20#include "min_allocator.h"21#include "test_macros.h"22
23int main(int, char**)24{
25{26typedef std::pair<const int, double> V;27V ar[] =28{29V(1, 1.5),30V(2, 2.5),31V(3, 3.5),32V(4, 4.5),33V(5, 5.5),34V(7, 7.5),35V(8, 8.5),36};37std::map<int, double> m(ar, ar+sizeof(ar)/sizeof(ar[0]));38assert(m.size() == 7);39assert(m.at(1) == 1.5);40m.at(1) = -1.5;41assert(m.at(1) == -1.5);42assert(m.at(2) == 2.5);43assert(m.at(3) == 3.5);44assert(m.at(4) == 4.5);45assert(m.at(5) == 5.5);46#ifndef TEST_HAS_NO_EXCEPTIONS47try48{49TEST_IGNORE_NODISCARD m.at(6);50assert(false);51}52catch (std::out_of_range&)53{54}55#endif56assert(m.at(7) == 7.5);57assert(m.at(8) == 8.5);58assert(m.size() == 7);59}60{61typedef std::pair<const int, double> V;62V ar[] =63{64V(1, 1.5),65V(2, 2.5),66V(3, 3.5),67V(4, 4.5),68V(5, 5.5),69V(7, 7.5),70V(8, 8.5),71};72const std::map<int, double> m(ar, ar+sizeof(ar)/sizeof(ar[0]));73assert(m.size() == 7);74assert(m.at(1) == 1.5);75assert(m.at(2) == 2.5);76assert(m.at(3) == 3.5);77assert(m.at(4) == 4.5);78assert(m.at(5) == 5.5);79#ifndef TEST_HAS_NO_EXCEPTIONS80try81{82TEST_IGNORE_NODISCARD m.at(6);83assert(false);84}85catch (std::out_of_range&)86{87}88#endif89assert(m.at(7) == 7.5);90assert(m.at(8) == 8.5);91assert(m.size() == 7);92}93#if TEST_STD_VER >= 1194{95typedef std::pair<const int, double> V;96V ar[] =97{98V(1, 1.5),99V(2, 2.5),100V(3, 3.5),101V(4, 4.5),102V(5, 5.5),103V(7, 7.5),104V(8, 8.5),105};106std::map<int, double, std::less<int>, min_allocator<V>> m(ar, ar+sizeof(ar)/sizeof(ar[0]));107assert(m.size() == 7);108assert(m.at(1) == 1.5);109m.at(1) = -1.5;110assert(m.at(1) == -1.5);111assert(m.at(2) == 2.5);112assert(m.at(3) == 3.5);113assert(m.at(4) == 4.5);114assert(m.at(5) == 5.5);115#ifndef TEST_HAS_NO_EXCEPTIONS116try117{118TEST_IGNORE_NODISCARD m.at(6);119assert(false);120}121catch (std::out_of_range&)122{123}124#endif125assert(m.at(7) == 7.5);126assert(m.at(8) == 8.5);127assert(m.size() == 7);128}129{130typedef std::pair<const int, double> V;131V ar[] =132{133V(1, 1.5),134V(2, 2.5),135V(3, 3.5),136V(4, 4.5),137V(5, 5.5),138V(7, 7.5),139V(8, 8.5),140};141const std::map<int, double, std::less<int>, min_allocator<V>> m(ar, ar+sizeof(ar)/sizeof(ar[0]));142assert(m.size() == 7);143assert(m.at(1) == 1.5);144assert(m.at(2) == 2.5);145assert(m.at(3) == 3.5);146assert(m.at(4) == 4.5);147assert(m.at(5) == 5.5);148#ifndef TEST_HAS_NO_EXCEPTIONS149try150{151TEST_IGNORE_NODISCARD m.at(6);152assert(false);153}154catch (std::out_of_range&)155{156}157#endif158assert(m.at(7) == 7.5);159assert(m.at(8) == 8.5);160assert(m.size() == 7);161}162#endif163
164return 0;165}
166