llvm-project
283 строки · 6.5 Кб
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// template <class Key, class T, class Compare, class Allocator>
14// void
15// swap(map<Key, T, Compare, Allocator>& x, map<Key, T, Compare, Allocator>& y);
16
17#include <map>18#include <cassert>19#include "test_macros.h"20#include "test_allocator.h"21#include "../../../test_compare.h"22#include "min_allocator.h"23
24int main(int, char**)25{
26typedef std::pair<const int, double> V;27{28typedef std::map<int, double> M;29{30M m1;31M m2;32M m1_save = m1;33M m2_save = m2;34swap(m1, m2);35assert(m1 == m2_save);36assert(m2 == m1_save);37}38{39V ar2[] =40{41V(5, 5),42V(6, 6),43V(7, 7),44V(8, 8),45V(9, 9),46V(10, 10),47V(11, 11),48V(12, 12)49};50M m1;51M m2(ar2, ar2+sizeof(ar2)/sizeof(ar2[0]));52M m1_save = m1;53M m2_save = m2;54swap(m1, m2);55assert(m1 == m2_save);56assert(m2 == m1_save);57}58{59V ar1[] =60{61V(1, 1),62V(2, 2),63V(3, 3),64V(4, 4)65};66M m1(ar1, ar1+sizeof(ar1)/sizeof(ar1[0]));67M m2;68M m1_save = m1;69M m2_save = m2;70swap(m1, m2);71assert(m1 == m2_save);72assert(m2 == m1_save);73}74{75V ar1[] =76{77V(1, 1),78V(2, 2),79V(3, 3),80V(4, 4)81};82V ar2[] =83{84V(5, 5),85V(6, 6),86V(7, 7),87V(8, 8),88V(9, 9),89V(10, 10),90V(11, 11),91V(12, 12)92};93M m1(ar1, ar1+sizeof(ar1)/sizeof(ar1[0]));94M m2(ar2, ar2+sizeof(ar2)/sizeof(ar2[0]));95M m1_save = m1;96M m2_save = m2;97swap(m1, m2);98assert(m1 == m2_save);99assert(m2 == m1_save);100}101}102{103typedef test_allocator<V> A;104typedef test_less<int> C;105typedef std::map<int, double, C, A> M;106V ar1[] =107{108V(1, 1),109V(2, 2),110V(3, 3),111V(4, 4)112};113V ar2[] =114{115V(5, 5),116V(6, 6),117V(7, 7),118V(8, 8),119V(9, 9),120V(10, 10),121V(11, 11),122V(12, 12)123};124M m1(ar1, ar1+sizeof(ar1)/sizeof(ar1[0]), C(1), A(1, 1));125M m2(ar2, ar2+sizeof(ar2)/sizeof(ar2[0]), C(2), A(1, 2));126M m1_save = m1;127M m2_save = m2;128swap(m1, m2);129assert(m1 == m2_save);130assert(m2 == m1_save);131assert(m1.key_comp() == C(2));132assert(m1.get_allocator().get_id() == 1); // not swapped133assert(m2.key_comp() == C(1));134assert(m2.get_allocator().get_id() == 2);135}136{137typedef other_allocator<V> A;138typedef test_less<int> C;139typedef std::map<int, double, C, A> M;140V ar1[] =141{142V(1, 1),143V(2, 2),144V(3, 3),145V(4, 4)146};147V ar2[] =148{149V(5, 5),150V(6, 6),151V(7, 7),152V(8, 8),153V(9, 9),154V(10, 10),155V(11, 11),156V(12, 12)157};158M m1(ar1, ar1+sizeof(ar1)/sizeof(ar1[0]), C(1), A(1));159M m2(ar2, ar2+sizeof(ar2)/sizeof(ar2[0]), C(2), A(2));160M m1_save = m1;161M m2_save = m2;162swap(m1, m2);163assert(m1 == m2_save);164assert(m2 == m1_save);165assert(m1.key_comp() == C(2));166assert(m1.get_allocator() == A(2));167assert(m2.key_comp() == C(1));168assert(m2.get_allocator() == A(1));169}170#if TEST_STD_VER >= 11171{172typedef std::map<int, double, std::less<int>, min_allocator<V>> M;173{174M m1;175M m2;176M m1_save = m1;177M m2_save = m2;178swap(m1, m2);179assert(m1 == m2_save);180assert(m2 == m1_save);181}182{183V ar2[] =184{185V(5, 5),186V(6, 6),187V(7, 7),188V(8, 8),189V(9, 9),190V(10, 10),191V(11, 11),192V(12, 12)193};194M m1;195M m2(ar2, ar2+sizeof(ar2)/sizeof(ar2[0]));196M m1_save = m1;197M m2_save = m2;198swap(m1, m2);199assert(m1 == m2_save);200assert(m2 == m1_save);201}202{203V ar1[] =204{205V(1, 1),206V(2, 2),207V(3, 3),208V(4, 4)209};210M m1(ar1, ar1+sizeof(ar1)/sizeof(ar1[0]));211M m2;212M m1_save = m1;213M m2_save = m2;214swap(m1, m2);215assert(m1 == m2_save);216assert(m2 == m1_save);217}218{219V ar1[] =220{221V(1, 1),222V(2, 2),223V(3, 3),224V(4, 4)225};226V ar2[] =227{228V(5, 5),229V(6, 6),230V(7, 7),231V(8, 8),232V(9, 9),233V(10, 10),234V(11, 11),235V(12, 12)236};237M m1(ar1, ar1+sizeof(ar1)/sizeof(ar1[0]));238M m2(ar2, ar2+sizeof(ar2)/sizeof(ar2[0]));239M m1_save = m1;240M m2_save = m2;241swap(m1, m2);242assert(m1 == m2_save);243assert(m2 == m1_save);244}245}246{247typedef min_allocator<V> A;248typedef test_less<int> C;249typedef std::map<int, double, C, A> M;250V ar1[] =251{252V(1, 1),253V(2, 2),254V(3, 3),255V(4, 4)256};257V ar2[] =258{259V(5, 5),260V(6, 6),261V(7, 7),262V(8, 8),263V(9, 9),264V(10, 10),265V(11, 11),266V(12, 12)267};268M m1(ar1, ar1+sizeof(ar1)/sizeof(ar1[0]), C(1), A());269M m2(ar2, ar2+sizeof(ar2)/sizeof(ar2[0]), C(2), A());270M m1_save = m1;271M m2_save = m2;272swap(m1, m2);273assert(m1 == m2_save);274assert(m2 == m1_save);275assert(m1.key_comp() == C(2));276assert(m1.get_allocator() == A());277assert(m2.key_comp() == C(1));278assert(m2.get_allocator() == A());279}280#endif281
282return 0;283}
284