brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.1 KiB · c747828 Raw
137 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);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 <template <class> class Alloc>25void test_alloc() {26  { // Simple check27    using Set = std::multiset<int, std::less<int>, Alloc<int> >;28 29    int arr[] = {1, 2, 2};30    const Set orig(std::begin(arr), std::end(arr));31    Set copy = orig;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 38    // Check that orig is still what is expected39    assert(orig.size() == 3);40    assert(*std::next(orig.begin(), 0) == 1);41    assert(*std::next(orig.begin(), 1) == 2);42    assert(*std::next(orig.begin(), 2) == 2);43    assert(std::next(orig.begin(), 3) == orig.end());44  }45 46  { // copy empty set47    using Set = std::multiset<int, std::less<int>, Alloc<int> >;48 49    const Set orig;50    Set copy = orig;51    assert(copy.size() == 0);52    assert(copy.begin() == copy.end());53 54    // Check that orig is still what is expected55    assert(orig.size() == 0);56    assert(orig.begin() == orig.end());57  }58 59  { // only some leaf nodes exist60    using Set = std::multiset<int, std::less<int>, Alloc<int> >;61 62    int arr[] = {1, 2, 3, 3, 5};63    const Set orig(std::begin(arr), std::end(arr));64    Set copy = orig;65    assert(copy.size() == 5);66    assert(*std::next(copy.begin(), 0) == 1);67    assert(*std::next(copy.begin(), 1) == 2);68    assert(*std::next(copy.begin(), 2) == 3);69    assert(*std::next(copy.begin(), 3) == 3);70    assert(*std::next(copy.begin(), 4) == 5);71    assert(std::next(copy.begin(), 5) == copy.end());72 73    // Check that orig is still what is expected74    assert(orig.size() == 5);75    assert(*std::next(orig.begin(), 0) == 1);76    assert(*std::next(orig.begin(), 1) == 2);77    assert(*std::next(orig.begin(), 2) == 3);78    assert(*std::next(orig.begin(), 3) == 3);79    assert(*std::next(orig.begin(), 4) == 5);80    assert(std::next(orig.begin(), 5) == orig.end());81  }82}83 84void test() {85  test_alloc<std::allocator>();86  test_alloc<min_allocator>(); // Make sure that fancy pointers work87 88  { // Ensure that the comparator is copied89    using Set = std::multiset<int, test_less<int> >;90 91    int arr[] = {1, 2, 2};92    const Set orig(std::begin(arr), std::end(arr), test_less<int>(3));93    Set copy = orig;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  { // Ensure that the allocator is copied103    using Set = std::multiset<int, std::less<int>, test_allocator<int> >;104 105    int arr[] = {1, 2, 2};106    const Set orig(std::begin(arr), std::end(arr), std::less<int>(), test_allocator<int>(10));107    Set copy = orig;108    assert(copy.size() == 3);109    assert(copy.get_allocator() == test_allocator<int>(10));110 111    // Check that orig is still what is expected112    assert(orig.size() == 3);113    assert(orig.get_allocator() == test_allocator<int>(10));114    assert(orig.get_allocator().get_id() != test_alloc_base::moved_value);115  }116 117  { // Ensure that soccc is handled properly118    using Set = std::multiset<int, std::less<int>, other_allocator<int> >;119 120    int arr[] = {1, 2, 2};121    const Set orig(std::begin(arr), std::end(arr), std::less<int>(), other_allocator<int>(10));122    Set copy = orig;123    assert(copy.size() == 3);124    assert(copy.get_allocator() == other_allocator<int>(-2));125 126    // Check that orig is still what is expected127    assert(orig.size() == 3);128    assert(orig.get_allocator() == other_allocator<int>(10));129  }130}131 132int main(int, char**) {133  test();134 135  return 0;136}137