141 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 map12 13// map(const map& m);14 15#include <cassert>16#include <map>17 18#include "min_allocator.h"19#include "../../../test_compare.h"20#include "test_allocator.h"21 22template <template <class> class Alloc>23void test_alloc() {24 { // Simple check25 using V = std::pair<const int, int>;26 using Map = std::map<int, int, std::less<int>, Alloc<V> >;27 28 V arr[] = {V(1, 1), V(2, 3), V(3, 6)};29 const Map orig(begin(arr), end(arr));30 Map copy = orig;31 assert(copy.size() == 3);32 assert(*std::next(copy.begin(), 0) == V(1, 1));33 assert(*std::next(copy.begin(), 1) == V(2, 3));34 assert(*std::next(copy.begin(), 2) == V(3, 6));35 assert(std::next(copy.begin(), 3) == copy.end());36 37 // Check that orig is still what is expected38 assert(orig.size() == 3);39 assert(*std::next(orig.begin(), 0) == V(1, 1));40 assert(*std::next(orig.begin(), 1) == V(2, 3));41 assert(*std::next(orig.begin(), 2) == V(3, 6));42 assert(std::next(orig.begin(), 3) == orig.end());43 }44 45 { // copy empty map46 using V = std::pair<const int, int>;47 using Map = std::map<int, int, std::less<int>, Alloc<V> >;48 49 const Map orig;50 Map copy = orig;51 assert(copy.size() == 0);52 assert(copy.begin() == copy.end());53 54 // Check that orig is still what is expected55 assert(orig.size() == 0);56 assert(orig.begin() == orig.end());57 }58 59 { // only some leaf nodes exist60 using V = std::pair<const int, int>;61 using Map = std::map<int, int, std::less<int>, Alloc<V> >;62 63 V arr[] = {V(1, 1), V(2, 3), V(3, 6), V(4, 7), V(5, 0)};64 const Map orig(begin(arr), end(arr));65 Map copy = orig;66 assert(copy.size() == 5);67 assert(*std::next(copy.begin(), 0) == V(1, 1));68 assert(*std::next(copy.begin(), 1) == V(2, 3));69 assert(*std::next(copy.begin(), 2) == V(3, 6));70 assert(*std::next(copy.begin(), 3) == V(4, 7));71 assert(*std::next(copy.begin(), 4) == V(5, 0));72 assert(std::next(copy.begin(), 5) == copy.end());73 74 // Check that orig is still what is expected75 assert(orig.size() == 5);76 assert(*std::next(orig.begin(), 0) == V(1, 1));77 assert(*std::next(orig.begin(), 1) == V(2, 3));78 assert(*std::next(orig.begin(), 2) == V(3, 6));79 assert(*std::next(orig.begin(), 3) == V(4, 7));80 assert(*std::next(orig.begin(), 4) == V(5, 0));81 assert(std::next(orig.begin(), 5) == orig.end());82 }83}84 85void test() {86 test_alloc<std::allocator>();87 test_alloc<min_allocator>(); // Make sure that fancy pointers work88 89 { // Ensure that the comparator is copied90 using V = std::pair<const int, int>;91 using Map = std::map<int, int, test_less<int> >;92 93 V arr[] = {V(1, 1), V(2, 3), V(3, 6)};94 const Map orig(begin(arr), end(arr), test_less<int>(3));95 Map copy = orig;96 assert(copy.size() == 3);97 assert(copy.key_comp() == test_less<int>(3));98 99 // Check that orig is still what is expected100 assert(orig.size() == 3);101 assert(orig.key_comp() == test_less<int>(3));102 }103 104 { // Ensure that the allocator is copied105 using V = std::pair<const int, int>;106 using Map = std::map<int, int, std::less<int>, test_allocator<V> >;107 108 V arr[] = {V(1, 1), V(2, 3), V(3, 6)};109 const Map orig(begin(arr), end(arr), std::less<int>(), test_allocator<V>(10));110 Map copy = orig;111 assert(copy.size() == 3);112 assert(copy.get_allocator() == test_allocator<V>(10));113 114 // Check that orig is still what is expected115 assert(orig.size() == 3);116 assert(orig.get_allocator() == test_allocator<V>(10));117 assert(orig.get_allocator().get_id() != test_alloc_base::moved_value);118 }119 120 { // Ensure that soccc is handled properly121 using V = std::pair<const int, int>;122 using Map = std::map<int, int, std::less<int>, other_allocator<V> >;123 124 V arr[] = {V(1, 1), V(2, 3), V(3, 6)};125 const Map orig(begin(arr), end(arr), std::less<int>(), other_allocator<V>(10));126 Map copy = orig;127 assert(copy.size() == 3);128 assert(copy.get_allocator() == other_allocator<V>(-2));129 130 // Check that orig is still what is expected131 assert(orig.size() == 3);132 assert(orig.get_allocator() == other_allocator<V>(10));133 }134}135 136int main(int, char**) {137 test();138 139 return 0;140}141