|
8 | 8 | #include <ranges> |
9 | 9 | #include <type_traits> |
10 | 10 |
|
| 11 | +// TODO: Not tested in this test suite: |
| 12 | +// constexpr, emplace (ensure only one copy made), exception safety on mutation |
| 13 | + |
11 | 14 | namespace { |
12 | 15 |
|
13 | 16 | // We run the tests on various element types with the help of GoogleTest's |
@@ -267,7 +270,7 @@ struct NonTrivial { |
267 | 270 | static std::size_t num_objects; |
268 | 271 | int value; |
269 | 272 |
|
270 | | - constexpr NonTrivial() noexcept : NonTrivial(int{}) {} |
| 273 | + constexpr NonTrivial() noexcept : NonTrivial(0) {} |
271 | 274 | constexpr NonTrivial(int v) noexcept : value(v) { |
272 | 275 | if (not std::is_constant_evaluated()) { |
273 | 276 | ++num_objects; |
@@ -1117,7 +1120,86 @@ TYPED_TEST(Constructors, CopyRanges) { |
1117 | 1120 | } |
1118 | 1121 |
|
1119 | 1122 | // 23.3.14.3 Size and capacity [inplace.vector.capacity] |
1120 | | -// TODO |
| 1123 | + |
| 1124 | +template <typename Param> class SizeNCapacity : public BasicTest<Param> {}; |
| 1125 | +TYPED_TEST_SUITE(SizeNCapacity, AllTypes); |
| 1126 | + |
| 1127 | +TYPED_TEST(SizeNCapacity, Capacity) { |
| 1128 | + // static constexpr size_type capacity() noexcept; |
| 1129 | + // static constexpr size_type max_size() noexcept; |
| 1130 | + // Returns: N. |
| 1131 | + |
| 1132 | + using IV = TestFixture::IV; |
| 1133 | + constexpr auto N = TestFixture::N; |
| 1134 | + |
| 1135 | + EXPECT_EQ(IV::capacity(), N); |
| 1136 | + IV device; |
| 1137 | + EXPECT_EQ(device.max_size(), N); |
| 1138 | +} |
| 1139 | + |
| 1140 | +TYPED_TEST(SizeNCapacity, ResizeDown) { |
| 1141 | + // constexpr void resize(size_type sz); |
| 1142 | + // Preconditions: T is Cpp17DefaultInsertable into inplace_vector. |
| 1143 | + // Effects: If sz < size(), erases the last size() - sz elements from the |
| 1144 | + // sequence. Otherwise, appends sz - size() default-inserted elements to the |
| 1145 | + // sequence. Remarks: If an exception is thrown, there are no effects on |
| 1146 | + // *this. |
| 1147 | + |
| 1148 | + using IV = TestFixture::IV; |
| 1149 | + using T = TestFixture::T; |
| 1150 | + |
| 1151 | + auto device = this->unique(); |
| 1152 | + |
| 1153 | + auto mid_size = std::midpoint(0ul, device.size()); |
| 1154 | + device.resize(mid_size); |
| 1155 | + EXPECT_EQ(device, IV(device.begin(), device.begin() + mid_size)); |
| 1156 | + |
| 1157 | + device.resize(0); |
| 1158 | + EXPECT_EQ(device, IV{}); |
| 1159 | +} |
| 1160 | + |
| 1161 | +TYPED_TEST(SizeNCapacity, ResizeUp) { |
| 1162 | + // constexpr void resize(size_type sz); |
| 1163 | + // Preconditions: T is Cpp17DefaultInsertable into inplace_vector. |
| 1164 | + // Effects: If sz < size(), erases the last size() - sz elements from the |
| 1165 | + // sequence. Otherwise, appends sz - size() default-inserted elements to the |
| 1166 | + // sequence. Remarks: If an exception is thrown, there are no effects on |
| 1167 | + // *this. |
| 1168 | + |
| 1169 | + using IV = TestFixture::IV; |
| 1170 | + using T = TestFixture::T; |
| 1171 | + |
| 1172 | + IV device; |
| 1173 | + |
| 1174 | + EXPECT_THROW(device.resize(device.capacity() + 1), beman::bad_alloc); |
| 1175 | + EXPECT_EQ(device, IV{}); |
| 1176 | + |
| 1177 | + if (device.capacity() == 0) |
| 1178 | + return; |
| 1179 | + |
| 1180 | + // Trying to pollute device[0] |
| 1181 | + device.push_back(T{255}); |
| 1182 | + device.pop_back(); |
| 1183 | + EXPECT_TRUE(device.empty()); |
| 1184 | + |
| 1185 | + device.resize(1); |
| 1186 | + EXPECT_EQ(device.size(), 1); |
| 1187 | + if (std::is_same_v<T, NonTriviallyDefaultConstructible> || |
| 1188 | + std::is_same_v<T, NonTrivial>) |
| 1189 | + EXPECT_EQ(device, IV{T{0}}); |
| 1190 | + |
| 1191 | + T front{341}; |
| 1192 | + device[0] = front; |
| 1193 | + device.resize(device.capacity()); |
| 1194 | + EXPECT_EQ(device[0], front); |
| 1195 | + |
| 1196 | + if (std::is_same_v<T, NonTriviallyDefaultConstructible> || |
| 1197 | + std::is_same_v<T, NonTrivial>) { |
| 1198 | + IV expected(device.capacity(), T{0}); |
| 1199 | + expected[0] = front; |
| 1200 | + EXPECT_EQ(device, expected); |
| 1201 | + } |
| 1202 | +} |
1121 | 1203 |
|
1122 | 1204 | // 23.3.14.4 Data [inplace.vector.data] |
1123 | 1205 |
|
@@ -1857,17 +1939,11 @@ TYPED_TEST(Modifiers, ShrinkToFitEmpty) { |
1857 | 1939 |
|
1858 | 1940 | TYPED_TEST(Modifiers, EraseSingle) { |
1859 | 1941 | // constexpr iterator erase(const_iterator position); |
1860 | | - // constexpr iterator erase(const_iterator first, const_iterator last); |
1861 | | - // constexpr void pop_back(); |
1862 | 1942 | // |
1863 | 1943 | // Effects: Invalidates iterators and references at or after the point of the |
1864 | 1944 | // erase. |
1865 | 1945 | // Throws: Nothing unless an exception is thrown by the assignment |
1866 | 1946 | // operator or move assignment operator of T. |
1867 | | - // Complexity: The destructor of T is called the number of times equal to the |
1868 | | - // number of the elements erased, but the assignment operator of T is called |
1869 | | - // the number of times equal to the number of elements after the erased |
1870 | | - // elements. |
1871 | 1947 |
|
1872 | 1948 | auto device = this->unique(); |
1873 | 1949 |
|
@@ -1900,17 +1976,11 @@ TYPED_TEST(Modifiers, EraseSingle) { |
1900 | 1976 |
|
1901 | 1977 | TYPED_TEST(Modifiers, EraseSingleConst) { |
1902 | 1978 | // constexpr iterator erase(const_iterator position); |
1903 | | - // constexpr iterator erase(const_iterator first, const_iterator last); |
1904 | | - // constexpr void pop_back(); |
1905 | 1979 | // |
1906 | 1980 | // Effects: Invalidates iterators and references at or after the point of the |
1907 | 1981 | // erase. |
1908 | 1982 | // Throws: Nothing unless an exception is thrown by the assignment |
1909 | 1983 | // operator or move assignment operator of T. |
1910 | | - // Complexity: The destructor of T is called the number of times equal to the |
1911 | | - // number of the elements erased, but the assignment operator of T is called |
1912 | | - // the number of times equal to the number of elements after the erased |
1913 | | - // elements. |
1914 | 1984 |
|
1915 | 1985 | auto device = this->unique(); |
1916 | 1986 |
|
|
0 commit comments