1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 // <functional> 10 11 // class function<R(ArgTypes...)> 12 13 // template <MoveConstructible R, MoveConstructible ... ArgTypes> 14 // void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&) noexcept; 15 16 17 #include <functional> 18 #include <cstdlib> 19 #include <cassert> 20 21 #include "test_macros.h" 22 #include "count_new.h" 23 24 class A 25 { 26 int data_[10]; 27 public: 28 static int count; 29 30 explicit A(int j) 31 { 32 ++count; 33 data_[0] = j; 34 } 35 36 A(const A& a) 37 { 38 ++count; 39 for (int i = 0; i < 10; ++i) 40 data_[i] = a.data_[i]; 41 } 42 43 ~A() {--count;} 44 45 int operator()(int i) const 46 { 47 for (int j = 0; j < 10; ++j) 48 i += data_[j]; 49 return i; 50 } 51 52 int id() const {return data_[0];} 53 }; 54 55 int A::count = 0; 56 57 int g(int) {return 0;} 58 int h(int) {return 1;} 59 60 int main(int, char**) 61 { 62 globalMemCounter.reset(); 63 assert(globalMemCounter.checkOutstandingNewEq(0)); 64 { 65 std::function<int(int)> f1 = A(1); 66 std::function<int(int)> f2 = A(2); 67 #if TEST_STD_VER >= 11 68 static_assert(noexcept(swap(f1, f2)), "" ); 69 #endif 70 assert(A::count == 2); 71 assert(globalMemCounter.checkOutstandingNewEq(2)); 72 assert(f1.target<A>()->id() == 1); 73 assert(f2.target<A>()->id() == 2); 74 swap(f1, f2); 75 assert(A::count == 2); 76 assert(globalMemCounter.checkOutstandingNewEq(2)); 77 assert(f1.target<A>()->id() == 2); 78 assert(f2.target<A>()->id() == 1); 79 } 80 assert(A::count == 0); 81 assert(globalMemCounter.checkOutstandingNewEq(0)); 82 { 83 std::function<int(int)> f1 = A(1); 84 std::function<int(int)> f2 = g; 85 #if TEST_STD_VER >= 11 86 static_assert(noexcept(swap(f1, f2)), "" ); 87 #endif 88 assert(A::count == 1); 89 assert(globalMemCounter.checkOutstandingNewEq(1)); 90 assert(f1.target<A>()->id() == 1); 91 assert(*f2.target<int(*)(int)>() == g); 92 swap(f1, f2); 93 assert(A::count == 1); 94 assert(globalMemCounter.checkOutstandingNewEq(1)); 95 assert(*f1.target<int(*)(int)>() == g); 96 assert(f2.target<A>()->id() == 1); 97 } 98 assert(A::count == 0); 99 assert(globalMemCounter.checkOutstandingNewEq(0)); 100 { 101 std::function<int(int)> f1 = g; 102 std::function<int(int)> f2 = A(1); 103 #if TEST_STD_VER >= 11 104 static_assert(noexcept(swap(f1, f2)), "" ); 105 #endif 106 assert(A::count == 1); 107 assert(globalMemCounter.checkOutstandingNewEq(1)); 108 assert(*f1.target<int(*)(int)>() == g); 109 assert(f2.target<A>()->id() == 1); 110 swap(f1, f2); 111 assert(A::count == 1); 112 assert(globalMemCounter.checkOutstandingNewEq(1)); 113 assert(f1.target<A>()->id() == 1); 114 assert(*f2.target<int(*)(int)>() == g); 115 } 116 assert(A::count == 0); 117 assert(globalMemCounter.checkOutstandingNewEq(0)); 118 { 119 std::function<int(int)> f1 = g; 120 std::function<int(int)> f2 = h; 121 #if TEST_STD_VER >= 11 122 static_assert(noexcept(swap(f1, f2)), "" ); 123 #endif 124 assert(A::count == 0); 125 assert(globalMemCounter.checkOutstandingNewEq(0)); 126 assert(*f1.target<int(*)(int)>() == g); 127 assert(*f2.target<int(*)(int)>() == h); 128 swap(f1, f2); 129 assert(A::count == 0); 130 assert(globalMemCounter.checkOutstandingNewEq(0)); 131 assert(*f1.target<int(*)(int)>() == h); 132 assert(*f2.target<int(*)(int)>() == g); 133 } 134 assert(A::count == 0); 135 assert(globalMemCounter.checkOutstandingNewEq(0)); 136 137 return 0; 138 } 139