|
15 | 15 |
|
16 | 16 | #include <algorithm> |
17 | 17 | #include <cassert> |
| 18 | +#include <vector> |
18 | 19 |
|
| 20 | +#include "sized_allocator.h" |
19 | 21 | #include "test_macros.h" |
20 | 22 | #include "test_iterators.h" |
21 | 23 | #include "user_defined_integral.h" |
22 | 24 |
|
23 | 25 | #if TEST_STD_VER > 17 |
24 | 26 | TEST_CONSTEXPR bool test_constexpr() { |
25 | | - const std::size_t N = 5; |
26 | | - int ib[] = {0, 0, 0, 0, 0, 0}; // one bigger than N |
27 | | - |
28 | | - auto it = std::fill_n(std::begin(ib), N, 5); |
29 | | - return it == (std::begin(ib) + N) |
30 | | - && std::all_of(std::begin(ib), it, [](int a) {return a == 5; }) |
31 | | - && *it == 0 // don't overwrite the last value in the output array |
32 | | - ; |
33 | | - } |
| 27 | + const std::size_t N = 5; |
| 28 | + int ib[] = {0, 0, 0, 0, 0, 0}; // one bigger than N |
| 29 | + |
| 30 | + auto it = std::fill_n(std::begin(ib), N, 5); |
| 31 | + return it == (std::begin(ib) + N) && std::all_of(std::begin(ib), it, [](int a) { return a == 5; }) && |
| 32 | + *it == 0 // don't overwrite the last value in the output array |
| 33 | + ; |
| 34 | +} |
34 | 35 | #endif |
35 | 36 |
|
36 | 37 | typedef UserDefinedIntegral<unsigned> UDI; |
37 | 38 |
|
38 | 39 | template <class Iter> |
39 | | -void |
40 | | -test_char() |
41 | | -{ |
42 | | - char a[4] = {}; |
43 | | - Iter it = std::fill_n(Iter(a), UDI(4), char(1)); |
44 | | - assert(base(it) == a + 4); |
45 | | - assert(a[0] == 1); |
46 | | - assert(a[1] == 1); |
47 | | - assert(a[2] == 1); |
48 | | - assert(a[3] == 1); |
| 40 | +void test_char() { |
| 41 | + char a[4] = {}; |
| 42 | + Iter it = std::fill_n(Iter(a), UDI(4), char(1)); |
| 43 | + assert(base(it) == a + 4); |
| 44 | + assert(a[0] == 1); |
| 45 | + assert(a[1] == 1); |
| 46 | + assert(a[2] == 1); |
| 47 | + assert(a[3] == 1); |
49 | 48 | } |
50 | 49 |
|
51 | 50 | template <class Iter> |
52 | | -void |
53 | | -test_int() |
54 | | -{ |
55 | | - int a[4] = {}; |
56 | | - Iter it = std::fill_n(Iter(a), UDI(4), 1); |
57 | | - assert(base(it) == a + 4); |
58 | | - assert(a[0] == 1); |
59 | | - assert(a[1] == 1); |
60 | | - assert(a[2] == 1); |
61 | | - assert(a[3] == 1); |
| 51 | +void test_int() { |
| 52 | + int a[4] = {}; |
| 53 | + Iter it = std::fill_n(Iter(a), UDI(4), 1); |
| 54 | + assert(base(it) == a + 4); |
| 55 | + assert(a[0] == 1); |
| 56 | + assert(a[1] == 1); |
| 57 | + assert(a[2] == 1); |
| 58 | + assert(a[3] == 1); |
62 | 59 | } |
63 | 60 |
|
64 | | -void |
65 | | -test_int_array() |
66 | | -{ |
67 | | - int a[4] = {}; |
68 | | - assert(std::fill_n(a, UDI(4), static_cast<char>(1)) == a + 4); |
69 | | - assert(a[0] == 1); |
70 | | - assert(a[1] == 1); |
71 | | - assert(a[2] == 1); |
72 | | - assert(a[3] == 1); |
| 61 | +void test_int_array() { |
| 62 | + int a[4] = {}; |
| 63 | + assert(std::fill_n(a, UDI(4), static_cast<char>(1)) == a + 4); |
| 64 | + assert(a[0] == 1); |
| 65 | + assert(a[1] == 1); |
| 66 | + assert(a[2] == 1); |
| 67 | + assert(a[3] == 1); |
73 | 68 | } |
74 | 69 |
|
75 | 70 | struct source { |
76 | | - source() : i(0) { } |
| 71 | + source() : i(0) {} |
77 | 72 |
|
78 | | - operator int() const { return i++; } |
79 | | - mutable int i; |
| 73 | + operator int() const { return i++; } |
| 74 | + mutable int i; |
80 | 75 | }; |
81 | 76 |
|
82 | | -void |
83 | | -test_int_array_struct_source() |
84 | | -{ |
85 | | - int a[4] = {}; |
86 | | - assert(std::fill_n(a, UDI(4), source()) == a + 4); |
87 | | - assert(a[0] == 0); |
88 | | - assert(a[1] == 1); |
89 | | - assert(a[2] == 2); |
90 | | - assert(a[3] == 3); |
| 77 | +void test_int_array_struct_source() { |
| 78 | + int a[4] = {}; |
| 79 | + assert(std::fill_n(a, UDI(4), source()) == a + 4); |
| 80 | + assert(a[0] == 0); |
| 81 | + assert(a[1] == 1); |
| 82 | + assert(a[2] == 2); |
| 83 | + assert(a[3] == 3); |
91 | 84 | } |
92 | 85 |
|
93 | 86 | struct test1 { |
94 | | - test1() : c(0) { } |
95 | | - test1(char xc) : c(xc + 1) { } |
96 | | - char c; |
| 87 | + test1() : c(0) {} |
| 88 | + test1(char xc) : c(xc + 1) {} |
| 89 | + char c; |
97 | 90 | }; |
98 | 91 |
|
99 | | -void |
100 | | -test_struct_array() |
101 | | -{ |
102 | | - test1 test1a[4] = {}; |
103 | | - assert(std::fill_n(test1a, UDI(4), static_cast<char>(10)) == test1a + 4); |
104 | | - assert(test1a[0].c == 11); |
105 | | - assert(test1a[1].c == 11); |
106 | | - assert(test1a[2].c == 11); |
107 | | - assert(test1a[3].c == 11); |
| 92 | +void test_struct_array() { |
| 93 | + test1 test1a[4] = {}; |
| 94 | + assert(std::fill_n(test1a, UDI(4), static_cast<char>(10)) == test1a + 4); |
| 95 | + assert(test1a[0].c == 11); |
| 96 | + assert(test1a[1].c == 11); |
| 97 | + assert(test1a[2].c == 11); |
| 98 | + assert(test1a[3].c == 11); |
108 | 99 | } |
109 | 100 |
|
110 | | -class A |
111 | | -{ |
112 | | - char a_; |
| 101 | +class A { |
| 102 | + char a_; |
| 103 | + |
113 | 104 | public: |
114 | | - A() {} |
115 | | - explicit A(char a) : a_(a) {} |
116 | | - operator unsigned char() const {return 'b';} |
| 105 | + A() {} |
| 106 | + explicit A(char a) : a_(a) {} |
| 107 | + operator unsigned char() const { return 'b'; } |
117 | 108 |
|
118 | | - friend bool operator==(const A& x, const A& y) |
119 | | - {return x.a_ == y.a_;} |
| 109 | + friend bool operator==(const A& x, const A& y) { return x.a_ == y.a_; } |
120 | 110 | }; |
121 | 111 |
|
122 | | -void |
123 | | -test5() |
124 | | -{ |
125 | | - A a[3]; |
126 | | - assert(std::fill_n(&a[0], UDI(3), A('a')) == a+3); |
127 | | - assert(a[0] == A('a')); |
128 | | - assert(a[1] == A('a')); |
129 | | - assert(a[2] == A('a')); |
| 112 | +void test5() { |
| 113 | + A a[3]; |
| 114 | + assert(std::fill_n(&a[0], UDI(3), A('a')) == a + 3); |
| 115 | + assert(a[0] == A('a')); |
| 116 | + assert(a[1] == A('a')); |
| 117 | + assert(a[2] == A('a')); |
130 | 118 | } |
131 | 119 |
|
132 | | -struct Storage |
133 | | -{ |
134 | | - union |
135 | | - { |
| 120 | +struct Storage { |
| 121 | + union { |
136 | 122 | unsigned char a; |
137 | 123 | unsigned char b; |
138 | 124 | }; |
139 | 125 | }; |
140 | 126 |
|
141 | | -void test6() |
142 | | -{ |
| 127 | +void test6() { |
143 | 128 | Storage foo[5]; |
144 | 129 | std::fill_n(&foo[0], UDI(5), Storage()); |
145 | 130 | } |
146 | 131 |
|
| 132 | +TEST_CONSTEXPR_CXX20 void test_bititer_with_custom_sized_types() { |
| 133 | + { |
| 134 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 135 | + std::vector<bool, Alloc> in(100, false, Alloc(1)); |
| 136 | + std::vector<bool, Alloc> expected(100, true, Alloc(1)); |
| 137 | + std::fill_n(in.begin(), in.size(), true); |
| 138 | + assert(in == expected); |
| 139 | + } |
| 140 | + { |
| 141 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 142 | + std::vector<bool, Alloc> in(200, false, Alloc(1)); |
| 143 | + std::vector<bool, Alloc> expected(200, true, Alloc(1)); |
| 144 | + std::fill_n(in.begin(), in.size(), true); |
| 145 | + assert(in == expected); |
| 146 | + } |
| 147 | + { |
| 148 | + using Alloc = sized_allocator<bool, std::uint32_t, std::int32_t>; |
| 149 | + std::vector<bool, Alloc> in(200, false, Alloc(1)); |
| 150 | + std::vector<bool, Alloc> expected(200, true, Alloc(1)); |
| 151 | + std::fill_n(in.begin(), in.size(), true); |
| 152 | + assert(in == expected); |
| 153 | + } |
| 154 | + { |
| 155 | + using Alloc = sized_allocator<bool, std::uint64_t, std::int64_t>; |
| 156 | + std::vector<bool, Alloc> in(200, false, Alloc(1)); |
| 157 | + std::vector<bool, Alloc> expected(200, true, Alloc(1)); |
| 158 | + std::fill_n(in.begin(), in.size(), true); |
| 159 | + assert(in == expected); |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +int main(int, char**) { |
| 164 | + test_char<cpp17_output_iterator<char*> >(); |
| 165 | + test_char<forward_iterator<char*> >(); |
| 166 | + test_char<bidirectional_iterator<char*> >(); |
| 167 | + test_char<random_access_iterator<char*> >(); |
| 168 | + test_char<char*>(); |
147 | 169 |
|
148 | | -int main(int, char**) |
149 | | -{ |
150 | | - test_char<cpp17_output_iterator<char*> >(); |
151 | | - test_char<forward_iterator<char*> >(); |
152 | | - test_char<bidirectional_iterator<char*> >(); |
153 | | - test_char<random_access_iterator<char*> >(); |
154 | | - test_char<char*>(); |
| 170 | + test_int<cpp17_output_iterator<int*> >(); |
| 171 | + test_int<forward_iterator<int*> >(); |
| 172 | + test_int<bidirectional_iterator<int*> >(); |
| 173 | + test_int<random_access_iterator<int*> >(); |
| 174 | + test_int<int*>(); |
155 | 175 |
|
156 | | - test_int<cpp17_output_iterator<int*> >(); |
157 | | - test_int<forward_iterator<int*> >(); |
158 | | - test_int<bidirectional_iterator<int*> >(); |
159 | | - test_int<random_access_iterator<int*> >(); |
160 | | - test_int<int*>(); |
| 176 | + test_int_array(); |
| 177 | + test_int_array_struct_source(); |
| 178 | + test_struct_array(); |
161 | 179 |
|
162 | | - test_int_array(); |
163 | | - test_int_array_struct_source(); |
164 | | - test_struct_array(); |
| 180 | + test5(); |
| 181 | + test6(); |
165 | 182 |
|
166 | | - test5(); |
167 | | - test6(); |
| 183 | + test_bititer_with_custom_sized_types(); |
168 | 184 |
|
169 | 185 | #if TEST_STD_VER > 17 |
170 | | - static_assert(test_constexpr()); |
| 186 | + static_assert(test_constexpr()); |
171 | 187 | #endif |
172 | 188 |
|
173 | 189 | return 0; |
|
0 commit comments