#include <vector>
#include <string>
#include <testsuite_allocator.h>
#include <testsuite_hooks.h>
using __gnu_cxx_test::copy_tracker;
using __gnu_cxx_test::allocation_tracker;
using __gnu_cxx_test::tracker_alloc;
using __gnu_cxx_test::copy_constructor;
using __gnu_cxx_test::assignment_operator;
template<typename T>
struct A { };
struct B { };
void test01()
{
bool test = true;
std::vector< A<B> > vec01;
std::vector< A<B> > vec02(5);
typedef std::vector< A<B> >::size_type size_type;
vec01 = vec02;
#ifdef DEBUG_ASSERT
assert(test);
#endif
}
template class std::vector<double>;
template class std::vector< A<B> >;
void test02()
{
std::vector<int> v1;
std::vector<int> v2 (v1);
}
void
test03()
{
bool test = true;
const int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
const int B[] = {7, 7, 7, 7, 7};
const int N = sizeof(A) / sizeof(int);
const int M = sizeof(B) / sizeof(int);
std::vector<int> v3(A, A + N);
VERIFY(std::equal(v3.begin(), v3.end(), A));
std::vector<int> v4(v3.begin(), v3.end());
VERIFY(std::equal(v4.begin(), v4.end(), A));
std::vector<int> v5(M, 7);
VERIFY(std::equal(v5.begin(), v5.end(), B));
VERIFY(std::equal(B, B + M, v5.begin()));
#ifdef DEBUG_ASSERT
assert(test);
#endif
}
void test04()
{
bool test = true;
const char* c_strings[5] = { "1", "2", "3", "4", "5" };
std::vector<std::string> strings(c_strings, c_strings + 5);
#ifdef DEBUG_ASSERT
assert(test);
#endif
}
void
test_default_ctor_exception_gurantee()
{
bool test = true;
typedef copy_tracker T;
typedef std::vector<T, tracker_alloc<T> > X;
copy_tracker::reset();
copy_constructor::throw_on(3);
allocation_tracker::resetCounts();
try
{
X a(7);
VERIFY(("no exception thrown", false));
}
catch (...)
{
}
VERIFY(("memory leak detected:",
allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal()));
}
void
test_copy_ctor_exception_gurantee()
{
bool test = true;
typedef copy_tracker T;
typedef std::vector<T, tracker_alloc<T> > X;
allocation_tracker::resetCounts();
{
X a(7);
copy_tracker::reset();
copy_constructor::throw_on(3);
try
{
X u(a);
VERIFY(("no exception thrown", false));
}
catch (...)
{
}
}
VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
copy_tracker::reset();
allocation_tracker::resetCounts();
}
void
test_assignment_operator_1()
{
bool test = true;
typedef copy_tracker T;
typedef std::vector<T, tracker_alloc<T> > X;
X r(9);
X a(r.size() - 2);
copy_tracker::reset();
allocation_tracker::resetCounts();
VERIFY(r.size() > a.size());
r = a;
VERIFY(r == a);
VERIFY(allocation_tracker::allocationTotal() == 0);
copy_tracker::reset();
allocation_tracker::resetCounts();
}
void
test_assignment_operator_2()
{
bool test = true;
typedef copy_tracker T;
typedef std::vector<T, tracker_alloc<T> > X;
X r(1);
r.reserve(17);
X a(r.size() + 7);
copy_tracker::reset();
allocation_tracker::resetCounts();
VERIFY(r.size() < a.size());
VERIFY(a.size() < r.capacity());
r = a;
VERIFY(r == a);
VERIFY(allocation_tracker::allocationTotal() == 0);
copy_tracker::reset();
allocation_tracker::resetCounts();
}
void
test_assignment_operator_3()
{
bool test = true;
typedef copy_tracker T;
typedef std::vector<T, tracker_alloc<T> > X;
allocation_tracker::resetCounts();
{
X r(1);
X a(r.capacity() + 7);
copy_tracker::reset();
VERIFY(r.capacity() < a.size());
r = a;
VERIFY(r == a);
}
VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
copy_tracker::reset();
allocation_tracker::resetCounts();
}
void
test_assignment_operator_3_exception_guarantee()
{
bool test = true;
typedef copy_tracker T;
typedef std::vector<T, tracker_alloc<T> > X;
allocation_tracker::resetCounts();
{
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(("no exception thrown", false));
}
catch (...)
{
}
}
VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
copy_tracker::reset();
allocation_tracker::resetCounts();
}
void
test_fill_assign_1()
{
bool test = true;
typedef copy_tracker T;
typedef std::vector<T, tracker_alloc<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();
allocation_tracker::resetCounts();
a.assign(new_size, t);
VERIFY(a.size() == new_size);
VERIFY(allocation_tracker::allocationTotal() == 0);
copy_tracker::reset();
allocation_tracker::resetCounts();
}
void
test_fill_assign_2()
{
bool test = true;
typedef copy_tracker T;
typedef std::vector<T, tracker_alloc<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();
allocation_tracker::resetCounts();
VERIFY(old_size < new_size);
VERIFY(new_size <= old_capacity);
a.assign(new_size, t);
VERIFY(a.size() == new_size);
VERIFY(allocation_tracker::allocationTotal() == 0);
copy_tracker::reset();
allocation_tracker::resetCounts();
}
void
test_fill_assign_3()
{
bool test = true;
typedef copy_tracker T;
typedef std::vector<T, tracker_alloc<T> > X;
allocation_tracker::resetCounts();
{
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();
VERIFY(new_size > old_capacity);
a.assign(new_size, t);
VERIFY(a.size() == new_size);
}
VERIFY(allocation_tracker::allocationTotal() > 0);
VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
copy_tracker::reset();
allocation_tracker::resetCounts();
}
void
test_fill_assign_3_exception_guarantee()
{
bool test = true;
typedef copy_tracker T;
typedef std::vector<T, tracker_alloc<T> > X;
allocation_tracker::resetCounts();
{
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(("no exception thrown", false));
}
catch (...)
{
}
VERIFY(a.size() == old_size);
VERIFY(a.capacity() == old_capacity);
}
VERIFY(allocation_tracker::allocationTotal() > 0);
VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
copy_tracker::reset();
allocation_tracker::resetCounts();
}
void
test_fill_assign_4()
{
bool test = true;
typedef copy_tracker T;
typedef std::vector<T, tracker_alloc<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();
allocation_tracker::resetCounts();
a.assign(new_size, new_value);
VERIFY(a.size() == new_size);
VERIFY(allocation_tracker::allocationTotal() == 0);
copy_tracker::reset();
allocation_tracker::resetCounts();
}
void
test_range_assign_1()
{
}
void
test_range_assign_2()
{
bool test = true;
typedef copy_tracker T;
typedef std::vector<T, tracker_alloc<T> > X;
X a(7);
X b(3);
X::size_type old_size = a.size();
copy_tracker::reset();
allocation_tracker::resetCounts();
VERIFY(b.size() < a.capacity());
a.assign(b.begin(), b.end());
VERIFY(a.size() == b.size());
VERIFY(a == b);
VERIFY(allocation_tracker::allocationTotal() == 0);
copy_tracker::reset();
allocation_tracker::resetCounts();
}
void
test_range_assign_3()
{
bool test = true;
typedef copy_tracker T;
typedef std::vector<T, tracker_alloc<T> > X;
X a(7);
a.reserve(a.size() + 7);
X b(a.size() + 3);
X::size_type old_size = a.size();
copy_tracker::reset();
allocation_tracker::resetCounts();
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(allocation_tracker::allocationTotal() == 0);
copy_tracker::reset();
allocation_tracker::resetCounts();
}
void
test_range_assign_4()
{
bool test = true;
typedef copy_tracker T;
typedef std::vector<T, tracker_alloc<T> > X;
allocation_tracker::resetCounts();
{
X a(7);
X b(a.capacity() + 7);
X::size_type old_size = a.size();
copy_tracker::reset();
VERIFY(b.size() > a.capacity());
a.assign(b.begin(), b.end());
VERIFY(a.size() == b.size());
VERIFY(a == b);
}
VERIFY(allocation_tracker::allocationTotal() > 0);
VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
copy_tracker::reset();
allocation_tracker::resetCounts();
}
void
test_range_assign_4_exception_guarantee()
{
bool test = true;
typedef copy_tracker T;
typedef std::vector<T, tracker_alloc<T> > X;
allocation_tracker::resetCounts();
{
X a(7);
X b(a.capacity() + 7);
X::size_type old_size = a.size();
copy_tracker::reset();
copy_constructor::throw_on(3);
VERIFY(b.size() > a.capacity());
try
{
a.assign(b.begin(), b.end());
VERIFY(("no exception thrown", false));
}
catch (...)
{
}
}
VERIFY(allocation_tracker::allocationTotal() > 0);
VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
copy_tracker::reset();
allocation_tracker::resetCounts();
}
int main()
{
test01();
test02();
test03();
test04();
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;
}