diff options
Diffstat (limited to 'test/std/utilities/memory/util.smartptr')
87 files changed, 5226 insertions, 0 deletions
diff --git a/test/std/utilities/memory/util.smartptr/nothing_to_do.pass.cpp b/test/std/utilities/memory/util.smartptr/nothing_to_do.pass.cpp new file mode 100644 index 000000000000..b58f5c55b643 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/nothing_to_do.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/test/std/utilities/memory/util.smartptr/util.smartptr.enab/enable_shared_from_this.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.enab/enable_shared_from_this.pass.cpp new file mode 100644 index 000000000000..77af13fa90d1 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.enab/enable_shared_from_this.pass.cpp @@ -0,0 +1,53 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// template<class T> +// class enable_shared_from_this +// { +// protected: +// enable_shared_from_this(); +// enable_shared_from_this(enable_shared_from_this const&); +// enable_shared_from_this& operator=(enable_shared_from_this const&); +// ~enable_shared_from_this(); +// public: +// shared_ptr<T> shared_from_this(); +// shared_ptr<T const> shared_from_this() const; +// }; + +#include <memory> +#include <cassert> + +struct T + : public std::enable_shared_from_this<T> +{ +}; + +struct Y : T {}; + +struct Z : Y {}; + +int main() +{ + { // https://llvm.org/bugs/show_bug.cgi?id=18843 + std::shared_ptr<T const> t1(new T); + std::shared_ptr<T const> t2(std::make_shared<T>()); + } + { + std::shared_ptr<Y> p(new Z); + std::shared_ptr<T> q = p->shared_from_this(); + assert(p == q); + assert(!p.owner_before(q) && !q.owner_before(p)); // p and q share ownership + } + { + std::shared_ptr<Y> p = std::make_shared<Z>(); + std::shared_ptr<T> q = p->shared_from_this(); + assert(p == q); + assert(!p.owner_before(q) && !q.owner_before(p)); // p and q share ownership + } +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.hash/hash_shared_ptr.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.hash/hash_shared_ptr.pass.cpp new file mode 100644 index 000000000000..990cb58722b1 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.hash/hash_shared_ptr.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> + +// template <class T> +// struct hash<shared_ptr<T>> +// { +// typedef shared_ptr<T> argument_type; +// typedef size_t result_type; +// size_t operator()(const shared_ptr<T>& p) const; +// }; + +#include <memory> +#include <cassert> + +int main() +{ + int* ptr = new int; + std::shared_ptr<int> p(ptr); + std::hash<std::shared_ptr<int> > f; + std::size_t h = f(p); + assert(h == std::hash<int*>()(ptr)); +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.hash/hash_unique_ptr.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.hash/hash_unique_ptr.pass.cpp new file mode 100644 index 000000000000..5cd4ab1f83d0 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.hash/hash_unique_ptr.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> + +// template <class T, class D> +// struct hash<unique_ptr<T, D>> +// { +// typedef unique_ptr<T, D> argument_type; +// typedef size_t result_type; +// size_t operator()(const unique_ptr<T, D>& p) const; +// }; + +#include <memory> +#include <cassert> + +int main() +{ + int* ptr = new int; + std::unique_ptr<int> p(ptr); + std::hash<std::unique_ptr<int> > f; + std::size_t h = f(p); + assert(h == std::hash<int*>()(ptr)); +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_compare_exchange_strong.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_compare_exchange_strong.pass.cpp new file mode 100644 index 000000000000..2d586e9c7fdd --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_compare_exchange_strong.pass.cpp @@ -0,0 +1,53 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads +// +// This test uses new symbols that were not defined in the libc++ shipped on +// darwin11 and darwin12: +// XFAIL: with_system_cxx_lib=x86_64-apple-darwin11 +// XFAIL: with_system_cxx_lib=x86_64-apple-darwin12 + +// <memory> + +// shared_ptr + +// template <class T> +// bool +// atomic_compare_exchange_strong(shared_ptr<T>* p, shared_ptr<T>* v, +// shared_ptr<T> w); + +#include <memory> +#include <cassert> + +int main() +{ +#if __has_feature(cxx_atomic) + { + std::shared_ptr<int> p(new int(4)); + std::shared_ptr<int> v(new int(3)); + std::shared_ptr<int> w(new int(2)); + bool b = std::atomic_compare_exchange_strong(&p, &v, w); + assert(b == false); + assert(*p == 4); + assert(*v == 4); + assert(*w == 2); + } + { + std::shared_ptr<int> p(new int(4)); + std::shared_ptr<int> v = p; + std::shared_ptr<int> w(new int(2)); + bool b = std::atomic_compare_exchange_strong(&p, &v, w); + assert(b == true); + assert(*p == 2); + assert(*v == 4); + assert(*w == 2); + } +#endif +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_compare_exchange_strong_explicit.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_compare_exchange_strong_explicit.pass.cpp new file mode 100644 index 000000000000..34da04cc1810 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_compare_exchange_strong_explicit.pass.cpp @@ -0,0 +1,58 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads +// +// This test uses new symbols that were not defined in the libc++ shipped on +// darwin11 and darwin12: +// XFAIL: with_system_cxx_lib=x86_64-apple-darwin11 +// XFAIL: with_system_cxx_lib=x86_64-apple-darwin12 + +// <memory> + +// shared_ptr + +// template <class T> +// bool +// atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v, +// shared_ptr<T> w, memory_order success, +// memory_order failure); + +#include <memory> +#include <cassert> + +int main() +{ +#if __has_feature(cxx_atomic) + { + std::shared_ptr<int> p(new int(4)); + std::shared_ptr<int> v(new int(3)); + std::shared_ptr<int> w(new int(2)); + bool b = std::atomic_compare_exchange_strong_explicit(&p, &v, w, + std::memory_order_seq_cst, + std::memory_order_seq_cst); + assert(b == false); + assert(*p == 4); + assert(*v == 4); + assert(*w == 2); + } + { + std::shared_ptr<int> p(new int(4)); + std::shared_ptr<int> v = p; + std::shared_ptr<int> w(new int(2)); + bool b = std::atomic_compare_exchange_strong_explicit(&p, &v, w, + std::memory_order_seq_cst, + std::memory_order_seq_cst); + assert(b == true); + assert(*p == 2); + assert(*v == 4); + assert(*w == 2); + } +#endif +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_compare_exchange_weak.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_compare_exchange_weak.pass.cpp new file mode 100644 index 000000000000..50b96e551fd3 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_compare_exchange_weak.pass.cpp @@ -0,0 +1,53 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads +// +// This test uses new symbols that were not defined in the libc++ shipped on +// darwin11 and darwin12: +// XFAIL: with_system_cxx_lib=x86_64-apple-darwin11 +// XFAIL: with_system_cxx_lib=x86_64-apple-darwin12 + +// <memory> + +// shared_ptr + +// template <class T> +// bool +// atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, +// shared_ptr<T> w); + +#include <memory> +#include <cassert> + +int main() +{ +#if __has_feature(cxx_atomic) + { + std::shared_ptr<int> p(new int(4)); + std::shared_ptr<int> v(new int(3)); + std::shared_ptr<int> w(new int(2)); + bool b = std::atomic_compare_exchange_weak(&p, &v, w); + assert(b == false); + assert(*p == 4); + assert(*v == 4); + assert(*w == 2); + } + { + std::shared_ptr<int> p(new int(4)); + std::shared_ptr<int> v = p; + std::shared_ptr<int> w(new int(2)); + bool b = std::atomic_compare_exchange_weak(&p, &v, w); + assert(b == true); + assert(*p == 2); + assert(*v == 4); + assert(*w == 2); + } +#endif +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_compare_exchange_weak_explicit.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_compare_exchange_weak_explicit.pass.cpp new file mode 100644 index 000000000000..d304319d251d --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_compare_exchange_weak_explicit.pass.cpp @@ -0,0 +1,58 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads +// +// This test uses new symbols that were not defined in the libc++ shipped on +// darwin11 and darwin12: +// XFAIL: with_system_cxx_lib=x86_64-apple-darwin11 +// XFAIL: with_system_cxx_lib=x86_64-apple-darwin12 + +// <memory> + +// shared_ptr + +// template <class T> +// bool +// atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v, +// shared_ptr<T> w, memory_order success, +// memory_order failure); + +#include <memory> +#include <cassert> + +int main() +{ +#if __has_feature(cxx_atomic) + { + std::shared_ptr<int> p(new int(4)); + std::shared_ptr<int> v(new int(3)); + std::shared_ptr<int> w(new int(2)); + bool b = std::atomic_compare_exchange_weak_explicit(&p, &v, w, + std::memory_order_seq_cst, + std::memory_order_seq_cst); + assert(b == false); + assert(*p == 4); + assert(*v == 4); + assert(*w == 2); + } + { + std::shared_ptr<int> p(new int(4)); + std::shared_ptr<int> v = p; + std::shared_ptr<int> w(new int(2)); + bool b = std::atomic_compare_exchange_weak_explicit(&p, &v, w, + std::memory_order_seq_cst, + std::memory_order_seq_cst); + assert(b == true); + assert(*p == 2); + assert(*v == 4); + assert(*w == 2); + } +#endif +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_exchange.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_exchange.pass.cpp new file mode 100644 index 000000000000..3b44c8ba9b33 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_exchange.pass.cpp @@ -0,0 +1,39 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads +// +// This test uses new symbols that were not defined in the libc++ shipped on +// darwin11 and darwin12: +// XFAIL: with_system_cxx_lib=x86_64-apple-darwin11 +// XFAIL: with_system_cxx_lib=x86_64-apple-darwin12 + +// <memory> + +// shared_ptr + +// template <class T> +// shared_ptr<T> +// atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r) + +#include <memory> +#include <cassert> + +int main() +{ +#if __has_feature(cxx_atomic) + { + std::shared_ptr<int> p(new int(4)); + std::shared_ptr<int> r(new int(3)); + r = std::atomic_exchange(&p, r); + assert(*p == 3); + assert(*r == 4); + } +#endif +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_exchange_explicit.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_exchange_explicit.pass.cpp new file mode 100644 index 000000000000..598a1b8da175 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_exchange_explicit.pass.cpp @@ -0,0 +1,39 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads +// +// This test uses new symbols that were not defined in the libc++ shipped on +// darwin11 and darwin12: +// XFAIL: with_system_cxx_lib=x86_64-apple-darwin11 +// XFAIL: with_system_cxx_lib=x86_64-apple-darwin12 + +// <memory> + +// shared_ptr + +// template <class T> +// shared_ptr<T> +// atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r) + +#include <memory> +#include <cassert> + +int main() +{ +#if __has_feature(cxx_atomic) + { + std::shared_ptr<int> p(new int(4)); + std::shared_ptr<int> r(new int(3)); + r = std::atomic_exchange_explicit(&p, r, std::memory_order_seq_cst); + assert(*p == 3); + assert(*r == 4); + } +#endif +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_is_lock_free.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_is_lock_free.pass.cpp new file mode 100644 index 000000000000..e3ac84a4fa50 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_is_lock_free.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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads + +// <memory> + +// shared_ptr + +// template<class T> +// bool +// atomic_is_lock_free(const shared_ptr<T>* p); + +#include <memory> +#include <cassert> + +int main() +{ +#if __has_feature(cxx_atomic) + { + const std::shared_ptr<int> p(new int(3)); + assert(std::atomic_is_lock_free(&p) == false); + } +#endif +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_load.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_load.pass.cpp new file mode 100644 index 000000000000..d4a39c878ac7 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_load.pass.cpp @@ -0,0 +1,37 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads +// +// This test uses new symbols that were not defined in the libc++ shipped on +// darwin11 and darwin12: +// XFAIL: with_system_cxx_lib=x86_64-apple-darwin11 +// XFAIL: with_system_cxx_lib=x86_64-apple-darwin12 + +// <memory> + +// shared_ptr + +// template <class T> +// shared_ptr<T> +// atomic_load(const shared_ptr<T>* p) + +#include <memory> +#include <cassert> + +int main() +{ +#if __has_feature(cxx_atomic) + { + std::shared_ptr<int> p(new int(3)); + std::shared_ptr<int> q = std::atomic_load(&p); + assert(*q == *p); + } +#endif +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_load_explicit.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_load_explicit.pass.cpp new file mode 100644 index 000000000000..af11dc8bc2c9 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_load_explicit.pass.cpp @@ -0,0 +1,37 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads +// +// This test uses new symbols that were not defined in the libc++ shipped on +// darwin11 and darwin12: +// XFAIL: with_system_cxx_lib=x86_64-apple-darwin11 +// XFAIL: with_system_cxx_lib=x86_64-apple-darwin12 + +// <memory> + +// shared_ptr + +// template <class T> +// shared_ptr<T> +// atomic_load_explicit(const shared_ptr<T>* p, memory_order mo) + +#include <memory> +#include <cassert> + +int main() +{ +#if __has_feature(cxx_atomic) + { + const std::shared_ptr<int> p(new int(3)); + std::shared_ptr<int> q = std::atomic_load_explicit(&p, std::memory_order_relaxed); + assert(*q == *p); + } +#endif +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_store.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_store.pass.cpp new file mode 100644 index 000000000000..7a85a9934ef0 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_store.pass.cpp @@ -0,0 +1,38 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads +// +// This test uses new symbols that were not defined in the libc++ shipped on +// darwin11 and darwin12: +// XFAIL: with_system_cxx_lib=x86_64-apple-darwin11 +// XFAIL: with_system_cxx_lib=x86_64-apple-darwin12 + +// <memory> + +// shared_ptr + +// template <class T> +// void +// atomic_store(shared_ptr<T>* p, shared_ptr<T> r) + +#include <memory> +#include <cassert> + +int main() +{ +#if __has_feature(cxx_atomic) + { + std::shared_ptr<int> p; + std::shared_ptr<int> r(new int(3)); + std::atomic_store(&p, r); + assert(*p == *r); + } +#endif +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_store_explicit.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_store_explicit.pass.cpp new file mode 100644 index 000000000000..c81266c55fa4 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic/atomic_store_explicit.pass.cpp @@ -0,0 +1,38 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads +// +// This test uses new symbols that were not defined in the libc++ shipped on +// darwin11 and darwin12: +// XFAIL: with_system_cxx_lib=x86_64-apple-darwin11 +// XFAIL: with_system_cxx_lib=x86_64-apple-darwin12 + +// <memory> + +// shared_ptr + +// template <class T> +// void +// atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo) + +#include <memory> +#include <cassert> + +int main() +{ +#if __has_feature(cxx_atomic) + { + std::shared_ptr<int> p; + std::shared_ptr<int> r(new int(3)); + std::atomic_store_explicit(&p, r, std::memory_order_seq_cst); + assert(*p == *r); + } +#endif +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.shared/test_deleter.h b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/test_deleter.h new file mode 100644 index 000000000000..0263061b3a84 --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/types.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/types.pass.cpp new file mode 100644 index 000000000000..8175312334f6 --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.getdeleter/get_deleter.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.getdeleter/get_deleter.pass.cpp new file mode 100644 index 000000000000..a6c62496fd62 --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/auto_ptr_Y.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/auto_ptr_Y.pass.cpp new file mode 100644 index 000000000000..21cdf4a13e47 --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr.pass.cpp new file mode 100644 index 000000000000..5d27a8865f08 --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr_Y.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr_Y.pass.cpp new file mode 100644 index 000000000000..abd3d378eb71 --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr_Y_rv.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr_Y_rv.pass.cpp new file mode 100644 index 000000000000..93956bcae663 --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr_rv.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/shared_ptr_rv.pass.cpp new file mode 100644 index 000000000000..4194890dda2d --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/unique_ptr_Y.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/unique_ptr_Y.pass.cpp new file mode 100644 index 000000000000..30e0fce21e04 --- /dev/null +++ b/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); // no longer true due to LWG 2415 + 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); // no longer true due to LWG 2415 + 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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cast/const_pointer_cast.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cast/const_pointer_cast.pass.cpp new file mode 100644 index 000000000000..7d771d03c71f --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cast/dynamic_pointer_cast.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cast/dynamic_pointer_cast.pass.cpp new file mode 100644 index 000000000000..4f88a5c4351a --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cast/static_pointer_cast.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cast/static_pointer_cast.pass.cpp new file mode 100644 index 000000000000..98fa13801a8c --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/cmp_nullptr.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/cmp_nullptr.pass.cpp new file mode 100644 index 000000000000..f40cbc3d0324 --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/eq.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/eq.pass.cpp new file mode 100644 index 000000000000..c4cd1693f496 --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/lt.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/lt.pass.cpp new file mode 100644 index 000000000000..5a90a9a325fa --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/auto_ptr.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/auto_ptr.pass.cpp new file mode 100644 index 000000000000..b2e61faff5ed --- /dev/null +++ b/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: sanitizer-new-delete + +#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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/default.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/default.pass.cpp new file mode 100644 index 000000000000..9af5d7ea8619 --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/nullptr_t.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/nullptr_t.pass.cpp new file mode 100644 index 000000000000..3a9b3a9ca1de --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/nullptr_t_deleter.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/nullptr_t_deleter.pass.cpp new file mode 100644 index 000000000000..7d4dc38d4b9a --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/nullptr_t_deleter_allocator.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/nullptr_t_deleter_allocator.pass.cpp new file mode 100644 index 000000000000..b67f31ee45a8 --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/nullptr_t_deleter_allocator_throw.pass.cpp b/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 000000000000..ab2c73e0c5f1 --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/nullptr_t_deleter_throw.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/nullptr_t_deleter_throw.pass.cpp new file mode 100644 index 000000000000..97d3f69fb5c4 --- /dev/null +++ b/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: sanitizer-new-delete + +#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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer.pass.cpp new file mode 100644 index 000000000000..c9cffa1fe25e --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer_deleter.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer_deleter.pass.cpp new file mode 100644 index 000000000000..43eedee176c2 --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer_deleter_allocator.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer_deleter_allocator.pass.cpp new file mode 100644 index 000000000000..1a9c09cdb78c --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer_deleter_allocator_throw.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer_deleter_allocator_throw.pass.cpp new file mode 100644 index 000000000000..4220993a5fd1 --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer_deleter_throw.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer_deleter_throw.pass.cpp new file mode 100644 index 000000000000..ead081645671 --- /dev/null +++ b/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: sanitizer-new-delete + +#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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer_throw.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer_throw.pass.cpp new file mode 100644 index 000000000000..041fe9a7853d --- /dev/null +++ b/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: sanitizer-new-delete + +#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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr.pass.cpp new file mode 100644 index 000000000000..e1dcdfc8165f --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr_Y.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr_Y.pass.cpp new file mode 100644 index 000000000000..8b5ffdc1475b --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr_Y_rv.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr_Y_rv.pass.cpp new file mode 100644 index 000000000000..f041d9451a6d --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr_pointer.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr_pointer.pass.cpp new file mode 100644 index 000000000000..fb5262f3b0ef --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr_rv.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/shared_ptr_rv.pass.cpp new file mode 100644 index 000000000000..b89178e201cf --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/unique_ptr.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/unique_ptr.pass.cpp new file mode 100644 index 000000000000..5e09d9a7934c --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/unique_ptr.pass.cpp @@ -0,0 +1,115 @@ +//===----------------------------------------------------------------------===// +// +// 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: sanitizer-new-delete + +#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); } + +template <typename T> +void assert_deleter ( T * ) { 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)); + } + +#if __cplusplus >= 201402L + // LWG 2415 + { + std::unique_ptr<int, void (*)(int*)> p(nullptr, assert_deleter<int>); + std::shared_ptr<int> p2(std::move(p)); // should not call deleter when going out of scope + } +#endif + +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/weak_ptr.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/weak_ptr.pass.cpp new file mode 100644 index 000000000000..a9d8aff145a7 --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/allocate_shared.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/allocate_shared.pass.cpp new file mode 100644 index 000000000000..aa77dab51515 --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/allocate_shared_no_variadics.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/allocate_shared_no_variadics.pass.cpp new file mode 100644 index 000000000000..8dcd50e49411 --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/make_shared.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/make_shared.pass.cpp new file mode 100644 index 000000000000..8cb972b0c1a1 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/make_shared.pass.cpp @@ -0,0 +1,80 @@ +//===----------------------------------------------------------------------===// +// +// 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); + +#include <memory> +#include <cassert> + +#include "count_new.hpp" + +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; + + +struct Foo +{ + Foo() = default; + virtual ~Foo() = default; +}; + + +int main() +{ + int nc = globalMemCounter.outstanding_new; + { + int i = 67; + char c = 'e'; + std::shared_ptr<A> p = std::make_shared<A>(i, c); + assert(globalMemCounter.checkOutstandingNewEq(nc+1)); + assert(A::count == 1); + assert(p->get_int() == 67); + assert(p->get_char() == 'e'); + } + + { // https://llvm.org/bugs/show_bug.cgi?id=24137 + std::shared_ptr<Foo> p1 = std::make_shared<Foo>(); + assert(p1.get()); + std::shared_ptr<const Foo> p2 = std::make_shared<const Foo>(); + assert(p2.get()); + } + +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + nc = globalMemCounter.outstanding_new; + { + char c = 'e'; + std::shared_ptr<A> p = std::make_shared<A>(67, c); + assert(globalMemCounter.checkOutstandingNewEq(nc+1)); + assert(A::count == 1); + assert(p->get_int() == 67); + assert(p->get_char() == 'e'); + } +#endif + assert(A::count == 0); +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/make_shared.volatile.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/make_shared.volatile.pass.cpp new file mode 100644 index 000000000000..1045f9347b38 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/make_shared.volatile.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 T, class... Args> shared_ptr<T> make_shared(Args&&... args); + +#include <memory> +#include <cassert> + +template <typename T> +void test(const T &t0) +{ + { + T t1 = t0; + std::shared_ptr<T> p0 = std::make_shared<T>(t0); + std::shared_ptr<T> p1 = std::make_shared<T>(t1); + assert(*p0 == t0); + assert(*p1 == t1); + } + + { + const T t1 = t0; + std::shared_ptr<const T> p0 = std::make_shared<const T>(t0); + std::shared_ptr<const T> p1 = std::make_shared<const T>(t1); + assert(*p0 == t0); + assert(*p1 == t1); + } + + { + volatile T t1 = t0; + std::shared_ptr<volatile T> p0 = std::make_shared<volatile T>(t0); + std::shared_ptr<volatile T> p1 = std::make_shared<volatile T>(t1); + assert(*p0 == t0); + assert(*p1 == t1); + } + + { + const volatile T t1 = t0; + std::shared_ptr<const volatile T> p0 = std::make_shared<const volatile T>(t0); + std::shared_ptr<const volatile T> p1 = std::make_shared<const volatile T>(t1); + assert(*p0 == t0); + assert(*p1 == t1); + } + +} + +int main() +{ + test<bool>(true); + test<int>(3); + test<double>(5.0); +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.dest/tested_elsewhere.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.dest/tested_elsewhere.pass.cpp new file mode 100644 index 000000000000..b58f5c55b643 --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.io/io.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.io/io.pass.cpp new file mode 100644 index 000000000000..b627ac1ccbc9 --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset.pass.cpp new file mode 100644 index 000000000000..7bffc06993fa --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer.pass.cpp new file mode 100644 index 000000000000..85a64d0f1b0b --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer_deleter.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer_deleter.pass.cpp new file mode 100644 index 000000000000..33965dfeb33c --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer_deleter_allocator.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/reset_pointer_deleter_allocator.pass.cpp new file mode 100644 index 000000000000..09070e2c059b --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/swap.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.mod/swap.pass.cpp new file mode 100644 index 000000000000..6d28a5043ca0 --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/arrow.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/arrow.pass.cpp new file mode 100644 index 000000000000..00281687521f --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/dereference.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/dereference.pass.cpp new file mode 100644 index 000000000000..378cd0514ca2 --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/op_bool.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/op_bool.pass.cpp new file mode 100644 index 000000000000..1b79d497005a --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/owner_before_shared_ptr.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/owner_before_shared_ptr.pass.cpp new file mode 100644 index 000000000000..3acd2f8c6f2d --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/owner_before_weak_ptr.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/owner_before_weak_ptr.pass.cpp new file mode 100644 index 000000000000..33447ba7da00 --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/unique.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/unique.pass.cpp new file mode 100644 index 000000000000..50ff692f9d42 --- /dev/null +++ b/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/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.spec/swap.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.spec/swap.pass.cpp new file mode 100644 index 000000000000..b40e4705acfe --- /dev/null +++ b/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); +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.weak/types.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/types.pass.cpp new file mode 100644 index 000000000000..45748d7db6fb --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/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 weak_ptr +// { +// public: +// typedef T element_type; +// ... +// }; + +#include <memory> + +struct A; // purposefully incomplete + +int main() +{ + static_assert((std::is_same<std::weak_ptr<A>::element_type, A>::value), ""); +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.ownerless/owner_less.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.ownerless/owner_less.pass.cpp new file mode 100644 index 000000000000..d091ae99fc27 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.ownerless/owner_less.pass.cpp @@ -0,0 +1,82 @@ +//===----------------------------------------------------------------------===// +// +// 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> struct owner_less; +// +// template <class T> +// struct owner_less<shared_ptr<T> > +// : binary_function<shared_ptr<T>, shared_ptr<T>, bool> +// { +// typedef bool result_type; +// bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const; +// bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const; +// bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const; +// }; +// +// template <class T> +// struct owner_less<weak_ptr<T> > +// : binary_function<weak_ptr<T>, weak_ptr<T>, bool> +// { +// typedef bool result_type; +// bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const; +// bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const; +// bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) 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); + + { + typedef std::owner_less<std::shared_ptr<int> > CS; + CS cs; + + static_assert((std::is_same<std::shared_ptr<int>, CS::first_argument_type>::value), "" ); + static_assert((std::is_same<std::shared_ptr<int>, CS::second_argument_type>::value), "" ); + static_assert((std::is_same<bool, CS::result_type>::value), "" ); + + assert(!cs(p1, p2)); + assert(!cs(p2, p1)); + assert(cs(p1 ,p3) || cs(p3, p1)); + assert(cs(p3, p1) == cs(p3, p2)); + + assert(!cs(p1, w2)); + assert(!cs(p2, w1)); + assert(cs(p1, w3) || cs(p3, w1)); + assert(cs(p3, w1) == cs(p3, w2)); + } + { + typedef std::owner_less<std::weak_ptr<int> > CS; + CS cs; + + static_assert((std::is_same<std::weak_ptr<int>, CS::first_argument_type>::value), "" ); + static_assert((std::is_same<std::weak_ptr<int>, CS::second_argument_type>::value), "" ); + static_assert((std::is_same<bool, CS::result_type>::value), "" ); + + assert(!cs(w1, w2)); + assert(!cs(w2, w1)); + assert(cs(w1, w3) || cs(w3, w1)); + assert(cs(w3, w1) == cs(w3, w2)); + + assert(!cs(w1, p2)); + assert(!cs(w2, p1)); + assert(cs(w1, p3) || cs(w3, p1)); + assert(cs(w3, p1) == cs(w3, p2)); + } +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.assign/shared_ptr_Y.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.assign/shared_ptr_Y.pass.cpp new file mode 100644 index 000000000000..6b32079c71b4 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.assign/shared_ptr_Y.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> + +// weak_ptr + +// template<class Y> weak_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); + { + std::weak_ptr<B> pB; + pB = pA; + assert(B::count == 1); + assert(A::count == 1); + assert(pB.use_count() == 1); + assert(pA.use_count() == 1); + } + assert(pA.use_count() == 1); + assert(B::count == 1); + assert(A::count == 1); + } + assert(B::count == 0); + assert(A::count == 0); +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.assign/weak_ptr.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.assign/weak_ptr.pass.cpp new file mode 100644 index 000000000000..e5713f375215 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.assign/weak_ptr.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> + +// weak_ptr + +// weak_ptr& operator=(const weak_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> ps(new A); + const std::weak_ptr<A> pA(ps); + { + std::weak_ptr<A> pB; + pB = pA; + assert(B::count == 1); + assert(A::count == 1); + assert(pB.use_count() == 1); + assert(pA.use_count() == 1); + } + 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> ps(new A); + std::weak_ptr<A> pA(ps); + { + std::weak_ptr<A> pB; + pB = std::move(pA); + assert(B::count == 1); + assert(A::count == 1); + assert(pB.use_count() == 1); + } + assert(B::count == 1); + assert(A::count == 1); + } + assert(B::count == 0); + assert(A::count == 0); +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.assign/weak_ptr_Y.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.assign/weak_ptr_Y.pass.cpp new file mode 100644 index 000000000000..5a03d926f7d6 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.assign/weak_ptr_Y.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> + +// weak_ptr + +// template<class Y> weak_ptr& operator=(const weak_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> ps(new A); + const std::weak_ptr<A> pA(ps); + { + std::weak_ptr<B> pB; + pB = pA; + assert(B::count == 1); + assert(A::count == 1); + assert(pB.use_count() == 1); + assert(pA.use_count() == 1); + } + 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> ps(new A); + std::weak_ptr<A> pA(ps); + { + std::weak_ptr<B> pB; + pB = std::move(pA); + assert(B::count == 1); + assert(A::count == 1); + assert(pB.use_count() == 1); + } + assert(B::count == 1); + assert(A::count == 1); + } + assert(B::count == 0); + assert(A::count == 0); +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.const/default.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.const/default.pass.cpp new file mode 100644 index 000000000000..28358db6a446 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.const/default.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> + +// template<class T> class weak_ptr + +// weak_ptr(); + +#include <memory> +#include <cassert> + +struct A; + +int main() +{ + std::weak_ptr<A> p; + assert(p.use_count() == 0); +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.const/shared_ptr_Y.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.const/shared_ptr_Y.pass.cpp new file mode 100644 index 000000000000..d70adb940eb2 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.const/shared_ptr_Y.pass.cpp @@ -0,0 +1,95 @@ +//===----------------------------------------------------------------------===// +// +// 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> + +// weak_ptr + +// template<class Y> weak_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::weak_ptr<B> >::value), ""); + static_assert((!std::is_convertible<std::weak_ptr<B>, std::shared_ptr<A> >::value), ""); + static_assert((!std::is_convertible<std::shared_ptr<A>, std::weak_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::weak_ptr<B> pB(pA); + assert(B::count == 1); + assert(A::count == 1); + assert(pB.use_count() == 1); + assert(pA.use_count() == 1); + } + 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::weak_ptr<B> pB(pA); + assert(B::count == 0); + assert(A::count == 0); + assert(pB.use_count() == 0); + assert(pA.use_count() == 0); + } + assert(pA.use_count() == 0); + assert(B::count == 0); + assert(A::count == 0); + } + assert(B::count == 0); + assert(A::count == 0); +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.const/weak_ptr.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.const/weak_ptr.pass.cpp new file mode 100644 index 000000000000..75bf3df90aa3 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.const/weak_ptr.pass.cpp @@ -0,0 +1,114 @@ +//===----------------------------------------------------------------------===// +// +// 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> + +// weak_ptr + +// weak_ptr(const weak_ptr& r); +// weak_ptr(weak_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; + +struct C +{ + static int count; + + C() {++count;} + C(const C&) {++count;} + virtual ~C() {--count;} +}; + +int C::count = 0; + +template <class T> +std::weak_ptr<T> source (std::shared_ptr<T> p) { return std::weak_ptr<T>(p); } + +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES +template <class T> +void sink (std::weak_ptr<T> &&) {} +#endif + +int main() +{ + { + const std::shared_ptr<A> ps(new A); + const std::weak_ptr<A> pA(ps); + assert(pA.use_count() == 1); + assert(B::count == 1); + assert(A::count == 1); + { + std::weak_ptr<A> pB(pA); + assert(B::count == 1); + assert(A::count == 1); + assert(pB.use_count() == 1); + assert(pA.use_count() == 1); + } + assert(pA.use_count() == 1); + assert(B::count == 1); + assert(A::count == 1); + } + assert(B::count == 0); + assert(A::count == 0); + { + std::weak_ptr<A> pA; + assert(pA.use_count() == 0); + assert(B::count == 0); + assert(A::count == 0); + { + std::weak_ptr<A> pB(pA); + assert(B::count == 0); + assert(A::count == 0); + assert(pB.use_count() == 0); + assert(pA.use_count() == 0); + } + assert(pA.use_count() == 0); + assert(B::count == 0); + assert(A::count == 0); + } + assert(B::count == 0); + assert(A::count == 0); + +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + { + std::shared_ptr<A> ps(new A); + std::weak_ptr<A> pA = source(ps); + assert(pA.use_count() == 1); + assert(A::count == 1); + sink(std::move(pA)); // kill off the weak pointer + } + assert(B::count == 0); + assert(A::count == 0); +#endif +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.const/weak_ptr_Y.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.const/weak_ptr_Y.pass.cpp new file mode 100644 index 000000000000..51a8fa5ae81f --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.const/weak_ptr_Y.pass.cpp @@ -0,0 +1,108 @@ +//===----------------------------------------------------------------------===// +// +// 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> + +// weak_ptr + +// template<class Y> weak_ptr(const weak_ptr<Y>& r); +// template<class Y> weak_ptr(weak_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; + +template <class T> +std::weak_ptr<T> source (std::shared_ptr<T> p) { return std::weak_ptr<T>(p); } + +int main() +{ + static_assert(( std::is_convertible<std::weak_ptr<A>, std::weak_ptr<B> >::value), ""); + static_assert((!std::is_convertible<std::weak_ptr<B>, std::weak_ptr<A> >::value), ""); + static_assert((!std::is_convertible<std::weak_ptr<A>, std::weak_ptr<C> >::value), ""); + { + const std::weak_ptr<A> pA(std::shared_ptr<A>(new A)); + assert(pA.use_count() == 0); + assert(B::count == 0); + assert(A::count == 0); + { + std::weak_ptr<B> pB(pA); + assert(B::count == 0); + assert(A::count == 0); + assert(pB.use_count() == 0); + assert(pA.use_count() == 0); + } + assert(pA.use_count() == 0); + assert(B::count == 0); + assert(A::count == 0); + } + assert(B::count == 0); + assert(A::count == 0); + { + std::weak_ptr<A> pA; + assert(pA.use_count() == 0); + assert(B::count == 0); + assert(A::count == 0); + { + std::weak_ptr<B> pB(pA); + assert(B::count == 0); + assert(A::count == 0); + assert(pB.use_count() == 0); + assert(pA.use_count() == 0); + } + 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> ps(new A); + std::weak_ptr<A> pA = source(ps); + std::weak_ptr<B> pB(std::move(pA)); + assert(pB.use_count() == 1); + } + assert(B::count == 0); + assert(A::count == 0); +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.dest/tested_elsewhere.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.dest/tested_elsewhere.pass.cpp new file mode 100644 index 000000000000..b58f5c55b643 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.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/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.mod/reset.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.mod/reset.pass.cpp new file mode 100644 index 000000000000..fa496d4bda59 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.mod/reset.pass.cpp @@ -0,0 +1,41 @@ +//===----------------------------------------------------------------------===// +// +// 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> + +// weak_ptr + +// void swap(weak_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> p1(new A); + std::weak_ptr<A> w1(p1); + assert(w1.use_count() == 1); + w1.reset(); + assert(w1.use_count() == 0); + assert(p1.use_count() == 1); + } + assert(A::count == 0); +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.mod/swap.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.mod/swap.pass.cpp new file mode 100644 index 000000000000..4001efb737e4 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.mod/swap.pass.cpp @@ -0,0 +1,49 @@ +//===----------------------------------------------------------------------===// +// +// 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> + +// weak_ptr + +// void swap(weak_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::weak_ptr<A> w1(p1); + { + std::shared_ptr<A> p2(ptr2); + std::weak_ptr<A> w2(p2); + w1.swap(w2); + assert(w1.use_count() == 1); + assert(w1.lock().get() == ptr2); + assert(w2.use_count() == 1); + assert(w2.lock().get() == ptr1); + assert(A::count == 2); + } + } + assert(A::count == 0); +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.obs/expired.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.obs/expired.pass.cpp new file mode 100644 index 000000000000..d61ac51afc62 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.obs/expired.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> + +// weak_ptr + +// bool expired() const; + +#include <memory> +#include <cassert> + +struct A +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +int main() +{ + { + std::weak_ptr<A> wp; + assert(wp.use_count() == 0); + assert(wp.expired() == (wp.use_count() == 0)); + } + { + std::shared_ptr<A> sp0(new A); + std::weak_ptr<A> wp(sp0); + assert(wp.use_count() == 1); + assert(wp.expired() == (wp.use_count() == 0)); + sp0.reset(); + assert(wp.use_count() == 0); + assert(wp.expired() == (wp.use_count() == 0)); + } +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.obs/lock.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.obs/lock.pass.cpp new file mode 100644 index 000000000000..956884b5c5c4 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.obs/lock.pass.cpp @@ -0,0 +1,58 @@ +//===----------------------------------------------------------------------===// +// +// 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> + +// weak_ptr + +// shared_ptr<T> lock() const; + +#include <memory> +#include <cassert> + +struct A +{ + static int count; + + A() {++count;} + A(const A&) {++count;} + ~A() {--count;} +}; + +int A::count = 0; + +int main() +{ + { + std::weak_ptr<A> wp; + std::shared_ptr<A> sp = wp.lock(); + assert(sp.use_count() == 0); + assert(sp.get() == 0); + assert(A::count == 0); + } + { + std::shared_ptr<A> sp0(new A); + std::weak_ptr<A> wp(sp0); + std::shared_ptr<A> sp = wp.lock(); + 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(); + std::shared_ptr<A> sp = wp.lock(); + assert(sp.use_count() == 0); + assert(sp.get() == 0); + assert(A::count == 0); + } + assert(A::count == 0); +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.obs/not_less_than.fail.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.obs/not_less_than.fail.cpp new file mode 100644 index 000000000000..ccffc2a66fb5 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.obs/not_less_than.fail.cpp @@ -0,0 +1,27 @@ +//===----------------------------------------------------------------------===// +// +// 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 weak_ptr; +// +// not less than comparable + +#include <memory> +#include <cassert> + +int main() +{ + const std::shared_ptr<int> p1(new int); + const std::shared_ptr<int> p2(new int); + const std::weak_ptr<int> w1(p1); + const std::weak_ptr<int> w2(p2); + + bool b = w1 < w2; +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.obs/owner_before_shared_ptr.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.obs/owner_before_shared_ptr.pass.cpp new file mode 100644 index 000000000000..4aa49cfe8a24 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.obs/owner_before_shared_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> + +// weak_ptr + +// template<class U> bool owner_before(const shared_ptr<U>& b); + +#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(!w1.owner_before(p2)); + assert(!w2.owner_before(p1)); + assert(w1.owner_before(p3) || w3.owner_before(p1)); + assert(w3.owner_before(p1) == w3.owner_before(p2)); +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.obs/owner_before_weak_ptr.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.obs/owner_before_weak_ptr.pass.cpp new file mode 100644 index 000000000000..9fe2b6e39035 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.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> + +// weak_ptr + +// template<class U> bool owner_before(const weak_ptr<U>& b); + +#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(!w1.owner_before(w2)); + assert(!w2.owner_before(w1)); + assert(w1.owner_before(w3) || w3.owner_before(w1)); + assert(w3.owner_before(w1) == w3.owner_before(w2)); +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.spec/swap.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.spec/swap.pass.cpp new file mode 100644 index 000000000000..e13d5aeaf636 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.spec/swap.pass.cpp @@ -0,0 +1,49 @@ +//===----------------------------------------------------------------------===// +// +// 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> + +// weak_ptr + +// template<class T> void swap(weak_ptr<T>& a, weak_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::weak_ptr<A> w1(p1); + { + std::shared_ptr<A> p2(ptr2); + std::weak_ptr<A> w2(p2); + swap(w1, w2); + assert(w1.use_count() == 1); + assert(w1.lock().get() == ptr2); + assert(w2.use_count() == 1); + assert(w2.lock().get() == ptr1); + assert(A::count == 2); + } + } + assert(A::count == 0); +} diff --git a/test/std/utilities/memory/util.smartptr/util.smartptr.weakptr/bad_weak_ptr.pass.cpp b/test/std/utilities/memory/util.smartptr/util.smartptr.weakptr/bad_weak_ptr.pass.cpp new file mode 100644 index 000000000000..cb895cd2bbf3 --- /dev/null +++ b/test/std/utilities/memory/util.smartptr/util.smartptr.weakptr/bad_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> + +// class bad_weak_ptr +// : public std::exception +// { +// public: +// bad_weak_ptr(); +// }; + +#include <memory> +#include <type_traits> +#include <cassert> +#include <cstring> + +int main() +{ + static_assert((std::is_base_of<std::exception, std::bad_weak_ptr>::value), ""); + std::bad_weak_ptr e; + std::bad_weak_ptr e2 = e; + e2 = e; + assert(std::strcmp(e.what(), "bad_weak_ptr") == 0); +} |