tut logo

basic/test_auto_ptr.cpp


basic/test_auto_ptr.cpp

tut auto_ptr tests

#include <tut/tut.hpp>
#include <memory>
using std::auto_ptr;
namespace tut
{
struct auto_ptr_data
{
bool exists;
struct existing
{
bool& s_;
existing(bool& s) : s_(s)
{
s_ = true;
}
~existing()
{
s_ = false;
}
};
auto_ptr_data(): exists(false) { }
virtual ~auto_ptr_data() { }
};
typedef test_group<auto_ptr_data> tf;
typedef tf::object object;
tf auto_ptr_group("std::auto_ptr");
template<>
template<>
void object::test<1>()
{
auto_ptr<existing> ap;
ensure(ap.get() == 0);
ensure(ap.operator->() == 0);
}
template<>
template<>
void object::test<2>()
{
{
auto_ptr<existing> ap(new existing(exists));
ensure("get", ap.get() != 0);
ensure_equals("constructed", exists, true);
}
// ptr left scope
ensure_equals("destructed", exists, false);
}
template<>
template<>
void object::test<3>()
{
auto_ptr<existing> ap(new existing(exists));
existing* p1 = ap.get();
existing* p2 = ap.operator->();
ensure("get equiv ->", p1 == p2);
// ensure no losing ownership
p1 = ap.get();
ensure("still owner", p1 == p2);
}
template<>
template<>
void object::test<4>()
{
{
auto_ptr<existing> ap(new existing(exists));
existing* p1 = ap.get();
auto_ptr<existing> ap2(ap.release());
ensure("same pointer", p1 == ap2.get());
ensure("lost ownership", ap.get() == 0);
}
ensure("destructed", exists == false);
}
template<>
template<>
void object::test<5>()
{
{
auto_ptr<existing> ap(new existing(exists));
existing* p1 = ap.get();
auto_ptr<existing> ap2;
ap2 = ap;
ensure("same pointer", p1 == ap2.get());
ensure("lost ownership", ap.get() == 0);
}
ensure("destructed", exists == false);
}
template<>
template<>
void object::test<6>()
{
{
auto_ptr<existing> ap(new existing(exists));
existing* p1 = ap.get();
auto_ptr<existing> ap2(ap);
ensure("same pointer", p1 == ap2.get());
ensure("lost ownership", ap.get() == 0);
}
ensure("destructed", exists == false);
}
}

All Rights Reserved. Generated on Wed Dec 18 2013 11:19:52 for TUT by doxygen 1.8.5