201 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// template <class InputIterator>14// void insert(InputIterator first, InputIterator last);15 16#include <array>17#include <cassert>18#include <map>19 20#include "test_iterators.h"21#include "min_allocator.h"22 23template <class Iter, class Alloc>24void test_alloc() {25 { // Check that an empty range works correctly26 { // Without elements in the container27 using Map = std::map<int, int, std::less<int>, Alloc>;28 29 std::array<std::pair<const int, int>, 0> arr;30 31 Map map;32 map.insert(Iter(arr.data()), Iter(arr.data() + arr.size()));33 assert(map.size() == 0);34 assert(map.begin() == map.end());35 }36 { // With 1 element in the container37 using Map = std::map<int, int, std::less<int>, Alloc>;38 using Pair = std::pair<const int, int>;39 40 std::array<Pair, 0> arr;41 42 Map map;43 map.insert(Pair(0, 0));44 map.insert(Iter(arr.data()), Iter(arr.data() + arr.size()));45 assert(map.size() == 1);46 assert(*std::next(map.begin(), 0) == Pair(0, 0));47 assert(std::next(map.begin(), 1) == map.end());48 }49 { // With multiple elements in the container50 using Map = std::map<int, int, std::less<int>, Alloc>;51 using Pair = std::pair<const int, int>;52 53 std::array<Pair, 0> arr;54 55 Map map;56 map.insert(Pair(0, 0));57 map.insert(Pair(1, 1));58 map.insert(Pair(2, 2));59 map.insert(Iter(arr.data()), Iter(arr.data() + arr.size()));60 assert(map.size() == 3);61 assert(*std::next(map.begin(), 0) == Pair(0, 0));62 assert(*std::next(map.begin(), 1) == Pair(1, 1));63 assert(*std::next(map.begin(), 2) == Pair(2, 2));64 assert(std::next(map.begin(), 3) == map.end());65 }66 }67 { // Check that 1 element is inserted correctly68 { // Without elements in the container69 using Map = std::map<int, int, std::less<int>, Alloc>;70 using Pair = std::pair<const int, int>;71 72 Pair arr[] = {Pair(1, 1)};73 74 Map map;75 map.insert(Iter(std::begin(arr)), Iter(std::end(arr)));76 assert(map.size() == 1);77 assert(*std::next(map.begin(), 0) == Pair(1, 1));78 assert(std::next(map.begin(), 1) == map.end());79 }80 { // With 1 element in the container - a different key81 using Map = std::map<int, int, std::less<int>, Alloc>;82 using Pair = std::pair<const int, int>;83 84 Pair arr[] = {Pair(1, 1)};85 86 Map map;87 map.insert(Pair(0, 0));88 map.insert(Iter(std::begin(arr)), Iter(std::end(arr)));89 assert(map.size() == 2);90 assert(*std::next(map.begin(), 0) == Pair(0, 0));91 assert(*std::next(map.begin(), 1) == Pair(1, 1));92 assert(std::next(map.begin(), 2) == map.end());93 }94 { // With 1 element in the container - the same key95 using Map = std::map<int, int, std::less<int>, Alloc>;96 using Pair = std::pair<const int, int>;97 98 Pair arr[] = {Pair(1, 1)};99 100 Map map;101 map.insert(Pair(1, 2));102 map.insert(Iter(std::begin(arr)), Iter(std::end(arr)));103 assert(map.size() == 1);104 assert(*std::next(map.begin(), 0) == Pair(1, 2));105 assert(std::next(map.begin(), 1) == map.end());106 }107 { // With multiple elements in the container108 using Map = std::map<int, int, std::less<int>, Alloc>;109 using Pair = std::pair<const int, int>;110 111 Pair arr[] = {Pair(1, 1)};112 113 Map map;114 map.insert(Pair(0, 0));115 map.insert(Pair(1, 1));116 map.insert(Pair(2, 2));117 map.insert(Iter(std::begin(arr)), Iter(std::end(arr)));118 assert(map.size() == 3);119 assert(*std::next(map.begin(), 0) == Pair(0, 0));120 assert(*std::next(map.begin(), 1) == Pair(1, 1));121 assert(*std::next(map.begin(), 2) == Pair(2, 2));122 assert(std::next(map.begin(), 3) == map.end());123 }124 }125 { // Check that multiple elements are inserted correctly126 { // Without elements in the container127 using Map = std::map<int, int, std::less<int>, Alloc>;128 using Pair = std::pair<const int, int>;129 130 Pair arr[] = {Pair(1, 1), Pair(1, 1), Pair(3, 3)};131 132 Map map;133 map.insert(Iter(std::begin(arr)), Iter(std::end(arr)));134 assert(map.size() == 2);135 assert(*std::next(map.begin(), 0) == Pair(1, 1));136 assert(*std::next(map.begin(), 1) == Pair(3, 3));137 assert(std::next(map.begin(), 2) == map.end());138 }139 { // With 1 element in the container - a different key140 using Map = std::map<int, int, std::less<int>, Alloc>;141 using Pair = std::pair<const int, int>;142 143 Pair arr[] = {Pair(1, 1), Pair(1, 1), Pair(3, 3)};144 145 Map map;146 map.insert(Pair(0, 0));147 map.insert(Iter(std::begin(arr)), Iter(std::end(arr)));148 assert(map.size() == 3);149 assert(*std::next(map.begin(), 0) == Pair(0, 0));150 assert(*std::next(map.begin(), 1) == Pair(1, 1));151 assert(*std::next(map.begin(), 2) == Pair(3, 3));152 assert(std::next(map.begin(), 3) == map.end());153 }154 { // With 1 element in the container - the same key155 using Map = std::map<int, int, std::less<int>, Alloc>;156 using Pair = std::pair<const int, int>;157 158 Pair arr[] = {Pair(1, 1), Pair(2, 2), Pair(3, 3)};159 160 Map map;161 map.insert(Pair(1, 1));162 map.insert(Iter(std::begin(arr)), Iter(std::end(arr)));163 assert(map.size() == 3);164 assert(*std::next(map.begin(), 0) == Pair(1, 1));165 assert(*std::next(map.begin(), 1) == Pair(2, 2));166 assert(*std::next(map.begin(), 2) == Pair(3, 3));167 assert(std::next(map.begin(), 3) == map.end());168 }169 { // With multiple elements in the container170 using Map = std::map<int, int, std::less<int>, Alloc>;171 using Pair = std::pair<const int, int>;172 173 Pair arr[] = {Pair(1, 1), Pair(3, 3), Pair(4, 4)};174 175 Map map;176 map.insert(Pair(0, 0));177 map.insert(Pair(1, 1));178 map.insert(Pair(2, 2));179 map.insert(Iter(std::begin(arr)), Iter(std::end(arr)));180 assert(map.size() == 5);181 assert(*std::next(map.begin(), 0) == Pair(0, 0));182 assert(*std::next(map.begin(), 1) == Pair(1, 1));183 assert(*std::next(map.begin(), 2) == Pair(2, 2));184 assert(*std::next(map.begin(), 3) == Pair(3, 3));185 assert(*std::next(map.begin(), 4) == Pair(4, 4));186 assert(std::next(map.begin(), 5) == map.end());187 }188 }189}190 191void test() {192 test_alloc<cpp17_input_iterator<std::pair<const int, int>*>, std::allocator<std::pair<const int, int> > >();193 test_alloc<cpp17_input_iterator<std::pair<const int, int>*>, min_allocator<std::pair<const int, int> > >();194}195 196int main(int, char**) {197 test();198 199 return 0;200}201