|
| 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 | +// std::function support for the "blocks" extension |
| 10 | + |
| 11 | +// UNSUPPORTED: c++98, c++03 |
| 12 | +// REQUIRES: has-fblocks |
| 13 | + |
| 14 | +// RUN: %{build} -fblocks |
| 15 | +// RUN: %{run} |
| 16 | + |
| 17 | +#include <functional> |
| 18 | +#include <cstdlib> |
| 19 | +#include <cassert> |
| 20 | + |
| 21 | +#include "test_macros.h" |
| 22 | +#include "count_new.h" |
| 23 | + |
| 24 | + |
| 25 | +struct A { |
| 26 | + static int count; |
| 27 | + int id_; |
| 28 | + explicit A(int id) { ++count; id_ = id; } |
| 29 | + A(const A &a) { id_ = a.id_; ++count; } |
| 30 | + ~A() { id_ = -1; --count; } |
| 31 | + int operator()() const { return -1; } |
| 32 | + int operator()(int i) const { return i; } |
| 33 | + int operator()(int, int) const { return -2; } |
| 34 | + int operator()(int, int, int) const { return -3; } |
| 35 | + int id() const { return id_; } |
| 36 | +}; |
| 37 | + |
| 38 | +int A::count = 0; |
| 39 | + |
| 40 | +int g(int) { return 0; } |
| 41 | + |
| 42 | +int main(int, char**) |
| 43 | +{ |
| 44 | + // swap |
| 45 | + { |
| 46 | + std::function<int(int)> f1 = g; |
| 47 | + std::function<int(int)> f2 = ^(int x) { return x + 1; }; |
| 48 | + assert(globalMemCounter.checkOutstandingNewEq(0)); |
| 49 | + assert(*f1.target<int(*)(int)>() == g); |
| 50 | + assert(*f2.target<int(^)(int)>() != 0); |
| 51 | + swap(f1, f2); |
| 52 | + assert(globalMemCounter.checkOutstandingNewEq(0)); |
| 53 | + assert(*f1.target<int(^)(int)>() != 0); |
| 54 | + assert(*f2.target<int(*)(int)>() == g); |
| 55 | + } |
| 56 | + |
| 57 | + // operator bool |
| 58 | + { |
| 59 | + std::function<int(int)> f; |
| 60 | + assert(!f); |
| 61 | + f = ^(int x) { return x+1; }; |
| 62 | + assert(f); |
| 63 | + } |
| 64 | + |
| 65 | + // operator() |
| 66 | + { |
| 67 | + std::function<int ()> r1(^{ return 4; }); |
| 68 | + assert(r1() == 4); |
| 69 | + } |
| 70 | + { |
| 71 | + __block bool called = false; |
| 72 | + std::function<void ()> r1(^{ called = true; }); |
| 73 | + r1(); |
| 74 | + assert(called); |
| 75 | + } |
| 76 | + { |
| 77 | + __block int param = 0; |
| 78 | + std::function<void (int)> r1(^(int x){ param = x; }); |
| 79 | + r1(4); |
| 80 | + assert(param == 4); |
| 81 | + } |
| 82 | + { |
| 83 | + std::function<int (int)> r1(^(int x){ return x + 4; }); |
| 84 | + assert(r1(3) == 7); |
| 85 | + } |
| 86 | + { |
| 87 | + __block int param1 = 0; |
| 88 | + __block int param2 = 0; |
| 89 | + std::function<void (int, int)> r1(^(int x, int y){ param1 = x; param2 = y; }); |
| 90 | + r1(3, 4); |
| 91 | + assert(param1 == 3); |
| 92 | + assert(param2 == 4); |
| 93 | + } |
| 94 | + { |
| 95 | + std::function<int (int, int)> r1(^(int x, int y){ return x + y; }); |
| 96 | + assert(r1(3, 4) == 7); |
| 97 | + } |
| 98 | + |
| 99 | + // swap |
| 100 | + { |
| 101 | + std::function<int(int)> f1 = A(999); |
| 102 | + std::function<int(int)> f2 = ^(int x) { return x + 1; }; |
| 103 | + assert(A::count == 1); |
| 104 | + assert(globalMemCounter.checkOutstandingNewEq(1)); |
| 105 | + assert(f1.target<A>()->id() == 999); |
| 106 | + assert((*f2.target<int(^)(int)>())(13) == 14); |
| 107 | + f1.swap(f2); |
| 108 | + assert(A::count == 1); |
| 109 | + assert(globalMemCounter.checkOutstandingNewEq(1)); |
| 110 | + assert((*f1.target<int(^)(int)>())(13) == 14); |
| 111 | + assert(f2.target<A>()->id() == 999); |
| 112 | + } |
| 113 | + assert(globalMemCounter.checkOutstandingNewEq(0)); |
| 114 | + assert(A::count == 0); |
| 115 | + |
| 116 | + // operator== and operator!= |
| 117 | + { |
| 118 | + std::function<int(int)> f; |
| 119 | + assert(f == nullptr); |
| 120 | + assert(nullptr == f); |
| 121 | + f = ^(int x) { return x + 1; }; |
| 122 | + assert(f != nullptr); |
| 123 | + assert(nullptr != f); |
| 124 | + } |
| 125 | + |
| 126 | + // target |
| 127 | + { |
| 128 | + int (^block)(int) = Block_copy(^(int x) { return x + 1; }); |
| 129 | + std::function<int(int)> f = block; |
| 130 | + assert(*f.target<int(^)(int)>() == block); |
| 131 | + assert(f.target<int(*)(int)>() == 0); |
| 132 | + Block_release(block); |
| 133 | + } |
| 134 | + |
| 135 | + // target_type |
| 136 | + { |
| 137 | + std::function<int(int)> f = ^(int x) { return x + 1; }; |
| 138 | + assert(f.target_type() == typeid(int(^)(int))); |
| 139 | + } |
| 140 | + |
| 141 | + return 0; |
| 142 | +} |
0 commit comments