107 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, const allocator_type& a);14 15#include <cassert>16#include <set>17 18#include "../../../test_compare.h"19#include "min_allocator.h"20#include "test_macros.h"21#include "test_allocator.h"22 23template <class Alloc>24void test_alloc(const Alloc& new_alloc) {25 { // Simple check26 using Set = std::set<int, std::less<int>, Alloc>;27 28 int arr[] = {1, 2, 3};29 const Set orig(std::begin(arr), std::end(arr));30 Set copy(orig, new_alloc);31 assert(copy.size() == 3);32 assert(*std::next(copy.begin(), 0) == 1);33 assert(*std::next(copy.begin(), 1) == 2);34 assert(*std::next(copy.begin(), 2) == 3);35 assert(std::next(copy.begin(), 3) == copy.end());36 assert(copy.get_allocator() == new_alloc);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) == 3);43 assert(std::next(orig.begin(), 3) == orig.end());44 }45 46 { // copy empty set47 using Set = std::set<int, std::less<int>, Alloc>;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::set<int, std::less<int>, Alloc>;61 62 int arr[] = {1, 2, 3, 4, 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) == 4);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) == 4);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<int>());86 test_alloc(test_allocator<int>(25)); // Make sure that the new allocator is actually used87 test_alloc(min_allocator<int>()); // Make sure that fancy pointers work88 89 { // Ensure that the comparator is copied90 int arr[] = {1, 2, 3};91 const std::set<int, test_less<int> > orig(std::begin(arr), std::end(arr), test_less<int>(3));92 std::set<int, test_less<int> > copy(orig, std::allocator<int>());93 assert(copy.size() == 3);94 assert(copy.key_comp() == test_less<int>(3));95 96 // Check that orig is still what is expected97 assert(orig.size() == 3);98 assert(orig.key_comp() == test_less<int>(3));99 }100}101 102int main(int, char**) {103 test();104 105 return 0;106}107