llvm-project
492 строки · 18.3 Кб
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// pair<iterator,iterator> equal_range(const key_type& k);
14// pair<const_iterator,const_iterator> equal_range(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 std::pair<M::iterator, M::iterator> R;31V ar[] =32{33V(5, 5),34V(7, 6),35V(9, 7),36V(11, 8),37V(13, 9),38V(15, 10),39V(17, 11),40V(19, 12)41};42M m(ar, ar+sizeof(ar)/sizeof(ar[0]));43R r = m.equal_range(5);44assert(r.first == std::next(m.begin(), 0));45assert(r.second == std::next(m.begin(), 1));46r = m.equal_range(7);47assert(r.first == std::next(m.begin(), 1));48assert(r.second == std::next(m.begin(), 2));49r = m.equal_range(9);50assert(r.first == std::next(m.begin(), 2));51assert(r.second == std::next(m.begin(), 3));52r = m.equal_range(11);53assert(r.first == std::next(m.begin(), 3));54assert(r.second == std::next(m.begin(), 4));55r = m.equal_range(13);56assert(r.first == std::next(m.begin(), 4));57assert(r.second == std::next(m.begin(), 5));58r = m.equal_range(15);59assert(r.first == std::next(m.begin(), 5));60assert(r.second == std::next(m.begin(), 6));61r = m.equal_range(17);62assert(r.first == std::next(m.begin(), 6));63assert(r.second == std::next(m.begin(), 7));64r = m.equal_range(19);65assert(r.first == std::next(m.begin(), 7));66assert(r.second == std::next(m.begin(), 8));67r = m.equal_range(4);68assert(r.first == std::next(m.begin(), 0));69assert(r.second == std::next(m.begin(), 0));70r = m.equal_range(6);71assert(r.first == std::next(m.begin(), 1));72assert(r.second == std::next(m.begin(), 1));73r = m.equal_range(8);74assert(r.first == std::next(m.begin(), 2));75assert(r.second == std::next(m.begin(), 2));76r = m.equal_range(10);77assert(r.first == std::next(m.begin(), 3));78assert(r.second == std::next(m.begin(), 3));79r = m.equal_range(12);80assert(r.first == std::next(m.begin(), 4));81assert(r.second == std::next(m.begin(), 4));82r = m.equal_range(14);83assert(r.first == std::next(m.begin(), 5));84assert(r.second == std::next(m.begin(), 5));85r = m.equal_range(16);86assert(r.first == std::next(m.begin(), 6));87assert(r.second == std::next(m.begin(), 6));88r = m.equal_range(18);89assert(r.first == std::next(m.begin(), 7));90assert(r.second == std::next(m.begin(), 7));91r = m.equal_range(20);92assert(r.first == std::next(m.begin(), 8));93assert(r.second == std::next(m.begin(), 8));94}95{96typedef std::pair<M::const_iterator, M::const_iterator> R;97V ar[] =98{99V(5, 5),100V(7, 6),101V(9, 7),102V(11, 8),103V(13, 9),104V(15, 10),105V(17, 11),106V(19, 12)107};108const M m(ar, ar+sizeof(ar)/sizeof(ar[0]));109R r = m.equal_range(5);110assert(r.first == std::next(m.begin(), 0));111assert(r.second == std::next(m.begin(), 1));112r = m.equal_range(7);113assert(r.first == std::next(m.begin(), 1));114assert(r.second == std::next(m.begin(), 2));115r = m.equal_range(9);116assert(r.first == std::next(m.begin(), 2));117assert(r.second == std::next(m.begin(), 3));118r = m.equal_range(11);119assert(r.first == std::next(m.begin(), 3));120assert(r.second == std::next(m.begin(), 4));121r = m.equal_range(13);122assert(r.first == std::next(m.begin(), 4));123assert(r.second == std::next(m.begin(), 5));124r = m.equal_range(15);125assert(r.first == std::next(m.begin(), 5));126assert(r.second == std::next(m.begin(), 6));127r = m.equal_range(17);128assert(r.first == std::next(m.begin(), 6));129assert(r.second == std::next(m.begin(), 7));130r = m.equal_range(19);131assert(r.first == std::next(m.begin(), 7));132assert(r.second == std::next(m.begin(), 8));133r = m.equal_range(4);134assert(r.first == std::next(m.begin(), 0));135assert(r.second == std::next(m.begin(), 0));136r = m.equal_range(6);137assert(r.first == std::next(m.begin(), 1));138assert(r.second == std::next(m.begin(), 1));139r = m.equal_range(8);140assert(r.first == std::next(m.begin(), 2));141assert(r.second == std::next(m.begin(), 2));142r = m.equal_range(10);143assert(r.first == std::next(m.begin(), 3));144assert(r.second == std::next(m.begin(), 3));145r = m.equal_range(12);146assert(r.first == std::next(m.begin(), 4));147assert(r.second == std::next(m.begin(), 4));148r = m.equal_range(14);149assert(r.first == std::next(m.begin(), 5));150assert(r.second == std::next(m.begin(), 5));151r = m.equal_range(16);152assert(r.first == std::next(m.begin(), 6));153assert(r.second == std::next(m.begin(), 6));154r = m.equal_range(18);155assert(r.first == std::next(m.begin(), 7));156assert(r.second == std::next(m.begin(), 7));157r = m.equal_range(20);158assert(r.first == std::next(m.begin(), 8));159assert(r.second == std::next(m.begin(), 8));160}161}162#if TEST_STD_VER >= 11163{164typedef std::pair<const int, double> V;165typedef std::map<int, double, std::less<int>, min_allocator<V>> M;166{167typedef std::pair<M::iterator, M::iterator> R;168V ar[] =169{170V(5, 5),171V(7, 6),172V(9, 7),173V(11, 8),174V(13, 9),175V(15, 10),176V(17, 11),177V(19, 12)178};179M m(ar, ar+sizeof(ar)/sizeof(ar[0]));180R r = m.equal_range(5);181assert(r.first == std::next(m.begin(), 0));182assert(r.second == std::next(m.begin(), 1));183r = m.equal_range(7);184assert(r.first == std::next(m.begin(), 1));185assert(r.second == std::next(m.begin(), 2));186r = m.equal_range(9);187assert(r.first == std::next(m.begin(), 2));188assert(r.second == std::next(m.begin(), 3));189r = m.equal_range(11);190assert(r.first == std::next(m.begin(), 3));191assert(r.second == std::next(m.begin(), 4));192r = m.equal_range(13);193assert(r.first == std::next(m.begin(), 4));194assert(r.second == std::next(m.begin(), 5));195r = m.equal_range(15);196assert(r.first == std::next(m.begin(), 5));197assert(r.second == std::next(m.begin(), 6));198r = m.equal_range(17);199assert(r.first == std::next(m.begin(), 6));200assert(r.second == std::next(m.begin(), 7));201r = m.equal_range(19);202assert(r.first == std::next(m.begin(), 7));203assert(r.second == std::next(m.begin(), 8));204r = m.equal_range(4);205assert(r.first == std::next(m.begin(), 0));206assert(r.second == std::next(m.begin(), 0));207r = m.equal_range(6);208assert(r.first == std::next(m.begin(), 1));209assert(r.second == std::next(m.begin(), 1));210r = m.equal_range(8);211assert(r.first == std::next(m.begin(), 2));212assert(r.second == std::next(m.begin(), 2));213r = m.equal_range(10);214assert(r.first == std::next(m.begin(), 3));215assert(r.second == std::next(m.begin(), 3));216r = m.equal_range(12);217assert(r.first == std::next(m.begin(), 4));218assert(r.second == std::next(m.begin(), 4));219r = m.equal_range(14);220assert(r.first == std::next(m.begin(), 5));221assert(r.second == std::next(m.begin(), 5));222r = m.equal_range(16);223assert(r.first == std::next(m.begin(), 6));224assert(r.second == std::next(m.begin(), 6));225r = m.equal_range(18);226assert(r.first == std::next(m.begin(), 7));227assert(r.second == std::next(m.begin(), 7));228r = m.equal_range(20);229assert(r.first == std::next(m.begin(), 8));230assert(r.second == std::next(m.begin(), 8));231}232{233typedef std::pair<M::const_iterator, M::const_iterator> R;234V ar[] =235{236V(5, 5),237V(7, 6),238V(9, 7),239V(11, 8),240V(13, 9),241V(15, 10),242V(17, 11),243V(19, 12)244};245const M m(ar, ar+sizeof(ar)/sizeof(ar[0]));246R r = m.equal_range(5);247assert(r.first == std::next(m.begin(), 0));248assert(r.second == std::next(m.begin(), 1));249r = m.equal_range(7);250assert(r.first == std::next(m.begin(), 1));251assert(r.second == std::next(m.begin(), 2));252r = m.equal_range(9);253assert(r.first == std::next(m.begin(), 2));254assert(r.second == std::next(m.begin(), 3));255r = m.equal_range(11);256assert(r.first == std::next(m.begin(), 3));257assert(r.second == std::next(m.begin(), 4));258r = m.equal_range(13);259assert(r.first == std::next(m.begin(), 4));260assert(r.second == std::next(m.begin(), 5));261r = m.equal_range(15);262assert(r.first == std::next(m.begin(), 5));263assert(r.second == std::next(m.begin(), 6));264r = m.equal_range(17);265assert(r.first == std::next(m.begin(), 6));266assert(r.second == std::next(m.begin(), 7));267r = m.equal_range(19);268assert(r.first == std::next(m.begin(), 7));269assert(r.second == std::next(m.begin(), 8));270r = m.equal_range(4);271assert(r.first == std::next(m.begin(), 0));272assert(r.second == std::next(m.begin(), 0));273r = m.equal_range(6);274assert(r.first == std::next(m.begin(), 1));275assert(r.second == std::next(m.begin(), 1));276r = m.equal_range(8);277assert(r.first == std::next(m.begin(), 2));278assert(r.second == std::next(m.begin(), 2));279r = m.equal_range(10);280assert(r.first == std::next(m.begin(), 3));281assert(r.second == std::next(m.begin(), 3));282r = m.equal_range(12);283assert(r.first == std::next(m.begin(), 4));284assert(r.second == std::next(m.begin(), 4));285r = m.equal_range(14);286assert(r.first == std::next(m.begin(), 5));287assert(r.second == std::next(m.begin(), 5));288r = m.equal_range(16);289assert(r.first == std::next(m.begin(), 6));290assert(r.second == std::next(m.begin(), 6));291r = m.equal_range(18);292assert(r.first == std::next(m.begin(), 7));293assert(r.second == std::next(m.begin(), 7));294r = m.equal_range(20);295assert(r.first == std::next(m.begin(), 8));296assert(r.second == std::next(m.begin(), 8));297}298}299#endif300#if TEST_STD_VER > 11301{302typedef std::pair<const int, double> V;303typedef std::map<int, double, std::less<>> M;304typedef std::pair<M::iterator, M::iterator> R;305
306V ar[] =307{308V(5, 5),309V(7, 6),310V(9, 7),311V(11, 8),312V(13, 9),313V(15, 10),314V(17, 11),315V(19, 12)316};317M m(ar, ar+sizeof(ar)/sizeof(ar[0]));318R r = m.equal_range(5);319assert(r.first == std::next(m.begin(), 0));320assert(r.second == std::next(m.begin(), 1));321r = m.equal_range(7);322assert(r.first == std::next(m.begin(), 1));323assert(r.second == std::next(m.begin(), 2));324r = m.equal_range(9);325assert(r.first == std::next(m.begin(), 2));326assert(r.second == std::next(m.begin(), 3));327r = m.equal_range(11);328assert(r.first == std::next(m.begin(), 3));329assert(r.second == std::next(m.begin(), 4));330r = m.equal_range(13);331assert(r.first == std::next(m.begin(), 4));332assert(r.second == std::next(m.begin(), 5));333r = m.equal_range(15);334assert(r.first == std::next(m.begin(), 5));335assert(r.second == std::next(m.begin(), 6));336r = m.equal_range(17);337assert(r.first == std::next(m.begin(), 6));338assert(r.second == std::next(m.begin(), 7));339r = m.equal_range(19);340assert(r.first == std::next(m.begin(), 7));341assert(r.second == std::next(m.begin(), 8));342r = m.equal_range(4);343assert(r.first == std::next(m.begin(), 0));344assert(r.second == std::next(m.begin(), 0));345r = m.equal_range(6);346assert(r.first == std::next(m.begin(), 1));347assert(r.second == std::next(m.begin(), 1));348r = m.equal_range(8);349assert(r.first == std::next(m.begin(), 2));350assert(r.second == std::next(m.begin(), 2));351r = m.equal_range(10);352assert(r.first == std::next(m.begin(), 3));353assert(r.second == std::next(m.begin(), 3));354r = m.equal_range(12);355assert(r.first == std::next(m.begin(), 4));356assert(r.second == std::next(m.begin(), 4));357r = m.equal_range(14);358assert(r.first == std::next(m.begin(), 5));359assert(r.second == std::next(m.begin(), 5));360r = m.equal_range(16);361assert(r.first == std::next(m.begin(), 6));362assert(r.second == std::next(m.begin(), 6));363r = m.equal_range(18);364assert(r.first == std::next(m.begin(), 7));365assert(r.second == std::next(m.begin(), 7));366r = m.equal_range(20);367assert(r.first == std::next(m.begin(), 8));368assert(r.second == std::next(m.begin(), 8));369
370r = m.equal_range(C2Int(5));371assert(r.first == std::next(m.begin(), 0));372assert(r.second == std::next(m.begin(), 1));373r = m.equal_range(C2Int(7));374assert(r.first == std::next(m.begin(), 1));375assert(r.second == std::next(m.begin(), 2));376r = m.equal_range(C2Int(9));377assert(r.first == std::next(m.begin(), 2));378assert(r.second == std::next(m.begin(), 3));379r = m.equal_range(C2Int(11));380assert(r.first == std::next(m.begin(), 3));381assert(r.second == std::next(m.begin(), 4));382r = m.equal_range(C2Int(13));383assert(r.first == std::next(m.begin(), 4));384assert(r.second == std::next(m.begin(), 5));385r = m.equal_range(C2Int(15));386assert(r.first == std::next(m.begin(), 5));387assert(r.second == std::next(m.begin(), 6));388r = m.equal_range(C2Int(17));389assert(r.first == std::next(m.begin(), 6));390assert(r.second == std::next(m.begin(), 7));391r = m.equal_range(C2Int(19));392assert(r.first == std::next(m.begin(), 7));393assert(r.second == std::next(m.begin(), 8));394r = m.equal_range(C2Int(4));395assert(r.first == std::next(m.begin(), 0));396assert(r.second == std::next(m.begin(), 0));397r = m.equal_range(C2Int(6));398assert(r.first == std::next(m.begin(), 1));399assert(r.second == std::next(m.begin(), 1));400r = m.equal_range(C2Int(8));401assert(r.first == std::next(m.begin(), 2));402assert(r.second == std::next(m.begin(), 2));403r = m.equal_range(C2Int(10));404assert(r.first == std::next(m.begin(), 3));405assert(r.second == std::next(m.begin(), 3));406r = m.equal_range(C2Int(12));407assert(r.first == std::next(m.begin(), 4));408assert(r.second == std::next(m.begin(), 4));409r = m.equal_range(C2Int(14));410assert(r.first == std::next(m.begin(), 5));411assert(r.second == std::next(m.begin(), 5));412r = m.equal_range(C2Int(16));413assert(r.first == std::next(m.begin(), 6));414assert(r.second == std::next(m.begin(), 6));415r = m.equal_range(C2Int(18));416assert(r.first == std::next(m.begin(), 7));417assert(r.second == std::next(m.begin(), 7));418r = m.equal_range(C2Int(20));419assert(r.first == std::next(m.begin(), 8));420assert(r.second == std::next(m.begin(), 8));421}422{423typedef PrivateConstructor PC;424typedef std::map<PC, double, std::less<>> M;425typedef std::pair<M::iterator, M::iterator> R;426
427M m;428m [ PC::make(5) ] = 5;429m [ PC::make(7) ] = 6;430m [ PC::make(9) ] = 7;431m [ PC::make(11) ] = 8;432m [ PC::make(13) ] = 9;433m [ PC::make(15) ] = 10;434m [ PC::make(17) ] = 11;435m [ PC::make(19) ] = 12;436
437R r = m.equal_range(5);438assert(r.first == std::next(m.begin(), 0));439assert(r.second == std::next(m.begin(), 1));440r = m.equal_range(7);441assert(r.first == std::next(m.begin(), 1));442assert(r.second == std::next(m.begin(), 2));443r = m.equal_range(9);444assert(r.first == std::next(m.begin(), 2));445assert(r.second == std::next(m.begin(), 3));446r = m.equal_range(11);447assert(r.first == std::next(m.begin(), 3));448assert(r.second == std::next(m.begin(), 4));449r = m.equal_range(13);450assert(r.first == std::next(m.begin(), 4));451assert(r.second == std::next(m.begin(), 5));452r = m.equal_range(15);453assert(r.first == std::next(m.begin(), 5));454assert(r.second == std::next(m.begin(), 6));455r = m.equal_range(17);456assert(r.first == std::next(m.begin(), 6));457assert(r.second == std::next(m.begin(), 7));458r = m.equal_range(19);459assert(r.first == std::next(m.begin(), 7));460assert(r.second == std::next(m.begin(), 8));461r = m.equal_range(4);462assert(r.first == std::next(m.begin(), 0));463assert(r.second == std::next(m.begin(), 0));464r = m.equal_range(6);465assert(r.first == std::next(m.begin(), 1));466assert(r.second == std::next(m.begin(), 1));467r = m.equal_range(8);468assert(r.first == std::next(m.begin(), 2));469assert(r.second == std::next(m.begin(), 2));470r = m.equal_range(10);471assert(r.first == std::next(m.begin(), 3));472assert(r.second == std::next(m.begin(), 3));473r = m.equal_range(12);474assert(r.first == std::next(m.begin(), 4));475assert(r.second == std::next(m.begin(), 4));476r = m.equal_range(14);477assert(r.first == std::next(m.begin(), 5));478assert(r.second == std::next(m.begin(), 5));479r = m.equal_range(16);480assert(r.first == std::next(m.begin(), 6));481assert(r.second == std::next(m.begin(), 6));482r = m.equal_range(18);483assert(r.first == std::next(m.begin(), 7));484assert(r.second == std::next(m.begin(), 7));485r = m.equal_range(20);486assert(r.first == std::next(m.begin(), 8));487assert(r.second == std::next(m.begin(), 8));488}489#endif490
491return 0;492}
493