Skip to content

Commit

Permalink
Verify that select_on_container_copy_construction is being used app…
Browse files Browse the repository at this point in the history
…ropriately
  • Loading branch information
gharveymn committed Dec 8, 2024
1 parent b9bd703 commit 7f5ed74
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 1 deletion.
48 changes: 47 additions & 1 deletion source/test/test_allocators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,8 @@ namespace gch
template <typename U>
constexpr GCH_IMPLICIT_CONVERSION
verifying_allocator (const verifying_allocator<U, PartialTraits>& other) noexcept
: base (other)
: base (other),
created_by_container_copy_construction (other.created_by_container_copy_construction)
{ }

GCH_NODISCARD
Expand Down Expand Up @@ -452,8 +453,53 @@ namespace gch
remove_object (p);
alloc_traits::destroy (*this, p);
}

verifying_allocator
select_on_container_copy_construction () const
{
verifying_allocator ret (*this);
ret.created_by_container_copy_construction = true;
return ret;
}

bool created_by_container_copy_construction = false;
};

namespace detail {

}
template <typename A>
constexpr int
verify_created_by_container_copy_construction (const A&)
{
return 0;
}

template <typename T, typename Traits>
constexpr int
verify_created_by_container_copy_construction (const verifying_allocator<T, Traits>& a)
{
assert (a.created_by_container_copy_construction
&& "select_on_container_copy_construction unexpectedly invoked");
return 0;
}

template <typename A>
constexpr int
verify_not_created_by_container_copy_construction (const A&)
{
return 0;
}

template <typename T, typename Traits>
constexpr int
verify_not_created_by_container_copy_construction (const verifying_allocator<T, Traits>& a)
{
assert (! a.created_by_container_copy_construction
&& "select_on_container_copy_construction unexpectedly not invoked");
return 0;
}

template <typename T, typename Traits>
constexpr
bool
Expand Down
4 changes: 4 additions & 0 deletions source/test/unit/member/assign/test-copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ struct tester

n.assign (m);
CHECK (n == m_cmp);
gch::test_types::verify_not_created_by_container_copy_construction (n.get_allocator ());
}
{
// vector_type<N> (ni) -> vector_type<M> (mi)
Expand All @@ -222,6 +223,7 @@ struct tester

m.assign (n);
CHECK (m == n_cmp);
gch::test_types::verify_not_created_by_container_copy_construction (m.get_allocator ());
}
{
// vector_type<M> (ni) -> vector_type<N> (mi)
Expand All @@ -233,6 +235,7 @@ struct tester

n.assign (m);
CHECK (n == n_cmp);
gch::test_types::verify_not_created_by_container_copy_construction (n.get_allocator ());
}
{
// vector_type<N> (mi) -> vector_type<M> (ni)
Expand All @@ -244,6 +247,7 @@ struct tester

m.assign (n);
CHECK (m == m_cmp);
gch::test_types::verify_not_created_by_container_copy_construction (m.get_allocator ());
}
}

Expand Down
4 changes: 4 additions & 0 deletions source/test/unit/member/assign/test-move.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ struct tester

n.assign (std::move (m));
CHECK (n == m_cmp);
gch::test_types::verify_not_created_by_container_copy_construction (n.get_allocator ());
}
{
// vector_type<N> (ni) -> vector_type<M> (mi)
Expand All @@ -329,6 +330,7 @@ struct tester

m.assign (std::move (n));
CHECK (m == n_cmp);
gch::test_types::verify_not_created_by_container_copy_construction (m.get_allocator ());
}
{
// vector_type<M> (ni) -> vector_type<N> (mi)
Expand All @@ -340,6 +342,7 @@ struct tester

n.assign (std::move (m));
CHECK (n == n_cmp);
gch::test_types::verify_not_created_by_container_copy_construction (n.get_allocator ());
}
{
// vector_type<N> (mi) -> vector_type<M> (ni)
Expand All @@ -351,6 +354,7 @@ struct tester

m.assign (std::move (n));
CHECK (m == m_cmp);
gch::test_types::verify_not_created_by_container_copy_construction (m.get_allocator ());
}
}

Expand Down
2 changes: 2 additions & 0 deletions source/test/unit/member/constructor/test-copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,15 @@ struct tester

vector_type<N> v (m);
CHECK (v == m_cmp);
gch::test_types::verify_created_by_container_copy_construction (v.get_allocator ());
}
{
vector_type<M> m (mi.begin (), mi.end (), m_alloc);
mi (m);

vector_type<N> v (m, m_alloc);
CHECK (v == m_cmp);
gch::test_types::verify_not_created_by_container_copy_construction (v.get_allocator ());
}
}

Expand Down
2 changes: 2 additions & 0 deletions source/test/unit/member/constructor/test-move.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,15 @@ struct tester

vector_type<N> v (std::move (m));
CHECK (v == m_cmp);
gch::test_types::verify_not_created_by_container_copy_construction (v.get_allocator ());
}
{
vector_type<M> m (mi.begin (), mi.end (), m_alloc);
mi (m);

vector_type<N> v (std::move (m), m_init_alloc);
CHECK (v == m_cmp);
gch::test_types::verify_not_created_by_container_copy_construction (v.get_allocator ());
}
}

Expand Down
3 changes: 3 additions & 0 deletions source/test/unit/member/constructor/test-range.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ struct tester
{
vector_type<N> v (mi.begin (), mi.end (), m_alloc);
CHECK (mi.size () == v.size () && std::equal (mi.begin (), mi.end (), v.begin ()));
gch::test_types::verify_not_created_by_container_copy_construction (v.get_allocator ());
}
{
vector_type<N> v (input_it (&*mi.begin ()), input_it (&*mi.end ()));
Expand All @@ -138,6 +139,7 @@ struct tester
{
vector_type<N> v (input_it (&*mi.begin ()), input_it (&*mi.end ()), m_alloc);
CHECK (mi.size () == v.size () && std::equal (mi.begin (), mi.end (), v.begin ()));
gch::test_types::verify_not_created_by_container_copy_construction (v.get_allocator ());
}
{
vector_type<N> v (forward_it (&*mi.begin ()), forward_it (&*mi.end ()));
Expand All @@ -146,6 +148,7 @@ struct tester
{
vector_type<N> v (forward_it (&*mi.begin ()), forward_it (&*mi.end ()), m_alloc);
CHECK (mi.size () == v.size () && std::equal (mi.begin (), mi.end (), v.begin ()));
gch::test_types::verify_not_created_by_container_copy_construction (v.get_allocator ());
}
}

Expand Down
4 changes: 4 additions & 0 deletions source/test/unit/member/swap/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ struct tester
n.swap (m);
CHECK (n == m_cmp);
CHECK (m == n_cmp);
gch::test_types::verify_not_created_by_container_copy_construction (n.get_allocator ());
gch::test_types::verify_not_created_by_container_copy_construction (m.get_allocator ());
}
{
// vector_type<N> (ni) -> vector_type<N> (mi)
Expand All @@ -134,6 +136,8 @@ struct tester
m.swap (n);
CHECK (n == m_cmp);
CHECK (m == n_cmp);
gch::test_types::verify_not_created_by_container_copy_construction (n.get_allocator ());
gch::test_types::verify_not_created_by_container_copy_construction (m.get_allocator ());
}
}

Expand Down

0 comments on commit 7f5ed74

Please sign in to comment.