Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // <memory> |
| 11 | |
| 12 | // allocator: |
| 13 | // template <class... Args> void construct(pointer p, Args&&... args); |
| 14 | |
| 15 | #include <memory> |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 16 | #include <cassert> |
| 17 | |
Eric Fiselier | b47a434 | 2015-07-28 07:31:50 +0000 | [diff] [blame] | 18 | #include "test_macros.h" |
Eric Fiselier | 4eb5b6d | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 19 | #include "count_new.hpp" |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 20 | |
| 21 | int A_constructed = 0; |
| 22 | |
| 23 | struct A |
| 24 | { |
| 25 | int data; |
| 26 | A() {++A_constructed;} |
| 27 | |
| 28 | A(const A&) {++A_constructed;} |
| 29 | |
| 30 | explicit A(int) {++A_constructed;} |
| 31 | A(int, int*) {++A_constructed;} |
| 32 | |
| 33 | ~A() {--A_constructed;} |
| 34 | }; |
| 35 | |
| 36 | int move_only_constructed = 0; |
| 37 | |
Eric Fiselier | b47a434 | 2015-07-28 07:31:50 +0000 | [diff] [blame] | 38 | #if TEST_STD_VER >= 11 |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 39 | class move_only |
| 40 | { |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 41 | int data; |
| 42 | |
Eric Fiselier | b47a434 | 2015-07-28 07:31:50 +0000 | [diff] [blame] | 43 | move_only(const move_only&) = delete; |
| 44 | move_only& operator=(const move_only&)= delete; |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 45 | |
| 46 | public: |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 47 | move_only(move_only&&) {++move_only_constructed;} |
Howard Hinnant | 6b41c60 | 2011-05-11 20:21:19 +0000 | [diff] [blame] | 48 | move_only& operator=(move_only&&) {return *this;} |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 49 | |
| 50 | move_only() {++move_only_constructed;} |
| 51 | ~move_only() {--move_only_constructed;} |
| 52 | }; |
Eric Fiselier | b47a434 | 2015-07-28 07:31:50 +0000 | [diff] [blame] | 53 | #endif // TEST_STD_VER >= 11 |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 54 | |
| 55 | int main() |
| 56 | { |
| 57 | { |
| 58 | std::allocator<A> a; |
Eric Fiselier | 4eb5b6d | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 59 | assert(globalMemCounter.checkOutstandingNewEq(0)); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 60 | assert(A_constructed == 0); |
| 61 | |
Eric Fiselier | 4eb5b6d | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 62 | globalMemCounter.last_new_size = 0; |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 63 | A* ap = a.allocate(3); |
Eric Fiselier | 4eb5b6d | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 64 | assert(globalMemCounter.checkOutstandingNewEq(1)); |
| 65 | assert(globalMemCounter.checkLastNewSizeEq(3 * sizeof(int))); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 66 | assert(A_constructed == 0); |
| 67 | |
| 68 | a.construct(ap); |
Eric Fiselier | 4eb5b6d | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 69 | assert(globalMemCounter.checkOutstandingNewEq(1)); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 70 | assert(A_constructed == 1); |
| 71 | |
| 72 | a.destroy(ap); |
Eric Fiselier | 4eb5b6d | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 73 | assert(globalMemCounter.checkOutstandingNewEq(1)); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 74 | assert(A_constructed == 0); |
| 75 | |
| 76 | a.construct(ap, A()); |
Eric Fiselier | 4eb5b6d | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 77 | assert(globalMemCounter.checkOutstandingNewEq(1)); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 78 | assert(A_constructed == 1); |
| 79 | |
| 80 | a.destroy(ap); |
Eric Fiselier | 4eb5b6d | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 81 | assert(globalMemCounter.checkOutstandingNewEq(1)); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 82 | assert(A_constructed == 0); |
| 83 | |
| 84 | a.construct(ap, 5); |
Eric Fiselier | 4eb5b6d | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 85 | assert(globalMemCounter.checkOutstandingNewEq(1)); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 86 | assert(A_constructed == 1); |
| 87 | |
| 88 | a.destroy(ap); |
Eric Fiselier | 4eb5b6d | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 89 | assert(globalMemCounter.checkOutstandingNewEq(1)); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 90 | assert(A_constructed == 0); |
| 91 | |
| 92 | a.construct(ap, 5, (int*)0); |
Eric Fiselier | 4eb5b6d | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 93 | assert(globalMemCounter.checkOutstandingNewEq(1)); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 94 | assert(A_constructed == 1); |
| 95 | |
| 96 | a.destroy(ap); |
Eric Fiselier | 4eb5b6d | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 97 | assert(globalMemCounter.checkOutstandingNewEq(1)); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 98 | assert(A_constructed == 0); |
| 99 | |
| 100 | a.deallocate(ap, 3); |
Eric Fiselier | 4eb5b6d | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 101 | assert(globalMemCounter.checkOutstandingNewEq(0)); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 102 | assert(A_constructed == 0); |
| 103 | } |
Eric Fiselier | b47a434 | 2015-07-28 07:31:50 +0000 | [diff] [blame] | 104 | #if TEST_STD_VER >= 11 |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 105 | { |
| 106 | std::allocator<move_only> a; |
Eric Fiselier | 4eb5b6d | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 107 | assert(globalMemCounter.checkOutstandingNewEq(0)); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 108 | assert(move_only_constructed == 0); |
| 109 | |
Eric Fiselier | 4eb5b6d | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 110 | globalMemCounter.last_new_size = 0; |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 111 | move_only* ap = a.allocate(3); |
Eric Fiselier | 4eb5b6d | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 112 | assert(globalMemCounter.checkOutstandingNewEq(1)); |
| 113 | assert(globalMemCounter.checkLastNewSizeEq(3 * sizeof(int))); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 114 | assert(move_only_constructed == 0); |
| 115 | |
| 116 | a.construct(ap); |
Eric Fiselier | 4eb5b6d | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 117 | assert(globalMemCounter.checkOutstandingNewEq(1)); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 118 | assert(move_only_constructed == 1); |
| 119 | |
| 120 | a.destroy(ap); |
Eric Fiselier | 4eb5b6d | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 121 | assert(globalMemCounter.checkOutstandingNewEq(1)); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 122 | assert(move_only_constructed == 0); |
| 123 | |
| 124 | a.construct(ap, move_only()); |
Eric Fiselier | 4eb5b6d | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 125 | assert(globalMemCounter.checkOutstandingNewEq(1)); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 126 | assert(move_only_constructed == 1); |
| 127 | |
| 128 | a.destroy(ap); |
Eric Fiselier | 4eb5b6d | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 129 | assert(globalMemCounter.checkOutstandingNewEq(1)); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 130 | assert(move_only_constructed == 0); |
| 131 | |
| 132 | a.deallocate(ap, 3); |
Eric Fiselier | 4eb5b6d | 2014-12-22 22:38:59 +0000 | [diff] [blame] | 133 | assert(globalMemCounter.checkOutstandingNewEq(0)); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 134 | assert(move_only_constructed == 0); |
| 135 | } |
Eric Fiselier | b47a434 | 2015-07-28 07:31:50 +0000 | [diff] [blame] | 136 | #endif |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 137 | } |