Skip to content

Commit

Permalink
[libcxx][test][NFC] Extend testing for vector.cons
Browse files Browse the repository at this point in the history
Extending std::vector tests in vector.cons module:

- std::vector::assign when source range is bigger than destination
  capacity
- construction of empty vector using copy ctor, initializer_list ctor and
  others

Reviewed By: ldionne, rarutyun, #libc

Differential Revision: https://reviews.llvm.org/D114954
  • Loading branch information
kboyarinov authored and rarutyun committed Dec 10, 2021
1 parent d3606a3 commit 82ff94a
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 7 deletions.
Expand Up @@ -66,11 +66,24 @@ void test_emplaceable_concept() {
#endif
}

// 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();

return 0;
}
Expand Up @@ -39,7 +39,12 @@ int main(int, char**)
test(d1);
test(d2);
}

{
std::vector<int> vec;
vec.reserve(32);
vec.resize(16); // destruction during assign
test(vec);
}
#if TEST_STD_VER >= 11
{
typedef std::vector<int, min_allocator<int>> V;
Expand Down
Expand Up @@ -26,13 +26,22 @@

template <class C, class Iterator>
void test(Iterator first, Iterator last) {
C c(first, last);
LIBCPP_ASSERT(c.__invariants());
assert(c.size() == static_cast<std::size_t>(std::distance(first, last)));
LIBCPP_ASSERT(is_contiguous_container_asan_correct(c));
for (typename C::const_iterator i = c.cbegin(), e = c.cend(); i != e;
++i, ++first)
{
C c(first, last);
LIBCPP_ASSERT(c.__invariants());
assert(c.size() == static_cast<std::size_t>(std::distance(first, last)));
LIBCPP_ASSERT(is_contiguous_container_asan_correct(c));
for (typename C::const_iterator i = c.cbegin(), e = c.cend(); i != e;
++i, ++first)
assert(*i == *first);
}
// Test with an empty range
{
C c(first, first);
LIBCPP_ASSERT(c.__invariants());
assert(c.empty());
LIBCPP_ASSERT(is_contiguous_container_asan_correct(c));
}
}

static void basic_test_cases() {
Expand Down
Expand Up @@ -62,12 +62,17 @@ test(typename C::size_type n)

int main(int, char**)
{
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));
assert(DefaultOnly::count == 0);
#endif
Expand Down
Expand Up @@ -32,10 +32,14 @@ test(typename C::size_type n, const typename C::value_type& x)

int main(int, char**)
{
test<std::vector<int> >(0, 3);
test<std::vector<int> >(50, 3);
// Add 1 for implementations that dynamically allocate a container proxy.
test<std::vector<int, limited_allocator<int, 50 + 1> > >(0, 5);
test<std::vector<int, limited_allocator<int, 50 + 1> > >(50, 5);
test<std::vector<int, limited_allocator<int, 50 + 1> > >(50, 5);
#if TEST_STD_VER >= 11
test<std::vector<int, min_allocator<int>> >(0, 3);
test<std::vector<int, min_allocator<int>> >(50, 3);
#endif

Expand Down
Expand Up @@ -33,8 +33,10 @@ test(typename C::size_type n, const typename C::value_type& x,

int main(int, char**)
{
test<std::vector<int> >(0, 3, std::allocator<int>());
test<std::vector<int> >(50, 3, std::allocator<int>());
#if TEST_STD_VER >= 11
test<std::vector<int, min_allocator<int>> >(0, 3, min_allocator<int>());
test<std::vector<int, min_allocator<int>> >(50, 3, min_allocator<int>());
#endif

Expand Down
Expand Up @@ -47,6 +47,18 @@ int main(int, char**)
assert(is_contiguous_container_asan_correct(v));
assert(is_contiguous_container_asan_correct(v2));
}
{
// Test copy ctor with empty source
std::vector<int, test_allocator<int> > v(test_allocator<int>(5));
std::vector<int, test_allocator<int> > v2 = v;
assert(is_contiguous_container_asan_correct(v));
assert(is_contiguous_container_asan_correct(v2));
assert(v2 == v);
assert(v2.get_allocator() == v.get_allocator());
assert(is_contiguous_container_asan_correct(v));
assert(is_contiguous_container_asan_correct(v2));
assert(v2.empty());
}
#if TEST_STD_VER >= 11
{
std::vector<int, other_allocator<int> > v(3, 2, other_allocator<int>(5));
Expand Down
Expand Up @@ -49,6 +49,14 @@ int main(int, char**)
assert(l2 == l);
assert(l2.get_allocator() == other_allocator<int>(3));
}
{
// Test copy ctor with allocator and empty source
std::vector<int, other_allocator<int> > l(other_allocator<int>(5));
std::vector<int, other_allocator<int> > l2(l, other_allocator<int>(3));
assert(l2 == l);
assert(l2.get_allocator() == other_allocator<int>(3));
assert(l2.empty());
}
#if TEST_STD_VER >= 11
{
int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 1, 0};
Expand Down
Expand Up @@ -42,6 +42,12 @@ int main(int, char**)
assert(d[2] == 5);
assert(d[3] == 6);
}
{
std::vector<int, min_allocator<int>> d({}, min_allocator<int>());
assert(d.size() == 0);
assert(d.empty());
assert(is_contiguous_container_asan_correct(d));
}

return 0;
}

0 comments on commit 82ff94a

Please sign in to comment.