Peng Liu 70480fcff6
[libc++] Validate vector<bool> copy/move-assignment operators in realistic scenarios (#119817)
The existing tests for `vector<bool>` copy- and move-assignment
operators are limited to 3 bits only, which are inadequate to cover
realistic scenarios. Most `vector<bool>` operations have code paths that
are executed only when multiple storage words are involved, with each
storage word typically comprising 64 bits on a 64-bit platform.
Furthermore, the existing tests fail to cover all combinations
`POCCA`/`POCMA`, along with different allocator equality and/or
reallocation scenarios, leaving some critical code paths untested.

This patch enhances the test coverage by introducing new tests covering
up to 5 storage words, ensuring that partial words in the front or tail,
and whole words in the middle are all properly tested. Moreover, these
new tests ensure that the copy- and move-assignment operators are tested
under all combinations of `POCCA`/`POCMA` and various allocator equality
scenarios, both with or without reallocations.
2025-03-19 12:08:20 -04:00

84 lines
2.6 KiB
C++

//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// <vector>
// vector<bool>
// vector& operator=(const vector& c);
#include <cassert>
#include <vector>
#include "min_allocator.h"
#include "test_allocator.h"
#include "test_macros.h"
TEST_CONSTEXPR_CXX20 void test_copy_assignment(unsigned N) {
//
// Test with insufficient space where reallocation occurs during assignment
//
{ // POCCA = true_type, thus copy-assign the allocator
std::vector<bool, other_allocator<bool> > l(N, true, other_allocator<bool>(5));
std::vector<bool, other_allocator<bool> > l2(other_allocator<bool>(3));
l2 = l;
assert(l2 == l);
assert(l2.get_allocator() == other_allocator<bool>(5));
}
{ // POCCA = false_type, thus allocator is unchanged
std::vector<bool, test_allocator<bool> > l(N + 64, true, test_allocator<bool>(5));
std::vector<bool, test_allocator<bool> > l2(10, false, test_allocator<bool>(3));
l2 = l;
assert(l2 == l);
assert(l2.get_allocator() == test_allocator<bool>(3));
}
{ // Stateless allocator
std::vector<bool, min_allocator<bool> > l(N + 64, true, min_allocator<bool>());
std::vector<bool, min_allocator<bool> > l2(N / 2, false, min_allocator<bool>());
l2 = l;
assert(l2 == l);
assert(l2.get_allocator() == min_allocator<bool>());
}
//
// Test with sufficient size where no reallocation occurs during assignment
//
{ // POCCA = false_type, thus allocator is unchanged
std::vector<bool, test_allocator<bool> > l(N, true, test_allocator<bool>(5));
std::vector<bool, test_allocator<bool> > l2(N + 64, false, test_allocator<bool>(3));
l2 = l;
assert(l2 == l);
assert(l2.get_allocator() == test_allocator<bool>(3));
}
{ // POCCA = true_type, thus copy-assign the allocator
std::vector<bool, other_allocator<bool> > l(N, true, other_allocator<bool>(5));
std::vector<bool, other_allocator<bool> > l2(N * 2, false, other_allocator<bool>(3));
l2.reserve(5);
l2 = l;
assert(l2 == l);
assert(l2.get_allocator() == other_allocator<bool>(5));
}
}
TEST_CONSTEXPR_CXX20 bool tests() {
test_copy_assignment(3);
test_copy_assignment(18);
test_copy_assignment(33);
test_copy_assignment(65);
test_copy_assignment(299);
return true;
}
int main(int, char**) {
tests();
#if TEST_STD_VER > 17
static_assert(tests());
#endif
return 0;
}