diff options
| author | Dimitry Andric <dim@FreeBSD.org> | 2015-09-06 18:46:46 +0000 |
|---|---|---|
| committer | Dimitry Andric <dim@FreeBSD.org> | 2015-09-06 18:46:46 +0000 |
| commit | 61b9a7258a7693d7f3674a5a1daf7b036ff1d382 (patch) | |
| tree | ec41ed70ffca97240e76f9a78bb2dedba28f310c /test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create | |
| parent | f857581820d15e410e9945d2fcd5f7163be25a96 (diff) | |
Notes
Diffstat (limited to 'test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create')
4 files changed, 337 insertions, 0 deletions
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 0000000000000..aa77dab51515f --- /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 0000000000000..8dcd50e494110 --- /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 0000000000000..8cb972b0c1a14 --- /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 0000000000000..1045f9347b381 --- /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); +} |
