mtklein | 1138be4 | 2016-01-24 19:49:24 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | #include "Test.h" |
| 8 | #include "SkTemplates.h" |
| 9 | #include <utility> |
| 10 | |
| 11 | namespace { |
| 12 | class Moveable { |
| 13 | public: |
| 14 | Moveable() {} |
| 15 | Moveable(Moveable&&) {} |
| 16 | Moveable& operator=(Moveable&&) { return *this; } |
| 17 | private: |
| 18 | Moveable(const Moveable&); |
| 19 | Moveable& operator=(const Moveable&); |
| 20 | }; |
| 21 | template <typename T> void deleter(T*) { } |
| 22 | template <typename T> struct Deleter { |
| 23 | void operator()(T* t) { delete static_cast<const Moveable*>(t); } |
| 24 | }; |
| 25 | } // namespace |
| 26 | |
| 27 | DEF_TEST(CPlusPlusEleven_RvalueAndMove, r) { |
| 28 | Moveable src1; Moveable dst1(std::move(src1)); |
| 29 | Moveable src2, dst2; dst2 = std::move(src2); |
| 30 | } |
| 31 | |
| 32 | #define TOO_BIG "The unique_ptr was bigger than expected." |
| 33 | #define WEIRD_SIZE "The unique_ptr was a different size than expected." |
| 34 | |
| 35 | DEF_TEST(CPlusPlusEleven_UniquePtr, r) { |
| 36 | struct SmallUniquePtr { |
| 37 | Moveable* p; |
| 38 | }; |
| 39 | struct BigUniquePtr { |
| 40 | void(*d)(Moveable*); |
| 41 | Moveable* p; |
| 42 | }; |
| 43 | |
| 44 | static_assert(sizeof(skstd::unique_ptr<Moveable>) == sizeof(SmallUniquePtr), TOO_BIG); |
| 45 | static_assert(sizeof(skstd::unique_ptr<Moveable[]>) == sizeof(SmallUniquePtr), TOO_BIG); |
| 46 | |
| 47 | using proc = void(*)(Moveable*); |
| 48 | static_assert(sizeof(skstd::unique_ptr<Moveable, proc>) == sizeof(BigUniquePtr), WEIRD_SIZE); |
| 49 | static_assert(sizeof(skstd::unique_ptr<Moveable[], proc>) == sizeof(BigUniquePtr), WEIRD_SIZE); |
| 50 | |
| 51 | { |
| 52 | skstd::unique_ptr<Moveable, void(*)(Moveable*)> u(nullptr, deleter<Moveable>); |
| 53 | static_assert(sizeof(u) == sizeof(BigUniquePtr), WEIRD_SIZE); |
| 54 | |
| 55 | auto u2 = std::move(u); |
| 56 | static_assert(sizeof(u2) == sizeof(BigUniquePtr), WEIRD_SIZE); |
| 57 | } |
| 58 | |
| 59 | { |
| 60 | skstd::unique_ptr<Moveable, void(*)(Moveable*)> u(nullptr, [](Moveable* m){ deleter(m); }); |
| 61 | static_assert(sizeof(u) == sizeof(BigUniquePtr), WEIRD_SIZE); |
| 62 | |
| 63 | auto u2 = std::move(u); |
| 64 | static_assert(sizeof(u2) == sizeof(BigUniquePtr), WEIRD_SIZE); |
| 65 | } |
| 66 | |
| 67 | { |
| 68 | auto d = [](Moveable* m){ deleter(m); }; |
| 69 | skstd::unique_ptr<Moveable, decltype(d)> u(nullptr, d); |
| 70 | static_assert(sizeof(u) == sizeof(SmallUniquePtr), TOO_BIG); |
| 71 | |
| 72 | auto u2 = std::move(u); |
| 73 | static_assert(sizeof(u2) == sizeof(SmallUniquePtr), TOO_BIG); |
| 74 | } |
| 75 | |
| 76 | { |
| 77 | skstd::unique_ptr<Moveable, Deleter<Moveable>> u(nullptr, Deleter<Moveable>()); |
| 78 | static_assert(sizeof(u) == sizeof(SmallUniquePtr), TOO_BIG); |
| 79 | |
| 80 | auto u2 = std::move(u); |
| 81 | static_assert(sizeof(u2) == sizeof(SmallUniquePtr), TOO_BIG); |
| 82 | } |
| 83 | |
| 84 | { |
| 85 | skstd::unique_ptr<Moveable, Deleter<Moveable>> u(new Moveable(), Deleter<Moveable>()); |
| 86 | static_assert(sizeof(u) == sizeof(SmallUniquePtr), TOO_BIG); |
| 87 | |
| 88 | auto u2 = std::move(u); |
| 89 | static_assert(sizeof(u2) == sizeof(SmallUniquePtr), TOO_BIG); |
| 90 | } |
| 91 | |
| 92 | { |
| 93 | skstd::unique_ptr<const void, Deleter<const void>> u(new Moveable(), Deleter<const void>()); |
| 94 | static_assert(sizeof(u) == sizeof(SmallUniquePtr), TOO_BIG); |
| 95 | |
| 96 | auto u2 = std::move(u); |
| 97 | static_assert(sizeof(u2) == sizeof(SmallUniquePtr), TOO_BIG); |
| 98 | } |
| 99 | } |