Skip to content
Snippets Groups Projects
Commit 9d0790da authored by Carsten Gräser's avatar Carsten Gräser
Browse files

*added test custom with deleter

*test correctness of pointer returned by get()
*added test for get() on empty shared_ptr
If you're happy the last test currently fails with a segfault.

Please merge to release

[[Imported from SVN: r5870]]
parent 12d02ec7
No related branches found
No related tags found
No related merge requests found
......@@ -12,6 +12,24 @@
#include <vector>
#include <cstdlib>
template<class T>
class Deleter
{
public:
Deleter(bool& deleted) :
deleted_(deleted)
{}
void operator() (T* p) const
{
delete p;
deleted_ = true;
}
private:
mutable bool& deleted_;
};
int main(){
using namespace Dune;
int ret=0;
......@@ -25,6 +43,58 @@ int main(){
ret=1;
}
// test custom deleter
bool deleted = false;
{
shared_ptr<int> foo(new int(1), Deleter<int>(deleted));
//test if deleter is called
deleted = false;
foo.reset(new int(2)); // this should call the deleter in the constructor
if (not (deleted))
{
std::cout << "Custom deleter not called!" << std::endl;
ret=1;
}
//test if old deleter is not called
deleted = false;
foo.reset(); // this should call no deleter
if (deleted)
{
std::cout << "Old deleter was called!" << std::endl;
ret=1;
}
//test if old deleter is not called
deleted = false;
foo.reset(new int(3), Deleter<int>(deleted)); // this should call no deleter
if (deleted)
{
std::cout << "Old deleter was called!" << std::endl;
ret=1;
}
// going out of scope should call the deleter
}
if (not (deleted))
{
std::cout << "Custom deleter not called!" << std::endl;
ret=1;
}
{
shared_ptr<int> foo(new int(1), Deleter<int>(deleted));
foo.reset(new int(4)); // this should call the deleter...
deleted = false;
// ... but going out of scope should call no deleter
}
if (deleted)
{
std::cout << "1Old deleter was called!" << std::endl;
ret=1;
}
// test constructor from a given pointer
shared_ptr<double> bar(new double(43.0));
assert(bar);
......@@ -33,14 +103,18 @@ int main(){
bar.reset();
assert(!bar);
// test get() for empty shared_ptr
assert(!bar.get());
// test reset(T*)
bar.reset(new double(44.0));
double* p = new double(44.0);
bar.reset(p);
assert(bar);
assert(bar.use_count()==1);
// test get()
double* barPtr = bar.get();
assert(barPtr);
assert(barPtr==p);
// test constructor from a given pointer
shared_ptr<double> b(new double(42.0));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment