116 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// UNSUPPORTED: c++0310 11// <vector>12// vector<bool>13 14// vector(vector&& c);15 16#include <array>17#include <cassert>18#include <vector>19 20#include "min_allocator.h"21#include "test_allocator.h"22#include "test_macros.h"23 24template <unsigned N, class A>25TEST_CONSTEXPR_CXX20 void test(const A& a) {26 std::vector<bool, A> v(N, false, a);27 std::vector<bool, A> original(N, false, a);28 for (unsigned i = 1; i < N; i += 2) {29 v[i] = true;30 original[i] = true;31 }32 std::vector<bool, A> v2 = std::move(v);33 assert(v2 == original);34 assert(v.empty()); // The moved-from vector is guaranteed to be empty after move-construction35 assert(v2.get_allocator() == original.get_allocator());36}37 38TEST_CONSTEXPR_CXX20 bool tests() {39 test_allocator_statistics alloc_stats;40 41 // Tests for move constructor with word size up to 5 (i.e., bit size > 256 for 64-bit system)42 {43 using A = std::allocator<bool>;44 test<5>(A());45 test<18>(A());46 test<33>(A());47 test<65>(A());48 test<299>(A());49 }50 {51 using A = other_allocator<bool>;52 test<5>(A(5));53 test<18>(A(5));54 test<33>(A(5));55 test<65>(A(5));56 test<299>(A(5));57 }58 {59 using A = min_allocator<bool>;60 test<5>(A());61 test<18>(A());62 test<33>(A());63 test<65>(A());64 test<299>(A());65 }66 {67 using A = test_allocator<bool>;68 test<5>(A(5, &alloc_stats));69 test<18>(A(5, &alloc_stats));70 test<33>(A(5, &alloc_stats));71 test<65>(A(5, &alloc_stats));72 test<299>(A(5, &alloc_stats));73 }74 75 { // Tests to verify the allocator statistics after move76 alloc_stats.clear();77 using Vect = std::vector<bool, test_allocator<bool> >;78 using AllocT = Vect::allocator_type;79 Vect v(test_allocator<bool>(42, 101, &alloc_stats));80 assert(alloc_stats.count == 1);81 {82 const AllocT& a = v.get_allocator();83 assert(alloc_stats.count == 2);84 assert(a.get_data() == 42);85 assert(a.get_id() == 101);86 }87 assert(alloc_stats.count == 1);88 alloc_stats.clear_ctor_counters();89 90 Vect v2 = std::move(v);91 assert(alloc_stats.count == 2);92 assert(alloc_stats.copied == 0);93 assert(alloc_stats.moved == 1);94 {95 const AllocT& a1 = v.get_allocator();96 assert(a1.get_id() == test_alloc_base::moved_value);97 assert(a1.get_data() == 42);98 99 const AllocT& a2 = v2.get_allocator();100 assert(a2.get_id() == 101);101 assert(a2.get_data() == 42);102 assert(a1 == a2);103 }104 }105 106 return true;107}108 109int main(int, char**) {110 tests();111#if TEST_STD_VER > 17112 static_assert(tests());113#endif114 return 0;115}116