llvm-project
262 строки · 6.8 Кб
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// iterator find(const key_type& k);
14// const_iterator find(const key_type& k) const;
15
16#include <map>17#include <cassert>18
19#include "test_macros.h"20#include "min_allocator.h"21#include "private_constructor.h"22#include "is_transparent.h"23
24int main(int, char**)25{
26{27typedef std::pair<const int, double> V;28typedef std::map<int, double> M;29{30typedef M::iterator R;31V ar[] =32{33V(5, 5),34V(6, 6),35V(7, 7),36V(8, 8),37V(9, 9),38V(10, 10),39V(11, 11),40V(12, 12)41};42M m(ar, ar+sizeof(ar)/sizeof(ar[0]));43R r = m.find(5);44assert(r == m.begin());45r = m.find(6);46assert(r == std::next(m.begin()));47r = m.find(7);48assert(r == std::next(m.begin(), 2));49r = m.find(8);50assert(r == std::next(m.begin(), 3));51r = m.find(9);52assert(r == std::next(m.begin(), 4));53r = m.find(10);54assert(r == std::next(m.begin(), 5));55r = m.find(11);56assert(r == std::next(m.begin(), 6));57r = m.find(12);58assert(r == std::next(m.begin(), 7));59r = m.find(4);60assert(r == std::next(m.begin(), 8));61}62{63typedef M::const_iterator R;64V ar[] =65{66V(5, 5),67V(6, 6),68V(7, 7),69V(8, 8),70V(9, 9),71V(10, 10),72V(11, 11),73V(12, 12)74};75const M m(ar, ar+sizeof(ar)/sizeof(ar[0]));76R r = m.find(5);77assert(r == m.begin());78r = m.find(6);79assert(r == std::next(m.begin()));80r = m.find(7);81assert(r == std::next(m.begin(), 2));82r = m.find(8);83assert(r == std::next(m.begin(), 3));84r = m.find(9);85assert(r == std::next(m.begin(), 4));86r = m.find(10);87assert(r == std::next(m.begin(), 5));88r = m.find(11);89assert(r == std::next(m.begin(), 6));90r = m.find(12);91assert(r == std::next(m.begin(), 7));92r = m.find(4);93assert(r == std::next(m.begin(), 8));94}95}96#if TEST_STD_VER >= 1197{98typedef std::pair<const int, double> V;99typedef std::map<int, double, std::less<int>, min_allocator<V>> M;100{101typedef M::iterator R;102V ar[] =103{104V(5, 5),105V(6, 6),106V(7, 7),107V(8, 8),108V(9, 9),109V(10, 10),110V(11, 11),111V(12, 12)112};113M m(ar, ar+sizeof(ar)/sizeof(ar[0]));114R r = m.find(5);115assert(r == m.begin());116r = m.find(6);117assert(r == std::next(m.begin()));118r = m.find(7);119assert(r == std::next(m.begin(), 2));120r = m.find(8);121assert(r == std::next(m.begin(), 3));122r = m.find(9);123assert(r == std::next(m.begin(), 4));124r = m.find(10);125assert(r == std::next(m.begin(), 5));126r = m.find(11);127assert(r == std::next(m.begin(), 6));128r = m.find(12);129assert(r == std::next(m.begin(), 7));130r = m.find(4);131assert(r == std::next(m.begin(), 8));132}133{134typedef M::const_iterator R;135V ar[] =136{137V(5, 5),138V(6, 6),139V(7, 7),140V(8, 8),141V(9, 9),142V(10, 10),143V(11, 11),144V(12, 12)145};146const M m(ar, ar+sizeof(ar)/sizeof(ar[0]));147R r = m.find(5);148assert(r == m.begin());149r = m.find(6);150assert(r == std::next(m.begin()));151r = m.find(7);152assert(r == std::next(m.begin(), 2));153r = m.find(8);154assert(r == std::next(m.begin(), 3));155r = m.find(9);156assert(r == std::next(m.begin(), 4));157r = m.find(10);158assert(r == std::next(m.begin(), 5));159r = m.find(11);160assert(r == std::next(m.begin(), 6));161r = m.find(12);162assert(r == std::next(m.begin(), 7));163r = m.find(4);164assert(r == std::next(m.begin(), 8));165}166}167#endif168#if TEST_STD_VER > 11169{170typedef std::pair<const int, double> V;171typedef std::map<int, double, std::less<>> M;172typedef M::iterator R;173
174V ar[] =175{176V(5, 5),177V(6, 6),178V(7, 7),179V(8, 8),180V(9, 9),181V(10, 10),182V(11, 11),183V(12, 12)184};185M m(ar, ar+sizeof(ar)/sizeof(ar[0]));186R r = m.find(5);187assert(r == m.begin());188r = m.find(6);189assert(r == std::next(m.begin()));190r = m.find(7);191assert(r == std::next(m.begin(), 2));192r = m.find(8);193assert(r == std::next(m.begin(), 3));194r = m.find(9);195assert(r == std::next(m.begin(), 4));196r = m.find(10);197assert(r == std::next(m.begin(), 5));198r = m.find(11);199assert(r == std::next(m.begin(), 6));200r = m.find(12);201assert(r == std::next(m.begin(), 7));202r = m.find(4);203assert(r == std::next(m.begin(), 8));204
205r = m.find(C2Int(5));206assert(r == m.begin());207r = m.find(C2Int(6));208assert(r == std::next(m.begin()));209r = m.find(C2Int(7));210assert(r == std::next(m.begin(), 2));211r = m.find(C2Int(8));212assert(r == std::next(m.begin(), 3));213r = m.find(C2Int(9));214assert(r == std::next(m.begin(), 4));215r = m.find(C2Int(10));216assert(r == std::next(m.begin(), 5));217r = m.find(C2Int(11));218assert(r == std::next(m.begin(), 6));219r = m.find(C2Int(12));220assert(r == std::next(m.begin(), 7));221r = m.find(C2Int(4));222assert(r == std::next(m.begin(), 8));223}224
225{226typedef PrivateConstructor PC;227typedef std::map<PC, double, std::less<>> M;228typedef M::iterator R;229
230M m;231m [ PC::make(5) ] = 5;232m [ PC::make(6) ] = 6;233m [ PC::make(7) ] = 7;234m [ PC::make(8) ] = 8;235m [ PC::make(9) ] = 9;236m [ PC::make(10) ] = 10;237m [ PC::make(11) ] = 11;238m [ PC::make(12) ] = 12;239
240R r = m.find(5);241assert(r == m.begin());242r = m.find(6);243assert(r == std::next(m.begin()));244r = m.find(7);245assert(r == std::next(m.begin(), 2));246r = m.find(8);247assert(r == std::next(m.begin(), 3));248r = m.find(9);249assert(r == std::next(m.begin(), 4));250r = m.find(10);251assert(r == std::next(m.begin(), 5));252r = m.find(11);253assert(r == std::next(m.begin(), 6));254r = m.find(12);255assert(r == std::next(m.begin(), 7));256r = m.find(4);257assert(r == std::next(m.begin(), 8));258}259#endif260
261return 0;262}
263