108 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// multiset(const multiset& m, const allocator_type& a);14 15#include <set>16#include <cassert>17#include <iterator>18 19#include "min_allocator.h"20#include "test_macros.h"21#include "../../../test_compare.h"22#include "test_allocator.h"23 24template <class Alloc>25void test_alloc(const Alloc& new_alloc) {26 { // Simple check27 using Set = std::multiset<int, std::less<int>, Alloc>;28 29 int arr[] = {1, 2, 2};30 const Set orig(std::begin(arr), std::end(arr));31 Set copy(orig, new_alloc);32 assert(copy.size() == 3);33 assert(*std::next(copy.begin(), 0) == 1);34 assert(*std::next(copy.begin(), 1) == 2);35 assert(*std::next(copy.begin(), 2) == 2);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) == 1);42 assert(*std::next(orig.begin(), 1) == 2);43 assert(*std::next(orig.begin(), 2) == 2);44 assert(std::next(orig.begin(), 3) == orig.end());45 }46 47 { // copy empty set48 using Set = std::multiset<int, std::less<int>, Alloc>;49 50 const Set orig;51 Set 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 Set = std::multiset<int, std::less<int>, Alloc>;62 63 int arr[] = {1, 2, 3, 3, 5};64 const Set orig(std::begin(arr), std::end(arr));65 Set copy = orig;66 assert(copy.size() == 5);67 assert(*std::next(copy.begin(), 0) == 1);68 assert(*std::next(copy.begin(), 1) == 2);69 assert(*std::next(copy.begin(), 2) == 3);70 assert(*std::next(copy.begin(), 3) == 3);71 assert(*std::next(copy.begin(), 4) == 5);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) == 1);77 assert(*std::next(orig.begin(), 1) == 2);78 assert(*std::next(orig.begin(), 2) == 3);79 assert(*std::next(orig.begin(), 3) == 3);80 assert(*std::next(orig.begin(), 4) == 5);81 assert(std::next(orig.begin(), 5) == orig.end());82 }83}84 85void test() {86 test_alloc(std::allocator<int>());87 test_alloc(test_allocator<int>(25)); // Make sure that the new allocator is actually used88 test_alloc(min_allocator<int>()); // Make sure that fancy pointers work89 90 { // Ensure that the comparator is copied91 int arr[] = {1, 2, 2};92 const std::multiset<int, test_less<int> > orig(std::begin(arr), std::end(arr), test_less<int>(3));93 std::multiset<int, test_less<int> > copy(orig, std::allocator<int>());94 assert(copy.size() == 3);95 assert(copy.key_comp() == test_less<int>(3));96 97 // Check that orig is still what is expected98 assert(orig.size() == 3);99 assert(orig.key_comp() == test_less<int>(3));100 }101}102 103int main(int, char**) {104 test();105 106 return 0;107}108