brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.1 KiB · 8fac335 Raw
135 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 set12 13// set(const set& m);14 15#include <cassert>16#include <set>17 18#include "min_allocator.h"19#include "../../../test_compare.h"20#include "test_allocator.h"21 22template <template <class> class Alloc>23void test_alloc() {24  { // Simple check25    using Set = std::set<int, std::less<int>, Alloc<int> >;26 27    int arr[] = {1, 2, 3};28    const Set orig(std::begin(arr), std::end(arr));29    Set copy = orig;30    assert(copy.size() == 3);31    assert(*std::next(copy.begin(), 0) == 1);32    assert(*std::next(copy.begin(), 1) == 2);33    assert(*std::next(copy.begin(), 2) == 3);34    assert(std::next(copy.begin(), 3) == copy.end());35 36    // Check that orig is still what is expected37    assert(orig.size() == 3);38    assert(*std::next(orig.begin(), 0) == 1);39    assert(*std::next(orig.begin(), 1) == 2);40    assert(*std::next(orig.begin(), 2) == 3);41    assert(std::next(orig.begin(), 3) == orig.end());42  }43 44  { // copy empty set45    using Set = std::set<int, std::less<int>, Alloc<int> >;46 47    const Set orig;48    Set copy = orig;49    assert(copy.size() == 0);50    assert(copy.begin() == copy.end());51 52    // Check that orig is still what is expected53    assert(orig.size() == 0);54    assert(orig.begin() == orig.end());55  }56 57  { // only some leaf nodes exist58    using Set = std::set<int, std::less<int>, Alloc<int> >;59 60    int arr[] = {1, 2, 3, 4, 5};61    const Set orig(std::begin(arr), std::end(arr));62    Set copy = orig;63    assert(copy.size() == 5);64    assert(*std::next(copy.begin(), 0) == 1);65    assert(*std::next(copy.begin(), 1) == 2);66    assert(*std::next(copy.begin(), 2) == 3);67    assert(*std::next(copy.begin(), 3) == 4);68    assert(*std::next(copy.begin(), 4) == 5);69    assert(std::next(copy.begin(), 5) == copy.end());70 71    // Check that orig is still what is expected72    assert(orig.size() == 5);73    assert(*std::next(orig.begin(), 0) == 1);74    assert(*std::next(orig.begin(), 1) == 2);75    assert(*std::next(orig.begin(), 2) == 3);76    assert(*std::next(orig.begin(), 3) == 4);77    assert(*std::next(orig.begin(), 4) == 5);78    assert(std::next(orig.begin(), 5) == orig.end());79  }80}81 82void test() {83  test_alloc<std::allocator>();84  test_alloc<min_allocator>(); // Make sure that fancy pointers work85 86  { // Ensure that the comparator is copied87    using Set = std::set<int, test_less<int> >;88 89    int arr[] = {1, 2, 3};90    const Set orig(std::begin(arr), std::end(arr), test_less<int>(3));91    Set copy = orig;92    assert(copy.size() == 3);93    assert(copy.key_comp() == test_less<int>(3));94 95    // Check that orig is still what is expected96    assert(orig.size() == 3);97    assert(orig.key_comp() == test_less<int>(3));98  }99 100  { // Ensure that the allocator is copied101    using Set = std::set<int, std::less<int>, test_allocator<int> >;102 103    int arr[] = {1, 2, 3};104    const Set orig(std::begin(arr), std::end(arr), std::less<int>(), test_allocator<int>(10));105    Set copy = orig;106    assert(copy.size() == 3);107    assert(copy.get_allocator() == test_allocator<int>(10));108 109    // Check that orig is still what is expected110    assert(orig.size() == 3);111    assert(orig.get_allocator() == test_allocator<int>(10));112    assert(orig.get_allocator().get_id() != test_alloc_base::moved_value);113  }114 115  { // Ensure that soccc is handled properly116    using Set = std::set<int, std::less<int>, other_allocator<int> >;117 118    int arr[] = {1, 2, 3};119    const Set orig(std::begin(arr), std::end(arr), std::less<int>(), other_allocator<int>(10));120    Set copy = orig;121    assert(copy.size() == 3);122    assert(copy.get_allocator() == other_allocator<int>(-2));123 124    // Check that orig is still what is expected125    assert(orig.size() == 3);126    assert(orig.get_allocator() == other_allocator<int>(10));127  }128}129 130int main(int, char**) {131  test();132 133  return 0;134}135