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-exception
6//
7//===----------------------------------------------------------------------===//
8
9// <vector>
10// vector<bool>
11
12// vector& operator=(const vector& c);
13
14#include <cassert>
15#include <vector>
16
17#include "min_allocator.h"
18#include "test_allocator.h"
19#include "test_macros.h"
20
21TEST_CONSTEXPR_CXX20 void test_copy_assignment(unsigned N) {
22 //
23 // Test with insufficient space where reallocation occurs during assignment
24 //
25 { // POCCA = true_type, thus copy-assign the allocator
26 std::vector<bool, other_allocator<bool> > l(N, true, other_allocator<bool>(5));
27 std::vector<bool, other_allocator<bool> > l2(other_allocator<bool>(3));
28 l2 = l;
29 assert(l2 == l);
30 assert(l2.get_allocator() == other_allocator<bool>(5));
31 }
32 { // POCCA = false_type, thus allocator is unchanged
33 std::vector<bool, test_allocator<bool> > l(N + 64, true, test_allocator<bool>(5));
34 std::vector<bool, test_allocator<bool> > l2(10, false, test_allocator<bool>(3));
35 l2 = l;
36 assert(l2 == l);
37 assert(l2.get_allocator() == test_allocator<bool>(3));
38 }
39 { // Stateless allocator
40 std::vector<bool, min_allocator<bool> > l(N + 64, true, min_allocator<bool>());
41 std::vector<bool, min_allocator<bool> > l2(N / 2, false, min_allocator<bool>());
42 l2 = l;
43 assert(l2 == l);
44 assert(l2.get_allocator() == min_allocator<bool>());
45 }
46
47 //
48 // Test with sufficient size where no reallocation occurs during assignment
49 //
50 { // POCCA = false_type, thus allocator is unchanged
51 std::vector<bool, test_allocator<bool> > l(N, true, test_allocator<bool>(5));
52 std::vector<bool, test_allocator<bool> > l2(N + 64, false, test_allocator<bool>(3));
53 l2 = l;
54 assert(l2 == l);
55 assert(l2.get_allocator() == test_allocator<bool>(3));
56 }
57 { // POCCA = true_type, thus copy-assign the allocator
58 std::vector<bool, other_allocator<bool> > l(N, true, other_allocator<bool>(5));
59 std::vector<bool, other_allocator<bool> > l2(N * 2, false, other_allocator<bool>(3));
60 l2.reserve(5);
61 l2 = l;
62 assert(l2 == l);
63 assert(l2.get_allocator() == other_allocator<bool>(5));
64 }
65}
66
67TEST_CONSTEXPR_CXX20 bool tests() {
68 test_copy_assignment(N: 3);
69 test_copy_assignment(N: 18);
70 test_copy_assignment(N: 33);
71 test_copy_assignment(N: 65);
72 test_copy_assignment(N: 299);
73
74 return true;
75}
76
77int main(int, char**) {
78 tests();
79#if TEST_STD_VER > 17
80 static_assert(tests());
81#endif
82 return 0;
83}
84

source code of libcxx/test/std/containers/sequences/vector.bool/assign_copy.pass.cpp