|
11 | 11 | // void flip();
|
12 | 12 |
|
13 | 13 | #include <cassert>
|
| 14 | +#include <memory> |
14 | 15 | #include <vector>
|
15 | 16 |
|
16 | 17 | #include "min_allocator.h"
|
17 | 18 | #include "test_allocator.h"
|
18 | 19 | #include "test_macros.h"
|
19 | 20 |
|
| 21 | +template <typename Allocator = std::allocator<bool> > |
| 22 | +TEST_CONSTEXPR_CXX20 void test_small_vector_flip(Allocator a) { |
| 23 | + bool b[] = {true, false, true}; |
| 24 | + std::vector<bool, Allocator> v(b, b + 3, a); |
| 25 | + v.flip(); |
| 26 | + assert(!v[0] && v[1] && !v[2]); |
| 27 | +} |
| 28 | + |
| 29 | +template <typename Allocator = std::allocator<bool> > |
| 30 | +TEST_CONSTEXPR_CXX20 void test_large_vector_flip(Allocator a) { |
| 31 | + std::vector<bool, Allocator > v(1000, false, a); |
| 32 | + for (std::size_t i = 0; i < v.size(); ++i) |
| 33 | + v[i] = i & 1; |
| 34 | + std::vector<bool, Allocator > original = v; |
| 35 | + v.flip(); |
| 36 | + for (size_t i = 0; i < v.size(); ++i) |
| 37 | + assert(v[i] == !original[i]); |
| 38 | + v.flip(); |
| 39 | + assert(v == original); |
| 40 | +} |
| 41 | + |
20 | 42 | TEST_CONSTEXPR_CXX20 bool tests() {
|
21 |
| - // |
22 |
| - // Testing flip() function with small vectors and various allocators |
23 |
| - // |
24 |
| - { |
25 |
| - std::vector<bool> v; |
26 |
| - v.push_back(true); |
27 |
| - v.push_back(false); |
28 |
| - v.push_back(true); |
29 |
| - v.flip(); |
30 |
| - assert(!v[0]); |
31 |
| - assert(v[1]); |
32 |
| - assert(!v[2]); |
33 |
| - } |
34 |
| - { |
35 |
| - std::vector<bool, min_allocator<bool> > v; |
36 |
| - v.push_back(true); |
37 |
| - v.push_back(false); |
38 |
| - v.push_back(true); |
39 |
| - v.flip(); |
40 |
| - assert(!v[0]); |
41 |
| - assert(v[1]); |
42 |
| - assert(!v[2]); |
43 |
| - } |
44 |
| - { |
45 |
| - std::vector<bool, test_allocator<bool> > v(test_allocator<bool>(5)); |
46 |
| - v.push_back(true); |
47 |
| - v.push_back(false); |
48 |
| - v.push_back(true); |
49 |
| - v.flip(); |
50 |
| - assert(!v[0]); |
51 |
| - assert(v[1]); |
52 |
| - assert(!v[2]); |
53 |
| - } |
| 43 | + // Test small vectors with different allocators |
| 44 | + test_small_vector_flip(std::allocator<bool>()); |
| 45 | + test_small_vector_flip(min_allocator<bool>()); |
| 46 | + test_small_vector_flip(test_allocator<bool>(5)); |
54 | 47 |
|
55 |
| - // |
56 |
| - // Testing flip() function with larger vectors |
57 |
| - // |
58 |
| - { |
59 |
| - std::vector<bool> v(1000); |
60 |
| - for (std::size_t i = 0; i < v.size(); ++i) |
61 |
| - v[i] = i & 1; |
62 |
| - std::vector<bool> original = v; |
63 |
| - v.flip(); |
64 |
| - for (size_t i = 0; i < v.size(); ++i) { |
65 |
| - assert(v[i] == !original[i]); |
66 |
| - } |
67 |
| - } |
68 |
| - { |
69 |
| - std::vector<bool, min_allocator<bool> > v(1000, false, min_allocator<bool>()); |
70 |
| - for (std::size_t i = 0; i < v.size(); ++i) |
71 |
| - v[i] = i & 1; |
72 |
| - std::vector<bool, min_allocator<bool> > original = v; |
73 |
| - v.flip(); |
74 |
| - for (size_t i = 0; i < v.size(); ++i) |
75 |
| - assert(v[i] == !original[i]); |
76 |
| - v.flip(); |
77 |
| - assert(v == original); |
78 |
| - } |
79 |
| - { |
80 |
| - std::vector<bool, test_allocator<bool> > v(1000, false, test_allocator<bool>(5)); |
81 |
| - for (std::size_t i = 0; i < v.size(); ++i) |
82 |
| - v[i] = i & 1; |
83 |
| - std::vector<bool, test_allocator<bool> > original = v; |
84 |
| - v.flip(); |
85 |
| - for (size_t i = 0; i < v.size(); ++i) |
86 |
| - assert(v[i] == !original[i]); |
87 |
| - v.flip(); |
88 |
| - assert(v == original); |
89 |
| - } |
| 48 | + // Test large vectors with different allocators |
| 49 | + test_large_vector_flip(std::allocator<bool>()); |
| 50 | + test_large_vector_flip(min_allocator<bool>()); |
| 51 | + test_large_vector_flip(test_allocator<bool>(5)); |
90 | 52 |
|
91 | 53 | return true;
|
92 | 54 | }
|
93 | 55 |
|
94 | 56 | int main(int, char**) {
|
95 | 57 | tests();
|
96 |
| -#if TEST_STD_VER > 17 |
| 58 | +#if TEST_STD_VER >= 20 |
97 | 59 | static_assert(tests());
|
98 | 60 | #endif
|
99 | 61 | return 0;
|
|
0 commit comments