diff options
author | Eric Fiselier <eric@efcs.ca> | 2014-12-20 01:40:03 +0000 |
---|---|---|
committer | Eric Fiselier <eric@efcs.ca> | 2014-12-20 01:40:03 +0000 |
commit | 5a83710e371fe68a06e6e3876c6a2c8b820a8976 (patch) | |
tree | afde4c82ad6704681781c5cd49baa3fbd05c85db /libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared | |
parent | f11e8eab527fba316c64112f6e05de1a79693a3e (diff) | |
download | bcm5719-llvm-5a83710e371fe68a06e6e3876c6a2c8b820a8976.tar.gz bcm5719-llvm-5a83710e371fe68a06e6e3876c6a2c8b820a8976.zip |
Move test into test/std subdirectory.
llvm-svn: 224658
Diffstat (limited to 'libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared')
52 files changed, 3504 insertions, 0 deletions
diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/test_deleter.h b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/test_deleter.h new file mode 100644 index 00000000000..0263061b3a8 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/test_deleter.h @@ -0,0 +1,68 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// Example move-only deleter + +#ifndef DELETER_H +#define DELETER_H + +#include <type_traits> +#include <cassert> + +#ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS +#define DELETE_FUNCTION = delete +#else +#define DELETE_FUNCTION { assert(false); } +#endif + +struct test_deleter_base +{ + static int count; + static int dealloc_count; +}; + +int test_deleter_base::count = 0; +int test_deleter_base::dealloc_count = 0; + +template <class T> +class test_deleter + : public test_deleter_base +{ + int state_; + +public: + + test_deleter() : state_(0) {++count;} + explicit test_deleter(int s) : state_(s) {++count;} + test_deleter(const test_deleter& d) + : state_(d.state_) {++count;} + ~test_deleter() {assert(state_ >= 0); --count; state_ = -1;} + + int state() const {return state_;} + void set_state(int i) {state_ = i;} + + void operator()(T* p) {assert(state_ >= 0); ++dealloc_count; delete p;} + + test_deleter* operator&() const DELETE_FUNCTION; +}; + +template <class T> +void +swap(test_deleter<T>& x, test_deleter<T>& y) +{ + test_deleter<T> t(std::move(x)); + x = std::move(y); + y = std::move(t); +} + +#endif // DELETER_H diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/types.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/types.pass.cpp new file mode 100644 index 00000000000..8175312334f --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/types.pass.cpp @@ -0,0 +1,26 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// template<class T> class shared_ptr +// { +// public: +// typedef T element_type; +// ... +// }; + +#include <memory> + +struct A; // purposefully incomplete + +int main() +{ + static_assert((std::is_same<std::shared_ptr<A>::element_type, A>::value), ""); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.getdeleter/get_deleter.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.getdeleter/get_deleter.pass.cpp new file mode 100644 index 00000000000..a6c62496fd6 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.getdeleter/get_deleter.pass.cpp @@ -0,0 +1,67 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// template<class D, class T> D* get_deleter(const shared_ptr<T>& p); + +#include <memory> +#include <cassert> +#include "../test_deleter.h" + +struct A +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +int main() +{ + { + { + A* ptr = new A; + std::shared_ptr<A> p(ptr, test_deleter<A>(3)); + test_deleter<A>* d = std::get_deleter<test_deleter<A> >(p); + assert(test_deleter<A>::count == 1); + assert(test_deleter<A>::dealloc_count == 0); + assert(d); + assert(d->state() == 3); + } + assert(A::count == 0); + assert(test_deleter<A>::count == 0); + assert(test_deleter<A>::dealloc_count == 1); + } + test_deleter<A>::dealloc_count = 0; + { + { + std::shared_ptr<A> p(nullptr, test_deleter<A>(3)); + test_deleter<A>* d = std::get_deleter<test_deleter<A> >(p); + assert(test_deleter<A>::count == 1); + assert(test_deleter<A>::dealloc_count == 0); + assert(d); + assert(d->state() == 3); + } + assert(A::count == 0); + assert(test_deleter<A>::count == 0); + assert(test_deleter<A>::dealloc_count == 1); + } + test_deleter<A>::dealloc_count = 0; + { + std::shared_ptr<A> p(nullptr, test_deleter<A>(3)); + std::default_delete<A>* d = std::get_deleter<std::default_delete<A> >(p); + assert(d == 0); + } +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/auto_ptr_Y.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/auto_ptr_Y.pass.cpp new file mode 100644 index 00000000000..21cdf4a13e4 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/auto_ptr_Y.pass.cpp @@ -0,0 +1,113 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); + +#include <memory> +#include <type_traits> +#include <cassert> + +struct B +{ + static int count; + + B() {++count;} + B(const B&) {++count;} + virtual ~B() {--count;} +}; + +int B::count = 0; + +struct A + : public B +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +int main() +{ + { + std::auto_ptr<A> pA(new A); + A* ptrA = pA.get(); + { + std::shared_ptr<B> pB(new B); + pB = std::move(pA); + assert(B::count == 1); + assert(A::count == 1); + assert(pB.use_count() == 1); + assert(pA.get() == 0); + assert(pB.get() == ptrA); + } + assert(B::count == 0); + assert(A::count == 0); + } + assert(B::count == 0); + assert(A::count == 0); + { + std::auto_ptr<A> pA; + A* ptrA = pA.get(); + { + std::shared_ptr<B> pB(new B); + pB = std::move(pA); + assert(B::count == 0); + assert(A::count == 0); + assert(pB.use_count() == 1); + assert(pA.get() == 0); + assert(pB.get() == ptrA); + } + assert(B::count == 0); + assert(A::count == 0); + } + assert(B::count == 0); + assert(A::count == 0); + { + std::auto_ptr<A> pA(new A); + A* ptrA = pA.get(); + { + std::shared_ptr<B> pB; + pB = std::move(pA); + assert(B::count == 1); + assert(A::count == 1); + assert(pB.use_count() == 1); + assert(pA.get() == 0); + assert(pB.get() == ptrA); + } + assert(B::count == 0); + assert(A::count == 0); + } + assert(B::count == 0); + assert(A::count == 0); + { + std::auto_ptr<A> pA; + A* ptrA = pA.get(); + { + std::shared_ptr<B> pB; + pB = std::move(pA); + assert(B::count == 0); + assert(A::count == 0); + assert(pB.use_count() == 1); + assert(pA.get() == 0); + assert(pB.get() == ptrA); + } + assert(B::count == 0); + assert(A::count == 0); + } + assert(B::count == 0); + assert(A::count == 0); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr.pass.cpp new file mode 100644 index 00000000000..5d27a8865f0 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr.pass.cpp @@ -0,0 +1,121 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// shared_ptr& operator=(const shared_ptr& r); + +#include <memory> +#include <type_traits> +#include <cassert> + +struct B +{ + static int count; + + B() {++count;} + B(const B&) {++count;} + virtual ~B() {--count;} +}; + +int B::count = 0; + +struct A + : public B +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +int main() +{ + { + const std::shared_ptr<A> pA(new A); + A* ptrA = pA.get(); + { + std::shared_ptr<A> pB(new A); + pB = pA; + assert(B::count == 1); + assert(A::count == 1); + assert(pB.use_count() == 2); + assert(pA.use_count() == 2); + assert(pA.get() == pB.get()); + assert(pB.get() == ptrA); + } + assert(pA.use_count() == 1); + assert(B::count == 1); + assert(A::count == 1); + } + assert(B::count == 0); + assert(A::count == 0); + { + const std::shared_ptr<A> pA; + A* ptrA = pA.get(); + { + std::shared_ptr<A> pB(new A); + pB = pA; + assert(B::count == 0); + assert(A::count == 0); + assert(pB.use_count() == 0); + assert(pA.use_count() == 0); + assert(pA.get() == pB.get()); + assert(pB.get() == ptrA); + } + assert(pA.use_count() == 0); + assert(B::count == 0); + assert(A::count == 0); + } + assert(B::count == 0); + assert(A::count == 0); + { + const std::shared_ptr<A> pA(new A); + A* ptrA = pA.get(); + { + std::shared_ptr<A> pB; + pB = pA; + assert(B::count == 1); + assert(A::count == 1); + assert(pB.use_count() == 2); + assert(pA.use_count() == 2); + assert(pA.get() == pB.get()); + assert(pB.get() == ptrA); + } + assert(pA.use_count() == 1); + assert(B::count == 1); + assert(A::count == 1); + } + assert(B::count == 0); + assert(A::count == 0); + { + const std::shared_ptr<A> pA; + A* ptrA = pA.get(); + { + std::shared_ptr<A> pB; + pB = pA; + assert(B::count == 0); + assert(A::count == 0); + assert(pB.use_count() == 0); + assert(pA.use_count() == 0); + assert(pA.get() == pB.get()); + assert(pB.get() == ptrA); + } + assert(pA.use_count() == 0); + assert(B::count == 0); + assert(A::count == 0); + } + assert(B::count == 0); + assert(A::count == 0); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr_Y.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr_Y.pass.cpp new file mode 100644 index 00000000000..abd3d378eb7 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr_Y.pass.cpp @@ -0,0 +1,121 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r); + +#include <memory> +#include <type_traits> +#include <cassert> + +struct B +{ + static int count; + + B() {++count;} + B(const B&) {++count;} + virtual ~B() {--count;} +}; + +int B::count = 0; + +struct A + : public B +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +int main() +{ + { + const std::shared_ptr<A> pA(new A); + A* ptrA = pA.get(); + { + std::shared_ptr<B> pB(new B); + pB = pA; + assert(B::count == 1); + assert(A::count == 1); + assert(pB.use_count() == 2); + assert(pA.use_count() == 2); + assert(pA.get() == pB.get()); + assert(pB.get() == ptrA); + } + assert(pA.use_count() == 1); + assert(B::count == 1); + assert(A::count == 1); + } + assert(B::count == 0); + assert(A::count == 0); + { + const std::shared_ptr<A> pA; + A* ptrA = pA.get(); + { + std::shared_ptr<B> pB(new B); + pB = pA; + assert(B::count == 0); + assert(A::count == 0); + assert(pB.use_count() == 0); + assert(pA.use_count() == 0); + assert(pA.get() == pB.get()); + assert(pB.get() == ptrA); + } + assert(pA.use_count() == 0); + assert(B::count == 0); + assert(A::count == 0); + } + assert(B::count == 0); + assert(A::count == 0); + { + const std::shared_ptr<A> pA(new A); + A* ptrA = pA.get(); + { + std::shared_ptr<B> pB; + pB = pA; + assert(B::count == 1); + assert(A::count == 1); + assert(pB.use_count() == 2); + assert(pA.use_count() == 2); + assert(pA.get() == pB.get()); + assert(pB.get() == ptrA); + } + assert(pA.use_count() == 1); + assert(B::count == 1); + assert(A::count == 1); + } + assert(B::count == 0); + assert(A::count == 0); + { + const std::shared_ptr<A> pA; + A* ptrA = pA.get(); + { + std::shared_ptr<B> pB; + pB = pA; + assert(B::count == 0); + assert(A::count == 0); + assert(pB.use_count() == 0); + assert(pA.use_count() == 0); + assert(pA.get() == pB.get()); + assert(pB.get() == ptrA); + } + assert(pA.use_count() == 0); + assert(B::count == 0); + assert(A::count == 0); + } + assert(B::count == 0); + assert(A::count == 0); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr_Y_rv.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr_Y_rv.pass.cpp new file mode 100644 index 00000000000..93956bcae66 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr_Y_rv.pass.cpp @@ -0,0 +1,123 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r); + +#include <memory> +#include <type_traits> +#include <cassert> + +struct B +{ + static int count; + + B() {++count;} + B(const B&) {++count;} + virtual ~B() {--count;} +}; + +int B::count = 0; + +struct A + : public B +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +int main() +{ +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + { + std::shared_ptr<A> pA(new A); + A* ptrA = pA.get(); + { + std::shared_ptr<B> pB(new B); + pB = std::move(pA); + assert(B::count == 1); + assert(A::count == 1); + assert(pB.use_count() == 1); + assert(pA.use_count() == 0); + assert(pA.get() == 0); + assert(pB.get() == ptrA); + } + assert(pA.use_count() == 0); + assert(B::count == 0); + assert(A::count == 0); + } + assert(B::count == 0); + assert(A::count == 0); + { + std::shared_ptr<A> pA; + A* ptrA = pA.get(); + { + std::shared_ptr<B> pB(new B); + pB = std::move(pA); + assert(B::count == 0); + assert(A::count == 0); + assert(pB.use_count() == 0); + assert(pA.use_count() == 0); + assert(pA.get() == 0); + assert(pB.get() == ptrA); + } + assert(pA.use_count() == 0); + assert(B::count == 0); + assert(A::count == 0); + } + assert(B::count == 0); + assert(A::count == 0); + { + std::shared_ptr<A> pA(new A); + A* ptrA = pA.get(); + { + std::shared_ptr<B> pB; + pB = std::move(pA); + assert(B::count == 1); + assert(A::count == 1); + assert(pB.use_count() == 1); + assert(pA.use_count() == 0); + assert(pA.get() == 0); + assert(pB.get() == ptrA); + } + assert(pA.use_count() == 0); + assert(B::count == 0); + assert(A::count == 0); + } + assert(B::count == 0); + assert(A::count == 0); + { + std::shared_ptr<A> pA; + A* ptrA = pA.get(); + { + std::shared_ptr<B> pB; + pB = std::move(pA); + assert(B::count == 0); + assert(A::count == 0); + assert(pB.use_count() == 0); + assert(pA.use_count() == 0); + assert(pA.get() == 0); + assert(pB.get() == ptrA); + } + assert(pA.use_count() == 0); + assert(B::count == 0); + assert(A::count == 0); + } + assert(B::count == 0); + assert(A::count == 0); +#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr_rv.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr_rv.pass.cpp new file mode 100644 index 00000000000..4194890dda2 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr_rv.pass.cpp @@ -0,0 +1,123 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// shared_ptr& operator=(shared_ptr&& r); + +#include <memory> +#include <type_traits> +#include <cassert> + +struct B +{ + static int count; + + B() {++count;} + B(const B&) {++count;} + virtual ~B() {--count;} +}; + +int B::count = 0; + +struct A + : public B +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +int main() +{ +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + { + std::shared_ptr<A> pA(new A); + A* ptrA = pA.get(); + { + std::shared_ptr<A> pB(new A); + pB = std::move(pA); + assert(B::count == 1); + assert(A::count == 1); + assert(pB.use_count() == 1); + assert(pA.use_count() == 0); + assert(pA.get() == 0); + assert(pB.get() == ptrA); + } + assert(pA.use_count() == 0); + assert(B::count == 0); + assert(A::count == 0); + } + assert(B::count == 0); + assert(A::count == 0); + { + std::shared_ptr<A> pA; + A* ptrA = pA.get(); + { + std::shared_ptr<A> pB(new A); + pB = std::move(pA); + assert(B::count == 0); + assert(A::count == 0); + assert(pB.use_count() == 0); + assert(pA.use_count() == 0); + assert(pA.get() == 0); + assert(pB.get() == ptrA); + } + assert(pA.use_count() == 0); + assert(B::count == 0); + assert(A::count == 0); + } + assert(B::count == 0); + assert(A::count == 0); + { + std::shared_ptr<A> pA(new A); + A* ptrA = pA.get(); + { + std::shared_ptr<A> pB; + pB = std::move(pA); + assert(B::count == 1); + assert(A::count == 1); + assert(pB.use_count() == 1); + assert(pA.use_count() == 0); + assert(pA.get() == 0); + assert(pB.get() == ptrA); + } + assert(pA.use_count() == 0); + assert(B::count == 0); + assert(A::count == 0); + } + assert(B::count == 0); + assert(A::count == 0); + { + std::shared_ptr<A> pA; + A* ptrA = pA.get(); + { + std::shared_ptr<A> pB; + pB = std::move(pA); + assert(B::count == 0); + assert(A::count == 0); + assert(pB.use_count() == 0); + assert(pA.use_count() == 0); + assert(pA.get() == 0); + assert(pB.get() == ptrA); + } + assert(pA.use_count() == 0); + assert(B::count == 0); + assert(A::count == 0); + } + assert(B::count == 0); + assert(A::count == 0); +#endif // _LIBCXX_HAS_NO_RVALUE_REFERENCES +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/unique_ptr_Y.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/unique_ptr_Y.pass.cpp new file mode 100644 index 00000000000..742d8c1b2dd --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/unique_ptr_Y.pass.cpp @@ -0,0 +1,113 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r); + +#include <memory> +#include <type_traits> +#include <cassert> + +struct B +{ + static int count; + + B() {++count;} + B(const B&) {++count;} + virtual ~B() {--count;} +}; + +int B::count = 0; + +struct A + : public B +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +int main() +{ + { + std::unique_ptr<A> pA(new A); + A* ptrA = pA.get(); + { + std::shared_ptr<B> pB(new B); + pB = std::move(pA); + assert(B::count == 1); + assert(A::count == 1); + assert(pB.use_count() == 1); + assert(pA.get() == 0); + assert(pB.get() == ptrA); + } + assert(B::count == 0); + assert(A::count == 0); + } + assert(B::count == 0); + assert(A::count == 0); + { + std::unique_ptr<A> pA; + A* ptrA = pA.get(); + { + std::shared_ptr<B> pB(new B); + pB = std::move(pA); + assert(B::count == 0); + assert(A::count == 0); + assert(pB.use_count() == 1); + assert(pA.get() == 0); + assert(pB.get() == ptrA); + } + assert(B::count == 0); + assert(A::count == 0); + } + assert(B::count == 0); + assert(A::count == 0); + { + std::unique_ptr<A> pA(new A); + A* ptrA = pA.get(); + { + std::shared_ptr<B> pB; + pB = std::move(pA); + assert(B::count == 1); + assert(A::count == 1); + assert(pB.use_count() == 1); + assert(pA.get() == 0); + assert(pB.get() == ptrA); + } + assert(B::count == 0); + assert(A::count == 0); + } + assert(B::count == 0); + assert(A::count == 0); + { + std::unique_ptr<A> pA; + A* ptrA = pA.get(); + { + std::shared_ptr<B> pB; + pB = std::move(pA); + assert(B::count == 0); + assert(A::count == 0); + assert(pB.use_count() == 1); + assert(pA.get() == 0); + assert(pB.get() == ptrA); + } + assert(B::count == 0); + assert(A::count == 0); + } + assert(B::count == 0); + assert(A::count == 0); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cast/const_pointer_cast.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cast/const_pointer_cast.pass.cpp new file mode 100644 index 00000000000..7d771d03c71 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cast/const_pointer_cast.pass.cpp @@ -0,0 +1,57 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// template<class T, class U> shared_ptr<T> const_pointer_cast(const shared_ptr<U>& r); + +#include <memory> +#include <type_traits> +#include <cassert> + +struct B +{ + static int count; + + B() {++count;} + B(const B&) {++count;} + virtual ~B() {--count;} +}; + +int B::count = 0; + +struct A + : public B +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +int main() +{ + { + const std::shared_ptr<const A> pA(new A); + std::shared_ptr<A> pB = std::const_pointer_cast<A>(pA); + assert(pB.get() == pA.get()); + assert(!pB.owner_before(pA) && !pA.owner_before(pB)); + } + { + const std::shared_ptr<const A> pA; + std::shared_ptr<A> pB = std::const_pointer_cast<A>(pA); + assert(pB.get() == pA.get()); + assert(!pB.owner_before(pA) && !pA.owner_before(pB)); + } +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cast/dynamic_pointer_cast.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cast/dynamic_pointer_cast.pass.cpp new file mode 100644 index 00000000000..4f88a5c4351 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cast/dynamic_pointer_cast.pass.cpp @@ -0,0 +1,57 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// template<class T, class U> shared_ptr<T> dynamic_pointer_cast(const shared_ptr<U>& r); + +#include <memory> +#include <type_traits> +#include <cassert> + +struct B +{ + static int count; + + B() {++count;} + B(const B&) {++count;} + virtual ~B() {--count;} +}; + +int B::count = 0; + +struct A + : public B +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +int main() +{ + { + const std::shared_ptr<B> pB(new A); + std::shared_ptr<A> pA = std::dynamic_pointer_cast<A>(pB); + assert(pA.get() == pB.get()); + assert(!pB.owner_before(pA) && !pA.owner_before(pB)); + } + { + const std::shared_ptr<B> pB(new B); + std::shared_ptr<A> pA = std::dynamic_pointer_cast<A>(pB); + assert(pA.get() == 0); + assert(pA.use_count() == 0); + } +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cast/static_pointer_cast.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cast/static_pointer_cast.pass.cpp new file mode 100644 index 00000000000..98fa13801a8 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cast/static_pointer_cast.pass.cpp @@ -0,0 +1,69 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// template<class T, class U> shared_ptr<T> static_pointer_cast(const shared_ptr<U>& r); + +#include <memory> +#include <type_traits> +#include <cassert> + +struct B +{ + static int count; + + B() {++count;} + B(const B&) {++count;} + virtual ~B() {--count;} +}; + +int B::count = 0; + +struct A + : public B +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +int main() +{ + { + const std::shared_ptr<A> pA(new A); + std::shared_ptr<B> pB = std::static_pointer_cast<B>(pA); + assert(pB.get() == pA.get()); + assert(!pB.owner_before(pA) && !pA.owner_before(pB)); + } + { + const std::shared_ptr<B> pA(new A); + std::shared_ptr<A> pB = std::static_pointer_cast<A>(pA); + assert(pB.get() == pA.get()); + assert(!pB.owner_before(pA) && !pA.owner_before(pB)); + } + { + const std::shared_ptr<A> pA; + std::shared_ptr<B> pB = std::static_pointer_cast<B>(pA); + assert(pB.get() == pA.get()); + assert(!pB.owner_before(pA) && !pA.owner_before(pB)); + } + { + const std::shared_ptr<B> pA; + std::shared_ptr<A> pB = std::static_pointer_cast<A>(pA); + assert(pB.get() == pA.get()); + assert(!pB.owner_before(pA) && !pA.owner_before(pB)); + } +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/cmp_nullptr.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/cmp_nullptr.pass.cpp new file mode 100644 index 00000000000..f40cbc3d032 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/cmp_nullptr.pass.cpp @@ -0,0 +1,69 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// template <class T> +// bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept; +// template <class T> +// bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept; +// template <class T> +// bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept; +// template <class T> +// bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept; +// template <class T> +// bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept; +// template <class T> +// bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept; +// template <class T> +// bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept; +// template <class T> +// bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept; +// template <class T> +// bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept; +// template <class T> +// bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept; +// template <class T> +// bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept; +// template <class T> +// bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept; + +#include <memory> +#include <cassert> + +void do_nothing(int*) {} + +int main() +{ + const std::shared_ptr<int> p1(new int(1)); + assert(!(p1 == nullptr)); + assert(!(nullptr == p1)); + assert(!(p1 < nullptr)); + assert( (nullptr < p1)); + assert(!(p1 <= nullptr)); + assert( (nullptr <= p1)); + assert( (p1 > nullptr)); + assert(!(nullptr > p1)); + assert( (p1 >= nullptr)); + assert(!(nullptr >= p1)); + + const std::shared_ptr<int> p2; + assert( (p2 == nullptr)); + assert( (nullptr == p2)); + assert(!(p2 < nullptr)); + assert(!(nullptr < p2)); + assert( (p2 <= nullptr)); + assert( (nullptr <= p2)); + assert(!(p2 > nullptr)); + assert(!(nullptr > p2)); + assert( (p2 >= nullptr)); + assert( (nullptr >= p2)); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/eq.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/eq.pass.cpp new file mode 100644 index 00000000000..c4cd1693f49 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/eq.pass.cpp @@ -0,0 +1,31 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// template<class T, class U> bool operator==(const shared_ptr<T>& a, const shared_ptr<U>& b); +// template<class T, class U> bool operator!=(const shared_ptr<T>& a, const shared_ptr<U>& b); + +#include <memory> +#include <cassert> + +void do_nothing(int*) {} + +int main() +{ + int* ptr1(new int); + int* ptr2(new int); + const std::shared_ptr<int> p1(ptr1); + const std::shared_ptr<int> p2(ptr2); + const std::shared_ptr<int> p3(ptr2, do_nothing); + assert(p1 != p2); + assert(p2 == p3); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/lt.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/lt.pass.cpp new file mode 100644 index 00000000000..5a90a9a325f --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/lt.pass.cpp @@ -0,0 +1,30 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// template<class T, class U> bool operator<(const shared_ptr<T>& a, const shared_ptr<U>& b); + +#include <memory> +#include <cassert> + +void do_nothing(int*) {} + +int main() +{ + int* ptr1(new int); + int* ptr2(new int); + const std::shared_ptr<int> p1(ptr1); + const std::shared_ptr<int> p2(ptr2); + const std::shared_ptr<int> p3(ptr2, do_nothing); + assert((p1 < p2) == (ptr1 < ptr2)); + assert(!(p2 < p3) && !(p3 < p2)); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/auto_ptr.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/auto_ptr.pass.cpp new file mode 100644 index 00000000000..28e49d6ecc4 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/auto_ptr.pass.cpp @@ -0,0 +1,104 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// template<class Y> explicit shared_ptr(auto_ptr<Y>&& r); + +// UNSUPPORTED: asan, msan + +#include <memory> +#include <new> +#include <cstdlib> +#include <cassert> + +bool throw_next = false; + +void* operator new(std::size_t s) throw(std::bad_alloc) +{ + if (throw_next) + throw std::bad_alloc(); + return std::malloc(s); +} + +void operator delete(void* p) throw() +{ + std::free(p); +} + +struct B +{ + static int count; + + B() {++count;} + B(const B&) {++count;} + virtual ~B() {--count;} +}; + +int B::count = 0; + +struct A + : public B +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +int main() +{ + { + std::auto_ptr<A> ptr(new A); + A* raw_ptr = ptr.get(); +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + std::shared_ptr<B> p(std::move(ptr)); +#else + std::shared_ptr<B> p(ptr); +#endif + assert(A::count == 1); + assert(B::count == 1); + assert(p.use_count() == 1); + assert(p.get() == raw_ptr); + assert(ptr.get() == 0); + } + assert(A::count == 0); + { + std::auto_ptr<A> ptr(new A); + A* raw_ptr = ptr.get(); + throw_next = true; + try + { +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + std::shared_ptr<B> p(std::move(ptr)); +#else + std::shared_ptr<B> p(ptr); +#endif + assert(false); + } + catch (...) + { +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + assert(A::count == 1); + assert(B::count == 1); + assert(ptr.get() == raw_ptr); +#else + // Without rvalue references, ptr got copied into + // the shared_ptr destructor and the copy was + // destroyed during unwinding. + assert(A::count == 0); + assert(B::count == 0); +#endif + } + } + assert(A::count == 0); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/default.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/default.pass.cpp new file mode 100644 index 00000000000..9af5d7ea861 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/default.pass.cpp @@ -0,0 +1,22 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr(); + +#include <memory> +#include <cassert> + +int main() +{ + std::shared_ptr<int> p; + assert(p.use_count() == 0); + assert(p.get() == 0); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/nullptr_t.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/nullptr_t.pass.cpp new file mode 100644 index 00000000000..3a9b3a9ca1d --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/nullptr_t.pass.cpp @@ -0,0 +1,22 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr(nullptr_t) + +#include <memory> +#include <cassert> + +int main() +{ + std::shared_ptr<int> p(nullptr); + assert(p.use_count() == 0); + assert(p.get() == 0); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/nullptr_t_deleter.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/nullptr_t_deleter.pass.cpp new file mode 100644 index 00000000000..7d4dc38d4b9 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/nullptr_t_deleter.pass.cpp @@ -0,0 +1,47 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// template<class D> shared_ptr(nullptr_t, D d); + +#include <memory> +#include <cassert> +#include "../test_deleter.h" + +struct A +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +int main() +{ + { + std::shared_ptr<A> p(nullptr, test_deleter<A>(3)); + assert(A::count == 0); + assert(p.use_count() == 1); + assert(p.get() == 0); + test_deleter<A>* d = std::get_deleter<test_deleter<A> >(p); + assert(test_deleter<A>::count == 1); + assert(test_deleter<A>::dealloc_count == 0); + assert(d); + assert(d->state() == 3); + } + assert(A::count == 0); + assert(test_deleter<A>::count == 0); + assert(test_deleter<A>::dealloc_count == 1); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/nullptr_t_deleter_allocator.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/nullptr_t_deleter_allocator.pass.cpp new file mode 100644 index 00000000000..b67f31ee45a --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/nullptr_t_deleter_allocator.pass.cpp @@ -0,0 +1,85 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// template<class D, class A> shared_ptr(nullptr_t, D d, A a); + +#include <memory> +#include <cassert> +#include "../test_deleter.h" +#include "test_allocator.h" +#include "min_allocator.h" + +struct A +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +int main() +{ + { + std::shared_ptr<A> p(nullptr, test_deleter<A>(3), test_allocator<A>(5)); + assert(A::count == 0); + assert(p.use_count() == 1); + assert(p.get() == 0); + test_deleter<A>* d = std::get_deleter<test_deleter<A> >(p); + assert(test_deleter<A>::count == 1); + assert(test_deleter<A>::dealloc_count == 0); + assert(d); + assert(d->state() == 3); + assert(test_allocator<A>::count == 1); + assert(test_allocator<A>::alloc_count == 1); + } + assert(A::count == 0); + assert(test_deleter<A>::count == 0); + assert(test_deleter<A>::dealloc_count == 1); + assert(test_allocator<A>::count == 0); + assert(test_allocator<A>::alloc_count == 0); + test_deleter<A>::dealloc_count = 0; + // Test an allocator with a minimal interface + { + std::shared_ptr<A> p(nullptr, test_deleter<A>(1), bare_allocator<void>()); + assert(A::count == 0); + assert(p.use_count() == 1); + assert(p.get() == 0); + test_deleter<A>* d = std::get_deleter<test_deleter<A> >(p); + assert(test_deleter<A>::count ==1); + assert(test_deleter<A>::dealloc_count == 0); + assert(d); + assert(d->state() == 1); + } + assert(A::count == 0); + assert(test_deleter<A>::count == 0); + assert(test_deleter<A>::dealloc_count == 1); + test_deleter<A>::dealloc_count = 0; +#if __cplusplus >= 201103L + // Test an allocator that returns class-type pointers + { + std::shared_ptr<A> p(nullptr, test_deleter<A>(1), min_allocator<void>()); + assert(A::count == 0); + assert(p.use_count() == 1); + assert(p.get() == 0); + test_deleter<A>* d = std::get_deleter<test_deleter<A> >(p); + assert(test_deleter<A>::count ==1); + assert(test_deleter<A>::dealloc_count == 0); + assert(d); + assert(d->state() == 1); + } + assert(A::count == 0); + assert(test_deleter<A>::count == 0); + assert(test_deleter<A>::dealloc_count == 1); +#endif +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/nullptr_t_deleter_allocator_throw.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/nullptr_t_deleter_allocator_throw.pass.cpp new file mode 100644 index 00000000000..ab2c73e0c5f --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/nullptr_t_deleter_allocator_throw.pass.cpp @@ -0,0 +1,46 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// template<class D, class A> shared_ptr(nullptr_t, D d, A a); + +#include <memory> +#include <cassert> +#include "../test_deleter.h" +#include "test_allocator.h" + +struct A +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +int main() +{ + try + { + test_allocator<A>::throw_after = 0; + std::shared_ptr<A> p(nullptr, test_deleter<A>(3), test_allocator<A>(5)); + assert(false); + } + catch (std::bad_alloc&) + { + assert(A::count == 0); + assert(test_deleter<A>::count == 0); + assert(test_deleter<A>::dealloc_count == 1); + assert(test_allocator<A>::count == 0); + assert(test_allocator<A>::alloc_count == 0); + } +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/nullptr_t_deleter_throw.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/nullptr_t_deleter_throw.pass.cpp new file mode 100644 index 00000000000..a8588cc6735 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/nullptr_t_deleter_throw.pass.cpp @@ -0,0 +1,63 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// template<class D> shared_ptr(nullptr_t, D d); + +// UNSUPPORTED: asan, msan + +#include <memory> +#include <cassert> +#include <new> +#include <cstdlib> +#include "../test_deleter.h" + +struct A +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +bool throw_next = false; + +void* operator new(std::size_t s) throw(std::bad_alloc) +{ + if (throw_next) + throw std::bad_alloc(); + return std::malloc(s); +} + +void operator delete(void* p) throw() +{ + std::free(p); +} + +int main() +{ + throw_next = true; + try + { + std::shared_ptr<A> p(nullptr, test_deleter<A>(3)); + assert(false); + } + catch (std::bad_alloc&) + { + assert(A::count == 0); + assert(test_deleter<A>::count == 0); + assert(test_deleter<A>::dealloc_count == 1); + } +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer.pass.cpp new file mode 100644 index 00000000000..c9cffa1fe25 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer.pass.cpp @@ -0,0 +1,46 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// template<class Y> explicit shared_ptr(Y* p); + +#include <memory> +#include <cassert> + +struct A +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +int main() +{ + { + A* ptr = new A; + std::shared_ptr<A> p(ptr); + assert(A::count == 1); + assert(p.use_count() == 1); + assert(p.get() == ptr); + } + assert(A::count == 0); + { + A* ptr = new A; + std::shared_ptr<void> p(ptr); + assert(A::count == 1); + assert(p.use_count() == 1); + assert(p.get() == ptr); + } + assert(A::count == 0); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer_deleter.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer_deleter.pass.cpp new file mode 100644 index 00000000000..43eedee176c --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer_deleter.pass.cpp @@ -0,0 +1,48 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// template<class Y, class D> shared_ptr(Y* p, D d); + +#include <memory> +#include <cassert> +#include "../test_deleter.h" + +struct A +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +int main() +{ + { + A* ptr = new A; + std::shared_ptr<A> p(ptr, test_deleter<A>(3)); + assert(A::count == 1); + assert(p.use_count() == 1); + assert(p.get() == ptr); + test_deleter<A>* d = std::get_deleter<test_deleter<A> >(p); + assert(test_deleter<A>::count == 1); + assert(test_deleter<A>::dealloc_count == 0); + assert(d); + assert(d->state() == 3); + } + assert(A::count == 0); + assert(test_deleter<A>::count == 0); + assert(test_deleter<A>::dealloc_count == 1); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer_deleter_allocator.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer_deleter_allocator.pass.cpp new file mode 100644 index 00000000000..1a9c09cdb78 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer_deleter_allocator.pass.cpp @@ -0,0 +1,89 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// template<class Y, class D, class A> shared_ptr(Y* p, D d, A a); + +#include <memory> +#include <cassert> +#include "../test_deleter.h" +#include "test_allocator.h" +#include "min_allocator.h" + +struct A +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + + +int main() +{ + { + A* ptr = new A; + std::shared_ptr<A> p(ptr, test_deleter<A>(3), test_allocator<A>(5)); + assert(A::count == 1); + assert(p.use_count() == 1); + assert(p.get() == ptr); + test_deleter<A>* d = std::get_deleter<test_deleter<A> >(p); + assert(test_deleter<A>::count == 1); + assert(test_deleter<A>::dealloc_count == 0); + assert(d); + assert(d->state() == 3); + assert(test_allocator<A>::count == 1); + assert(test_allocator<A>::alloc_count == 1); + } + assert(A::count == 0); + assert(test_deleter<A>::count == 0); + assert(test_deleter<A>::dealloc_count == 1); + assert(test_allocator<A>::count == 0); + assert(test_allocator<A>::alloc_count == 0); + test_deleter<A>::dealloc_count = 0; + // Test an allocator with a minimal interface + { + A* ptr = new A; + std::shared_ptr<A> p(ptr, test_deleter<A>(3), bare_allocator<void>()); + assert(A::count == 1); + assert(p.use_count() == 1); + assert(p.get() == ptr); + test_deleter<A>* d = std::get_deleter<test_deleter<A> >(p); + assert(test_deleter<A>::count == 1); + assert(test_deleter<A>::dealloc_count == 0); + assert(d); + assert(d->state() == 3); + } + assert(A::count == 0); + assert(test_deleter<A>::count == 0); + assert(test_deleter<A>::dealloc_count == 1); + test_deleter<A>::dealloc_count = 0; +#if __cplusplus >= 201103L + // Test an allocator that returns class-type pointers + { + A* ptr = new A; + std::shared_ptr<A> p(ptr, test_deleter<A>(3), min_allocator<void>()); + assert(A::count == 1); + assert(p.use_count() == 1); + assert(p.get() == ptr); + test_deleter<A>* d = std::get_deleter<test_deleter<A> >(p); + assert(test_deleter<A>::count == 1); + assert(test_deleter<A>::dealloc_count == 0); + assert(d); + assert(d->state() == 3); + } + assert(A::count == 0); + assert(test_deleter<A>::count == 0); + assert(test_deleter<A>::dealloc_count == 1); +#endif +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer_deleter_allocator_throw.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer_deleter_allocator_throw.pass.cpp new file mode 100644 index 00000000000..4220993a5fd --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer_deleter_allocator_throw.pass.cpp @@ -0,0 +1,47 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// template<class Y, class D, class A> shared_ptr(Y* p, D d, A a); + +#include <memory> +#include <cassert> +#include "../test_deleter.h" +#include "test_allocator.h" + +struct A +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +int main() +{ + A* ptr = new A; + try + { + test_allocator<A>::throw_after = 0; + std::shared_ptr<A> p(ptr, test_deleter<A>(3), test_allocator<A>(5)); + assert(false); + } + catch (std::bad_alloc&) + { + assert(A::count == 0); + assert(test_deleter<A>::count == 0); + assert(test_deleter<A>::dealloc_count == 1); + assert(test_allocator<A>::count == 0); + assert(test_allocator<A>::alloc_count == 0); + } +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer_deleter_throw.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer_deleter_throw.pass.cpp new file mode 100644 index 00000000000..b024f7e15a5 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer_deleter_throw.pass.cpp @@ -0,0 +1,64 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// template<class Y, class D> shared_ptr(Y* p, D d); + +// UNSUPPORTED: asan, msan + +#include <memory> +#include <cassert> +#include <new> +#include <cstdlib> +#include "../test_deleter.h" + +struct A +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +bool throw_next = false; + +void* operator new(std::size_t s) throw(std::bad_alloc) +{ + if (throw_next) + throw std::bad_alloc(); + return std::malloc(s); +} + +void operator delete(void* p) throw() +{ + std::free(p); +} + +int main() +{ + A* ptr = new A; + throw_next = true; + try + { + std::shared_ptr<A> p(ptr, test_deleter<A>(3)); + assert(false); + } + catch (std::bad_alloc&) + { + assert(A::count == 0); + assert(test_deleter<A>::count == 0); + assert(test_deleter<A>::dealloc_count == 1); + } +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer_throw.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer_throw.pass.cpp new file mode 100644 index 00000000000..28fb8bfd91c --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer_throw.pass.cpp @@ -0,0 +1,62 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// template<class Y> explicit shared_ptr(Y* p); + +// UNSUPPORTED: asan, msan + +#include <memory> +#include <new> +#include <cstdlib> +#include <cassert> + +struct A +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +bool throw_next = false; + +void* operator new(std::size_t s) throw(std::bad_alloc) +{ + if (throw_next) + throw std::bad_alloc(); + return std::malloc(s); +} + +void operator delete(void* p) throw() +{ + std::free(p); +} + +int main() +{ + { + A* ptr = new A; + throw_next = true; + assert(A::count == 1); + try + { + std::shared_ptr<A> p(ptr); + assert(false); + } + catch (std::bad_alloc&) + { + assert(A::count == 0); + } + } +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr.pass.cpp new file mode 100644 index 00000000000..e1dcdfc8165 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr.pass.cpp @@ -0,0 +1,62 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// shared_ptr(const shared_ptr& r); + +#include <memory> +#include <cassert> + +struct A +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +int main() +{ + { + std::shared_ptr<A> pA(new A); + assert(pA.use_count() == 1); + assert(A::count == 1); + { + std::shared_ptr<A> pA2(pA); + assert(A::count == 1); + assert(pA.use_count() == 2); + assert(pA2.use_count() == 2); + assert(pA2.get() == pA.get()); + } + assert(pA.use_count() == 1); + assert(A::count == 1); + } + assert(A::count == 0); + { + std::shared_ptr<A> pA; + assert(pA.use_count() == 0); + assert(A::count == 0); + { + std::shared_ptr<A> pA2(pA); + assert(A::count == 0); + assert(pA.use_count() == 0); + assert(pA2.use_count() == 0); + assert(pA2.get() == pA.get()); + } + assert(pA.use_count() == 0); + assert(A::count == 0); + } + assert(A::count == 0); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr_Y.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr_Y.pass.cpp new file mode 100644 index 00000000000..8b5ffdc1475 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr_Y.pass.cpp @@ -0,0 +1,97 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// template<class Y> shared_ptr(const shared_ptr<Y>& r); + +#include <memory> +#include <type_traits> +#include <cassert> + +struct B +{ + static int count; + + B() {++count;} + B(const B&) {++count;} + virtual ~B() {--count;} +}; + +int B::count = 0; + +struct A + : public B +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +struct C +{ + static int count; + + C() {++count;} + C(const C&) {++count;} + virtual ~C() {--count;} +}; + +int C::count = 0; + +int main() +{ + static_assert(( std::is_convertible<std::shared_ptr<A>, std::shared_ptr<B> >::value), ""); + static_assert((!std::is_convertible<std::shared_ptr<B>, std::shared_ptr<A> >::value), ""); + static_assert((!std::is_convertible<std::shared_ptr<A>, std::shared_ptr<C> >::value), ""); + { + const std::shared_ptr<A> pA(new A); + assert(pA.use_count() == 1); + assert(B::count == 1); + assert(A::count == 1); + { + std::shared_ptr<B> pB(pA); + assert(B::count == 1); + assert(A::count == 1); + assert(pB.use_count() == 2); + assert(pA.use_count() == 2); + assert(pA.get() == pB.get()); + } + assert(pA.use_count() == 1); + assert(B::count == 1); + assert(A::count == 1); + } + assert(B::count == 0); + assert(A::count == 0); + { + std::shared_ptr<A> pA; + assert(pA.use_count() == 0); + assert(B::count == 0); + assert(A::count == 0); + { + std::shared_ptr<B> pB(pA); + assert(B::count == 0); + assert(A::count == 0); + assert(pB.use_count() == 0); + assert(pA.use_count() == 0); + assert(pA.get() == pB.get()); + } + assert(pA.use_count() == 0); + assert(B::count == 0); + assert(A::count == 0); + } + assert(B::count == 0); + assert(A::count == 0); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr_Y_rv.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr_Y_rv.pass.cpp new file mode 100644 index 00000000000..f041d9451a6 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr_Y_rv.pass.cpp @@ -0,0 +1,109 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// template<class Y> shared_ptr(shared_ptr<Y>&& r); + +#include <memory> +#include <type_traits> +#include <cassert> + +struct B +{ + static int count; + + B() {++count;} + B(const B&) {++count;} + virtual ~B() {--count;} +}; + +int B::count = 0; + +struct A + : public B +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +struct C +{ + static int count; + + C() {++count;} + C(const C&) {++count;} + virtual ~C() {--count;} +}; + +int C::count = 0; + +int main() +{ + static_assert(( std::is_convertible<std::shared_ptr<A>, std::shared_ptr<B> >::value), ""); + static_assert((!std::is_convertible<std::shared_ptr<B>, std::shared_ptr<A> >::value), ""); + static_assert((!std::is_convertible<std::shared_ptr<A>, std::shared_ptr<C> >::value), ""); + { + std::shared_ptr<A> pA(new A); + assert(pA.use_count() == 1); + assert(B::count == 1); + assert(A::count == 1); + { + B* p = pA.get(); + std::shared_ptr<B> pB(std::move(pA)); + assert(B::count == 1); + assert(A::count == 1); +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + assert(pB.use_count() == 1); + assert(pA.use_count() == 0); +#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES + assert(pB.use_count() == 2); + assert(pA.use_count() == 2); +#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES + assert(p == pB.get()); + } +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + assert(pA.use_count() == 0); + assert(B::count == 0); + assert(A::count == 0); +#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES + assert(pA.use_count() == 1); + assert(B::count == 1); + assert(A::count == 1); +#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES + } + assert(B::count == 0); + assert(A::count == 0); + { + std::shared_ptr<A> pA; + assert(pA.use_count() == 0); + assert(B::count == 0); + assert(A::count == 0); + { + std::shared_ptr<B> pB(pA); + assert(B::count == 0); + assert(A::count == 0); + assert(pB.use_count() == 0); + assert(pA.use_count() == 0); + assert(pA.get() == pB.get()); + } + assert(pA.use_count() == 0); + assert(B::count == 0); + assert(A::count == 0); + } + assert(B::count == 0); + assert(A::count == 0); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr_pointer.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr_pointer.pass.cpp new file mode 100644 index 00000000000..fb5262f3b0e --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr_pointer.pass.cpp @@ -0,0 +1,61 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p); + +#include <memory> +#include <cassert> + +struct B +{ + static int count; + + B() {++count;} + B(const B&) {++count;} + ~B() {--count;} +}; + +int B::count = 0; + +struct A +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +int main() +{ + { + std::shared_ptr<A> pA(new A); + assert(pA.use_count() == 1); + { + B b; + std::shared_ptr<B> pB(pA, &b); + assert(A::count == 1); + assert(B::count == 1); + assert(pA.use_count() == 2); + assert(pB.use_count() == 2); + assert(pB.get() == &b); + } + assert(pA.use_count() == 1); + assert(A::count == 1); + assert(B::count == 0); + } + assert(A::count == 0); + assert(B::count == 0); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr_rv.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr_rv.pass.cpp new file mode 100644 index 00000000000..b89178e201c --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr_rv.pass.cpp @@ -0,0 +1,73 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// shared_ptr(shared_ptr&& r); + +#include <memory> +#include <cassert> + +struct A +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +int main() +{ + { + std::shared_ptr<A> pA(new A); + assert(pA.use_count() == 1); + assert(A::count == 1); + { + A* p = pA.get(); + std::shared_ptr<A> pA2(std::move(pA)); + assert(A::count == 1); +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + assert(pA.use_count() == 0); + assert(pA2.use_count() == 1); +#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES + assert(pA.use_count() == 2); + assert(pA2.use_count() == 2); +#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES + assert(pA2.get() == p); + } +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + assert(pA.use_count() == 0); + assert(A::count == 0); +#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES + assert(pA.use_count() == 1); + assert(A::count == 1); +#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES + } + assert(A::count == 0); + { + std::shared_ptr<A> pA; + assert(pA.use_count() == 0); + assert(A::count == 0); + { + std::shared_ptr<A> pA2(std::move(pA)); + assert(A::count == 0); + assert(pA.use_count() == 0); + assert(pA2.use_count() == 0); + assert(pA2.get() == pA.get()); + } + assert(pA.use_count() == 0); + assert(A::count == 0); + } + assert(A::count == 0); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/unique_ptr.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/unique_ptr.pass.cpp new file mode 100644 index 00000000000..dc2a6afef21 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/unique_ptr.pass.cpp @@ -0,0 +1,103 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// template <class Y, class D> explicit shared_ptr(unique_ptr<Y, D>&&r); + +// UNSUPPORTED: asan, msan + +#include <memory> +#include <new> +#include <cstdlib> +#include <cassert> + +bool throw_next = false; + +void* operator new(std::size_t s) throw(std::bad_alloc) +{ + if (throw_next) + throw std::bad_alloc(); + return std::malloc(s); +} + +void operator delete(void* p) throw() +{ + std::free(p); +} + +struct B +{ + static int count; + + B() {++count;} + B(const B&) {++count;} + virtual ~B() {--count;} +}; + +int B::count = 0; + +struct A + : public B +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +void fn ( const std::shared_ptr<int> &) {} +void fn ( const std::shared_ptr<B> &) { assert (false); } + +int main() +{ + { + std::unique_ptr<A> ptr(new A); + A* raw_ptr = ptr.get(); + std::shared_ptr<B> p(std::move(ptr)); + assert(A::count == 1); + assert(B::count == 1); + assert(p.use_count() == 1); + assert(p.get() == raw_ptr); + assert(ptr.get() == 0); + } + assert(A::count == 0); + { + std::unique_ptr<A> ptr(new A); + A* raw_ptr = ptr.get(); + throw_next = true; + try + { + std::shared_ptr<B> p(std::move(ptr)); + assert(false); + } + catch (...) + { +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + assert(A::count == 1); + assert(B::count == 1); + assert(ptr.get() == raw_ptr); +#else + assert(A::count == 0); + assert(B::count == 0); + assert(ptr.get() == 0); +#endif + } + } + assert(A::count == 0); + + // LWG 2399 + { + throw_next = false; + fn(std::unique_ptr<int>(new int)); + } +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/weak_ptr.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/weak_ptr.pass.cpp new file mode 100644 index 00000000000..a9d8aff145a --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/weak_ptr.pass.cpp @@ -0,0 +1,79 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// template<class Y> explicit shared_ptr(const weak_ptr<Y>& r); + +#include <memory> +#include <cassert> + +struct B +{ + static int count; + + B() {++count;} + B(const B&) {++count;} + virtual ~B() {--count;} +}; + +int B::count = 0; + +struct A + : public B +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +int main() +{ + { + std::weak_ptr<A> wp; + try + { + std::shared_ptr<A> sp(wp); + assert(false); + } + catch (std::bad_weak_ptr&) + { + } + assert(A::count == 0); + } + { + std::shared_ptr<A> sp0(new A); + std::weak_ptr<A> wp(sp0); + std::shared_ptr<A> sp(wp); + assert(sp.use_count() == 2); + assert(sp.get() == sp0.get()); + assert(A::count == 1); + } + assert(A::count == 0); + { + std::shared_ptr<A> sp0(new A); + std::weak_ptr<A> wp(sp0); + sp0.reset(); + try + { + std::shared_ptr<A> sp(wp); + assert(false); + } + catch (std::bad_weak_ptr&) + { + } + } + assert(A::count == 0); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/allocate_shared.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/allocate_shared.pass.cpp new file mode 100644 index 00000000000..aa77dab5151 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/allocate_shared.pass.cpp @@ -0,0 +1,78 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// template<class T, class A, class... Args> +// shared_ptr<T> allocate_shared(const A& a, Args&&... args); + +#include <memory> +#include <new> +#include <cstdlib> +#include <cassert> +#include "test_allocator.h" +#include "min_allocator.h" + +int new_count = 0; + +struct A +{ + static int count; + + A(int i, char c) : int_(i), char_(c) {++count;} + A(const A& a) + : int_(a.int_), char_(a.char_) + {++count;} + ~A() {--count;} + + int get_int() const {return int_;} + char get_char() const {return char_;} +private: + int int_; + char char_; +}; + +int A::count = 0; + +int main() +{ + { + int i = 67; + char c = 'e'; + std::shared_ptr<A> p = std::allocate_shared<A>(test_allocator<A>(54), i, c); + assert(test_allocator<A>::alloc_count == 1); + assert(A::count == 1); + assert(p->get_int() == 67); + assert(p->get_char() == 'e'); + } + assert(A::count == 0); + assert(test_allocator<A>::alloc_count == 0); +#if __cplusplus >= 201103L + { + int i = 67; + char c = 'e'; + std::shared_ptr<A> p = std::allocate_shared<A>(min_allocator<void>(), i, c); + assert(A::count == 1); + assert(p->get_int() == 67); + assert(p->get_char() == 'e'); + } + assert(A::count == 0); + { + int i = 68; + char c = 'f'; + std::shared_ptr<A> p = std::allocate_shared<A>(bare_allocator<void>(), i, c); + assert(A::count == 1); + assert(p->get_int() == 68); + assert(p->get_char() == 'f'); + } + assert(A::count == 0); +#endif +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/allocate_shared_no_variadics.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/allocate_shared_no_variadics.pass.cpp new file mode 100644 index 00000000000..8dcd50e4941 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/allocate_shared_no_variadics.pass.cpp @@ -0,0 +1,118 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// template<class T, class A, class... Args> +// shared_ptr<T> allocate_shared(const A& a, Args&&... args); + +#define _LIBCPP_HAS_NO_VARIADICS +#include <memory> +#include <new> +#include <cstdlib> +#include <cassert> +#include "test_allocator.h" +#include "min_allocator.h" + +struct Zero +{ + static int count; + Zero() {++count;} + Zero(Zero const &) {++count;} + ~Zero() {--count;} +}; + +int Zero::count = 0; + +struct One +{ + static int count; + int value; + explicit One(int v) : value(v) {++count;} + One(One const & o) : value(o.value) {++count;} + ~One() {--count;} +}; + +int One::count = 0; + + +struct Two +{ + static int count; + int value; + Two(int v, int) : value(v) {++count;} + Two(Two const & o) : value(o.value) {++count;} + ~Two() {--count;} +}; + +int Two::count = 0; + +struct Three +{ + static int count; + int value; + Three(int v, int, int) : value(v) {++count;} + Three(Three const & o) : value(o.value) {++count;} + ~Three() {--count;} +}; + +int Three::count = 0; + +template <class Alloc> +void test() +{ + int const bad = -1; + { + std::shared_ptr<Zero> p = std::allocate_shared<Zero>(Alloc()); + assert(Zero::count == 1); + } + assert(Zero::count == 0); + { + int const i = 42; + std::shared_ptr<One> p = std::allocate_shared<One>(Alloc(), i); + assert(One::count == 1); + assert(p->value == i); + } + assert(One::count == 0); + { + int const i = 42; + std::shared_ptr<Two> p = std::allocate_shared<Two>(Alloc(), i, bad); + assert(Two::count == 1); + assert(p->value == i); + } + assert(Two::count == 0); + { + int const i = 42; + std::shared_ptr<Three> p = std::allocate_shared<Three>(Alloc(), i, bad, bad); + assert(Three::count == 1); + assert(p->value == i); + } + assert(Three::count == 0); +} + +int main() +{ + { + int i = 67; + int const bad = -1; + std::shared_ptr<Two> p = std::allocate_shared<Two>(test_allocator<Two>(54), i, bad); + assert(test_allocator<Two>::alloc_count == 1); + assert(Two::count == 1); + assert(p->value == 67); + } + assert(Two::count == 0); + assert(test_allocator<Two>::alloc_count == 0); + + test<bare_allocator<void> >(); +#if __cplusplus >= 201103L + test<min_allocator<void> >(); +#endif +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/make_shared.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/make_shared.pass.cpp new file mode 100644 index 00000000000..30a4984003b --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/make_shared.pass.cpp @@ -0,0 +1,79 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// template<class T, class... Args> shared_ptr<T> make_shared(Args&&... args); + +// UNSUPPORTED: asan, msan + +#include <memory> +#include <new> +#include <cstdlib> +#include <cassert> + +int new_count = 0; + +void* operator new(std::size_t s) throw(std::bad_alloc) +{ + ++new_count; + return std::malloc(s); +} + +void operator delete(void* p) throw() +{ + std::free(p); +} + +struct A +{ + static int count; + + A(int i, char c) : int_(i), char_(c) {++count;} + A(const A& a) + : int_(a.int_), char_(a.char_) + {++count;} + ~A() {--count;} + + int get_int() const {return int_;} + char get_char() const {return char_;} +private: + int int_; + char char_; +}; + +int A::count = 0; + +int main() +{ + int nc = new_count; + { + int i = 67; + char c = 'e'; + std::shared_ptr<A> p = std::make_shared<A>(i, c); + assert(new_count == nc+1); + assert(A::count == 1); + assert(p->get_int() == 67); + assert(p->get_char() == 'e'); + } +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + nc = new_count; + { + char c = 'e'; + std::shared_ptr<A> p = std::make_shared<A>(67, c); + assert(new_count == nc+1); + assert(A::count == 1); + assert(p->get_int() == 67); + assert(p->get_char() == 'e'); + } +#endif + assert(A::count == 0); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.dest/tested_elsewhere.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.dest/tested_elsewhere.pass.cpp new file mode 100644 index 00000000000..b58f5c55b64 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.dest/tested_elsewhere.pass.cpp @@ -0,0 +1,12 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +int main() +{ +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.io/io.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.io/io.pass.cpp new file mode 100644 index 00000000000..b627ac1ccbc --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.io/io.pass.cpp @@ -0,0 +1,29 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// template<class CharT, class Traits, class Y> +// basic_ostream<CharT, Traits>& +// operator<<(basic_ostream<CharT, Traits>& os, shared_ptr<Y> const& p); + +#include <memory> +#include <sstream> +#include <cassert> + +int main() +{ + std::shared_ptr<int> p(new int(3)); + std::ostringstream os; + assert(os.str().empty()); + os << p; + assert(!os.str().empty()); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset.pass.cpp new file mode 100644 index 00000000000..7bffc06993f --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset.pass.cpp @@ -0,0 +1,62 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// void reset(); + +#include <memory> +#include <cassert> + +struct B +{ + static int count; + + B() {++count;} + B(const B&) {++count;} + virtual ~B() {--count;} +}; + +int B::count = 0; + +struct A + : public B +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +int main() +{ + { + std::shared_ptr<B> p(new B); + p.reset(); + assert(A::count == 0); + assert(B::count == 0); + assert(p.use_count() == 0); + assert(p.get() == 0); + } + assert(A::count == 0); + { + std::shared_ptr<B> p; + p.reset(); + assert(A::count == 0); + assert(B::count == 0); + assert(p.use_count() == 0); + assert(p.get() == 0); + } + assert(A::count == 0); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer.pass.cpp new file mode 100644 index 00000000000..85a64d0f1b0 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer.pass.cpp @@ -0,0 +1,64 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// template<class Y> void reset(Y* p); + +#include <memory> +#include <cassert> + +struct B +{ + static int count; + + B() {++count;} + B(const B&) {++count;} + virtual ~B() {--count;} +}; + +int B::count = 0; + +struct A + : public B +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +int main() +{ + { + std::shared_ptr<B> p(new B); + A* ptr = new A; + p.reset(ptr); + assert(A::count == 1); + assert(B::count == 1); + assert(p.use_count() == 1); + assert(p.get() == ptr); + } + assert(A::count == 0); + { + std::shared_ptr<B> p; + A* ptr = new A; + p.reset(ptr); + assert(A::count == 1); + assert(B::count == 1); + assert(p.use_count() == 1); + assert(p.get() == ptr); + } + assert(A::count == 0); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer_deleter.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer_deleter.pass.cpp new file mode 100644 index 00000000000..33965dfeb33 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer_deleter.pass.cpp @@ -0,0 +1,79 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// template<class Y, class D> void reset(Y* p, D d); + +#include <memory> +#include <cassert> +#include "../test_deleter.h" + +struct B +{ + static int count; + + B() {++count;} + B(const B&) {++count;} + virtual ~B() {--count;} +}; + +int B::count = 0; + +struct A + : public B +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +int main() +{ + { + std::shared_ptr<B> p(new B); + A* ptr = new A; + p.reset(ptr, test_deleter<A>(3)); + assert(A::count == 1); + assert(B::count == 1); + assert(p.use_count() == 1); + assert(p.get() == ptr); + test_deleter<A>* d = std::get_deleter<test_deleter<A> >(p); + assert(test_deleter<A>::count == 1); + assert(test_deleter<A>::dealloc_count == 0); + assert(d); + assert(d->state() == 3); + } + assert(A::count == 0); + assert(test_deleter<A>::count == 0); + assert(test_deleter<A>::dealloc_count == 1); + { + std::shared_ptr<B> p; + A* ptr = new A; + p.reset(ptr, test_deleter<A>(3)); + assert(A::count == 1); + assert(B::count == 1); + assert(p.use_count() == 1); + assert(p.get() == ptr); + test_deleter<A>* d = std::get_deleter<test_deleter<A> >(p); + assert(test_deleter<A>::count == 1); + assert(test_deleter<A>::dealloc_count == 1); + assert(d); + assert(d->state() == 3); + } + assert(A::count == 0); + assert(test_deleter<A>::count == 0); + assert(test_deleter<A>::dealloc_count == 2); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer_deleter_allocator.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer_deleter_allocator.pass.cpp new file mode 100644 index 00000000000..09070e2c059 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer_deleter_allocator.pass.cpp @@ -0,0 +1,88 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// template<class Y, class D, class A> void reset(Y* p, D d, A a); + +#include <memory> +#include <cassert> +#include "../test_deleter.h" +#include "test_allocator.h" + +struct B +{ + static int count; + + B() {++count;} + B(const B&) {++count;} + virtual ~B() {--count;} +}; + +int B::count = 0; + +struct A + : public B +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +int main() +{ + { + std::shared_ptr<B> p(new B); + A* ptr = new A; + p.reset(ptr, test_deleter<A>(3), test_allocator<A>(4)); + assert(A::count == 1); + assert(B::count == 1); + assert(p.use_count() == 1); + assert(p.get() == ptr); + test_deleter<A>* d = std::get_deleter<test_deleter<A> >(p); + assert(test_deleter<A>::count == 1); + assert(test_deleter<A>::dealloc_count == 0); + assert(d); + assert(d->state() == 3); + assert(test_allocator<A>::count == 1); + assert(test_allocator<A>::alloc_count == 1); + } + assert(A::count == 0); + assert(test_deleter<A>::count == 0); + assert(test_deleter<A>::dealloc_count == 1); + assert(test_allocator<A>::count == 0); + assert(test_allocator<A>::alloc_count == 0); + { + std::shared_ptr<B> p; + A* ptr = new A; + p.reset(ptr, test_deleter<A>(3), test_allocator<A>(4)); + assert(A::count == 1); + assert(B::count == 1); + assert(p.use_count() == 1); + assert(p.get() == ptr); + test_deleter<A>* d = std::get_deleter<test_deleter<A> >(p); + assert(test_deleter<A>::count == 1); + assert(test_deleter<A>::dealloc_count == 1); + assert(d); + assert(d->state() == 3); + assert(test_allocator<A>::count == 1); + assert(test_allocator<A>::alloc_count == 1); + } + assert(A::count == 0); + assert(test_deleter<A>::count == 0); + assert(test_deleter<A>::dealloc_count == 2); + assert(test_allocator<A>::count == 0); + assert(test_allocator<A>::alloc_count == 0); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/swap.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/swap.pass.cpp new file mode 100644 index 00000000000..6d28a5043ca --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/swap.pass.cpp @@ -0,0 +1,104 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// void swap(shared_ptr& r); + +#include <memory> +#include <cassert> + +struct A +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +int main() +{ + { + A* ptr1 = new A; + A* ptr2 = new A; + std::shared_ptr<A> p1(ptr1); + { + std::shared_ptr<A> p2(ptr2); + p1.swap(p2); + assert(p1.use_count() == 1); + assert(p1.get() == ptr2); + assert(p2.use_count() == 1); + assert(p2.get() == ptr1); + assert(A::count == 2); + } + assert(p1.use_count() == 1); + assert(p1.get() == ptr2); + assert(A::count == 1); + } + assert(A::count == 0); + { + A* ptr1 = new A; + A* ptr2 = 0; + std::shared_ptr<A> p1(ptr1); + { + std::shared_ptr<A> p2; + p1.swap(p2); + assert(p1.use_count() == 0); + assert(p1.get() == ptr2); + assert(p2.use_count() == 1); + assert(p2.get() == ptr1); + assert(A::count == 1); + } + assert(p1.use_count() == 0); + assert(p1.get() == ptr2); + assert(A::count == 0); + } + assert(A::count == 0); + { + A* ptr1 = 0; + A* ptr2 = new A; + std::shared_ptr<A> p1; + { + std::shared_ptr<A> p2(ptr2); + p1.swap(p2); + assert(p1.use_count() == 1); + assert(p1.get() == ptr2); + assert(p2.use_count() == 0); + assert(p2.get() == ptr1); + assert(A::count == 1); + } + assert(p1.use_count() == 1); + assert(p1.get() == ptr2); + assert(A::count == 1); + } + assert(A::count == 0); + { + A* ptr1 = 0; + A* ptr2 = 0; + std::shared_ptr<A> p1; + { + std::shared_ptr<A> p2; + p1.swap(p2); + assert(p1.use_count() == 0); + assert(p1.get() == ptr2); + assert(p2.use_count() == 0); + assert(p2.get() == ptr1); + assert(A::count == 0); + } + assert(p1.use_count() == 0); + assert(p1.get() == ptr2); + assert(A::count == 0); + } + assert(A::count == 0); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/arrow.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/arrow.pass.cpp new file mode 100644 index 00000000000..00281687521 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/arrow.pass.cpp @@ -0,0 +1,29 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// T* operator->() const; + +#include <memory> +#include <utility> +#include <cassert> + +int main() +{ + const std::shared_ptr<std::pair<int, int> > p(new std::pair<int, int>(3, 4)); + assert(p->first == 3); + assert(p->second == 4); + p->first = 5; + p->second = 6; + assert(p->first == 5); + assert(p->second == 6); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/dereference.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/dereference.pass.cpp new file mode 100644 index 00000000000..378cd0514ca --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/dereference.pass.cpp @@ -0,0 +1,25 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// T& operator*() const; + +#include <memory> +#include <cassert> + +int main() +{ + const std::shared_ptr<int> p(new int(32)); + assert(*p == 32); + *p = 3; + assert(*p == 3); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/op_bool.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/op_bool.pass.cpp new file mode 100644 index 00000000000..1b79d497005 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/op_bool.pass.cpp @@ -0,0 +1,29 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// explicit operator bool() const; + +#include <memory> +#include <cassert> + +int main() +{ + { + const std::shared_ptr<int> p(new int(32)); + assert(p); + } + { + const std::shared_ptr<int> p; + assert(!p); + } +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/owner_before_shared_ptr.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/owner_before_shared_ptr.pass.cpp new file mode 100644 index 00000000000..3acd2f8c6f2 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/owner_before_shared_ptr.pass.cpp @@ -0,0 +1,28 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// template <class U> bool owner_before(shared_ptr<U> const& b) const; + +#include <memory> +#include <cassert> + +int main() +{ + const std::shared_ptr<int> p1(new int); + const std::shared_ptr<int> p2 = p1; + const std::shared_ptr<int> p3(new int); + assert(!p1.owner_before(p2)); + assert(!p2.owner_before(p1)); + assert(p1.owner_before(p3) || p3.owner_before(p1)); + assert(p3.owner_before(p1) == p3.owner_before(p2)); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/owner_before_weak_ptr.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/owner_before_weak_ptr.pass.cpp new file mode 100644 index 00000000000..33447ba7da0 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/owner_before_weak_ptr.pass.cpp @@ -0,0 +1,31 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// template <class U> bool owner_before(weak_ptr<U> const& b) const; + +#include <memory> +#include <cassert> + +int main() +{ + const std::shared_ptr<int> p1(new int); + const std::shared_ptr<int> p2 = p1; + const std::shared_ptr<int> p3(new int); + const std::weak_ptr<int> w1(p1); + const std::weak_ptr<int> w2(p2); + const std::weak_ptr<int> w3(p3); + assert(!p1.owner_before(w2)); + assert(!p2.owner_before(w1)); + assert(p1.owner_before(w3) || p3.owner_before(w1)); + assert(p3.owner_before(w1) == p3.owner_before(w2)); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/unique.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/unique.pass.cpp new file mode 100644 index 00000000000..50ff692f9d4 --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/unique.pass.cpp @@ -0,0 +1,28 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// bool unique() const; + +#include <memory> +#include <cassert> + +int main() +{ + const std::shared_ptr<int> p(new int(32)); + assert(p.unique()); + { + std::shared_ptr<int> p2 = p; + assert(!p.unique()); + } + assert(p.unique()); +} diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.spec/swap.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.spec/swap.pass.cpp new file mode 100644 index 00000000000..b40e4705acf --- /dev/null +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.spec/swap.pass.cpp @@ -0,0 +1,104 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <memory> + +// shared_ptr + +// template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b); + +#include <memory> +#include <cassert> + +struct A +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +int main() +{ + { + A* ptr1 = new A; + A* ptr2 = new A; + std::shared_ptr<A> p1(ptr1); + { + std::shared_ptr<A> p2(ptr2); + swap(p1, p2); + assert(p1.use_count() == 1); + assert(p1.get() == ptr2); + assert(p2.use_count() == 1); + assert(p2.get() == ptr1); + assert(A::count == 2); + } + assert(p1.use_count() == 1); + assert(p1.get() == ptr2); + assert(A::count == 1); + } + assert(A::count == 0); + { + A* ptr1 = new A; + A* ptr2 = 0; + std::shared_ptr<A> p1(ptr1); + { + std::shared_ptr<A> p2; + swap(p1, p2); + assert(p1.use_count() == 0); + assert(p1.get() == ptr2); + assert(p2.use_count() == 1); + assert(p2.get() == ptr1); + assert(A::count == 1); + } + assert(p1.use_count() == 0); + assert(p1.get() == ptr2); + assert(A::count == 0); + } + assert(A::count == 0); + { + A* ptr1 = 0; + A* ptr2 = new A; + std::shared_ptr<A> p1; + { + std::shared_ptr<A> p2(ptr2); + swap(p1, p2); + assert(p1.use_count() == 1); + assert(p1.get() == ptr2); + assert(p2.use_count() == 0); + assert(p2.get() == ptr1); + assert(A::count == 1); + } + assert(p1.use_count() == 1); + assert(p1.get() == ptr2); + assert(A::count == 1); + } + assert(A::count == 0); + { + A* ptr1 = 0; + A* ptr2 = 0; + std::shared_ptr<A> p1; + { + std::shared_ptr<A> p2; + swap(p1, p2); + assert(p1.use_count() == 0); + assert(p1.get() == ptr2); + assert(p2.use_count() == 0); + assert(p2.get() == ptr1); + assert(A::count == 0); + } + assert(p1.use_count() == 0); + assert(p1.get() == ptr2); + assert(A::count == 0); + } + assert(A::count == 0); +} |