112 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, const allocator_type& a);14 15#include <cassert>16#include <map>17 18#include "test_macros.h"19#include "../../../test_compare.h"20#include "test_allocator.h"21#include "min_allocator.h"22 23template <class Alloc>24void test_alloc(const Alloc& new_alloc) {25 { // Simple check26 using V = std::pair<const int, int>;27 using Map = std::map<int, int, std::less<int>, Alloc>;28 29 V arr[] = {V(1, 1), V(2, 3), V(3, 6)};30 const Map orig(begin(arr), end(arr));31 Map copy(orig, new_alloc);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(3, 6));36 assert(std::next(copy.begin(), 3) == copy.end());37 assert(copy.get_allocator() == new_alloc);38 39 // Check that orig is still what is expected40 assert(orig.size() == 3);41 assert(*std::next(orig.begin(), 0) == V(1, 1));42 assert(*std::next(orig.begin(), 1) == V(2, 3));43 assert(*std::next(orig.begin(), 2) == V(3, 6));44 assert(std::next(orig.begin(), 3) == orig.end());45 }46 47 { // copy empty map48 using Map = std::map<int, int, std::less<int>, Alloc>;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::map<int, int, std::less<int>, Alloc>;63 64 V arr[] = {V(1, 1), V(2, 3), V(3, 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(3, 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(3, 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<std::pair<const int, int> >());88 test_alloc(test_allocator<std::pair<const int, int> >(25)); // Make sure that the new allocator is actually used89 test_alloc(min_allocator<std::pair<const int, int> >()); // Make sure that fancy pointers work90 91 { // Ensure that the comparator is copied92 using V = std::pair<const int, int>;93 using Map = std::map<int, int, test_less<int> >;94 95 V arr[] = {V(1, 1), V(2, 3), V(3, 6)};96 const Map orig(begin(arr), end(arr), test_less<int>(3));97 Map copy(orig, std::allocator<V>());98 assert(copy.size() == 3);99 assert(copy.key_comp() == test_less<int>(3));100 101 // Check that orig is still what is expected102 assert(orig.size() == 3);103 assert(orig.key_comp() == test_less<int>(3));104 }105}106 107int main(int, char**) {108 test();109 110 return 0;111}112