105 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& operator=(vector&& c);15 16#include <cassert>17#include <utility>18#include <vector>19 20#include "min_allocator.h"21#include "test_allocator.h"22#include "test_macros.h"23 24TEST_CONSTEXPR_CXX20 void test_move_assignment(unsigned N) {25 //26 // Testing for container move where either POCMA = true_type or the allocators compare equal27 //28 { // Test with POCMA = true_type29 std::vector<bool, other_allocator<bool> > l(N, true, other_allocator<bool>(5));30 std::vector<bool, other_allocator<bool> > lo(N, true, other_allocator<bool>(5));31 std::vector<bool, other_allocator<bool> > l2(N + 10, false, other_allocator<bool>(42));32 l2 = std::move(l);33 assert(l2 == lo);34 LIBCPP_ASSERT(l.empty()); // After move, source vector is in a vliad but unspecified state. libc++ leaves it empty.35 assert(l2.get_allocator() == lo.get_allocator());36 }37 { // Test with POCMA = false_type and allocators compare equal38 std::vector<bool, test_allocator<bool> > l(N, true, test_allocator<bool>(5));39 std::vector<bool, test_allocator<bool> > lo(N, true, test_allocator<bool>(5));40 std::vector<bool, test_allocator<bool> > l2(N + 10, false, test_allocator<bool>(5));41 l2 = std::move(l);42 assert(l2 == lo);43 LIBCPP_ASSERT(l.empty());44 assert(l2.get_allocator() == lo.get_allocator());45 }46 { // Test with POCMA = false_type and allocators compare equal47 std::vector<bool, min_allocator<bool> > l(N, true, min_allocator<bool>{});48 std::vector<bool, min_allocator<bool> > lo(N, true, min_allocator<bool>{});49 std::vector<bool, min_allocator<bool> > l2(N + 10, false, min_allocator<bool>{});50 l2 = std::move(l);51 assert(l2 == lo);52 LIBCPP_ASSERT(l.empty());53 assert(l2.get_allocator() == lo.get_allocator());54 }55 56 //57 // Testing for element-wise move where POCMA = false_type and allocators compare unequal58 //59 { // Test with reallocation during the element-wise move due to empty destination vector.60 std::vector<bool, test_allocator<bool> > l(N, true, test_allocator<bool>(5));61 std::vector<bool, test_allocator<bool> > lo(N, true, test_allocator<bool>(5));62 std::vector<bool, test_allocator<bool> > l2(test_allocator<bool>(42));63 l2 = std::move(l);64 assert(l2 == lo);65 LIBCPP_ASSERT(!l.empty());66 assert(l2.get_allocator() == test_allocator<bool>(42));67 }68 { // Test with reallocation occurs during the element-wise move due to insufficient destination space.69 std::vector<bool, test_allocator<bool> > l(N + 64, true, test_allocator<bool>(5));70 std::vector<bool, test_allocator<bool> > lo(N + 64, true, test_allocator<bool>(5));71 std::vector<bool, test_allocator<bool> > l2(10, false, test_allocator<bool>(42));72 l2 = std::move(l);73 assert(l2 == lo);74 LIBCPP_ASSERT(!l.empty());75 assert(l2.get_allocator() == test_allocator<bool>(42));76 }77 { // Test without reallocation where source vector elements fit within destination size.78 std::vector<bool, test_allocator<bool> > l(N, true, test_allocator<bool>(5));79 std::vector<bool, test_allocator<bool> > lo(N, true, test_allocator<bool>(5));80 std::vector<bool, test_allocator<bool> > l2(N * 2, false, test_allocator<bool>(42));81 l2 = std::move(l);82 assert(l2 == lo);83 LIBCPP_ASSERT(!l.empty());84 assert(l2.get_allocator() == test_allocator<bool>(42));85 }86}87 88TEST_CONSTEXPR_CXX20 bool tests() {89 test_move_assignment(3);90 test_move_assignment(18);91 test_move_assignment(33);92 test_move_assignment(65);93 test_move_assignment(299);94 95 return true;96}97 98int main(int, char**) {99 tests();100#if TEST_STD_VER > 17101 static_assert(tests());102#endif103 return 0;104}105