197 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// <set>10 11// class multiset12 13// template <class InputIterator>14// void insert(InputIterator first, InputIterator last);15 16#include <array>17#include <cassert>18#include <set>19 20#include "min_allocator.h"21#include "test_iterators.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::multiset<int, std::less<int>, Alloc>;28 29 std::array<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::multiset<int, std::less<int>, Alloc>;38 39 std::array<int, 0> arr;40 41 Map map;42 map.insert(0);43 map.insert(Iter(arr.data()), Iter(arr.data() + arr.size()));44 assert(map.size() == 1);45 assert(*std::next(map.begin(), 0) == 0);46 assert(std::next(map.begin(), 1) == map.end());47 }48 { // With multiple elements in the container49 using Map = std::multiset<int, std::less<int>, Alloc>;50 51 std::array<int, 0> arr;52 53 Map map;54 map.insert(0);55 map.insert(1);56 map.insert(2);57 map.insert(Iter(arr.data()), Iter(arr.data() + arr.size()));58 assert(map.size() == 3);59 assert(*std::next(map.begin(), 0) == 0);60 assert(*std::next(map.begin(), 1) == 1);61 assert(*std::next(map.begin(), 2) == 2);62 assert(std::next(map.begin(), 3) == map.end());63 }64 }65 { // Check that 1 element is inserted correctly66 { // Without elements in the container67 using Map = std::multiset<int, std::less<int>, Alloc>;68 69 int arr[] = {1};70 71 Map map;72 map.insert(Iter(std::begin(arr)), Iter(std::end(arr)));73 assert(map.size() == 1);74 assert(*std::next(map.begin(), 0) == 1);75 assert(std::next(map.begin(), 1) == map.end());76 }77 { // With 1 element in the container - a different key78 using Map = std::multiset<int, std::less<int>, Alloc>;79 80 int arr[] = {1};81 82 Map map;83 map.insert(0);84 map.insert(Iter(std::begin(arr)), Iter(std::end(arr)));85 assert(map.size() == 2);86 assert(*std::next(map.begin(), 0) == 0);87 assert(*std::next(map.begin(), 1) == 1);88 assert(std::next(map.begin(), 2) == map.end());89 }90 { // With 1 element in the container - the same key91 using Map = std::multiset<int, std::less<int>, Alloc>;92 93 int arr[] = {1};94 95 Map map;96 map.insert(1);97 map.insert(Iter(std::begin(arr)), Iter(std::end(arr)));98 assert(map.size() == 2);99 assert(*std::next(map.begin(), 0) == 1);100 assert(*std::next(map.begin(), 1) == 1);101 assert(std::next(map.begin(), 2) == map.end());102 }103 { // With multiple elements in the container104 using Map = std::multiset<int, std::less<int>, Alloc>;105 106 int arr[] = {1};107 108 Map map;109 map.insert(0);110 map.insert(1);111 map.insert(2);112 map.insert(Iter(std::begin(arr)), Iter(std::end(arr)));113 assert(map.size() == 4);114 assert(*std::next(map.begin(), 0) == 0);115 assert(*std::next(map.begin(), 1) == 1);116 assert(*std::next(map.begin(), 2) == 1);117 assert(*std::next(map.begin(), 3) == 2);118 assert(std::next(map.begin(), 4) == map.end());119 }120 }121 { // Check that multiple elements are inserted correctly122 { // Without elements in the container123 using Map = std::multiset<int, std::less<int>, Alloc>;124 125 int arr[] = {1, 1, 3};126 127 Map map;128 map.insert(Iter(std::begin(arr)), Iter(std::end(arr)));129 assert(map.size() == 3);130 assert(*std::next(map.begin(), 0) == 1);131 assert(*std::next(map.begin(), 1) == 1);132 assert(*std::next(map.begin(), 2) == 3);133 assert(std::next(map.begin(), 3) == map.end());134 }135 { // With 1 element in the container - a different key136 using Map = std::multiset<int, std::less<int>, Alloc>;137 138 int arr[] = {1, 1, 3};139 140 Map map;141 map.insert(0);142 map.insert(Iter(std::begin(arr)), Iter(std::end(arr)));143 assert(map.size() == 4);144 assert(*std::next(map.begin(), 0) == 0);145 assert(*std::next(map.begin(), 1) == 1);146 assert(*std::next(map.begin(), 2) == 1);147 assert(*std::next(map.begin(), 3) == 3);148 assert(std::next(map.begin(), 4) == map.end());149 }150 { // With 1 element in the container - the same key151 using Map = std::multiset<int, std::less<int>, Alloc>;152 153 int arr[] = {1, 2, 3};154 155 Map map;156 map.insert(1);157 map.insert(Iter(std::begin(arr)), Iter(std::end(arr)));158 assert(map.size() == 4);159 assert(*std::next(map.begin(), 0) == 1);160 assert(*std::next(map.begin(), 1) == 1);161 assert(*std::next(map.begin(), 2) == 2);162 assert(*std::next(map.begin(), 3) == 3);163 assert(std::next(map.begin(), 4) == map.end());164 }165 { // With multiple elements in the container166 using Map = std::multiset<int, std::less<int>, Alloc>;167 168 int arr[] = {1, 3, 4};169 170 Map map;171 map.insert(0);172 map.insert(1);173 map.insert(2);174 map.insert(Iter(std::begin(arr)), Iter(std::end(arr)));175 assert(map.size() == 6);176 assert(*std::next(map.begin(), 0) == 0);177 assert(*std::next(map.begin(), 1) == 1);178 assert(*std::next(map.begin(), 2) == 1);179 assert(*std::next(map.begin(), 3) == 2);180 assert(*std::next(map.begin(), 4) == 3);181 assert(*std::next(map.begin(), 5) == 4);182 assert(std::next(map.begin(), 6) == map.end());183 }184 }185}186 187void test() {188 test_alloc<cpp17_input_iterator<int*>, std::allocator<int> >();189 test_alloc<cpp17_input_iterator<int*>, min_allocator<int> >();190}191 192int main(int, char**) {193 test();194 195 return 0;196}197