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.hpp" 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 assert(globalMemCounter.checkOutstandingNewEq(0)); 63 { 64 std::function<int(int)> f1 = A(1); 65 std::function<int(int)> f2 = A(2); 66 #if TEST_STD_VER >= 11 67 static_assert(noexcept(swap(f1, f2)), "" ); 68 #endif 69 assert(A::count == 2); 70 assert(globalMemCounter.checkOutstandingNewEq(2)); 71 assert(f1.target<A>()->id() == 1); 72 assert(f2.target<A>()->id() == 2); 73 swap(f1, f2); 74 assert(A::count == 2); 75 assert(globalMemCounter.checkOutstandingNewEq(2)); 76 assert(f1.target<A>()->id() == 2); 77 assert(f2.target<A>()->id() == 1); 78 } 79 assert(A::count == 0); 80 assert(globalMemCounter.checkOutstandingNewEq(0)); 81 { 82 std::function<int(int)> f1 = A(1); 83 std::function<int(int)> f2 = g; 84 #if TEST_STD_VER >= 11 85 static_assert(noexcept(swap(f1, f2)), "" ); 86 #endif 87 assert(A::count == 1); 88 assert(globalMemCounter.checkOutstandingNewEq(1)); 89 assert(f1.target<A>()->id() == 1); 90 assert(*f2.target<int(*)(int)>() == g); 91 swap(f1, f2); 92 assert(A::count == 1); 93 assert(globalMemCounter.checkOutstandingNewEq(1)); 94 assert(*f1.target<int(*)(int)>() == g); 95 assert(f2.target<A>()->id() == 1); 96 } 97 assert(A::count == 0); 98 assert(globalMemCounter.checkOutstandingNewEq(0)); 99 { 100 std::function<int(int)> f1 = g; 101 std::function<int(int)> f2 = A(1); 102 #if TEST_STD_VER >= 11 103 static_assert(noexcept(swap(f1, f2)), "" ); 104 #endif 105 assert(A::count == 1); 106 assert(globalMemCounter.checkOutstandingNewEq(1)); 107 assert(*f1.target<int(*)(int)>() == g); 108 assert(f2.target<A>()->id() == 1); 109 swap(f1, f2); 110 assert(A::count == 1); 111 assert(globalMemCounter.checkOutstandingNewEq(1)); 112 assert(f1.target<A>()->id() == 1); 113 assert(*f2.target<int(*)(int)>() == g); 114 } 115 assert(A::count == 0); 116 assert(globalMemCounter.checkOutstandingNewEq(0)); 117 { 118 std::function<int(int)> f1 = g; 119 std::function<int(int)> f2 = h; 120 #if TEST_STD_VER >= 11 121 static_assert(noexcept(swap(f1, f2)), "" ); 122 #endif 123 assert(A::count == 0); 124 assert(globalMemCounter.checkOutstandingNewEq(0)); 125 assert(*f1.target<int(*)(int)>() == g); 126 assert(*f2.target<int(*)(int)>() == h); 127 swap(f1, f2); 128 assert(A::count == 0); 129 assert(globalMemCounter.checkOutstandingNewEq(0)); 130 assert(*f1.target<int(*)(int)>() == h); 131 assert(*f2.target<int(*)(int)>() == g); 132 } 133 assert(A::count == 0); 134 assert(globalMemCounter.checkOutstandingNewEq(0)); 135 136 return 0; 137 } 138