[libc++][NFC] Slight refactoring of some std::vector tests

This commit is contained in:
Louis Dionne 2022-05-06 10:49:14 -04:00
parent b3d5bb3b30
commit 3442ff17a5
2 changed files with 55 additions and 65 deletions

View File

@ -23,7 +23,7 @@
#endif
void test_emplaceable_concept() {
void test() {
#if TEST_STD_VER >= 11
int arr1[] = {42};
int arr2[] = {1, 101, 42};
@ -64,26 +64,22 @@ void test_emplaceable_concept() {
}
}
#endif
// Test with a number of elements in the source range that is greater than capacity
{
typedef forward_iterator<int*> It;
std::vector<int> dst(10);
size_t n = dst.capacity() * 2;
std::vector<int> src(n);
dst.assign(It(src.data()), It(src.data() + src.size()));
assert(dst == src);
}
}
// Test with a number of elements in the source range
// that is greater than capacity
void test_assign_bigger() {
typedef forward_iterator<int*> It;
std::vector<int> dst(10);
size_t n = dst.capacity() * 2;
std::vector<int> src(n);
dst.assign(It(src.data()), It(src.data() + src.size()));
assert(dst == src);
}
int main(int, char**)
{
test_emplaceable_concept();
test_assign_bigger();
int main(int, char**) {
test();
return 0;
}

View File

@ -9,6 +9,7 @@
// <vector>
// explicit vector(size_type n);
// explicit vector(size_type n, const Allocator& alloc = Allocator());
#include <vector>
#include <cassert>
@ -20,62 +21,55 @@
#include "asan_testing.h"
template <class C>
void
test2(typename C::size_type n, typename C::allocator_type const& a = typename C::allocator_type ())
void test(typename C::size_type n,
typename C::allocator_type const& a = typename C::allocator_type())
{
#if TEST_STD_VER >= 14
C c(n, a);
LIBCPP_ASSERT(c.__invariants());
assert(c.size() == n);
assert(c.get_allocator() == a);
LIBCPP_ASSERT(is_contiguous_container_asan_correct(c));
for (typename C::const_iterator i = c.cbegin(), e = c.cend(); i != e; ++i)
assert(*i == typename C::value_type());
#else
((void)n);
((void)a);
#endif
}
template <class C>
void
test1(typename C::size_type n)
{
C c(n);
LIBCPP_ASSERT(c.__invariants());
assert(c.size() == n);
assert(c.get_allocator() == typename C::allocator_type());
LIBCPP_ASSERT(is_contiguous_container_asan_correct(c));
(void)a;
// Test without a custom allocator
{
C c(n);
LIBCPP_ASSERT(c.__invariants());
assert(c.size() == n);
assert(c.get_allocator() == typename C::allocator_type());
LIBCPP_ASSERT(is_contiguous_container_asan_correct(c));
#if TEST_STD_VER >= 11
for (typename C::const_iterator i = c.cbegin(), e = c.cend(); i != e; ++i)
assert(*i == typename C::value_type());
for (typename C::const_iterator i = c.cbegin(), e = c.cend(); i != e; ++i)
assert(*i == typename C::value_type());
#endif
}
// Test with a custom allocator
#if TEST_STD_VER >= 14
{
C c(n, a);
LIBCPP_ASSERT(c.__invariants());
assert(c.size() == n);
assert(c.get_allocator() == a);
LIBCPP_ASSERT(is_contiguous_container_asan_correct(c));
for (typename C::const_iterator i = c.cbegin(), e = c.cend(); i != e; ++i)
assert(*i == typename C::value_type());
}
#endif
}
template <class C>
void
test(typename C::size_type n)
{
test1<C> ( n );
test2<C> ( n );
}
int main(int, char**)
{
void tests() {
test<std::vector<int> >(0);
test<std::vector<int> >(50);
test<std::vector<DefaultOnly> >(0);
test<std::vector<DefaultOnly> >(500);
assert(DefaultOnly::count == 0);
#if TEST_STD_VER >= 11
test<std::vector<int, min_allocator<int>> >(0);
test<std::vector<int, min_allocator<int>> >(50);
test<std::vector<DefaultOnly, min_allocator<DefaultOnly>> >(0);
test<std::vector<DefaultOnly, min_allocator<DefaultOnly>> >(500);
test2<std::vector<DefaultOnly, test_allocator<DefaultOnly>> >( 0, test_allocator<DefaultOnly>(23));
test2<std::vector<DefaultOnly, test_allocator<DefaultOnly>> >( 100, test_allocator<DefaultOnly>(23));
test<std::vector<int, min_allocator<int>>>(0);
test<std::vector<int, min_allocator<int>>>(50);
test<std::vector<DefaultOnly, min_allocator<DefaultOnly>>>(0);
test<std::vector<DefaultOnly, min_allocator<DefaultOnly>>>(500);
test<std::vector<DefaultOnly, test_allocator<DefaultOnly>>>(0, test_allocator<DefaultOnly>(23));
test<std::vector<DefaultOnly, test_allocator<DefaultOnly>>>(100, test_allocator<DefaultOnly>(23));
assert(DefaultOnly::count == 0);
#endif
return 0;
}
int main(int, char**) {
tests();
return 0;
}