brintos

brintos / llvm-project-archived public Read only

0
0
Text · 7.2 KiB · 3f145cc Raw
208 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// template <class InputIterator>14//   void insert(InputIterator first, InputIterator last);15 16#include <array>17#include <cassert>18#include <map>19 20#include "min_allocator.h"21#include "test_iterators.h"22#include "test_macros.h"23 24template <class Iter, class Alloc>25void test_alloc() {26  {   // Check that an empty range works correctly27    { // Without elements in the container28      using Map = std::multimap<int, int, std::less<int>, Alloc>;29 30      std::array<std::pair<const int, int>, 0> arr;31 32      Map map;33      map.insert(Iter(arr.data()), Iter(arr.data() + arr.size()));34      assert(map.size() == 0);35      assert(map.begin() == map.end());36    }37    { // With 1 element in the container38      using Map  = std::multimap<int, int, std::less<int>, Alloc>;39      using Pair = std::pair<const int, int>;40 41      std::array<Pair, 0> arr;42 43      Map map;44      map.insert(Pair(0, 0));45      map.insert(Iter(arr.data()), Iter(arr.data() + arr.size()));46      assert(map.size() == 1);47      assert(*std::next(map.begin(), 0) == Pair(0, 0));48      assert(std::next(map.begin(), 1) == map.end());49    }50    { // With multiple elements in the container51      using Map  = std::multimap<int, int, std::less<int>, Alloc>;52      using Pair = std::pair<const int, int>;53 54      std::array<Pair, 0> arr;55 56      Map map;57      map.insert(Pair(0, 0));58      map.insert(Pair(1, 1));59      map.insert(Pair(2, 2));60      map.insert(Iter(arr.data()), Iter(arr.data() + arr.size()));61      assert(map.size() == 3);62      assert(*std::next(map.begin(), 0) == Pair(0, 0));63      assert(*std::next(map.begin(), 1) == Pair(1, 1));64      assert(*std::next(map.begin(), 2) == Pair(2, 2));65      assert(std::next(map.begin(), 3) == map.end());66    }67  }68  {   // Check that 1 element is inserted correctly69    { // Without elements in the container70      using Map  = std::multimap<int, int, std::less<int>, Alloc>;71      using Pair = std::pair<const int, int>;72 73      Pair arr[] = {Pair(1, 1)};74 75      Map map;76      map.insert(Iter(std::begin(arr)), Iter(std::end(arr)));77      assert(map.size() == 1);78      assert(*std::next(map.begin(), 0) == Pair(1, 1));79      assert(std::next(map.begin(), 1) == map.end());80    }81    { // With 1 element in the container - a different key82      using Map  = std::multimap<int, int, std::less<int>, Alloc>;83      using Pair = std::pair<const int, int>;84 85      Pair arr[] = {Pair(1, 1)};86 87      Map map;88      map.insert(Pair(0, 0));89      map.insert(Iter(std::begin(arr)), Iter(std::end(arr)));90      assert(map.size() == 2);91      assert(*std::next(map.begin(), 0) == Pair(0, 0));92      assert(*std::next(map.begin(), 1) == Pair(1, 1));93      assert(std::next(map.begin(), 2) == map.end());94    }95    { // With 1 element in the container - the same key96      using Map  = std::multimap<int, int, std::less<int>, Alloc>;97      using Pair = std::pair<const int, int>;98 99      Pair arr[] = {Pair(1, 1)};100 101      Map map;102      map.insert(Pair(1, 1));103      map.insert(Iter(std::begin(arr)), Iter(std::end(arr)));104      assert(map.size() == 2);105      assert(*std::next(map.begin(), 0) == Pair(1, 1));106      assert(*std::next(map.begin(), 1) == Pair(1, 1));107      assert(std::next(map.begin(), 2) == map.end());108    }109    { // With multiple elements in the container110      using Map  = std::multimap<int, int, std::less<int>, Alloc>;111      using Pair = std::pair<const int, int>;112 113      Pair arr[] = {Pair(1, 1)};114 115      Map map;116      map.insert(Pair(0, 0));117      map.insert(Pair(1, 1));118      map.insert(Pair(2, 2));119      map.insert(Iter(std::begin(arr)), Iter(std::end(arr)));120      assert(map.size() == 4);121      assert(*std::next(map.begin(), 0) == Pair(0, 0));122      assert(*std::next(map.begin(), 1) == Pair(1, 1));123      assert(*std::next(map.begin(), 2) == Pair(1, 1));124      assert(*std::next(map.begin(), 3) == Pair(2, 2));125      assert(std::next(map.begin(), 4) == map.end());126    }127  }128  {   // Check that multiple elements are inserted correctly129    { // Without elements in the container130      using Map  = std::multimap<int, int, std::less<int>, Alloc>;131      using Pair = std::pair<const int, int>;132 133      Pair arr[] = {Pair(1, 1), Pair(1, 1), Pair(3, 3)};134 135      Map map;136      map.insert(Iter(std::begin(arr)), Iter(std::end(arr)));137      assert(map.size() == 3);138      assert(*std::next(map.begin(), 0) == Pair(1, 1));139      assert(*std::next(map.begin(), 1) == Pair(1, 1));140      assert(*std::next(map.begin(), 2) == Pair(3, 3));141      assert(std::next(map.begin(), 3) == map.end());142    }143    { // With 1 element in the container - a different key144      using Map  = std::multimap<int, int, std::less<int>, Alloc>;145      using Pair = std::pair<const int, int>;146 147      Pair arr[] = {Pair(1, 1), Pair(1, 1), Pair(3, 3)};148 149      Map map;150      map.insert(Pair(0, 0));151      map.insert(Iter(std::begin(arr)), Iter(std::end(arr)));152      assert(map.size() == 4);153      assert(*std::next(map.begin(), 0) == Pair(0, 0));154      assert(*std::next(map.begin(), 1) == Pair(1, 1));155      assert(*std::next(map.begin(), 2) == Pair(1, 1));156      assert(*std::next(map.begin(), 3) == Pair(3, 3));157      assert(std::next(map.begin(), 4) == map.end());158    }159    { // With 1 element in the container - the same key160      using Map  = std::multimap<int, int, std::less<int>, Alloc>;161      using Pair = std::pair<const int, int>;162 163      Pair arr[] = {Pair(1, 1), Pair(2, 2), Pair(3, 3)};164 165      Map map;166      map.insert(Pair(1, 1));167      map.insert(Iter(std::begin(arr)), Iter(std::end(arr)));168      assert(map.size() == 4);169      assert(*std::next(map.begin(), 0) == Pair(1, 1));170      assert(*std::next(map.begin(), 1) == Pair(1, 1));171      assert(*std::next(map.begin(), 2) == Pair(2, 2));172      assert(*std::next(map.begin(), 3) == Pair(3, 3));173      assert(std::next(map.begin(), 4) == map.end());174    }175    { // With multiple elements in the container176      using Map  = std::multimap<int, int, std::less<int>, Alloc>;177      using Pair = std::pair<const int, int>;178 179      Pair arr[] = {Pair(1, 1), Pair(3, 3), Pair(4, 4)};180 181      Map map;182      map.insert(Pair(0, 0));183      map.insert(Pair(1, 1));184      map.insert(Pair(2, 2));185      map.insert(Iter(std::begin(arr)), Iter(std::end(arr)));186      assert(map.size() == 6);187      assert(*std::next(map.begin(), 0) == Pair(0, 0));188      assert(*std::next(map.begin(), 1) == Pair(1, 1));189      assert(*std::next(map.begin(), 2) == Pair(1, 1));190      assert(*std::next(map.begin(), 3) == Pair(2, 2));191      assert(*std::next(map.begin(), 4) == Pair(3, 3));192      assert(*std::next(map.begin(), 5) == Pair(4, 4));193      assert(std::next(map.begin(), 6) == map.end());194    }195  }196}197 198void test() {199  test_alloc<cpp17_input_iterator<std::pair<const int, int>*>, std::allocator<std::pair<const int, int> > >();200  test_alloc<cpp17_input_iterator<std::pair<const int, int>*>, min_allocator<std::pair<const int, int> > >();201}202 203int main(int, char**) {204  test();205 206  return 0;207}208