142 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// <map>10 11// class multimap12 13// multimap(const multimap& m);14 15#include <map>16#include <cassert>17 18#include "test_macros.h"19#include "../../../test_compare.h"20#include "test_allocator.h"21#include "min_allocator.h"22 23template <template <class> class Alloc>24void test_alloc() {25 { // Simple check26 using V = std::pair<const int, int>;27 using Map = std::multimap<int, int, std::less<int>, Alloc<V> >;28 29 V arr[] = {V(1, 1), V(2, 3), V(2, 6)};30 const Map orig(begin(arr), end(arr));31 Map copy = orig;32 assert(copy.size() == 3);33 assert(*std::next(copy.begin(), 0) == V(1, 1));34 assert(*std::next(copy.begin(), 1) == V(2, 3));35 assert(*std::next(copy.begin(), 2) == V(2, 6));36 assert(std::next(copy.begin(), 3) == copy.end());37 38 // Check that orig is still what is expected39 assert(orig.size() == 3);40 assert(*std::next(orig.begin(), 0) == V(1, 1));41 assert(*std::next(orig.begin(), 1) == V(2, 3));42 assert(*std::next(orig.begin(), 2) == V(2, 6));43 assert(std::next(orig.begin(), 3) == orig.end());44 }45 46 { // copy empty map47 using V = std::pair<const int, int>;48 using Map = std::multimap<int, int, std::less<int>, Alloc<V> >;49 50 const Map orig;51 Map copy = orig;52 assert(copy.size() == 0);53 assert(copy.begin() == copy.end());54 55 // Check that orig is still what is expected56 assert(orig.size() == 0);57 assert(orig.begin() == orig.end());58 }59 60 { // only some leaf nodes exist61 using V = std::pair<const int, int>;62 using Map = std::multimap<int, int, std::less<int>, Alloc<V> >;63 64 V arr[] = {V(1, 1), V(2, 3), V(2, 6), V(4, 7), V(5, 0)};65 const Map orig(begin(arr), end(arr));66 Map copy = orig;67 assert(copy.size() == 5);68 assert(*std::next(copy.begin(), 0) == V(1, 1));69 assert(*std::next(copy.begin(), 1) == V(2, 3));70 assert(*std::next(copy.begin(), 2) == V(2, 6));71 assert(*std::next(copy.begin(), 3) == V(4, 7));72 assert(*std::next(copy.begin(), 4) == V(5, 0));73 assert(std::next(copy.begin(), 5) == copy.end());74 75 // Check that orig is still what is expected76 assert(orig.size() == 5);77 assert(*std::next(orig.begin(), 0) == V(1, 1));78 assert(*std::next(orig.begin(), 1) == V(2, 3));79 assert(*std::next(orig.begin(), 2) == V(2, 6));80 assert(*std::next(orig.begin(), 3) == V(4, 7));81 assert(*std::next(orig.begin(), 4) == V(5, 0));82 assert(std::next(orig.begin(), 5) == orig.end());83 }84}85 86void test() {87 test_alloc<std::allocator>();88 test_alloc<min_allocator>(); // Make sure that fancy pointers work89 90 { // Ensure that the comparator is copied91 using V = std::pair<const int, int>;92 using Map = std::multimap<int, int, test_less<int> >;93 94 V arr[] = {V(1, 1), V(2, 3), V(2, 6)};95 const Map orig(begin(arr), end(arr), test_less<int>(3));96 Map copy = orig;97 assert(copy.size() == 3);98 assert(copy.key_comp() == test_less<int>(3));99 100 // Check that orig is still what is expected101 assert(orig.size() == 3);102 assert(orig.key_comp() == test_less<int>(3));103 }104 105 { // Ensure that the allocator is copied106 using V = std::pair<const int, int>;107 using Map = std::multimap<int, int, std::less<int>, test_allocator<V> >;108 109 V arr[] = {V(1, 1), V(2, 3), V(2, 6)};110 const Map orig(begin(arr), end(arr), std::less<int>(), test_allocator<V>(10));111 Map copy = orig;112 assert(copy.size() == 3);113 assert(copy.get_allocator() == test_allocator<V>(10));114 115 // Check that orig is still what is expected116 assert(orig.size() == 3);117 assert(orig.get_allocator() == test_allocator<V>(10));118 assert(orig.get_allocator().get_id() != test_alloc_base::moved_value);119 }120 121 { // Ensure that soccc is handled properly122 using V = std::pair<const int, int>;123 using Map = std::multimap<int, int, std::less<int>, other_allocator<V> >;124 125 V arr[] = {V(1, 1), V(2, 3), V(2, 6)};126 const Map orig(begin(arr), end(arr), std::less<int>(), other_allocator<V>(10));127 Map copy = orig;128 assert(copy.size() == 3);129 assert(copy.get_allocator() == other_allocator<V>(-2));130 131 // Check that orig is still what is expected132 assert(orig.size() == 3);133 assert(orig.get_allocator() == other_allocator<V>(10));134 }135}136 137int main(int, char**) {138 test();139 140 return 0;141}142