#include <vector>
#include <string>
#include <testsuite_allocator.h>
#include <testsuite_hooks.h>
using __gnu_test::copy_tracker;
using __gnu_test::tracker_allocator_counter;
using __gnu_test::tracker_allocator;
using __gnu_test::copy_constructor;
using __gnu_test::assignment_operator;
void
test_default_ctor_exception_gurantee()
{
bool test __attribute__((unused)) = true;
typedef copy_tracker T;
typedef std::vector<T, tracker_allocator<T> > X;
copy_tracker::reset();
copy_constructor::throw_on(3);
tracker_allocator_counter::reset();
try
{
X a(7);
VERIFY(false);
}
catch (...)
{
}
VERIFY( tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count() );
}
void
test_copy_ctor_exception_gurantee()
{
bool test __attribute__((unused)) = true;
typedef copy_tracker T;
typedef std::vector<T, tracker_allocator<T> > X;
tracker_allocator_counter::reset();
{
X a(7);
copy_tracker::reset();
copy_constructor::throw_on(3);
try
{
X u(a);
VERIFY(false);
}
catch (...)
{
}
}
VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
copy_tracker::reset();
tracker_allocator_counter::reset();
}
void
test_assignment_operator_1()
{
bool test __attribute__((unused)) = true;
typedef copy_tracker T;
typedef std::vector<T, tracker_allocator<T> > X;
X r(9);
X a(r.size() - 2);
copy_tracker::reset();
tracker_allocator_counter::reset();
VERIFY(r.size() > a.size());
r = a;
VERIFY(r == a);
VERIFY(tracker_allocator_counter::get_allocation_count() == 0);
copy_tracker::reset();
tracker_allocator_counter::reset();
}
void
test_assignment_operator_2()
{
bool test __attribute__((unused)) = true;
typedef copy_tracker T;
typedef std::vector<T, tracker_allocator<T> > X;
X r(1);
r.reserve(17);
X a(r.size() + 7);
copy_tracker::reset();
tracker_allocator_counter::reset();
VERIFY(r.size() < a.size());
VERIFY(a.size() < r.capacity());
r = a;
VERIFY(r == a);
VERIFY(tracker_allocator_counter::get_allocation_count() == 0);
copy_tracker::reset();
tracker_allocator_counter::reset();
}
void
test_assignment_operator_3()
{
bool test __attribute__((unused)) = true;
typedef copy_tracker T;
typedef std::vector<T, tracker_allocator<T> > X;
tracker_allocator_counter::reset();
{
X r(1);
X a(r.capacity() + 7);
copy_tracker::reset();
VERIFY(r.capacity() < a.size());
r = a;
VERIFY(r == a);
}
VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
copy_tracker::reset();
tracker_allocator_counter::reset();
}
void
test_assignment_operator_3_exception_guarantee()
{
bool test __attribute__((unused)) = true;
typedef copy_tracker T;
typedef std::vector<T, tracker_allocator<T> > X;
tracker_allocator_counter::reset();
{
X r(1);
X a(r.capacity() + 7);
copy_tracker::reset();
copy_constructor::throw_on(3);
VERIFY(r.capacity() < a.size());
try
{
r = a;
VERIFY(false);
}
catch (...)
{
}
}
VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
copy_tracker::reset();
tracker_allocator_counter::reset();
}
void
test_fill_assign_1()
{
bool test __attribute__((unused)) = true;
typedef copy_tracker T;
typedef std::vector<T, tracker_allocator<T> > X;
X a(7);
X::size_type old_size = a.size();
X::size_type new_size = old_size - 2;
const T t;
copy_tracker::reset();
tracker_allocator_counter::reset();
a.assign(new_size, t);
VERIFY(a.size() == new_size);
VERIFY(tracker_allocator_counter::get_allocation_count() == 0);
copy_tracker::reset();
tracker_allocator_counter::reset();
}
void
test_fill_assign_2()
{
bool test __attribute__((unused)) = true;
typedef copy_tracker T;
typedef std::vector<T, tracker_allocator<T> > X;
X a(7);
a.reserve(11);
X::size_type old_size = a.size();
X::size_type old_capacity = a.capacity();
X::size_type new_size = old_size + 2;
const T t;
copy_tracker::reset();
tracker_allocator_counter::reset();
VERIFY(old_size < new_size);
VERIFY(new_size <= old_capacity);
a.assign(new_size, t);
VERIFY(a.size() == new_size);
VERIFY(tracker_allocator_counter::get_allocation_count() == 0);
copy_tracker::reset();
tracker_allocator_counter::reset();
}
void
test_fill_assign_3()
{
bool test __attribute__((unused)) = true;
typedef copy_tracker T;
typedef std::vector<T, tracker_allocator<T> > X;
tracker_allocator_counter::reset();
{
X a(7);
X::size_type old_capacity = a.capacity();
X::size_type new_size = old_capacity + 4;
const T t;
copy_tracker::reset();
VERIFY(new_size > old_capacity);
a.assign(new_size, t);
VERIFY(a.size() == new_size);
}
VERIFY(tracker_allocator_counter::get_allocation_count() > 0);
VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
copy_tracker::reset();
tracker_allocator_counter::reset();
}
void
test_fill_assign_3_exception_guarantee()
{
bool test __attribute__((unused)) = true;
typedef copy_tracker T;
typedef std::vector<T, tracker_allocator<T> > X;
tracker_allocator_counter::reset();
{
X a(7);
X::size_type old_size = a.size();
X::size_type old_capacity = a.capacity();
X::size_type new_size = old_capacity + 4;
const T t;
copy_tracker::reset();
copy_constructor::throw_on(3);
VERIFY(new_size > old_capacity);
try
{
a.assign(new_size, t);
VERIFY(false);
}
catch (...)
{
}
VERIFY(a.size() == old_size);
VERIFY(a.capacity() == old_capacity);
}
VERIFY(tracker_allocator_counter::get_allocation_count() > 0);
VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
copy_tracker::reset();
tracker_allocator_counter::reset();
}
void
test_fill_assign_4()
{
bool test __attribute__((unused)) = true;
typedef copy_tracker T;
typedef std::vector<T, tracker_allocator<T> > X;
X a(7);
X::size_type old_size = a.size();
X::size_type new_size = old_size - 2;
X::size_type new_value = 117;
copy_tracker::reset();
tracker_allocator_counter::reset();
a.assign(new_size, new_value);
VERIFY(a.size() == new_size);
VERIFY(tracker_allocator_counter::get_allocation_count() == 0);
copy_tracker::reset();
tracker_allocator_counter::reset();
}
void
test_range_assign_1()
{
}
void
test_range_assign_2()
{
bool test __attribute__((unused)) = true;
typedef copy_tracker T;
typedef std::vector<T, tracker_allocator<T> > X;
X a(7);
X b(3);
copy_tracker::reset();
tracker_allocator_counter::reset();
VERIFY(b.size() < a.capacity());
a.assign(b.begin(), b.end());
VERIFY(a.size() == b.size());
VERIFY(a == b);
VERIFY(tracker_allocator_counter::get_allocation_count() == 0);
copy_tracker::reset();
tracker_allocator_counter::reset();
}
void
test_range_assign_3()
{
bool test __attribute__((unused)) = true;
typedef copy_tracker T;
typedef std::vector<T, tracker_allocator<T> > X;
X a(7);
a.reserve(a.size() + 7);
X b(a.size() + 3);
copy_tracker::reset();
tracker_allocator_counter::reset();
VERIFY(a.size() < b.size());
VERIFY(b.size() < a.capacity());
a.assign(b.begin(), b.end());
VERIFY(a.size() == b.size());
VERIFY(a == b);
VERIFY(tracker_allocator_counter::get_allocation_count() == 0);
copy_tracker::reset();
tracker_allocator_counter::reset();
}
void
test_range_assign_4()
{
bool test __attribute__((unused)) = true;
typedef copy_tracker T;
typedef std::vector<T, tracker_allocator<T> > X;
tracker_allocator_counter::reset();
{
X a(7);
X b(a.capacity() + 7);
copy_tracker::reset();
VERIFY(b.size() > a.capacity());
a.assign(b.begin(), b.end());
VERIFY(a.size() == b.size());
VERIFY(a == b);
}
VERIFY(tracker_allocator_counter::get_allocation_count() > 0);
VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
copy_tracker::reset();
tracker_allocator_counter::reset();
}
void
test_range_assign_4_exception_guarantee()
{
bool test __attribute__((unused)) = true;
typedef copy_tracker T;
typedef std::vector<T, tracker_allocator<T> > X;
tracker_allocator_counter::reset();
{
X a(7);
X b(a.capacity() + 7);
copy_tracker::reset();
copy_constructor::throw_on(3);
VERIFY(b.size() > a.capacity());
try
{
a.assign(b.begin(), b.end());
VERIFY(false);
}
catch (...)
{
}
}
VERIFY(tracker_allocator_counter::get_allocation_count() > 0);
VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
copy_tracker::reset();
tracker_allocator_counter::reset();
}
int main()
{
test_default_ctor_exception_gurantee();
test_copy_ctor_exception_gurantee();
test_assignment_operator_1();
test_assignment_operator_2();
test_assignment_operator_3();
test_assignment_operator_3_exception_guarantee();
test_fill_assign_1();
test_fill_assign_2();
test_fill_assign_3();
test_fill_assign_3_exception_guarantee();
test_fill_assign_4();
test_range_assign_1();
test_range_assign_2();
test_range_assign_3();
test_range_assign_4();
test_range_assign_4_exception_guarantee();
return 0;
}