diff options
author | Kevin R. Thornton <krthornt@uci.edu> | 2016-02-22 11:30:45 -0800 |
---|---|---|
committer | Kevin R. Thornton <krthornt@uci.edu> | 2016-02-22 11:30:45 -0800 |
commit | 895da799f3b87e1ad7d2262a34ad4975afb85e0d (patch) | |
tree | bfe17e88d3514c0e9aac5b8c98903a18124042bb | |
parent | 6a620200af41d7c532765fc6181d7896c4bf9596 (diff) | |
download | cython-895da799f3b87e1ad7d2262a34ad4975afb85e0d.tar.gz |
test of unique_ptr with std::default_delete and a custom deleter
-rw-r--r-- | tests/run/cpp_smart_ptr.pyx | 24 | ||||
-rw-r--r-- | tests/run/cpp_smart_ptr_helper.h | 11 |
2 files changed, 34 insertions, 1 deletions
diff --git a/tests/run/cpp_smart_ptr.pyx b/tests/run/cpp_smart_ptr.pyx index 27171d8d3..0ca97da62 100644 --- a/tests/run/cpp_smart_ptr.pyx +++ b/tests/run/cpp_smart_ptr.pyx @@ -2,12 +2,16 @@ # mode: run # tag: cpp, werror -from libcpp.memory cimport unique_ptr, shared_ptr +from libcpp.memory cimport unique_ptr, shared_ptr, default_delete +from libcpp cimport nullptr cdef extern from "cpp_smart_ptr_helper.h": cdef cppclass CountAllocDealloc: CountAllocDealloc(int*, int*) + cdef cppclass FreePtr[T]: + pass + def test_unique_ptr(): """ >>> test_unique_ptr() @@ -19,3 +23,21 @@ def test_unique_ptr(): x_ptr.reset() assert alloc_count == 1 assert dealloc_count == 1 + + ##Repeat the above test with an explicit default_delete type + alloc_count = 0 + dealloc_count = 0 + cdef unique_ptr[CountAllocDealloc,default_delete[CountAllocDealloc]] x_ptr2 + x_ptr2.reset(new CountAllocDealloc(&alloc_count, &dealloc_count)) + assert alloc_count == 1 + x_ptr2.reset() + assert alloc_count == 1 + assert dealloc_count == 1 + + alloc_count = 0 + dealloc_count = 0 + cdef unique_ptr[CountAllocDealloc,FreePtr[CountAllocDealloc]] x_ptr3 + x_ptr3.reset(new CountAllocDealloc(&alloc_count, &dealloc_count)) + assert x_ptr3.get() != nullptr; + x_ptr3.reset() + assert x_ptr3.get() == nullptr; diff --git a/tests/run/cpp_smart_ptr_helper.h b/tests/run/cpp_smart_ptr_helper.h index 9fa7df913..9081801f9 100644 --- a/tests/run/cpp_smart_ptr_helper.h +++ b/tests/run/cpp_smart_ptr_helper.h @@ -11,3 +11,14 @@ class CountAllocDealloc { int* _alloc_count; int* _dealloc_count; }; + +template<typename T> +struct FreePtr { + void operator()( T * t ) noexcept + { + if(t != nullptr) { + delete t; + t=nullptr; + } + } +}; |