| Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| Daniel Cheng | cd23b8b | 2022-09-16 17:16:24 | [diff] [blame] | 5 | #include "base/functional/bind.h" |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 6 | |
| jdoerrie | 9d7236f6 | 2019-03-05 13:00:23 | [diff] [blame] | 7 | #include <functional> |
| dcheng | 69f2a04 | 2015-12-14 20:31:52 | [diff] [blame] | 8 | #include <memory> |
| Peter Kasting | a16713f | 2023-09-02 23:03:10 | [diff] [blame] | 9 | #include <string> |
| dcheng | 69f2a04 | 2015-12-14 20:31:52 | [diff] [blame] | 10 | #include <utility> |
| dcheng | 53b4cea | 2016-02-02 04:09:33 | [diff] [blame] | 11 | #include <vector> |
| dcheng | 69f2a04 | 2015-12-14 20:31:52 | [diff] [blame] | 12 | |
| Paul Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 13 | #include "base/allocator/partition_alloc_features.h" |
| 14 | #include "base/allocator/partition_alloc_support.h" |
| Arthur Sonzogni | 44769c8 | 2025-12-12 16:01:42 | [diff] [blame] | 15 | #include "base/compiler_specific.h" |
| Daniel Cheng | cd23b8b | 2022-09-16 17:16:24 | [diff] [blame] | 16 | #include "base/functional/callback.h" |
| Lei Zhang | 8a5af199 | 2025-06-07 07:15:45 | [diff] [blame] | 17 | #include "base/logging.h" |
| dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 18 | #include "base/memory/ptr_util.h" |
| Lukasz Anforowicz | 34d1682a | 2021-11-19 17:12:17 | [diff] [blame] | 19 | #include "base/memory/raw_ptr.h" |
| danakj | 92061c1 | 2022-07-28 23:53:50 | [diff] [blame] | 20 | #include "base/memory/raw_ref.h" |
| [email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 21 | #include "base/memory/ref_counted.h" |
| [email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 22 | #include "base/memory/weak_ptr.h" |
| danakj | 779e830 | 2020-10-27 18:35:58 | [diff] [blame] | 23 | #include "base/strings/string_number_conversions.h" |
| Guido Urdaneta | ef4e9194 | 2020-11-09 15:06:24 | [diff] [blame] | 24 | #include "base/test/bind.h" |
| gab | c964a85 | 2016-08-01 16:39:56 | [diff] [blame] | 25 | #include "base/test/gtest_util.h" |
| Paul Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 26 | #include "base/test/scoped_feature_list.h" |
| avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 27 | #include "build/build_config.h" |
| Arthur Sonzogni | fd39d61 | 2024-06-26 08:16:23 | [diff] [blame] | 28 | #include "partition_alloc/buildflags.h" |
| Yuki Shiino | 985ab91e | 2024-03-14 07:20:46 | [diff] [blame] | 29 | #include "partition_alloc/dangling_raw_ptr_checks.h" |
| Yuki Shiino | 985ab91e | 2024-03-14 07:20:46 | [diff] [blame] | 30 | #include "partition_alloc/partition_alloc_for_testing.h" |
| 31 | #include "partition_alloc/partition_root.h" |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 32 | #include "testing/gmock/include/gmock/gmock.h" |
| 33 | #include "testing/gtest/include/gtest/gtest.h" |
| 34 | |
| Daniel Cheng | 91f6fbaf | 2022-09-16 12:07:48 | [diff] [blame] | 35 | using ::testing::_; |
| tzik | efea4f5 | 2018-08-02 15:20:46 | [diff] [blame] | 36 | using ::testing::AnyNumber; |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 37 | using ::testing::ByMove; |
| tzik | efea4f5 | 2018-08-02 15:20:46 | [diff] [blame] | 38 | using ::testing::Mock; |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 39 | using ::testing::Return; |
| 40 | using ::testing::StrictMock; |
| 41 | |
| 42 | namespace base { |
| 43 | namespace { |
| 44 | |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 45 | class NoRef { |
| 46 | public: |
| Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 47 | NoRef() = default; |
| David Bienvenu | b4b441e | 2020-09-23 05:49:57 | [diff] [blame] | 48 | NoRef(const NoRef&) = delete; |
| 49 | // Particularly important in this test to ensure no copies are made. |
| 50 | NoRef& operator=(const NoRef&) = delete; |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 51 | |
| tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame] | 52 | MOCK_METHOD0(VoidMethod0, void()); |
| 53 | MOCK_CONST_METHOD0(VoidConstMethod0, void()); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 54 | |
| tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame] | 55 | MOCK_METHOD0(IntMethod0, int()); |
| 56 | MOCK_CONST_METHOD0(IntConstMethod0, int()); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 57 | |
| dcheng | 172b6ad | 2016-09-24 05:05:57 | [diff] [blame] | 58 | MOCK_METHOD1(VoidMethodWithIntArg, void(int)); |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 59 | MOCK_METHOD0(UniquePtrMethod0, std::unique_ptr<int>()); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | class HasRef : public NoRef { |
| 63 | public: |
| Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 64 | HasRef() = default; |
| David Bienvenu | b4b441e | 2020-09-23 05:49:57 | [diff] [blame] | 65 | HasRef(const HasRef&) = delete; |
| 66 | // Particularly important in this test to ensure no copies are made. |
| 67 | HasRef& operator=(const HasRef&) = delete; |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 68 | |
| tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame] | 69 | MOCK_CONST_METHOD0(AddRef, void()); |
| 70 | MOCK_CONST_METHOD0(Release, bool()); |
| tzik | efea4f5 | 2018-08-02 15:20:46 | [diff] [blame] | 71 | MOCK_CONST_METHOD0(HasAtLeastOneRef, bool()); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 72 | }; |
| 73 | |
| [email protected] | 690bda88 | 2011-04-13 22:40:46 | [diff] [blame] | 74 | class HasRefPrivateDtor : public HasRef { |
| 75 | private: |
| Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 76 | ~HasRefPrivateDtor() = default; |
| [email protected] | 690bda88 | 2011-04-13 22:40:46 | [diff] [blame] | 77 | }; |
| 78 | |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 79 | static const int kParentValue = 1; |
| 80 | static const int kChildValue = 2; |
| 81 | |
| 82 | class Parent { |
| 83 | public: |
| tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame] | 84 | void AddRef() const {} |
| 85 | void Release() const {} |
| tzik | efea4f5 | 2018-08-02 15:20:46 | [diff] [blame] | 86 | bool HasAtLeastOneRef() const { return true; } |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 87 | virtual void VirtualSet() { value = kParentValue; } |
| 88 | void NonVirtualSet() { value = kParentValue; } |
| 89 | int value; |
| 90 | }; |
| 91 | |
| 92 | class Child : public Parent { |
| 93 | public: |
| dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 94 | void VirtualSet() override { value = kChildValue; } |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 95 | void NonVirtualSet() { value = kChildValue; } |
| 96 | }; |
| 97 | |
| 98 | class NoRefParent { |
| 99 | public: |
| 100 | virtual void VirtualSet() { value = kParentValue; } |
| 101 | void NonVirtualSet() { value = kParentValue; } |
| 102 | int value; |
| 103 | }; |
| 104 | |
| 105 | class NoRefChild : public NoRefParent { |
| dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 106 | void VirtualSet() override { value = kChildValue; } |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 107 | void NonVirtualSet() { value = kChildValue; } |
| 108 | }; |
| 109 | |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 110 | // Used for probing the number of copies and moves that occur if a type must be |
| 111 | // coerced during argument forwarding in the Run() methods. |
| 112 | struct DerivedCopyMoveCounter { |
| 113 | DerivedCopyMoveCounter(int* copies, |
| 114 | int* assigns, |
| 115 | int* move_constructs, |
| 116 | int* move_assigns) |
| 117 | : copies_(copies), |
| 118 | assigns_(assigns), |
| 119 | move_constructs_(move_constructs), |
| 120 | move_assigns_(move_assigns) {} |
| Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 121 | raw_ptr<int> copies_; |
| 122 | raw_ptr<int> assigns_; |
| 123 | raw_ptr<int> move_constructs_; |
| 124 | raw_ptr<int> move_assigns_; |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 125 | }; |
| 126 | |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 127 | // Used for probing the number of copies and moves in an argument. |
| 128 | class CopyMoveCounter { |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 129 | public: |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 130 | CopyMoveCounter(int* copies, |
| 131 | int* assigns, |
| 132 | int* move_constructs, |
| 133 | int* move_assigns) |
| 134 | : copies_(copies), |
| 135 | assigns_(assigns), |
| 136 | move_constructs_(move_constructs), |
| 137 | move_assigns_(move_assigns) {} |
| 138 | |
| 139 | CopyMoveCounter(const CopyMoveCounter& other) |
| 140 | : copies_(other.copies_), |
| 141 | assigns_(other.assigns_), |
| 142 | move_constructs_(other.move_constructs_), |
| 143 | move_assigns_(other.move_assigns_) { |
| 144 | (*copies_)++; |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 145 | } |
| 146 | |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 147 | CopyMoveCounter(CopyMoveCounter&& other) |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 148 | : copies_(other.copies_), |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 149 | assigns_(other.assigns_), |
| 150 | move_constructs_(other.move_constructs_), |
| 151 | move_assigns_(other.move_assigns_) { |
| 152 | (*move_constructs_)++; |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 153 | } |
| 154 | |
| [email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 155 | // Probing for copies from coercion. |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 156 | explicit CopyMoveCounter(const DerivedCopyMoveCounter& other) |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 157 | : copies_(other.copies_), |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 158 | assigns_(other.assigns_), |
| 159 | move_constructs_(other.move_constructs_), |
| 160 | move_assigns_(other.move_assigns_) { |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 161 | (*copies_)++; |
| 162 | } |
| 163 | |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 164 | // Probing for moves from coercion. |
| 165 | explicit CopyMoveCounter(DerivedCopyMoveCounter&& other) |
| 166 | : copies_(other.copies_), |
| 167 | assigns_(other.assigns_), |
| 168 | move_constructs_(other.move_constructs_), |
| 169 | move_assigns_(other.move_assigns_) { |
| 170 | (*move_constructs_)++; |
| 171 | } |
| 172 | |
| 173 | const CopyMoveCounter& operator=(const CopyMoveCounter& rhs) { |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 174 | copies_ = rhs.copies_; |
| 175 | assigns_ = rhs.assigns_; |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 176 | move_constructs_ = rhs.move_constructs_; |
| 177 | move_assigns_ = rhs.move_assigns_; |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 178 | |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 179 | (*assigns_)++; |
| 180 | |
| 181 | return *this; |
| 182 | } |
| 183 | |
| 184 | const CopyMoveCounter& operator=(CopyMoveCounter&& rhs) { |
| 185 | copies_ = rhs.copies_; |
| 186 | assigns_ = rhs.assigns_; |
| 187 | move_constructs_ = rhs.move_constructs_; |
| 188 | move_assigns_ = rhs.move_assigns_; |
| 189 | |
| 190 | (*move_assigns_)++; |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 191 | |
| 192 | return *this; |
| 193 | } |
| 194 | |
| Daniel Cheng | 91f6fbaf | 2022-09-16 12:07:48 | [diff] [blame] | 195 | int copies() const { return *copies_; } |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 196 | |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 197 | private: |
| Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 198 | raw_ptr<int> copies_; |
| 199 | raw_ptr<int> assigns_; |
| 200 | raw_ptr<int> move_constructs_; |
| 201 | raw_ptr<int> move_assigns_; |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 202 | }; |
| 203 | |
| 204 | // Used for probing the number of copies in an argument. The instance is a |
| 205 | // copyable and non-movable type. |
| 206 | class CopyCounter { |
| 207 | public: |
| 208 | CopyCounter(int* copies, int* assigns) |
| 209 | : counter_(copies, assigns, nullptr, nullptr) {} |
| Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 210 | CopyCounter(const CopyCounter& other) = default; |
| 211 | CopyCounter& operator=(const CopyCounter& other) = default; |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 212 | |
| 213 | explicit CopyCounter(const DerivedCopyMoveCounter& other) : counter_(other) {} |
| 214 | |
| 215 | int copies() const { return counter_.copies(); } |
| 216 | |
| 217 | private: |
| 218 | CopyMoveCounter counter_; |
| 219 | }; |
| 220 | |
| 221 | // Used for probing the number of moves in an argument. The instance is a |
| 222 | // non-copyable and movable type. |
| 223 | class MoveCounter { |
| 224 | public: |
| 225 | MoveCounter(int* move_constructs, int* move_assigns) |
| 226 | : counter_(nullptr, nullptr, move_constructs, move_assigns) {} |
| 227 | MoveCounter(MoveCounter&& other) : counter_(std::move(other.counter_)) {} |
| 228 | MoveCounter& operator=(MoveCounter&& other) { |
| 229 | counter_ = std::move(other.counter_); |
| 230 | return *this; |
| 231 | } |
| 232 | |
| 233 | explicit MoveCounter(DerivedCopyMoveCounter&& other) |
| 234 | : counter_(std::move(other)) {} |
| 235 | |
| 236 | private: |
| 237 | CopyMoveCounter counter_; |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 238 | }; |
| 239 | |
| [email protected] | 08aa455 | 2011-10-15 00:34:42 | [diff] [blame] | 240 | class DeleteCounter { |
| 241 | public: |
| Daniel Cheng | 91f6fbaf | 2022-09-16 12:07:48 | [diff] [blame] | 242 | explicit DeleteCounter(int* deletes) : deletes_(deletes) {} |
| [email protected] | 08aa455 | 2011-10-15 00:34:42 | [diff] [blame] | 243 | |
| Daniel Cheng | 91f6fbaf | 2022-09-16 12:07:48 | [diff] [blame] | 244 | ~DeleteCounter() { (*deletes_)++; } |
| [email protected] | 08aa455 | 2011-10-15 00:34:42 | [diff] [blame] | 245 | |
| 246 | void VoidMethod0() {} |
| 247 | |
| 248 | private: |
| Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 249 | raw_ptr<int> deletes_; |
| [email protected] | 08aa455 | 2011-10-15 00:34:42 | [diff] [blame] | 250 | }; |
| 251 | |
| [email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 252 | template <typename T> |
| 253 | T PassThru(T scoper) { |
| dcheng | 69f2a04 | 2015-12-14 20:31:52 | [diff] [blame] | 254 | return scoper; |
| [email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 255 | } |
| 256 | |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 257 | // Some test functions that we can Bind to. |
| 258 | template <typename T> |
| 259 | T PolymorphicIdentity(T t) { |
| 260 | return t; |
| 261 | } |
| 262 | |
| tzik | 7fe3a68 | 2015-12-18 02:23:26 | [diff] [blame] | 263 | template <typename... Ts> |
| 264 | struct VoidPolymorphic { |
| 265 | static void Run(Ts... t) {} |
| 266 | }; |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 267 | |
| 268 | int Identity(int n) { |
| 269 | return n; |
| 270 | } |
| 271 | |
| 272 | int ArrayGet(const int array[], int n) { |
| Arthur Sonzogni | 44769c8 | 2025-12-12 16:01:42 | [diff] [blame] | 273 | return UNSAFE_TODO(array[n]); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | int Sum(int a, int b, int c, int d, int e, int f) { |
| 277 | return a + b + c + d + e + f; |
| 278 | } |
| 279 | |
| 280 | const char* CStringIdentity(const char* s) { |
| 281 | return s; |
| 282 | } |
| 283 | |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 284 | int GetCopies(const CopyMoveCounter& counter) { |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 285 | return counter.copies(); |
| 286 | } |
| 287 | |
| 288 | int UnwrapNoRefParent(NoRefParent p) { |
| 289 | return p.value; |
| 290 | } |
| 291 | |
| 292 | int UnwrapNoRefParentPtr(NoRefParent* p) { |
| 293 | return p->value; |
| 294 | } |
| 295 | |
| 296 | int UnwrapNoRefParentConstRef(const NoRefParent& p) { |
| 297 | return p.value; |
| 298 | } |
| 299 | |
| Daniel Cheng | 91f6fbaf | 2022-09-16 12:07:48 | [diff] [blame] | 300 | void RefArgSet(int& n) { |
| [email protected] | c18b105 | 2011-03-24 02:02:17 | [diff] [blame] | 301 | n = 2; |
| 302 | } |
| 303 | |
| Daniel Cheng | 91f6fbaf | 2022-09-16 12:07:48 | [diff] [blame] | 304 | void PtrArgSet(int* n) { |
| [email protected] | e24f876 | 2011-12-20 00:10:04 | [diff] [blame] | 305 | *n = 2; |
| 306 | } |
| 307 | |
| [email protected] | 9354058 | 2011-05-16 22:35:14 | [diff] [blame] | 308 | int FunctionWithWeakFirstParam(WeakPtr<NoRef> o, int n) { |
| 309 | return n; |
| 310 | } |
| 311 | |
| [email protected] | edd2f1b | 2013-06-22 20:32:50 | [diff] [blame] | 312 | int FunctionWithScopedRefptrFirstParam(const scoped_refptr<HasRef>& o, int n) { |
| 313 | return n; |
| 314 | } |
| 315 | |
| kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 316 | void TakesACallback(const RepeatingClosure& callback) { |
| [email protected] | e24f876 | 2011-12-20 00:10:04 | [diff] [blame] | 317 | callback.Run(); |
| 318 | } |
| 319 | |
| tzik | d58a8923 | 2018-04-26 04:29:53 | [diff] [blame] | 320 | int Noexcept() noexcept { |
| 321 | return 42; |
| 322 | } |
| 323 | |
| Ian McKellar | 29f262d | 2022-07-20 20:23:41 | [diff] [blame] | 324 | class NoexceptFunctor { |
| 325 | public: |
| 326 | int operator()() noexcept { return 42; } |
| 327 | }; |
| 328 | |
| 329 | class ConstNoexceptFunctor { |
| 330 | public: |
| 331 | int operator()() noexcept { return 42; } |
| 332 | }; |
| 333 | |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 334 | class BindTest : public ::testing::Test { |
| 335 | public: |
| 336 | BindTest() { |
| 337 | const_has_ref_ptr_ = &has_ref_; |
| 338 | const_no_ref_ptr_ = &no_ref_; |
| 339 | static_func_mock_ptr = &static_func_mock_; |
| 340 | } |
| David Bienvenu | b4b441e | 2020-09-23 05:49:57 | [diff] [blame] | 341 | BindTest(const BindTest&) = delete; |
| 342 | BindTest& operator=(const BindTest&) = delete; |
| Daniel Cheng | c8f0b40 | 2018-04-27 17:28:20 | [diff] [blame] | 343 | ~BindTest() override = default; |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 344 | |
| Daniel Cheng | 91f6fbaf | 2022-09-16 12:07:48 | [diff] [blame] | 345 | static void VoidFunc0() { static_func_mock_ptr->VoidMethod0(); } |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 346 | |
| tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame] | 347 | static int IntFunc0() { return static_func_mock_ptr->IntMethod0(); } |
| tzik | d58a8923 | 2018-04-26 04:29:53 | [diff] [blame] | 348 | int NoexceptMethod() noexcept { return 42; } |
| 349 | int ConstNoexceptMethod() const noexcept { return 42; } |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 350 | |
| 351 | protected: |
| 352 | StrictMock<NoRef> no_ref_; |
| 353 | StrictMock<HasRef> has_ref_; |
| Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 354 | raw_ptr<const HasRef> const_has_ref_ptr_; |
| 355 | raw_ptr<const NoRef> const_no_ref_ptr_; |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 356 | StrictMock<NoRef> static_func_mock_; |
| 357 | |
| 358 | // Used by the static functions to perform expectations. |
| 359 | static StrictMock<NoRef>* static_func_mock_ptr; |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 360 | }; |
| 361 | |
| 362 | StrictMock<NoRef>* BindTest::static_func_mock_ptr; |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 363 | StrictMock<NoRef>* g_func_mock_ptr; |
| 364 | |
| 365 | void VoidFunc0() { |
| 366 | g_func_mock_ptr->VoidMethod0(); |
| 367 | } |
| 368 | |
| 369 | int IntFunc0() { |
| 370 | return g_func_mock_ptr->IntMethod0(); |
| 371 | } |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 372 | |
| tzik | 4d4da50 | 2016-08-23 04:23:49 | [diff] [blame] | 373 | TEST_F(BindTest, BasicTest) { |
| kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 374 | RepeatingCallback<int(int, int, int)> cb = BindRepeating(&Sum, 32, 16, 8); |
| tzik | 4d4da50 | 2016-08-23 04:23:49 | [diff] [blame] | 375 | EXPECT_EQ(92, cb.Run(13, 12, 11)); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 376 | |
| kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 377 | RepeatingCallback<int(int, int, int, int, int, int)> c1 = BindRepeating(&Sum); |
| tzik | 4d4da50 | 2016-08-23 04:23:49 | [diff] [blame] | 378 | EXPECT_EQ(69, c1.Run(14, 13, 12, 11, 10, 9)); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 379 | |
| kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 380 | RepeatingCallback<int(int, int, int)> c2 = BindRepeating(c1, 32, 16, 8); |
| tzik | 4d4da50 | 2016-08-23 04:23:49 | [diff] [blame] | 381 | EXPECT_EQ(86, c2.Run(11, 10, 9)); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 382 | |
| kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 383 | RepeatingCallback<int()> c3 = BindRepeating(c2, 4, 2, 1); |
| tzik | 4d4da50 | 2016-08-23 04:23:49 | [diff] [blame] | 384 | EXPECT_EQ(63, c3.Run()); |
| [email protected] | cea20fe4 | 2011-09-30 09:09:34 | [diff] [blame] | 385 | } |
| 386 | |
| kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 387 | // Test that currying the rvalue result of another BindRepeating() works |
| 388 | // correctly. |
| 389 | // - rvalue should be usable as argument to BindRepeating(). |
| 390 | // - multiple runs of resulting RepeatingCallback remain valid. |
| [email protected] | e24f876 | 2011-12-20 00:10:04 | [diff] [blame] | 391 | TEST_F(BindTest, CurryingRvalueResultOfBind) { |
| 392 | int n = 0; |
| Daniel Cheng | 91f6fbaf | 2022-09-16 12:07:48 | [diff] [blame] | 393 | RepeatingClosure cb = |
| 394 | BindRepeating(&TakesACallback, BindRepeating(&PtrArgSet, &n)); |
| [email protected] | e24f876 | 2011-12-20 00:10:04 | [diff] [blame] | 395 | |
| kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 396 | // If we implement BindRepeating() such that the return value has |
| 397 | // auto_ptr-like semantics, the second call here will fail because ownership |
| 398 | // of the internal BindState<> would have been transferred to a *temporary* |
| 399 | // construction of a RepeatingCallback object on the first call. |
| [email protected] | e24f876 | 2011-12-20 00:10:04 | [diff] [blame] | 400 | cb.Run(); |
| 401 | EXPECT_EQ(2, n); |
| 402 | |
| 403 | n = 0; |
| 404 | cb.Run(); |
| 405 | EXPECT_EQ(2, n); |
| 406 | } |
| 407 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 408 | TEST_F(BindTest, RepeatingCallbackBasicTest) { |
| 409 | RepeatingCallback<int(int)> c0 = BindRepeating(&Sum, 1, 2, 4, 8, 16); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 410 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 411 | // RepeatingCallback can run via a lvalue-reference. |
| 412 | EXPECT_EQ(63, c0.Run(32)); |
| [email protected] | 81814bce | 2011-09-10 03:03:00 | [diff] [blame] | 413 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 414 | // It is valid to call a RepeatingCallback more than once. |
| 415 | EXPECT_EQ(54, c0.Run(23)); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 416 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 417 | // BindRepeating can handle a RepeatingCallback as the target functor. |
| 418 | RepeatingCallback<int()> c1 = BindRepeating(c0, 11); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 419 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 420 | // RepeatingCallback can run via a rvalue-reference. |
| 421 | EXPECT_EQ(42, std::move(c1).Run()); |
| 422 | |
| 423 | // BindRepeating can handle a rvalue-reference of RepeatingCallback. |
| 424 | EXPECT_EQ(32, BindRepeating(std::move(c0), 1).Run()); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 425 | } |
| 426 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 427 | TEST_F(BindTest, OnceCallbackBasicTest) { |
| 428 | OnceCallback<int(int)> c0 = BindOnce(&Sum, 1, 2, 4, 8, 16); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 429 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 430 | // OnceCallback can run via a rvalue-reference. |
| 431 | EXPECT_EQ(63, std::move(c0).Run(32)); |
| 432 | |
| 433 | // After running via the rvalue-reference, the value of the OnceCallback |
| 434 | // is undefined. The implementation simply clears the instance after the |
| 435 | // invocation. |
| Peter Kasting | 6218bbad | 2025-01-10 11:26:40 | [diff] [blame] | 436 | EXPECT_TRUE(c0.is_null()); // NOLINT(bugprone-use-after-move) |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 437 | |
| 438 | c0 = BindOnce(&Sum, 2, 3, 5, 7, 11); |
| 439 | |
| 440 | // BindOnce can handle a rvalue-reference of OnceCallback as the target |
| 441 | // functor. |
| 442 | OnceCallback<int()> c1 = BindOnce(std::move(c0), 13); |
| 443 | EXPECT_EQ(41, std::move(c1).Run()); |
| 444 | |
| 445 | RepeatingCallback<int(int)> c2 = BindRepeating(&Sum, 2, 3, 5, 7, 11); |
| 446 | EXPECT_EQ(41, BindOnce(c2, 13).Run()); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 447 | } |
| 448 | |
| [email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 449 | // IgnoreResult adapter test. |
| 450 | // - Function with return value. |
| 451 | // - Method with return value. |
| 452 | // - Const Method with return. |
| 453 | // - Method with return value bound to WeakPtr<>. |
| 454 | // - Const Method with return bound to WeakPtr<>. |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 455 | TEST_F(BindTest, IgnoreResultForRepeating) { |
| [email protected] | e8bfc31d | 2011-09-28 00:26:37 | [diff] [blame] | 456 | EXPECT_CALL(static_func_mock_, IntMethod0()).WillOnce(Return(1337)); |
| [email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 457 | EXPECT_CALL(has_ref_, AddRef()).Times(2); |
| 458 | EXPECT_CALL(has_ref_, Release()).Times(2); |
| tzik | efea4f5 | 2018-08-02 15:20:46 | [diff] [blame] | 459 | EXPECT_CALL(has_ref_, HasAtLeastOneRef()).WillRepeatedly(Return(true)); |
| [email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 460 | EXPECT_CALL(has_ref_, IntMethod0()).WillOnce(Return(10)); |
| 461 | EXPECT_CALL(has_ref_, IntConstMethod0()).WillOnce(Return(11)); |
| 462 | EXPECT_CALL(no_ref_, IntMethod0()).WillOnce(Return(12)); |
| 463 | EXPECT_CALL(no_ref_, IntConstMethod0()).WillOnce(Return(13)); |
| 464 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 465 | RepeatingClosure normal_func_cb = BindRepeating(IgnoreResult(&IntFunc0)); |
| [email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 466 | normal_func_cb.Run(); |
| 467 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 468 | RepeatingClosure non_void_method_cb = |
| 469 | BindRepeating(IgnoreResult(&HasRef::IntMethod0), &has_ref_); |
| [email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 470 | non_void_method_cb.Run(); |
| 471 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 472 | RepeatingClosure non_void_const_method_cb = |
| 473 | BindRepeating(IgnoreResult(&HasRef::IntConstMethod0), &has_ref_); |
| [email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 474 | non_void_const_method_cb.Run(); |
| 475 | |
| 476 | WeakPtrFactory<NoRef> weak_factory(&no_ref_); |
| Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 477 | WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_.get()); |
| [email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 478 | |
| Daniel Cheng | 91f6fbaf | 2022-09-16 12:07:48 | [diff] [blame] | 479 | RepeatingClosure non_void_weak_method_cb = BindRepeating( |
| 480 | IgnoreResult(&NoRef::IntMethod0), weak_factory.GetWeakPtr()); |
| [email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 481 | non_void_weak_method_cb.Run(); |
| 482 | |
| Daniel Cheng | 91f6fbaf | 2022-09-16 12:07:48 | [diff] [blame] | 483 | RepeatingClosure non_void_weak_const_method_cb = BindRepeating( |
| 484 | IgnoreResult(&NoRef::IntConstMethod0), weak_factory.GetWeakPtr()); |
| [email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 485 | non_void_weak_const_method_cb.Run(); |
| 486 | |
| 487 | weak_factory.InvalidateWeakPtrs(); |
| 488 | non_void_weak_const_method_cb.Run(); |
| 489 | non_void_weak_method_cb.Run(); |
| [email protected] | e8bfc31d | 2011-09-28 00:26:37 | [diff] [blame] | 490 | } |
| 491 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 492 | TEST_F(BindTest, IgnoreResultForOnce) { |
| 493 | EXPECT_CALL(static_func_mock_, IntMethod0()).WillOnce(Return(1337)); |
| 494 | EXPECT_CALL(has_ref_, AddRef()).Times(2); |
| 495 | EXPECT_CALL(has_ref_, Release()).Times(2); |
| tzik | efea4f5 | 2018-08-02 15:20:46 | [diff] [blame] | 496 | EXPECT_CALL(has_ref_, HasAtLeastOneRef()).WillRepeatedly(Return(true)); |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 497 | EXPECT_CALL(has_ref_, IntMethod0()).WillOnce(Return(10)); |
| 498 | EXPECT_CALL(has_ref_, IntConstMethod0()).WillOnce(Return(11)); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 499 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 500 | OnceClosure normal_func_cb = BindOnce(IgnoreResult(&IntFunc0)); |
| 501 | std::move(normal_func_cb).Run(); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 502 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 503 | OnceClosure non_void_method_cb = |
| 504 | BindOnce(IgnoreResult(&HasRef::IntMethod0), &has_ref_); |
| 505 | std::move(non_void_method_cb).Run(); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 506 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 507 | OnceClosure non_void_const_method_cb = |
| 508 | BindOnce(IgnoreResult(&HasRef::IntConstMethod0), &has_ref_); |
| 509 | std::move(non_void_const_method_cb).Run(); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 510 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 511 | WeakPtrFactory<NoRef> weak_factory(&no_ref_); |
| Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 512 | WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_.get()); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 513 | |
| Daniel Cheng | 91f6fbaf | 2022-09-16 12:07:48 | [diff] [blame] | 514 | OnceClosure non_void_weak_method_cb = |
| 515 | BindOnce(IgnoreResult(&NoRef::IntMethod0), weak_factory.GetWeakPtr()); |
| 516 | OnceClosure non_void_weak_const_method_cb = BindOnce( |
| 517 | IgnoreResult(&NoRef::IntConstMethod0), weak_factory.GetWeakPtr()); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 518 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 519 | weak_factory.InvalidateWeakPtrs(); |
| 520 | std::move(non_void_weak_const_method_cb).Run(); |
| 521 | std::move(non_void_weak_method_cb).Run(); |
| [email protected] | c18b105 | 2011-03-24 02:02:17 | [diff] [blame] | 522 | } |
| 523 | |
| danakj | 779e830 | 2020-10-27 18:35:58 | [diff] [blame] | 524 | TEST_F(BindTest, IgnoreResultForRepeatingCallback) { |
| 525 | std::string s; |
| 526 | RepeatingCallback<int(int)> cb = BindRepeating( |
| 527 | [](std::string* s, int i) { |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 528 | *s += "Run" + NumberToString(i); |
| danakj | 779e830 | 2020-10-27 18:35:58 | [diff] [blame] | 529 | return 5; |
| 530 | }, |
| 531 | &s); |
| 532 | RepeatingCallback<void(int)> noreturn = BindRepeating(IgnoreResult(cb)); |
| 533 | noreturn.Run(2); |
| 534 | EXPECT_EQ(s, "Run2"); |
| 535 | } |
| 536 | |
| 537 | TEST_F(BindTest, IgnoreResultForOnceCallback) { |
| 538 | std::string s; |
| 539 | OnceCallback<int(int)> cb = BindOnce( |
| 540 | [](std::string* s, int i) { |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 541 | *s += "Run" + NumberToString(i); |
| danakj | 779e830 | 2020-10-27 18:35:58 | [diff] [blame] | 542 | return 5; |
| 543 | }, |
| 544 | &s); |
| 545 | OnceCallback<void(int)> noreturn = BindOnce(IgnoreResult(std::move(cb))); |
| 546 | std::move(noreturn).Run(2); |
| 547 | EXPECT_EQ(s, "Run2"); |
| 548 | } |
| 549 | |
| kylechar | 72e6f78 | 2021-03-17 17:43:38 | [diff] [blame] | 550 | void SetFromRef(int& ref) { |
| 551 | EXPECT_EQ(ref, 1); |
| 552 | ref = 2; |
| 553 | EXPECT_EQ(ref, 2); |
| 554 | } |
| 555 | |
| 556 | TEST_F(BindTest, BindOnceWithNonConstRef) { |
| 557 | int v = 1; |
| 558 | |
| 559 | // Mutates `v` because it's not bound to callback instead it's forwarded by |
| 560 | // Run(). |
| 561 | auto cb1 = BindOnce(SetFromRef); |
| 562 | std::move(cb1).Run(v); |
| 563 | EXPECT_EQ(v, 2); |
| 564 | v = 1; |
| 565 | |
| 566 | // Mutates `v` through std::reference_wrapper bound to callback. |
| 567 | auto cb2 = BindOnce(SetFromRef, std::ref(v)); |
| 568 | std::move(cb2).Run(); |
| 569 | EXPECT_EQ(v, 2); |
| 570 | v = 1; |
| 571 | |
| 572 | // Everything past here following will make a copy of the argument. The copy |
| 573 | // will be mutated and leave `v` unmodified. |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 574 | auto cb3 = BindOnce(SetFromRef, OwnedRef(v)); |
| kylechar | 72e6f78 | 2021-03-17 17:43:38 | [diff] [blame] | 575 | std::move(cb3).Run(); |
| 576 | EXPECT_EQ(v, 1); |
| 577 | |
| 578 | int& ref = v; |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 579 | auto cb4 = BindOnce(SetFromRef, OwnedRef(ref)); |
| kylechar | 72e6f78 | 2021-03-17 17:43:38 | [diff] [blame] | 580 | std::move(cb4).Run(); |
| 581 | EXPECT_EQ(v, 1); |
| 582 | |
| 583 | const int cv = 1; |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 584 | auto cb5 = BindOnce(SetFromRef, OwnedRef(cv)); |
| kylechar | 72e6f78 | 2021-03-17 17:43:38 | [diff] [blame] | 585 | std::move(cb5).Run(); |
| 586 | EXPECT_EQ(cv, 1); |
| 587 | |
| 588 | const int& cref = v; |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 589 | auto cb6 = BindOnce(SetFromRef, OwnedRef(cref)); |
| kylechar | 72e6f78 | 2021-03-17 17:43:38 | [diff] [blame] | 590 | std::move(cb6).Run(); |
| 591 | EXPECT_EQ(cref, 1); |
| 592 | |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 593 | auto cb7 = BindOnce(SetFromRef, OwnedRef(1)); |
| kylechar | 72e6f78 | 2021-03-17 17:43:38 | [diff] [blame] | 594 | std::move(cb7).Run(); |
| 595 | } |
| 596 | |
| 597 | TEST_F(BindTest, BindRepeatingWithNonConstRef) { |
| 598 | int v = 1; |
| 599 | |
| 600 | // Mutates `v` because it's not bound to callback instead it's forwarded by |
| 601 | // Run(). |
| 602 | auto cb1 = BindRepeating(SetFromRef); |
| 603 | std::move(cb1).Run(v); |
| 604 | EXPECT_EQ(v, 2); |
| 605 | v = 1; |
| 606 | |
| 607 | // Mutates `v` through std::reference_wrapper bound to callback. |
| 608 | auto cb2 = BindRepeating(SetFromRef, std::ref(v)); |
| 609 | std::move(cb2).Run(); |
| 610 | EXPECT_EQ(v, 2); |
| 611 | v = 1; |
| 612 | |
| 613 | // Everything past here following will make a copy of the argument. The copy |
| 614 | // will be mutated and leave `v` unmodified. |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 615 | auto cb3 = BindRepeating(SetFromRef, OwnedRef(v)); |
| kylechar | 72e6f78 | 2021-03-17 17:43:38 | [diff] [blame] | 616 | std::move(cb3).Run(); |
| 617 | EXPECT_EQ(v, 1); |
| 618 | |
| 619 | int& ref = v; |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 620 | auto cb4 = BindRepeating(SetFromRef, OwnedRef(ref)); |
| kylechar | 72e6f78 | 2021-03-17 17:43:38 | [diff] [blame] | 621 | std::move(cb4).Run(); |
| 622 | EXPECT_EQ(v, 1); |
| 623 | |
| 624 | const int cv = 1; |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 625 | auto cb5 = BindRepeating(SetFromRef, OwnedRef(cv)); |
| kylechar | 72e6f78 | 2021-03-17 17:43:38 | [diff] [blame] | 626 | std::move(cb5).Run(); |
| 627 | EXPECT_EQ(cv, 1); |
| 628 | |
| 629 | const int& cref = v; |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 630 | auto cb6 = BindRepeating(SetFromRef, OwnedRef(cref)); |
| kylechar | 72e6f78 | 2021-03-17 17:43:38 | [diff] [blame] | 631 | std::move(cb6).Run(); |
| 632 | EXPECT_EQ(cref, 1); |
| 633 | |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 634 | auto cb7 = BindRepeating(SetFromRef, OwnedRef(1)); |
| kylechar | 72e6f78 | 2021-03-17 17:43:38 | [diff] [blame] | 635 | std::move(cb7).Run(); |
| 636 | } |
| 637 | |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 638 | // Functions that take reference parameters. |
| 639 | // - Forced reference parameter type still stores a copy. |
| 640 | // - Forced const reference parameter type still stores a copy. |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 641 | TEST_F(BindTest, ReferenceArgumentBindingForRepeating) { |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 642 | int n = 1; |
| 643 | int& ref_n = n; |
| 644 | const int& const_ref_n = n; |
| 645 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 646 | RepeatingCallback<int()> ref_copies_cb = BindRepeating(&Identity, ref_n); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 647 | EXPECT_EQ(n, ref_copies_cb.Run()); |
| 648 | n++; |
| 649 | EXPECT_EQ(n - 1, ref_copies_cb.Run()); |
| 650 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 651 | RepeatingCallback<int()> const_ref_copies_cb = |
| 652 | BindRepeating(&Identity, const_ref_n); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 653 | EXPECT_EQ(n, const_ref_copies_cb.Run()); |
| 654 | n++; |
| 655 | EXPECT_EQ(n - 1, const_ref_copies_cb.Run()); |
| 656 | } |
| 657 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 658 | TEST_F(BindTest, ReferenceArgumentBindingForOnce) { |
| 659 | int n = 1; |
| 660 | int& ref_n = n; |
| 661 | const int& const_ref_n = n; |
| 662 | |
| 663 | OnceCallback<int()> ref_copies_cb = BindOnce(&Identity, ref_n); |
| 664 | n++; |
| 665 | EXPECT_EQ(n - 1, std::move(ref_copies_cb).Run()); |
| 666 | |
| Daniel Cheng | 91f6fbaf | 2022-09-16 12:07:48 | [diff] [blame] | 667 | OnceCallback<int()> const_ref_copies_cb = BindOnce(&Identity, const_ref_n); |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 668 | n++; |
| 669 | EXPECT_EQ(n - 1, std::move(const_ref_copies_cb).Run()); |
| 670 | } |
| 671 | |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 672 | // Check that we can pass in arrays and have them be stored as a pointer. |
| 673 | // - Array of values stores a pointer. |
| 674 | // - Array of const values stores a pointer. |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 675 | TEST_F(BindTest, ArrayArgumentBindingForRepeating) { |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 676 | int array[4] = {1, 1, 1, 1}; |
| Daniel Cheng | 91f6fbaf | 2022-09-16 12:07:48 | [diff] [blame] | 677 | const int(*const_array_ptr)[4] = &array; |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 678 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 679 | RepeatingCallback<int()> array_cb = BindRepeating(&ArrayGet, array, 1); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 680 | EXPECT_EQ(1, array_cb.Run()); |
| 681 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 682 | RepeatingCallback<int()> const_array_cb = |
| 683 | BindRepeating(&ArrayGet, *const_array_ptr, 1); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 684 | EXPECT_EQ(1, const_array_cb.Run()); |
| 685 | |
| 686 | array[1] = 3; |
| 687 | EXPECT_EQ(3, array_cb.Run()); |
| 688 | EXPECT_EQ(3, const_array_cb.Run()); |
| 689 | } |
| 690 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 691 | TEST_F(BindTest, ArrayArgumentBindingForOnce) { |
| 692 | int array[4] = {1, 1, 1, 1}; |
| Daniel Cheng | 91f6fbaf | 2022-09-16 12:07:48 | [diff] [blame] | 693 | const int(*const_array_ptr)[4] = &array; |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 694 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 695 | OnceCallback<int()> array_cb = BindOnce(&ArrayGet, array, 1); |
| Daniel Cheng | 91f6fbaf | 2022-09-16 12:07:48 | [diff] [blame] | 696 | OnceCallback<int()> const_array_cb = BindOnce(&ArrayGet, *const_array_ptr, 1); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 697 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 698 | array[1] = 3; |
| 699 | EXPECT_EQ(3, std::move(array_cb).Run()); |
| 700 | EXPECT_EQ(3, std::move(const_array_cb).Run()); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 701 | } |
| 702 | |
| [email protected] | 9354058 | 2011-05-16 22:35:14 | [diff] [blame] | 703 | // WeakPtr() support. |
| 704 | // - Method bound to WeakPtr<> to non-const object. |
| 705 | // - Const method bound to WeakPtr<> to non-const object. |
| 706 | // - Const method bound to WeakPtr<> to const object. |
| 707 | // - Normal Function with WeakPtr<> as P1 can have return type and is |
| 708 | // not canceled. |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 709 | TEST_F(BindTest, WeakPtrForRepeating) { |
| [email protected] | 9354058 | 2011-05-16 22:35:14 | [diff] [blame] | 710 | EXPECT_CALL(no_ref_, VoidMethod0()); |
| 711 | EXPECT_CALL(no_ref_, VoidConstMethod0()).Times(2); |
| 712 | |
| 713 | WeakPtrFactory<NoRef> weak_factory(&no_ref_); |
| Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 714 | WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_.get()); |
| [email protected] | 9354058 | 2011-05-16 22:35:14 | [diff] [blame] | 715 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 716 | RepeatingClosure method_cb = |
| 717 | BindRepeating(&NoRef::VoidMethod0, weak_factory.GetWeakPtr()); |
| [email protected] | 9354058 | 2011-05-16 22:35:14 | [diff] [blame] | 718 | method_cb.Run(); |
| 719 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 720 | RepeatingClosure const_method_cb = |
| 721 | BindRepeating(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr()); |
| [email protected] | 9354058 | 2011-05-16 22:35:14 | [diff] [blame] | 722 | const_method_cb.Run(); |
| 723 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 724 | RepeatingClosure const_method_const_ptr_cb = |
| 725 | BindRepeating(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr()); |
| [email protected] | 9354058 | 2011-05-16 22:35:14 | [diff] [blame] | 726 | const_method_const_ptr_cb.Run(); |
| 727 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 728 | RepeatingCallback<int(int)> normal_func_cb = |
| 729 | BindRepeating(&FunctionWithWeakFirstParam, weak_factory.GetWeakPtr()); |
| [email protected] | 9354058 | 2011-05-16 22:35:14 | [diff] [blame] | 730 | EXPECT_EQ(1, normal_func_cb.Run(1)); |
| 731 | |
| 732 | weak_factory.InvalidateWeakPtrs(); |
| 733 | const_weak_factory.InvalidateWeakPtrs(); |
| 734 | |
| 735 | method_cb.Run(); |
| 736 | const_method_cb.Run(); |
| 737 | const_method_const_ptr_cb.Run(); |
| 738 | |
| 739 | // Still runs even after the pointers are invalidated. |
| 740 | EXPECT_EQ(2, normal_func_cb.Run(2)); |
| 741 | } |
| 742 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 743 | TEST_F(BindTest, WeakPtrForOnce) { |
| 744 | WeakPtrFactory<NoRef> weak_factory(&no_ref_); |
| Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 745 | WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_.get()); |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 746 | |
| 747 | OnceClosure method_cb = |
| 748 | BindOnce(&NoRef::VoidMethod0, weak_factory.GetWeakPtr()); |
| 749 | OnceClosure const_method_cb = |
| 750 | BindOnce(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr()); |
| 751 | OnceClosure const_method_const_ptr_cb = |
| 752 | BindOnce(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr()); |
| kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 753 | OnceCallback<int(int)> normal_func_cb = |
| 754 | BindOnce(&FunctionWithWeakFirstParam, weak_factory.GetWeakPtr()); |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 755 | |
| 756 | weak_factory.InvalidateWeakPtrs(); |
| 757 | const_weak_factory.InvalidateWeakPtrs(); |
| 758 | |
| 759 | std::move(method_cb).Run(); |
| 760 | std::move(const_method_cb).Run(); |
| 761 | std::move(const_method_const_ptr_cb).Run(); |
| 762 | |
| 763 | // Still runs even after the pointers are invalidated. |
| 764 | EXPECT_EQ(2, std::move(normal_func_cb).Run(2)); |
| 765 | } |
| 766 | |
| jdoerrie | 9d7236f6 | 2019-03-05 13:00:23 | [diff] [blame] | 767 | // std::cref() wrapper support. |
| 768 | // - Binding w/o std::cref takes a copy. |
| 769 | // - Binding a std::cref takes a reference. |
| 770 | // - Binding std::cref to a function std::cref does not copy on invoke. |
| 771 | TEST_F(BindTest, StdCrefForRepeating) { |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 772 | int n = 1; |
| 773 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 774 | RepeatingCallback<int()> copy_cb = BindRepeating(&Identity, n); |
| jdoerrie | 9d7236f6 | 2019-03-05 13:00:23 | [diff] [blame] | 775 | RepeatingCallback<int()> const_ref_cb = |
| 776 | BindRepeating(&Identity, std::cref(n)); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 777 | EXPECT_EQ(n, copy_cb.Run()); |
| 778 | EXPECT_EQ(n, const_ref_cb.Run()); |
| 779 | n++; |
| 780 | EXPECT_EQ(n - 1, copy_cb.Run()); |
| 781 | EXPECT_EQ(n, const_ref_cb.Run()); |
| 782 | |
| 783 | int copies = 0; |
| 784 | int assigns = 0; |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 785 | int move_constructs = 0; |
| 786 | int move_assigns = 0; |
| 787 | CopyMoveCounter counter(&copies, &assigns, &move_constructs, &move_assigns); |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 788 | RepeatingCallback<int()> all_const_ref_cb = |
| jdoerrie | 9d7236f6 | 2019-03-05 13:00:23 | [diff] [blame] | 789 | BindRepeating(&GetCopies, std::cref(counter)); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 790 | EXPECT_EQ(0, all_const_ref_cb.Run()); |
| 791 | EXPECT_EQ(0, copies); |
| 792 | EXPECT_EQ(0, assigns); |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 793 | EXPECT_EQ(0, move_constructs); |
| 794 | EXPECT_EQ(0, move_assigns); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 795 | } |
| 796 | |
| jdoerrie | 9d7236f6 | 2019-03-05 13:00:23 | [diff] [blame] | 797 | TEST_F(BindTest, StdCrefForOnce) { |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 798 | int n = 1; |
| [email protected] | edd2f1b | 2013-06-22 20:32:50 | [diff] [blame] | 799 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 800 | OnceCallback<int()> copy_cb = BindOnce(&Identity, n); |
| jdoerrie | 9d7236f6 | 2019-03-05 13:00:23 | [diff] [blame] | 801 | OnceCallback<int()> const_ref_cb = BindOnce(&Identity, std::cref(n)); |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 802 | n++; |
| 803 | EXPECT_EQ(n - 1, std::move(copy_cb).Run()); |
| 804 | EXPECT_EQ(n, std::move(const_ref_cb).Run()); |
| 805 | |
| 806 | int copies = 0; |
| 807 | int assigns = 0; |
| 808 | int move_constructs = 0; |
| 809 | int move_assigns = 0; |
| 810 | CopyMoveCounter counter(&copies, &assigns, &move_constructs, &move_assigns); |
| 811 | OnceCallback<int()> all_const_ref_cb = |
| jdoerrie | 9d7236f6 | 2019-03-05 13:00:23 | [diff] [blame] | 812 | BindOnce(&GetCopies, std::cref(counter)); |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 813 | EXPECT_EQ(0, std::move(all_const_ref_cb).Run()); |
| 814 | EXPECT_EQ(0, copies); |
| 815 | EXPECT_EQ(0, assigns); |
| 816 | EXPECT_EQ(0, move_constructs); |
| 817 | EXPECT_EQ(0, move_assigns); |
| [email protected] | edd2f1b | 2013-06-22 20:32:50 | [diff] [blame] | 818 | } |
| 819 | |
| [email protected] | 08aa455 | 2011-10-15 00:34:42 | [diff] [blame] | 820 | // Test Owned() support. |
| jdoerrie | 68f2d51b | 2019-02-28 17:32:44 | [diff] [blame] | 821 | TEST_F(BindTest, OwnedForRepeatingRawPtr) { |
| [email protected] | 08aa455 | 2011-10-15 00:34:42 | [diff] [blame] | 822 | int deletes = 0; |
| 823 | DeleteCounter* counter = new DeleteCounter(&deletes); |
| 824 | |
| 825 | // If we don't capture, delete happens on Callback destruction/reset. |
| 826 | // return the same value. |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 827 | RepeatingCallback<DeleteCounter*()> no_capture_cb = |
| 828 | BindRepeating(&PolymorphicIdentity<DeleteCounter*>, Owned(counter)); |
| [email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 829 | ASSERT_EQ(counter, no_capture_cb.Run()); |
| 830 | ASSERT_EQ(counter, no_capture_cb.Run()); |
| [email protected] | 08aa455 | 2011-10-15 00:34:42 | [diff] [blame] | 831 | EXPECT_EQ(0, deletes); |
| 832 | no_capture_cb.Reset(); // This should trigger a delete. |
| 833 | EXPECT_EQ(1, deletes); |
| 834 | |
| 835 | deletes = 0; |
| 836 | counter = new DeleteCounter(&deletes); |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 837 | RepeatingClosure own_object_cb = |
| 838 | BindRepeating(&DeleteCounter::VoidMethod0, Owned(counter)); |
| [email protected] | 08aa455 | 2011-10-15 00:34:42 | [diff] [blame] | 839 | own_object_cb.Run(); |
| 840 | EXPECT_EQ(0, deletes); |
| 841 | own_object_cb.Reset(); |
| 842 | EXPECT_EQ(1, deletes); |
| 843 | } |
| 844 | |
| jdoerrie | 68f2d51b | 2019-02-28 17:32:44 | [diff] [blame] | 845 | TEST_F(BindTest, OwnedForOnceRawPtr) { |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 846 | int deletes = 0; |
| 847 | DeleteCounter* counter = new DeleteCounter(&deletes); |
| 848 | |
| 849 | // If we don't capture, delete happens on Callback destruction/reset. |
| 850 | // return the same value. |
| 851 | OnceCallback<DeleteCounter*()> no_capture_cb = |
| 852 | BindOnce(&PolymorphicIdentity<DeleteCounter*>, Owned(counter)); |
| 853 | EXPECT_EQ(0, deletes); |
| 854 | no_capture_cb.Reset(); // This should trigger a delete. |
| 855 | EXPECT_EQ(1, deletes); |
| 856 | |
| 857 | deletes = 0; |
| 858 | counter = new DeleteCounter(&deletes); |
| 859 | OnceClosure own_object_cb = |
| 860 | BindOnce(&DeleteCounter::VoidMethod0, Owned(counter)); |
| 861 | EXPECT_EQ(0, deletes); |
| 862 | own_object_cb.Reset(); |
| 863 | EXPECT_EQ(1, deletes); |
| 864 | } |
| 865 | |
| jdoerrie | 68f2d51b | 2019-02-28 17:32:44 | [diff] [blame] | 866 | TEST_F(BindTest, OwnedForRepeatingUniquePtr) { |
| 867 | int deletes = 0; |
| 868 | auto counter = std::make_unique<DeleteCounter>(&deletes); |
| 869 | DeleteCounter* raw_counter = counter.get(); |
| 870 | |
| 871 | // If we don't capture, delete happens on Callback destruction/reset. |
| 872 | // return the same value. |
| 873 | RepeatingCallback<DeleteCounter*()> no_capture_cb = BindRepeating( |
| 874 | &PolymorphicIdentity<DeleteCounter*>, Owned(std::move(counter))); |
| 875 | ASSERT_EQ(raw_counter, no_capture_cb.Run()); |
| 876 | ASSERT_EQ(raw_counter, no_capture_cb.Run()); |
| 877 | EXPECT_EQ(0, deletes); |
| 878 | no_capture_cb.Reset(); // This should trigger a delete. |
| 879 | EXPECT_EQ(1, deletes); |
| 880 | |
| 881 | deletes = 0; |
| 882 | counter = std::make_unique<DeleteCounter>(&deletes); |
| 883 | RepeatingClosure own_object_cb = |
| 884 | BindRepeating(&DeleteCounter::VoidMethod0, Owned(std::move(counter))); |
| 885 | own_object_cb.Run(); |
| 886 | EXPECT_EQ(0, deletes); |
| 887 | own_object_cb.Reset(); |
| 888 | EXPECT_EQ(1, deletes); |
| 889 | } |
| 890 | |
| 891 | TEST_F(BindTest, OwnedForOnceUniquePtr) { |
| 892 | int deletes = 0; |
| 893 | auto counter = std::make_unique<DeleteCounter>(&deletes); |
| 894 | |
| 895 | // If we don't capture, delete happens on Callback destruction/reset. |
| 896 | // return the same value. |
| 897 | OnceCallback<DeleteCounter*()> no_capture_cb = |
| 898 | BindOnce(&PolymorphicIdentity<DeleteCounter*>, Owned(std::move(counter))); |
| 899 | EXPECT_EQ(0, deletes); |
| 900 | no_capture_cb.Reset(); // This should trigger a delete. |
| 901 | EXPECT_EQ(1, deletes); |
| 902 | |
| 903 | deletes = 0; |
| 904 | counter = std::make_unique<DeleteCounter>(&deletes); |
| 905 | OnceClosure own_object_cb = |
| 906 | BindOnce(&DeleteCounter::VoidMethod0, Owned(std::move(counter))); |
| 907 | EXPECT_EQ(0, deletes); |
| 908 | own_object_cb.Reset(); |
| 909 | EXPECT_EQ(1, deletes); |
| 910 | } |
| 911 | |
| Jan Wilken Dörrie | 1d011fb | 2021-03-11 19:37:37 | [diff] [blame] | 912 | // Tests OwnedRef |
| 913 | TEST_F(BindTest, OwnedRefForCounter) { |
| 914 | int counter = 0; |
| 915 | RepeatingCallback<int()> counter_callback = |
| 916 | BindRepeating([](int& counter) { return ++counter; }, OwnedRef(counter)); |
| 917 | |
| 918 | EXPECT_EQ(1, counter_callback.Run()); |
| 919 | EXPECT_EQ(2, counter_callback.Run()); |
| 920 | EXPECT_EQ(3, counter_callback.Run()); |
| 921 | EXPECT_EQ(4, counter_callback.Run()); |
| 922 | |
| 923 | EXPECT_EQ(0, counter); // counter should remain unchanged. |
| 924 | } |
| 925 | |
| 926 | TEST_F(BindTest, OwnedRefForIgnoringArguments) { |
| 927 | OnceCallback<std::string(std::string)> echo_callback = |
| 928 | BindOnce([](int& ignore, std::string s) { return s; }, OwnedRef(0)); |
| 929 | |
| 930 | EXPECT_EQ("Hello World", std::move(echo_callback).Run("Hello World")); |
| 931 | } |
| 932 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 933 | template <typename T> |
| Daniel Cheng | 91f6fbaf | 2022-09-16 12:07:48 | [diff] [blame] | 934 | class BindVariantsTest : public ::testing::Test {}; |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 935 | |
| 936 | struct RepeatingTestConfig { |
| 937 | template <typename Signature> |
| 938 | using CallbackType = RepeatingCallback<Signature>; |
| 939 | using ClosureType = RepeatingClosure; |
| 940 | |
| 941 | template <typename F, typename... Args> |
| Peter Kasting | 52d553c13 | 2023-12-11 22:43:48 | [diff] [blame] | 942 | static auto Bind(F&& f, Args&&... args) { |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 943 | return BindRepeating(std::forward<F>(f), std::forward<Args>(args)...); |
| 944 | } |
| 945 | }; |
| 946 | |
| 947 | struct OnceTestConfig { |
| 948 | template <typename Signature> |
| 949 | using CallbackType = OnceCallback<Signature>; |
| 950 | using ClosureType = OnceClosure; |
| 951 | |
| 952 | template <typename F, typename... Args> |
| Peter Kasting | 52d553c13 | 2023-12-11 22:43:48 | [diff] [blame] | 953 | static auto Bind(F&& f, Args&&... args) { |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 954 | return BindOnce(std::forward<F>(f), std::forward<Args>(args)...); |
| 955 | } |
| 956 | }; |
| 957 | |
| Daniel Cheng | 91f6fbaf | 2022-09-16 12:07:48 | [diff] [blame] | 958 | using BindVariantsTestConfig = |
| 959 | ::testing::Types<RepeatingTestConfig, OnceTestConfig>; |
| Victor Costan | ebc5273 | 2019-02-15 02:39:47 | [diff] [blame] | 960 | TYPED_TEST_SUITE(BindVariantsTest, BindVariantsTestConfig); |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 961 | |
| 962 | template <typename TypeParam, typename Signature> |
| 963 | using CallbackType = typename TypeParam::template CallbackType<Signature>; |
| 964 | |
| 965 | // Function type support. |
| 966 | // - Normal function. |
| 967 | // - Normal function bound with non-refcounted first argument. |
| 968 | // - Method bound to non-const object. |
| 969 | // - Method bound to scoped_refptr. |
| 970 | // - Const method bound to non-const object. |
| 971 | // - Const method bound to const object. |
| 972 | // - Derived classes can be used with pointers to non-virtual base functions. |
| 973 | // - Derived classes can be used with pointers to virtual base functions (and |
| 974 | // preserve virtual dispatch). |
| 975 | TYPED_TEST(BindVariantsTest, FunctionTypeSupport) { |
| 976 | using ClosureType = typename TypeParam::ClosureType; |
| 977 | |
| 978 | StrictMock<HasRef> has_ref; |
| 979 | StrictMock<NoRef> no_ref; |
| 980 | StrictMock<NoRef> static_func_mock; |
| 981 | const HasRef* const_has_ref_ptr = &has_ref; |
| 982 | g_func_mock_ptr = &static_func_mock; |
| 983 | |
| 984 | EXPECT_CALL(static_func_mock, VoidMethod0()); |
| 985 | EXPECT_CALL(has_ref, AddRef()).Times(4); |
| 986 | EXPECT_CALL(has_ref, Release()).Times(4); |
| tzik | efea4f5 | 2018-08-02 15:20:46 | [diff] [blame] | 987 | EXPECT_CALL(has_ref, HasAtLeastOneRef()).WillRepeatedly(Return(true)); |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 988 | EXPECT_CALL(has_ref, VoidMethod0()).Times(2); |
| 989 | EXPECT_CALL(has_ref, VoidConstMethod0()).Times(2); |
| 990 | |
| 991 | ClosureType normal_cb = TypeParam::Bind(&VoidFunc0); |
| 992 | CallbackType<TypeParam, NoRef*()> normal_non_refcounted_cb = |
| 993 | TypeParam::Bind(&PolymorphicIdentity<NoRef*>, &no_ref); |
| 994 | std::move(normal_cb).Run(); |
| 995 | EXPECT_EQ(&no_ref, std::move(normal_non_refcounted_cb).Run()); |
| 996 | |
| 997 | ClosureType method_cb = TypeParam::Bind(&HasRef::VoidMethod0, &has_ref); |
| kylechar | 973a041 | 2017-09-26 18:40:29 | [diff] [blame] | 998 | ClosureType method_refptr_cb = |
| 999 | TypeParam::Bind(&HasRef::VoidMethod0, WrapRefCounted(&has_ref)); |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 1000 | ClosureType const_method_nonconst_obj_cb = |
| 1001 | TypeParam::Bind(&HasRef::VoidConstMethod0, &has_ref); |
| 1002 | ClosureType const_method_const_obj_cb = |
| 1003 | TypeParam::Bind(&HasRef::VoidConstMethod0, const_has_ref_ptr); |
| 1004 | std::move(method_cb).Run(); |
| 1005 | std::move(method_refptr_cb).Run(); |
| 1006 | std::move(const_method_nonconst_obj_cb).Run(); |
| 1007 | std::move(const_method_const_obj_cb).Run(); |
| 1008 | |
| 1009 | Child child; |
| 1010 | child.value = 0; |
| 1011 | ClosureType virtual_set_cb = TypeParam::Bind(&Parent::VirtualSet, &child); |
| 1012 | std::move(virtual_set_cb).Run(); |
| 1013 | EXPECT_EQ(kChildValue, child.value); |
| 1014 | |
| 1015 | child.value = 0; |
| 1016 | ClosureType non_virtual_set_cb = |
| 1017 | TypeParam::Bind(&Parent::NonVirtualSet, &child); |
| 1018 | std::move(non_virtual_set_cb).Run(); |
| 1019 | EXPECT_EQ(kParentValue, child.value); |
| 1020 | } |
| 1021 | |
| 1022 | // Return value support. |
| 1023 | // - Function with return value. |
| 1024 | // - Method with return value. |
| 1025 | // - Const method with return value. |
| 1026 | // - Move-only return value. |
| 1027 | TYPED_TEST(BindVariantsTest, ReturnValues) { |
| 1028 | StrictMock<NoRef> static_func_mock; |
| 1029 | StrictMock<HasRef> has_ref; |
| 1030 | g_func_mock_ptr = &static_func_mock; |
| 1031 | const HasRef* const_has_ref_ptr = &has_ref; |
| 1032 | |
| 1033 | EXPECT_CALL(static_func_mock, IntMethod0()).WillOnce(Return(1337)); |
| 1034 | EXPECT_CALL(has_ref, AddRef()).Times(4); |
| 1035 | EXPECT_CALL(has_ref, Release()).Times(4); |
| tzik | efea4f5 | 2018-08-02 15:20:46 | [diff] [blame] | 1036 | EXPECT_CALL(has_ref, HasAtLeastOneRef()).WillRepeatedly(Return(true)); |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 1037 | EXPECT_CALL(has_ref, IntMethod0()).WillOnce(Return(31337)); |
| 1038 | EXPECT_CALL(has_ref, IntConstMethod0()) |
| 1039 | .WillOnce(Return(41337)) |
| 1040 | .WillOnce(Return(51337)); |
| 1041 | EXPECT_CALL(has_ref, UniquePtrMethod0()) |
| Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 1042 | .WillOnce(Return(ByMove(std::make_unique<int>(42)))); |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 1043 | |
| 1044 | CallbackType<TypeParam, int()> normal_cb = TypeParam::Bind(&IntFunc0); |
| 1045 | CallbackType<TypeParam, int()> method_cb = |
| 1046 | TypeParam::Bind(&HasRef::IntMethod0, &has_ref); |
| 1047 | CallbackType<TypeParam, int()> const_method_nonconst_obj_cb = |
| 1048 | TypeParam::Bind(&HasRef::IntConstMethod0, &has_ref); |
| 1049 | CallbackType<TypeParam, int()> const_method_const_obj_cb = |
| 1050 | TypeParam::Bind(&HasRef::IntConstMethod0, const_has_ref_ptr); |
| 1051 | CallbackType<TypeParam, std::unique_ptr<int>()> move_only_rv_cb = |
| 1052 | TypeParam::Bind(&HasRef::UniquePtrMethod0, &has_ref); |
| 1053 | EXPECT_EQ(1337, std::move(normal_cb).Run()); |
| 1054 | EXPECT_EQ(31337, std::move(method_cb).Run()); |
| 1055 | EXPECT_EQ(41337, std::move(const_method_nonconst_obj_cb).Run()); |
| 1056 | EXPECT_EQ(51337, std::move(const_method_const_obj_cb).Run()); |
| 1057 | EXPECT_EQ(42, *std::move(move_only_rv_cb).Run()); |
| 1058 | } |
| 1059 | |
| 1060 | // Argument binding tests. |
| 1061 | // - Argument binding to primitive. |
| 1062 | // - Argument binding to primitive pointer. |
| 1063 | // - Argument binding to a literal integer. |
| 1064 | // - Argument binding to a literal string. |
| 1065 | // - Argument binding with template function. |
| 1066 | // - Argument binding to an object. |
| 1067 | // - Argument binding to pointer to incomplete type. |
| 1068 | // - Argument gets type converted. |
| 1069 | // - Pointer argument gets converted. |
| 1070 | // - Const Reference forces conversion. |
| 1071 | TYPED_TEST(BindVariantsTest, ArgumentBinding) { |
| 1072 | int n = 2; |
| 1073 | |
| 1074 | EXPECT_EQ(n, TypeParam::Bind(&Identity, n).Run()); |
| 1075 | EXPECT_EQ(&n, TypeParam::Bind(&PolymorphicIdentity<int*>, &n).Run()); |
| 1076 | EXPECT_EQ(3, TypeParam::Bind(&Identity, 3).Run()); |
| 1077 | EXPECT_STREQ("hi", TypeParam::Bind(&CStringIdentity, "hi").Run()); |
| 1078 | EXPECT_EQ(4, TypeParam::Bind(&PolymorphicIdentity<int>, 4).Run()); |
| 1079 | |
| 1080 | NoRefParent p; |
| 1081 | p.value = 5; |
| 1082 | EXPECT_EQ(5, TypeParam::Bind(&UnwrapNoRefParent, p).Run()); |
| 1083 | |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 1084 | NoRefChild c; |
| 1085 | c.value = 6; |
| 1086 | EXPECT_EQ(6, TypeParam::Bind(&UnwrapNoRefParent, c).Run()); |
| 1087 | |
| 1088 | c.value = 7; |
| 1089 | EXPECT_EQ(7, TypeParam::Bind(&UnwrapNoRefParentPtr, &c).Run()); |
| 1090 | |
| 1091 | c.value = 8; |
| 1092 | EXPECT_EQ(8, TypeParam::Bind(&UnwrapNoRefParentConstRef, c).Run()); |
| 1093 | } |
| 1094 | |
| 1095 | // Unbound argument type support tests. |
| 1096 | // - Unbound value. |
| 1097 | // - Unbound pointer. |
| 1098 | // - Unbound reference. |
| 1099 | // - Unbound const reference. |
| 1100 | // - Unbound unsized array. |
| 1101 | // - Unbound sized array. |
| 1102 | // - Unbound array-of-arrays. |
| 1103 | TYPED_TEST(BindVariantsTest, UnboundArgumentTypeSupport) { |
| 1104 | CallbackType<TypeParam, void(int)> unbound_value_cb = |
| 1105 | TypeParam::Bind(&VoidPolymorphic<int>::Run); |
| 1106 | CallbackType<TypeParam, void(int*)> unbound_pointer_cb = |
| 1107 | TypeParam::Bind(&VoidPolymorphic<int*>::Run); |
| 1108 | CallbackType<TypeParam, void(int&)> unbound_ref_cb = |
| 1109 | TypeParam::Bind(&VoidPolymorphic<int&>::Run); |
| 1110 | CallbackType<TypeParam, void(const int&)> unbound_const_ref_cb = |
| 1111 | TypeParam::Bind(&VoidPolymorphic<const int&>::Run); |
| 1112 | CallbackType<TypeParam, void(int[])> unbound_unsized_array_cb = |
| 1113 | TypeParam::Bind(&VoidPolymorphic<int[]>::Run); |
| 1114 | CallbackType<TypeParam, void(int[2])> unbound_sized_array_cb = |
| 1115 | TypeParam::Bind(&VoidPolymorphic<int[2]>::Run); |
| 1116 | CallbackType<TypeParam, void(int[][2])> unbound_array_of_arrays_cb = |
| 1117 | TypeParam::Bind(&VoidPolymorphic<int[][2]>::Run); |
| 1118 | CallbackType<TypeParam, void(int&)> unbound_ref_with_bound_arg = |
| 1119 | TypeParam::Bind(&VoidPolymorphic<int, int&>::Run, 1); |
| 1120 | } |
| 1121 | |
| 1122 | // Function with unbound reference parameter. |
| 1123 | // - Original parameter is modified by callback. |
| 1124 | TYPED_TEST(BindVariantsTest, UnboundReferenceSupport) { |
| 1125 | int n = 0; |
| 1126 | CallbackType<TypeParam, void(int&)> unbound_ref_cb = |
| 1127 | TypeParam::Bind(&RefArgSet); |
| 1128 | std::move(unbound_ref_cb).Run(n); |
| 1129 | EXPECT_EQ(2, n); |
| 1130 | } |
| 1131 | |
| 1132 | // Unretained() wrapper support. |
| 1133 | // - Method bound to Unretained() non-const object. |
| 1134 | // - Const method bound to Unretained() non-const object. |
| 1135 | // - Const method bound to Unretained() const object. |
| 1136 | TYPED_TEST(BindVariantsTest, Unretained) { |
| 1137 | StrictMock<NoRef> no_ref; |
| 1138 | const NoRef* const_no_ref_ptr = &no_ref; |
| 1139 | |
| 1140 | EXPECT_CALL(no_ref, VoidMethod0()); |
| 1141 | EXPECT_CALL(no_ref, VoidConstMethod0()).Times(2); |
| 1142 | |
| 1143 | TypeParam::Bind(&NoRef::VoidMethod0, Unretained(&no_ref)).Run(); |
| 1144 | TypeParam::Bind(&NoRef::VoidConstMethod0, Unretained(&no_ref)).Run(); |
| 1145 | TypeParam::Bind(&NoRef::VoidConstMethod0, Unretained(const_no_ref_ptr)).Run(); |
| 1146 | } |
| 1147 | |
| 1148 | TYPED_TEST(BindVariantsTest, ScopedRefptr) { |
| 1149 | StrictMock<HasRef> has_ref; |
| 1150 | EXPECT_CALL(has_ref, AddRef()).Times(1); |
| 1151 | EXPECT_CALL(has_ref, Release()).Times(1); |
| tzik | efea4f5 | 2018-08-02 15:20:46 | [diff] [blame] | 1152 | EXPECT_CALL(has_ref, HasAtLeastOneRef()).WillRepeatedly(Return(true)); |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 1153 | |
| 1154 | const scoped_refptr<HasRef> refptr(&has_ref); |
| jdoerrie | 9d7236f6 | 2019-03-05 13:00:23 | [diff] [blame] | 1155 | CallbackType<TypeParam, int()> scoped_refptr_const_ref_cb = TypeParam::Bind( |
| 1156 | &FunctionWithScopedRefptrFirstParam, std::cref(refptr), 1); |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 1157 | EXPECT_EQ(1, std::move(scoped_refptr_const_ref_cb).Run()); |
| 1158 | } |
| 1159 | |
| 1160 | TYPED_TEST(BindVariantsTest, UniquePtrReceiver) { |
| Charles Meng | 300bcec7 | 2025-11-10 19:13:49 | [diff] [blame] | 1161 | auto no_ref = std::make_unique<StrictMock<NoRef>>(); |
| tzik | 4435e804 | 2016-05-11 23:05:05 | [diff] [blame] | 1162 | EXPECT_CALL(*no_ref, VoidMethod0()).Times(1); |
| tzik | b9499fd9 | 2016-10-12 04:55:02 | [diff] [blame] | 1163 | TypeParam::Bind(&NoRef::VoidMethod0, std::move(no_ref)).Run(); |
| tzik | 4435e804 | 2016-05-11 23:05:05 | [diff] [blame] | 1164 | } |
| 1165 | |
| Daniel Cheng | 00c072a | 2022-06-11 18:50:37 | [diff] [blame] | 1166 | TYPED_TEST(BindVariantsTest, ImplicitRefPtrReceiver) { |
| 1167 | StrictMock<HasRef> has_ref; |
| 1168 | EXPECT_CALL(has_ref, AddRef()).Times(1); |
| 1169 | EXPECT_CALL(has_ref, Release()).Times(1); |
| 1170 | EXPECT_CALL(has_ref, HasAtLeastOneRef()).WillRepeatedly(Return(true)); |
| 1171 | |
| 1172 | HasRef* ptr = &has_ref; |
| 1173 | auto ptr_cb = TypeParam::Bind(&HasRef::HasAtLeastOneRef, ptr); |
| 1174 | EXPECT_EQ(1, std::move(ptr_cb).Run()); |
| 1175 | } |
| 1176 | |
| 1177 | TYPED_TEST(BindVariantsTest, RawPtrReceiver) { |
| 1178 | StrictMock<HasRef> has_ref; |
| 1179 | EXPECT_CALL(has_ref, AddRef()).Times(1); |
| 1180 | EXPECT_CALL(has_ref, Release()).Times(1); |
| 1181 | EXPECT_CALL(has_ref, HasAtLeastOneRef()).WillRepeatedly(Return(true)); |
| 1182 | |
| 1183 | raw_ptr<HasRef> rawptr(&has_ref); |
| 1184 | auto rawptr_cb = TypeParam::Bind(&HasRef::HasAtLeastOneRef, rawptr); |
| 1185 | EXPECT_EQ(1, std::move(rawptr_cb).Run()); |
| 1186 | } |
| 1187 | |
| danakj | 92061c1 | 2022-07-28 23:53:50 | [diff] [blame] | 1188 | TYPED_TEST(BindVariantsTest, UnretainedRawRefReceiver) { |
| 1189 | StrictMock<HasRef> has_ref; |
| 1190 | EXPECT_CALL(has_ref, AddRef()).Times(0); |
| 1191 | EXPECT_CALL(has_ref, Release()).Times(0); |
| 1192 | EXPECT_CALL(has_ref, HasAtLeastOneRef()).WillRepeatedly(Return(true)); |
| 1193 | |
| 1194 | raw_ref<HasRef> raw_has_ref(has_ref); |
| 1195 | auto has_ref_cb = |
| 1196 | TypeParam::Bind(&HasRef::HasAtLeastOneRef, Unretained(raw_has_ref)); |
| 1197 | EXPECT_EQ(1, std::move(has_ref_cb).Run()); |
| 1198 | |
| 1199 | StrictMock<NoRef> no_ref; |
| 1200 | EXPECT_CALL(has_ref, IntMethod0()).WillRepeatedly(Return(1)); |
| 1201 | |
| 1202 | raw_ref<NoRef> raw_no_ref(has_ref); |
| 1203 | auto no_ref_cb = TypeParam::Bind(&NoRef::IntMethod0, Unretained(raw_no_ref)); |
| 1204 | EXPECT_EQ(1, std::move(no_ref_cb).Run()); |
| 1205 | } |
| 1206 | |
| dcheng | f10b773 | 2016-01-21 19:37:55 | [diff] [blame] | 1207 | // Tests for Passed() wrapper support: |
| [email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 1208 | // - Passed() can be constructed from a pointer to scoper. |
| 1209 | // - Passed() can be constructed from a scoper rvalue. |
| 1210 | // - Using Passed() gives Callback Ownership. |
| 1211 | // - Ownership is transferred from Callback to callee on the first Run(). |
| 1212 | // - Callback supports unbound arguments. |
| dcheng | f10b773 | 2016-01-21 19:37:55 | [diff] [blame] | 1213 | template <typename T> |
| Daniel Cheng | 91f6fbaf | 2022-09-16 12:07:48 | [diff] [blame] | 1214 | class BindMoveOnlyTypeTest : public ::testing::Test {}; |
| dcheng | f10b773 | 2016-01-21 19:37:55 | [diff] [blame] | 1215 | |
| 1216 | struct CustomDeleter { |
| 1217 | void operator()(DeleteCounter* c) { delete c; } |
| 1218 | }; |
| 1219 | |
| 1220 | using MoveOnlyTypesToTest = |
| dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 1221 | ::testing::Types<std::unique_ptr<DeleteCounter>, |
| dcheng | f10b773 | 2016-01-21 19:37:55 | [diff] [blame] | 1222 | std::unique_ptr<DeleteCounter, CustomDeleter>>; |
| Victor Costan | ebc5273 | 2019-02-15 02:39:47 | [diff] [blame] | 1223 | TYPED_TEST_SUITE(BindMoveOnlyTypeTest, MoveOnlyTypesToTest); |
| dcheng | f10b773 | 2016-01-21 19:37:55 | [diff] [blame] | 1224 | |
| 1225 | TYPED_TEST(BindMoveOnlyTypeTest, PassedToBoundCallback) { |
| [email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 1226 | int deletes = 0; |
| 1227 | |
| dcheng | f10b773 | 2016-01-21 19:37:55 | [diff] [blame] | 1228 | TypeParam ptr(new DeleteCounter(&deletes)); |
| kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 1229 | RepeatingCallback<TypeParam()> callback = |
| 1230 | BindRepeating(&PassThru<TypeParam>, Passed(&ptr)); |
| [email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 1231 | EXPECT_FALSE(ptr.get()); |
| 1232 | EXPECT_EQ(0, deletes); |
| 1233 | |
| 1234 | // If we never invoke the Callback, it retains ownership and deletes. |
| [email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 1235 | callback.Reset(); |
| [email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 1236 | EXPECT_EQ(1, deletes); |
| [email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 1237 | } |
| 1238 | |
| dcheng | f10b773 | 2016-01-21 19:37:55 | [diff] [blame] | 1239 | TYPED_TEST(BindMoveOnlyTypeTest, PassedWithRvalue) { |
| dcheng | 69f2a04 | 2015-12-14 20:31:52 | [diff] [blame] | 1240 | int deletes = 0; |
| kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 1241 | RepeatingCallback<TypeParam()> callback = BindRepeating( |
| dcheng | f10b773 | 2016-01-21 19:37:55 | [diff] [blame] | 1242 | &PassThru<TypeParam>, Passed(TypeParam(new DeleteCounter(&deletes)))); |
| dcheng | 69f2a04 | 2015-12-14 20:31:52 | [diff] [blame] | 1243 | EXPECT_EQ(0, deletes); |
| 1244 | |
| 1245 | // If we never invoke the Callback, it retains ownership and deletes. |
| dcheng | f10b773 | 2016-01-21 19:37:55 | [diff] [blame] | 1246 | callback.Reset(); |
| dcheng | 69f2a04 | 2015-12-14 20:31:52 | [diff] [blame] | 1247 | EXPECT_EQ(1, deletes); |
| dcheng | f10b773 | 2016-01-21 19:37:55 | [diff] [blame] | 1248 | } |
| dcheng | 69f2a04 | 2015-12-14 20:31:52 | [diff] [blame] | 1249 | |
| dcheng | f10b773 | 2016-01-21 19:37:55 | [diff] [blame] | 1250 | // Check that ownership can be transferred back out. |
| 1251 | TYPED_TEST(BindMoveOnlyTypeTest, ReturnMoveOnlyType) { |
| 1252 | int deletes = 0; |
| dcheng | 69f2a04 | 2015-12-14 20:31:52 | [diff] [blame] | 1253 | DeleteCounter* counter = new DeleteCounter(&deletes); |
| kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 1254 | RepeatingCallback<TypeParam()> callback = |
| 1255 | BindRepeating(&PassThru<TypeParam>, Passed(TypeParam(counter))); |
| dcheng | f10b773 | 2016-01-21 19:37:55 | [diff] [blame] | 1256 | TypeParam result = callback.Run(); |
| dcheng | 69f2a04 | 2015-12-14 20:31:52 | [diff] [blame] | 1257 | ASSERT_EQ(counter, result.get()); |
| 1258 | EXPECT_EQ(0, deletes); |
| 1259 | |
| 1260 | // Resetting does not delete since ownership was transferred. |
| 1261 | callback.Reset(); |
| 1262 | EXPECT_EQ(0, deletes); |
| 1263 | |
| 1264 | // Ensure that we actually did get ownership. |
| 1265 | result.reset(); |
| 1266 | EXPECT_EQ(1, deletes); |
| dcheng | f10b773 | 2016-01-21 19:37:55 | [diff] [blame] | 1267 | } |
| dcheng | 69f2a04 | 2015-12-14 20:31:52 | [diff] [blame] | 1268 | |
| dcheng | f10b773 | 2016-01-21 19:37:55 | [diff] [blame] | 1269 | TYPED_TEST(BindMoveOnlyTypeTest, UnboundForwarding) { |
| 1270 | int deletes = 0; |
| 1271 | TypeParam ptr(new DeleteCounter(&deletes)); |
| dcheng | 69f2a04 | 2015-12-14 20:31:52 | [diff] [blame] | 1272 | // Test unbound argument forwarding. |
| kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 1273 | RepeatingCallback<TypeParam(TypeParam)> cb_unbound = |
| 1274 | BindRepeating(&PassThru<TypeParam>); |
| dcheng | 69f2a04 | 2015-12-14 20:31:52 | [diff] [blame] | 1275 | cb_unbound.Run(std::move(ptr)); |
| dcheng | f10b773 | 2016-01-21 19:37:55 | [diff] [blame] | 1276 | EXPECT_EQ(1, deletes); |
| dcheng | 69f2a04 | 2015-12-14 20:31:52 | [diff] [blame] | 1277 | } |
| 1278 | |
| dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 1279 | void VerifyVector(const std::vector<std::unique_ptr<int>>& v) { |
| dcheng | 53b4cea | 2016-02-02 04:09:33 | [diff] [blame] | 1280 | ASSERT_EQ(1u, v.size()); |
| 1281 | EXPECT_EQ(12345, *v[0]); |
| 1282 | } |
| 1283 | |
| dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 1284 | std::vector<std::unique_ptr<int>> AcceptAndReturnMoveOnlyVector( |
| 1285 | std::vector<std::unique_ptr<int>> v) { |
| dcheng | 53b4cea | 2016-02-02 04:09:33 | [diff] [blame] | 1286 | VerifyVector(v); |
| 1287 | return v; |
| 1288 | } |
| 1289 | |
| 1290 | // Test that a vector containing move-only types can be used with Callback. |
| 1291 | TEST_F(BindTest, BindMoveOnlyVector) { |
| dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 1292 | using MoveOnlyVector = std::vector<std::unique_ptr<int>>; |
| dcheng | 53b4cea | 2016-02-02 04:09:33 | [diff] [blame] | 1293 | |
| 1294 | MoveOnlyVector v; |
| David Benjamin | dd43629 | 2018-10-11 16:28:00 | [diff] [blame] | 1295 | v.push_back(std::make_unique<int>(12345)); |
| dcheng | 53b4cea | 2016-02-02 04:09:33 | [diff] [blame] | 1296 | |
| 1297 | // Early binding should work: |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 1298 | RepeatingCallback<MoveOnlyVector()> bound_cb = |
| 1299 | BindRepeating(&AcceptAndReturnMoveOnlyVector, Passed(&v)); |
| dcheng | 53b4cea | 2016-02-02 04:09:33 | [diff] [blame] | 1300 | MoveOnlyVector intermediate_result = bound_cb.Run(); |
| 1301 | VerifyVector(intermediate_result); |
| 1302 | |
| 1303 | // As should passing it as an argument to Run(): |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 1304 | RepeatingCallback<MoveOnlyVector(MoveOnlyVector)> unbound_cb = |
| 1305 | BindRepeating(&AcceptAndReturnMoveOnlyVector); |
| dcheng | 53b4cea | 2016-02-02 04:09:33 | [diff] [blame] | 1306 | MoveOnlyVector final_result = unbound_cb.Run(std::move(intermediate_result)); |
| 1307 | VerifyVector(final_result); |
| 1308 | } |
| 1309 | |
| Peter Kasting | f818a6f | 2024-02-21 18:21:28 | [diff] [blame] | 1310 | // Using Passed() on a functor should not cause a compile error. |
| 1311 | TEST_F(BindTest, PassedFunctor) { |
| 1312 | struct S { |
| 1313 | void operator()() const {} |
| 1314 | }; |
| 1315 | |
| 1316 | BindRepeating(Passed(S())).Run(); |
| 1317 | } |
| 1318 | |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 1319 | // Argument copy-constructor usage for non-reference copy-only parameters. |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 1320 | // - Bound arguments are only copied once. |
| 1321 | // - Forwarded arguments are only copied once. |
| [email protected] | 206a2ae8 | 2011-12-22 21:12:58 | [diff] [blame] | 1322 | // - Forwarded arguments with coercions are only copied twice (once for the |
| 1323 | // coercion, and one for the final dispatch). |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 1324 | TEST_F(BindTest, ArgumentCopies) { |
| 1325 | int copies = 0; |
| 1326 | int assigns = 0; |
| 1327 | |
| 1328 | CopyCounter counter(&copies, &assigns); |
| Colin Blundell | 6cc94b5 | 2021-05-05 11:13:38 | [diff] [blame] | 1329 | BindRepeating(&VoidPolymorphic<CopyCounter>::Run, counter); |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 1330 | EXPECT_EQ(1, copies); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 1331 | EXPECT_EQ(0, assigns); |
| 1332 | |
| 1333 | copies = 0; |
| 1334 | assigns = 0; |
| Colin Blundell | 6cc94b5 | 2021-05-05 11:13:38 | [diff] [blame] | 1335 | BindRepeating(&VoidPolymorphic<CopyCounter>::Run, |
| 1336 | CopyCounter(&copies, &assigns)); |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 1337 | EXPECT_EQ(1, copies); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 1338 | EXPECT_EQ(0, assigns); |
| 1339 | |
| 1340 | copies = 0; |
| 1341 | assigns = 0; |
| Colin Blundell | 6cc94b5 | 2021-05-05 11:13:38 | [diff] [blame] | 1342 | BindRepeating(&VoidPolymorphic<CopyCounter>::Run).Run(counter); |
| tzik | a43eff0 | 2016-03-09 05:46:05 | [diff] [blame] | 1343 | EXPECT_EQ(2, copies); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 1344 | EXPECT_EQ(0, assigns); |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 1345 | |
| 1346 | copies = 0; |
| 1347 | assigns = 0; |
| Colin Blundell | 6cc94b5 | 2021-05-05 11:13:38 | [diff] [blame] | 1348 | BindRepeating(&VoidPolymorphic<CopyCounter>::Run) |
| 1349 | .Run(CopyCounter(&copies, &assigns)); |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 1350 | EXPECT_EQ(1, copies); |
| 1351 | EXPECT_EQ(0, assigns); |
| 1352 | |
| 1353 | copies = 0; |
| 1354 | assigns = 0; |
| 1355 | DerivedCopyMoveCounter derived(&copies, &assigns, nullptr, nullptr); |
| Colin Blundell | 6cc94b5 | 2021-05-05 11:13:38 | [diff] [blame] | 1356 | BindRepeating(&VoidPolymorphic<CopyCounter>::Run).Run(CopyCounter(derived)); |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 1357 | EXPECT_EQ(2, copies); |
| 1358 | EXPECT_EQ(0, assigns); |
| 1359 | |
| 1360 | copies = 0; |
| 1361 | assigns = 0; |
| Colin Blundell | 6cc94b5 | 2021-05-05 11:13:38 | [diff] [blame] | 1362 | BindRepeating(&VoidPolymorphic<CopyCounter>::Run) |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 1363 | .Run(CopyCounter( |
| 1364 | DerivedCopyMoveCounter(&copies, &assigns, nullptr, nullptr))); |
| 1365 | EXPECT_EQ(2, copies); |
| 1366 | EXPECT_EQ(0, assigns); |
| 1367 | } |
| 1368 | |
| 1369 | // Argument move-constructor usage for move-only parameters. |
| 1370 | // - Bound arguments passed by move are not copied. |
| 1371 | TEST_F(BindTest, ArgumentMoves) { |
| 1372 | int move_constructs = 0; |
| 1373 | int move_assigns = 0; |
| 1374 | |
| Colin Blundell | 6cc94b5 | 2021-05-05 11:13:38 | [diff] [blame] | 1375 | BindRepeating(&VoidPolymorphic<const MoveCounter&>::Run, |
| 1376 | MoveCounter(&move_constructs, &move_assigns)); |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 1377 | EXPECT_EQ(1, move_constructs); |
| 1378 | EXPECT_EQ(0, move_assigns); |
| 1379 | |
| 1380 | // TODO(tzik): Support binding move-only type into a non-reference parameter |
| 1381 | // of a variant of Callback. |
| 1382 | |
| tzik | a43eff0 | 2016-03-09 05:46:05 | [diff] [blame] | 1383 | move_constructs = 0; |
| 1384 | move_assigns = 0; |
| Colin Blundell | 6cc94b5 | 2021-05-05 11:13:38 | [diff] [blame] | 1385 | BindRepeating(&VoidPolymorphic<MoveCounter>::Run) |
| tzik | a43eff0 | 2016-03-09 05:46:05 | [diff] [blame] | 1386 | .Run(MoveCounter(&move_constructs, &move_assigns)); |
| 1387 | EXPECT_EQ(1, move_constructs); |
| 1388 | EXPECT_EQ(0, move_assigns); |
| 1389 | |
| 1390 | move_constructs = 0; |
| 1391 | move_assigns = 0; |
| Colin Blundell | 6cc94b5 | 2021-05-05 11:13:38 | [diff] [blame] | 1392 | BindRepeating(&VoidPolymorphic<MoveCounter>::Run) |
| tzik | a43eff0 | 2016-03-09 05:46:05 | [diff] [blame] | 1393 | .Run(MoveCounter(DerivedCopyMoveCounter( |
| 1394 | nullptr, nullptr, &move_constructs, &move_assigns))); |
| 1395 | EXPECT_EQ(2, move_constructs); |
| 1396 | EXPECT_EQ(0, move_assigns); |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | // Argument constructor usage for non-reference movable-copyable |
| 1400 | // parameters. |
| 1401 | // - Bound arguments passed by move are not copied. |
| 1402 | // - Forwarded arguments are only copied once. |
| 1403 | // - Forwarded arguments with coercions are only copied once and moved once. |
| 1404 | TEST_F(BindTest, ArgumentCopiesAndMoves) { |
| 1405 | int copies = 0; |
| 1406 | int assigns = 0; |
| 1407 | int move_constructs = 0; |
| 1408 | int move_assigns = 0; |
| 1409 | |
| 1410 | CopyMoveCounter counter(&copies, &assigns, &move_constructs, &move_assigns); |
| Colin Blundell | 6cc94b5 | 2021-05-05 11:13:38 | [diff] [blame] | 1411 | BindRepeating(&VoidPolymorphic<CopyMoveCounter>::Run, counter); |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 1412 | EXPECT_EQ(1, copies); |
| 1413 | EXPECT_EQ(0, assigns); |
| 1414 | EXPECT_EQ(0, move_constructs); |
| 1415 | EXPECT_EQ(0, move_assigns); |
| 1416 | |
| 1417 | copies = 0; |
| 1418 | assigns = 0; |
| 1419 | move_constructs = 0; |
| 1420 | move_assigns = 0; |
| Colin Blundell | 6cc94b5 | 2021-05-05 11:13:38 | [diff] [blame] | 1421 | BindRepeating( |
| 1422 | &VoidPolymorphic<CopyMoveCounter>::Run, |
| 1423 | CopyMoveCounter(&copies, &assigns, &move_constructs, &move_assigns)); |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 1424 | EXPECT_EQ(0, copies); |
| 1425 | EXPECT_EQ(0, assigns); |
| 1426 | EXPECT_EQ(1, move_constructs); |
| 1427 | EXPECT_EQ(0, move_assigns); |
| 1428 | |
| 1429 | copies = 0; |
| 1430 | assigns = 0; |
| 1431 | move_constructs = 0; |
| 1432 | move_assigns = 0; |
| Colin Blundell | 6cc94b5 | 2021-05-05 11:13:38 | [diff] [blame] | 1433 | BindRepeating(&VoidPolymorphic<CopyMoveCounter>::Run).Run(counter); |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 1434 | EXPECT_EQ(1, copies); |
| 1435 | EXPECT_EQ(0, assigns); |
| tzik | a43eff0 | 2016-03-09 05:46:05 | [diff] [blame] | 1436 | EXPECT_EQ(1, move_constructs); |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 1437 | EXPECT_EQ(0, move_assigns); |
| 1438 | |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 1439 | copies = 0; |
| 1440 | assigns = 0; |
| 1441 | move_constructs = 0; |
| 1442 | move_assigns = 0; |
| Colin Blundell | 6cc94b5 | 2021-05-05 11:13:38 | [diff] [blame] | 1443 | BindRepeating(&VoidPolymorphic<CopyMoveCounter>::Run) |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 1444 | .Run(CopyMoveCounter(&copies, &assigns, &move_constructs, &move_assigns)); |
| tzik | a43eff0 | 2016-03-09 05:46:05 | [diff] [blame] | 1445 | EXPECT_EQ(0, copies); |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 1446 | EXPECT_EQ(0, assigns); |
| tzik | a43eff0 | 2016-03-09 05:46:05 | [diff] [blame] | 1447 | EXPECT_EQ(1, move_constructs); |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 1448 | EXPECT_EQ(0, move_assigns); |
| 1449 | |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 1450 | DerivedCopyMoveCounter derived_counter(&copies, &assigns, &move_constructs, |
| 1451 | &move_assigns); |
| 1452 | copies = 0; |
| 1453 | assigns = 0; |
| 1454 | move_constructs = 0; |
| 1455 | move_assigns = 0; |
| Colin Blundell | 6cc94b5 | 2021-05-05 11:13:38 | [diff] [blame] | 1456 | BindRepeating(&VoidPolymorphic<CopyMoveCounter>::Run) |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 1457 | .Run(CopyMoveCounter(derived_counter)); |
| tzik | a43eff0 | 2016-03-09 05:46:05 | [diff] [blame] | 1458 | EXPECT_EQ(1, copies); |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 1459 | EXPECT_EQ(0, assigns); |
| tzik | a43eff0 | 2016-03-09 05:46:05 | [diff] [blame] | 1460 | EXPECT_EQ(1, move_constructs); |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 1461 | EXPECT_EQ(0, move_assigns); |
| 1462 | |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 1463 | copies = 0; |
| 1464 | assigns = 0; |
| 1465 | move_constructs = 0; |
| 1466 | move_assigns = 0; |
| Colin Blundell | 6cc94b5 | 2021-05-05 11:13:38 | [diff] [blame] | 1467 | BindRepeating(&VoidPolymorphic<CopyMoveCounter>::Run) |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 1468 | .Run(CopyMoveCounter(DerivedCopyMoveCounter( |
| 1469 | &copies, &assigns, &move_constructs, &move_assigns))); |
| tzik | a43eff0 | 2016-03-09 05:46:05 | [diff] [blame] | 1470 | EXPECT_EQ(0, copies); |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 1471 | EXPECT_EQ(0, assigns); |
| tzik | a43eff0 | 2016-03-09 05:46:05 | [diff] [blame] | 1472 | EXPECT_EQ(2, move_constructs); |
| tzik | 52dcd67 | 2016-02-15 11:54:30 | [diff] [blame] | 1473 | EXPECT_EQ(0, move_assigns); |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 1474 | } |
| 1475 | |
| Peter Kasting | ca576273 | 2023-12-05 09:28:38 | [diff] [blame] | 1476 | TEST_F(BindTest, RepeatingWithoutPassed) { |
| 1477 | // It should be possible to use a move-only type with `BindRepeating` without |
| 1478 | // `Passed` if running the callback does not require copying the instance. |
| 1479 | struct S { |
| 1480 | S() = default; |
| 1481 | S(S&&) = default; |
| 1482 | S& operator=(S&&) = default; |
| 1483 | } s; |
| 1484 | BindRepeating([](const S&) {}, std::move(s)); |
| 1485 | } |
| 1486 | |
| tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 1487 | TEST_F(BindTest, CapturelessLambda) { |
| Colin Blundell | 6cc94b5 | 2021-05-05 11:13:38 | [diff] [blame] | 1488 | EXPECT_EQ(42, BindRepeating([] { return 42; }).Run()); |
| 1489 | EXPECT_EQ(42, BindRepeating([](int i) { return i * 7; }, 6).Run()); |
| tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 1490 | |
| 1491 | int x = 1; |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 1492 | RepeatingCallback<void(int)> cb = |
| kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 1493 | BindRepeating([](int* x, int i) { *x *= i; }, Unretained(&x)); |
| tzik | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 1494 | cb.Run(6); |
| 1495 | EXPECT_EQ(6, x); |
| 1496 | cb.Run(7); |
| 1497 | EXPECT_EQ(42, x); |
| 1498 | } |
| 1499 | |
| tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 1500 | TEST_F(BindTest, EmptyFunctor) { |
| 1501 | struct NonEmptyFunctor { |
| 1502 | int operator()() const { return x; } |
| 1503 | int x = 42; |
| 1504 | }; |
| 1505 | |
| 1506 | struct EmptyFunctor { |
| 1507 | int operator()() { return 42; } |
| 1508 | }; |
| 1509 | |
| 1510 | struct EmptyFunctorConst { |
| 1511 | int operator()() const { return 42; } |
| 1512 | }; |
| 1513 | |
| Peter Kasting | 336972e | 2024-02-09 22:08:00 | [diff] [blame] | 1514 | EXPECT_EQ(42, BindLambdaForTesting(NonEmptyFunctor()).Run()); |
| tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 1515 | EXPECT_EQ(42, BindOnce(EmptyFunctor()).Run()); |
| 1516 | EXPECT_EQ(42, BindOnce(EmptyFunctorConst()).Run()); |
| 1517 | EXPECT_EQ(42, BindRepeating(EmptyFunctorConst()).Run()); |
| 1518 | } |
| 1519 | |
| 1520 | TEST_F(BindTest, CapturingLambdaForTesting) { |
| Jan Wilken Dörrie | 4dc6fad7 | 2019-05-22 07:27:56 | [diff] [blame] | 1521 | // Test copyable lambdas. |
| tzik | 4b83dce | 2017-12-04 19:04:27 | [diff] [blame] | 1522 | int x = 6; |
| 1523 | EXPECT_EQ(42, BindLambdaForTesting([=](int y) { return x * y; }).Run(7)); |
| Jan Wilken Dörrie | 4dc6fad7 | 2019-05-22 07:27:56 | [diff] [blame] | 1524 | EXPECT_EQ(42, |
| 1525 | BindLambdaForTesting([=](int y) mutable { return x *= y; }).Run(7)); |
| tzik | 4b83dce | 2017-12-04 19:04:27 | [diff] [blame] | 1526 | auto f = [x](std::unique_ptr<int> y) { return x * *y; }; |
| 1527 | EXPECT_EQ(42, BindLambdaForTesting(f).Run(std::make_unique<int>(7))); |
| Jan Wilken Dörrie | 4dc6fad7 | 2019-05-22 07:27:56 | [diff] [blame] | 1528 | |
| 1529 | // Test move-only lambdas. |
| 1530 | auto y = std::make_unique<int>(7); |
| 1531 | auto g = [y = std::move(y)](int& x) mutable { |
| 1532 | return x * *std::exchange(y, nullptr); |
| 1533 | }; |
| 1534 | EXPECT_EQ(42, BindLambdaForTesting(std::move(g)).Run(x)); |
| 1535 | |
| 1536 | y = std::make_unique<int>(7); |
| 1537 | auto h = [x, y = std::move(y)] { return x * *y; }; |
| 1538 | EXPECT_EQ(42, BindLambdaForTesting(std::move(h)).Run()); |
| tzik | f98654b | 2017-12-02 03:28:58 | [diff] [blame] | 1539 | } |
| 1540 | |
| tzik | 59aa6bb1 | 2016-09-08 10:58:53 | [diff] [blame] | 1541 | TEST_F(BindTest, Cancellation) { |
| dcheng | 172b6ad | 2016-09-24 05:05:57 | [diff] [blame] | 1542 | EXPECT_CALL(no_ref_, VoidMethodWithIntArg(_)).Times(2); |
| tzik | 59aa6bb1 | 2016-09-08 10:58:53 | [diff] [blame] | 1543 | |
| 1544 | WeakPtrFactory<NoRef> weak_factory(&no_ref_); |
| tzik | 44adf07 | 2016-10-07 04:34:54 | [diff] [blame] | 1545 | RepeatingCallback<void(int)> cb = |
| 1546 | BindRepeating(&NoRef::VoidMethodWithIntArg, weak_factory.GetWeakPtr()); |
| 1547 | RepeatingClosure cb2 = BindRepeating(cb, 8); |
| 1548 | OnceClosure cb3 = BindOnce(cb, 8); |
| 1549 | |
| 1550 | OnceCallback<void(int)> cb4 = |
| 1551 | BindOnce(&NoRef::VoidMethodWithIntArg, weak_factory.GetWeakPtr()); |
| 1552 | EXPECT_FALSE(cb4.IsCancelled()); |
| 1553 | |
| 1554 | OnceClosure cb5 = BindOnce(std::move(cb4), 8); |
| tzik | 59aa6bb1 | 2016-09-08 10:58:53 | [diff] [blame] | 1555 | |
| 1556 | EXPECT_FALSE(cb.IsCancelled()); |
| 1557 | EXPECT_FALSE(cb2.IsCancelled()); |
| tzik | 44adf07 | 2016-10-07 04:34:54 | [diff] [blame] | 1558 | EXPECT_FALSE(cb3.IsCancelled()); |
| 1559 | EXPECT_FALSE(cb5.IsCancelled()); |
| tzik | 59aa6bb1 | 2016-09-08 10:58:53 | [diff] [blame] | 1560 | |
| dcheng | 172b6ad | 2016-09-24 05:05:57 | [diff] [blame] | 1561 | cb.Run(6); |
| tzik | 59aa6bb1 | 2016-09-08 10:58:53 | [diff] [blame] | 1562 | cb2.Run(); |
| 1563 | |
| 1564 | weak_factory.InvalidateWeakPtrs(); |
| 1565 | |
| 1566 | EXPECT_TRUE(cb.IsCancelled()); |
| 1567 | EXPECT_TRUE(cb2.IsCancelled()); |
| tzik | 44adf07 | 2016-10-07 04:34:54 | [diff] [blame] | 1568 | EXPECT_TRUE(cb3.IsCancelled()); |
| 1569 | EXPECT_TRUE(cb5.IsCancelled()); |
| tzik | 59aa6bb1 | 2016-09-08 10:58:53 | [diff] [blame] | 1570 | |
| dcheng | 172b6ad | 2016-09-24 05:05:57 | [diff] [blame] | 1571 | cb.Run(6); |
| tzik | 59aa6bb1 | 2016-09-08 10:58:53 | [diff] [blame] | 1572 | cb2.Run(); |
| tzik | 44adf07 | 2016-10-07 04:34:54 | [diff] [blame] | 1573 | std::move(cb3).Run(); |
| 1574 | std::move(cb5).Run(); |
| tzik | 59aa6bb1 | 2016-09-08 10:58:53 | [diff] [blame] | 1575 | } |
| 1576 | |
| tzik | 27d1e31 | 2016-09-13 05:28:59 | [diff] [blame] | 1577 | TEST_F(BindTest, OnceCallback) { |
| tzik | 27d1e31 | 2016-09-13 05:28:59 | [diff] [blame] | 1578 | // Check if Callback variants have declarations of conversions as expected. |
| 1579 | // Copy constructor and assignment of RepeatingCallback. |
| Daniel Cheng | 91f6fbaf | 2022-09-16 12:07:48 | [diff] [blame] | 1580 | static_assert( |
| Andrew Rayskiy | b42fe752 | 2023-10-17 13:16:19 | [diff] [blame] | 1581 | std::is_constructible_v<RepeatingClosure, const RepeatingClosure&>, |
| tzik | 27d1e31 | 2016-09-13 05:28:59 | [diff] [blame] | 1582 | "RepeatingClosure should be copyable."); |
| Andrew Rayskiy | b42fe752 | 2023-10-17 13:16:19 | [diff] [blame] | 1583 | static_assert(std::is_assignable_v<RepeatingClosure, const RepeatingClosure&>, |
| 1584 | "RepeatingClosure should be copy-assignable."); |
| tzik | 27d1e31 | 2016-09-13 05:28:59 | [diff] [blame] | 1585 | |
| 1586 | // Move constructor and assignment of RepeatingCallback. |
| Andrew Rayskiy | b42fe752 | 2023-10-17 13:16:19 | [diff] [blame] | 1587 | static_assert(std::is_constructible_v<RepeatingClosure, RepeatingClosure&&>, |
| 1588 | "RepeatingClosure should be movable."); |
| 1589 | static_assert(std::is_assignable_v<RepeatingClosure, RepeatingClosure&&>, |
| danakj | 560f5dd2e | 2017-04-04 20:55:29 | [diff] [blame] | 1590 | "RepeatingClosure should be move-assignable"); |
| tzik | 27d1e31 | 2016-09-13 05:28:59 | [diff] [blame] | 1591 | |
| 1592 | // Conversions from OnceCallback to RepeatingCallback. |
| Andrew Rayskiy | b42fe752 | 2023-10-17 13:16:19 | [diff] [blame] | 1593 | static_assert(!std::is_constructible_v<RepeatingClosure, const OnceClosure&>, |
| 1594 | "OnceClosure should not be convertible to RepeatingClosure."); |
| 1595 | static_assert(!std::is_assignable_v<RepeatingClosure, const OnceClosure&>, |
| 1596 | "OnceClosure should not be convertible to RepeatingClosure."); |
| tzik | 27d1e31 | 2016-09-13 05:28:59 | [diff] [blame] | 1597 | |
| 1598 | // Destructive conversions from OnceCallback to RepeatingCallback. |
| Andrew Rayskiy | b42fe752 | 2023-10-17 13:16:19 | [diff] [blame] | 1599 | static_assert(!std::is_constructible_v<RepeatingClosure, OnceClosure&&>, |
| Daniel Cheng | 91f6fbaf | 2022-09-16 12:07:48 | [diff] [blame] | 1600 | "OnceClosure should not be convertible to RepeatingClosure."); |
| Andrew Rayskiy | b42fe752 | 2023-10-17 13:16:19 | [diff] [blame] | 1601 | static_assert(!std::is_assignable_v<RepeatingClosure, OnceClosure&&>, |
| danakj | 560f5dd2e | 2017-04-04 20:55:29 | [diff] [blame] | 1602 | "OnceClosure should not be convertible to RepeatingClosure."); |
| tzik | 27d1e31 | 2016-09-13 05:28:59 | [diff] [blame] | 1603 | |
| 1604 | // Copy constructor and assignment of OnceCallback. |
| Andrew Rayskiy | b42fe752 | 2023-10-17 13:16:19 | [diff] [blame] | 1605 | static_assert(!std::is_constructible_v<OnceClosure, const OnceClosure&>, |
| Daniel Cheng | 91f6fbaf | 2022-09-16 12:07:48 | [diff] [blame] | 1606 | "OnceClosure should not be copyable."); |
| Andrew Rayskiy | b42fe752 | 2023-10-17 13:16:19 | [diff] [blame] | 1607 | static_assert(!std::is_assignable_v<OnceClosure, const OnceClosure&>, |
| danakj | 560f5dd2e | 2017-04-04 20:55:29 | [diff] [blame] | 1608 | "OnceClosure should not be copy-assignable"); |
| tzik | 27d1e31 | 2016-09-13 05:28:59 | [diff] [blame] | 1609 | |
| 1610 | // Move constructor and assignment of OnceCallback. |
| Andrew Rayskiy | b42fe752 | 2023-10-17 13:16:19 | [diff] [blame] | 1611 | static_assert(std::is_constructible_v<OnceClosure, OnceClosure&&>, |
| Daniel Cheng | 91f6fbaf | 2022-09-16 12:07:48 | [diff] [blame] | 1612 | "OnceClosure should be movable."); |
| Andrew Rayskiy | b42fe752 | 2023-10-17 13:16:19 | [diff] [blame] | 1613 | static_assert(std::is_assignable_v<OnceClosure, OnceClosure&&>, |
| danakj | 560f5dd2e | 2017-04-04 20:55:29 | [diff] [blame] | 1614 | "OnceClosure should be move-assignable."); |
| tzik | 27d1e31 | 2016-09-13 05:28:59 | [diff] [blame] | 1615 | |
| 1616 | // Conversions from RepeatingCallback to OnceCallback. |
| Andrew Rayskiy | b42fe752 | 2023-10-17 13:16:19 | [diff] [blame] | 1617 | static_assert(std::is_constructible_v<OnceClosure, const RepeatingClosure&>, |
| 1618 | "RepeatingClosure should be convertible to OnceClosure."); |
| 1619 | static_assert(std::is_assignable_v<OnceClosure, const RepeatingClosure&>, |
| danakj | 560f5dd2e | 2017-04-04 20:55:29 | [diff] [blame] | 1620 | "RepeatingClosure should be convertible to OnceClosure."); |
| tzik | 27d1e31 | 2016-09-13 05:28:59 | [diff] [blame] | 1621 | |
| 1622 | // Destructive conversions from RepeatingCallback to OnceCallback. |
| Andrew Rayskiy | b42fe752 | 2023-10-17 13:16:19 | [diff] [blame] | 1623 | static_assert(std::is_constructible_v<OnceClosure, RepeatingClosure&&>, |
| Daniel Cheng | 91f6fbaf | 2022-09-16 12:07:48 | [diff] [blame] | 1624 | "RepeatingClosure should be convertible to OnceClosure."); |
| Andrew Rayskiy | b42fe752 | 2023-10-17 13:16:19 | [diff] [blame] | 1625 | static_assert(std::is_assignable_v<OnceClosure, RepeatingClosure&&>, |
| danakj | 560f5dd2e | 2017-04-04 20:55:29 | [diff] [blame] | 1626 | "RepeatingClosure should be covretible to OnceClosure."); |
| tzik | 27d1e31 | 2016-09-13 05:28:59 | [diff] [blame] | 1627 | |
| 1628 | OnceClosure cb = BindOnce(&VoidPolymorphic<>::Run); |
| 1629 | std::move(cb).Run(); |
| 1630 | |
| 1631 | // RepeatingCallback should be convertible to OnceCallback. |
| 1632 | OnceClosure cb2 = BindRepeating(&VoidPolymorphic<>::Run); |
| 1633 | std::move(cb2).Run(); |
| 1634 | |
| 1635 | RepeatingClosure cb3 = BindRepeating(&VoidPolymorphic<>::Run); |
| 1636 | cb = cb3; |
| 1637 | std::move(cb).Run(); |
| 1638 | |
| Peter Kasting | 6218bbad | 2025-01-10 11:26:40 | [diff] [blame] | 1639 | cb = std::move(cb2); // NOLINT(bugprone-use-after-move) |
| tzik | 27d1e31 | 2016-09-13 05:28:59 | [diff] [blame] | 1640 | |
| Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 1641 | OnceCallback<void(int)> cb4 = |
| 1642 | BindOnce(&VoidPolymorphic<std::unique_ptr<int>, int>::Run, |
| 1643 | std::make_unique<int>(0)); |
| tzik | 27d1e31 | 2016-09-13 05:28:59 | [diff] [blame] | 1644 | BindOnce(std::move(cb4), 1).Run(); |
| 1645 | } |
| 1646 | |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 1647 | // Callback construction and assignment tests. |
| 1648 | // - Construction from an InvokerStorageHolder should not cause ref/deref. |
| 1649 | // - Assignment from other callback should only cause one ref |
| 1650 | // |
| 1651 | // TODO(ajwong): Is there actually a way to test this? |
| 1652 | |
| Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 1653 | #if BUILDFLAG(IS_WIN) |
| [email protected] | 054ac754 | 2011-02-27 01:25:59 | [diff] [blame] | 1654 | int __fastcall FastCallFunc(int n) { |
| 1655 | return n; |
| 1656 | } |
| 1657 | |
| 1658 | int __stdcall StdCallFunc(int n) { |
| 1659 | return n; |
| 1660 | } |
| 1661 | |
| 1662 | // Windows specific calling convention support. |
| 1663 | // - Can bind a __fastcall function. |
| 1664 | // - Can bind a __stdcall function. |
| Sorin Jianu | 6bc2e35 | 2020-04-01 18:01:41 | [diff] [blame] | 1665 | // - Can bind const and non-const __stdcall methods. |
| [email protected] | 054ac754 | 2011-02-27 01:25:59 | [diff] [blame] | 1666 | TEST_F(BindTest, WindowsCallingConventions) { |
| Sorin Jianu | 6bc2e35 | 2020-04-01 18:01:41 | [diff] [blame] | 1667 | auto fastcall_cb = BindRepeating(&FastCallFunc, 1); |
| [email protected] | 054ac754 | 2011-02-27 01:25:59 | [diff] [blame] | 1668 | EXPECT_EQ(1, fastcall_cb.Run()); |
| 1669 | |
| Sorin Jianu | 6bc2e35 | 2020-04-01 18:01:41 | [diff] [blame] | 1670 | auto stdcall_cb = BindRepeating(&StdCallFunc, 2); |
| [email protected] | 054ac754 | 2011-02-27 01:25:59 | [diff] [blame] | 1671 | EXPECT_EQ(2, stdcall_cb.Run()); |
| Sorin Jianu | 6bc2e35 | 2020-04-01 18:01:41 | [diff] [blame] | 1672 | |
| 1673 | class MethodHolder { |
| 1674 | public: |
| 1675 | int __stdcall Func(int n) { return n; } |
| 1676 | int __stdcall ConstFunc(int n) const { return -n; } |
| 1677 | }; |
| 1678 | |
| 1679 | MethodHolder obj; |
| 1680 | auto stdcall_method_cb = |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 1681 | BindRepeating(&MethodHolder::Func, Unretained(&obj), 1); |
| Sorin Jianu | 6bc2e35 | 2020-04-01 18:01:41 | [diff] [blame] | 1682 | EXPECT_EQ(1, stdcall_method_cb.Run()); |
| 1683 | |
| 1684 | const MethodHolder const_obj; |
| 1685 | auto stdcall_const_method_cb = |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 1686 | BindRepeating(&MethodHolder::ConstFunc, Unretained(&const_obj), 1); |
| Sorin Jianu | 6bc2e35 | 2020-04-01 18:01:41 | [diff] [blame] | 1687 | EXPECT_EQ(-1, stdcall_const_method_cb.Run()); |
| [email protected] | 054ac754 | 2011-02-27 01:25:59 | [diff] [blame] | 1688 | } |
| 1689 | #endif |
| 1690 | |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 1691 | // Test unwrapping the various wrapping functions. |
| 1692 | |
| 1693 | TEST_F(BindTest, UnwrapUnretained) { |
| 1694 | int i = 0; |
| 1695 | auto unretained = Unretained(&i); |
| 1696 | EXPECT_EQ(&i, internal::Unwrap(unretained)); |
| 1697 | EXPECT_EQ(&i, internal::Unwrap(std::move(unretained))); |
| 1698 | } |
| 1699 | |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 1700 | TEST_F(BindTest, UnwrapRetainedRef) { |
| 1701 | auto p = MakeRefCounted<RefCountedData<int>>(); |
| 1702 | auto retained_ref = RetainedRef(p); |
| 1703 | EXPECT_EQ(p.get(), internal::Unwrap(retained_ref)); |
| 1704 | EXPECT_EQ(p.get(), internal::Unwrap(std::move(retained_ref))); |
| 1705 | } |
| 1706 | |
| 1707 | TEST_F(BindTest, UnwrapOwned) { |
| jdoerrie | 68f2d51b | 2019-02-28 17:32:44 | [diff] [blame] | 1708 | { |
| 1709 | int* p = new int; |
| 1710 | auto owned = Owned(p); |
| 1711 | EXPECT_EQ(p, internal::Unwrap(owned)); |
| 1712 | EXPECT_EQ(p, internal::Unwrap(std::move(owned))); |
| 1713 | } |
| 1714 | |
| 1715 | { |
| 1716 | auto p = std::make_unique<int>(); |
| 1717 | int* raw_p = p.get(); |
| 1718 | auto owned = Owned(std::move(p)); |
| 1719 | EXPECT_EQ(raw_p, internal::Unwrap(owned)); |
| 1720 | EXPECT_EQ(raw_p, internal::Unwrap(std::move(owned))); |
| 1721 | } |
| Peter Kasting | a85265e3 | 2018-02-15 08:30:23 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | TEST_F(BindTest, UnwrapPassed) { |
| 1725 | int* p = new int; |
| 1726 | auto passed = Passed(WrapUnique(p)); |
| 1727 | EXPECT_EQ(p, internal::Unwrap(passed).get()); |
| 1728 | |
| 1729 | p = new int; |
| 1730 | EXPECT_EQ(p, internal::Unwrap(Passed(WrapUnique(p))).get()); |
| 1731 | } |
| 1732 | |
| tzik | d58a8923 | 2018-04-26 04:29:53 | [diff] [blame] | 1733 | TEST_F(BindTest, BindNoexcept) { |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 1734 | EXPECT_EQ(42, BindOnce(&Noexcept).Run()); |
| 1735 | EXPECT_EQ(42, BindOnce(&BindTest::NoexceptMethod, Unretained(this)).Run()); |
| 1736 | EXPECT_EQ(42, |
| 1737 | BindOnce(&BindTest::ConstNoexceptMethod, Unretained(this)).Run()); |
| 1738 | EXPECT_EQ(42, BindOnce(NoexceptFunctor()).Run()); |
| 1739 | EXPECT_EQ(42, BindOnce(ConstNoexceptFunctor()).Run()); |
| tzik | d58a8923 | 2018-04-26 04:29:53 | [diff] [blame] | 1740 | } |
| 1741 | |
| Lukasz Anforowicz | 34d1682a | 2021-11-19 17:12:17 | [diff] [blame] | 1742 | int PingPong(int* i_ptr) { |
| 1743 | return *i_ptr; |
| 1744 | } |
| 1745 | |
| 1746 | TEST_F(BindTest, BindAndCallbacks) { |
| 1747 | int i = 123; |
| 1748 | raw_ptr<int> p = &i; |
| 1749 | |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 1750 | auto callback = BindOnce(PingPong, Unretained(p)); |
| Lukasz Anforowicz | 34d1682a | 2021-11-19 17:12:17 | [diff] [blame] | 1751 | int res = std::move(callback).Run(); |
| 1752 | EXPECT_EQ(123, res); |
| 1753 | } |
| 1754 | |
| Peter Kasting | a16713f | 2023-09-02 23:03:10 | [diff] [blame] | 1755 | TEST_F(BindTest, ConvertibleArgs) { |
| 1756 | // Create two types S and T, such that you can convert a T to an S, but you |
| 1757 | // cannot construct an S from a T. |
| 1758 | struct T; |
| 1759 | class S { |
| 1760 | friend struct T; |
| 1761 | explicit S(const T&) {} |
| 1762 | }; |
| 1763 | struct T { |
| 1764 | // NOLINTNEXTLINE(google-explicit-constructor) |
| 1765 | operator S() const { return S(*this); } |
| 1766 | }; |
| 1767 | static_assert(!std::is_constructible_v<S, T>); |
| 1768 | static_assert(std::is_convertible_v<T, S>); |
| 1769 | |
| 1770 | // Ensure it's possible to pass a T to a function expecting an S. |
| 1771 | void (*foo)(S) = +[](S) {}; |
| 1772 | const T t; |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 1773 | auto callback = BindOnce(foo, t); |
| Peter Kasting | a16713f | 2023-09-02 23:03:10 | [diff] [blame] | 1774 | std::move(callback).Run(); |
| 1775 | } |
| 1776 | |
| Peter Kasting | bb885329 | 2024-02-15 19:46:18 | [diff] [blame] | 1777 | TEST_F(BindTest, OverloadedOperator) { |
| 1778 | // Bind should be able to pick the correct `operator()()` to invoke on a |
| 1779 | // functor from the supplied args. |
| 1780 | struct S { |
| 1781 | int operator()(int x) { return x; } |
| 1782 | std::string operator()(std::string s) { return s; } |
| 1783 | } s; |
| 1784 | |
| 1785 | EXPECT_EQ(42, BindOnce(s, 42).Run()); |
| 1786 | EXPECT_EQ("Hello", BindOnce(s, "Hello").Run()); |
| 1787 | } |
| 1788 | |
| Peter Kasting | 2eef24f4 | 2024-02-21 18:42:22 | [diff] [blame] | 1789 | TEST_F(BindTest, OverloadedOperatorQualifiers) { |
| 1790 | // Bind should be able to pick the correct `operator()()` to invoke on a |
| 1791 | // functor when the only difference between the overloads is their qualifiers. |
| 1792 | struct S { |
| 1793 | int operator()() const& { return 1; } |
| 1794 | int operator()() && { return 2; } |
| 1795 | } s; |
| 1796 | |
| 1797 | // `BindRepeating()` normally stores a value and passes a const ref to the |
| 1798 | // invoked method, regardless of whether lvalue or rvalue was originally |
| 1799 | // provided. |
| 1800 | EXPECT_EQ(1, BindRepeating(s).Run()); |
| 1801 | EXPECT_EQ(1, BindRepeating(S()).Run()); |
| 1802 | |
| 1803 | // The exception is if `Passed()` is used, which tells `BindRepeating()` to |
| 1804 | // move the specified argument during invocation. |
| 1805 | EXPECT_EQ(2, BindRepeating(Passed(S())).Run()); |
| 1806 | |
| 1807 | // `BindOnce()` also stores a value, but it always moves that value during |
| 1808 | // invocation, regardless of whether lvalue or rvalue was originally provided. |
| 1809 | EXPECT_EQ(2, BindOnce(s).Run()); |
| 1810 | EXPECT_EQ(2, BindOnce(S()).Run()); |
| 1811 | } |
| 1812 | |
| Peter Kasting | bb885329 | 2024-02-15 19:46:18 | [diff] [blame] | 1813 | TEST_F(BindTest, OverloadedOperatorInexactMatch) { |
| 1814 | // The Bind machinery guesses signatures for overloaded `operator()()`s based |
| 1815 | // on the decay_t<>s of the bound args. But as long as all args are bound and |
| 1816 | // are convertible to exactly one overload's params, everything should work, |
| 1817 | // even if the guess is slightly incorrect. |
| 1818 | struct S { |
| 1819 | int operator()(int x) { return x; } |
| 1820 | // Machinery will guess that param type here is `std::string`. |
| 1821 | std::string operator()(const std::string& s) { return s; } |
| 1822 | } s; |
| 1823 | |
| 1824 | EXPECT_EQ(42, BindOnce(s, 42).Run()); |
| 1825 | EXPECT_EQ("Hello", BindOnce(s, "Hello").Run()); |
| 1826 | } |
| 1827 | |
| Andrey Kosyakov | 64b025b | 2023-03-31 21:40:15 | [diff] [blame] | 1828 | } // namespace |
| 1829 | |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 1830 | // This simulates a race weak pointer that, unlike our `WeakPtr<>`, |
| Andrey Kosyakov | 64b025b | 2023-03-31 21:40:15 | [diff] [blame] | 1831 | // may become invalidated between `operator bool()` is tested and `Lock()` |
| 1832 | // is called in the implementation of `Unwrap()`. |
| 1833 | template <typename T> |
| 1834 | struct MockRacyWeakPtr { |
| 1835 | explicit MockRacyWeakPtr(T*) {} |
| 1836 | T* Lock() const { return nullptr; } |
| 1837 | |
| 1838 | explicit operator bool() const { return true; } |
| 1839 | }; |
| 1840 | |
| 1841 | template <typename T> |
| 1842 | struct IsWeakReceiver<MockRacyWeakPtr<T>> : std::true_type {}; |
| 1843 | |
| 1844 | template <typename T> |
| 1845 | struct BindUnwrapTraits<MockRacyWeakPtr<T>> { |
| 1846 | static T* Unwrap(const MockRacyWeakPtr<T>& o) { return o.Lock(); } |
| 1847 | }; |
| 1848 | |
| 1849 | template <typename T> |
| 1850 | struct MaybeValidTraits<MockRacyWeakPtr<T>> { |
| 1851 | static bool MaybeValid(const MockRacyWeakPtr<T>& o) { return true; } |
| 1852 | }; |
| 1853 | |
| 1854 | namespace { |
| 1855 | |
| 1856 | // Note this only covers a case of racy weak pointer invalidation. Other |
| 1857 | // weak pointer scenarios (such as a valid pointer) are covered |
| 1858 | // in BindTest.WeakPtrFor{Once,Repeating}. |
| 1859 | TEST_F(BindTest, BindRacyWeakPtrTest) { |
| 1860 | MockRacyWeakPtr<NoRef> weak(&no_ref_); |
| 1861 | |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 1862 | RepeatingClosure cb = BindRepeating(&NoRef::VoidMethod0, weak); |
| Andrey Kosyakov | 64b025b | 2023-03-31 21:40:15 | [diff] [blame] | 1863 | cb.Run(); |
| 1864 | } |
| 1865 | |
| [email protected] | 8cf362c | 2012-11-20 08:28:14 | [diff] [blame] | 1866 | // Test null callbacks cause a DCHECK. |
| 1867 | TEST(BindDeathTest, NullCallback) { |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 1868 | RepeatingCallback<void(int)> null_cb; |
| [email protected] | 8cf362c | 2012-11-20 08:28:14 | [diff] [blame] | 1869 | ASSERT_TRUE(null_cb.is_null()); |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 1870 | EXPECT_CHECK_DEATH(BindRepeating(null_cb, 42)); |
| Sami Kyostila | 712b4e3 | 2020-04-27 16:51:08 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | TEST(BindDeathTest, NullFunctionPointer) { |
| 1874 | void (*null_function)(int) = nullptr; |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 1875 | EXPECT_DCHECK_DEATH(BindRepeating(null_function, 42)); |
| Sami Kyostila | 712b4e3 | 2020-04-27 16:51:08 | [diff] [blame] | 1876 | } |
| 1877 | |
| 1878 | TEST(BindDeathTest, NullCallbackWithoutBoundArgs) { |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 1879 | OnceCallback<void(int)> null_cb; |
| Sami Kyostila | 712b4e3 | 2020-04-27 16:51:08 | [diff] [blame] | 1880 | ASSERT_TRUE(null_cb.is_null()); |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 1881 | EXPECT_CHECK_DEATH(BindOnce(std::move(null_cb))); |
| [email protected] | 8cf362c | 2012-11-20 08:28:14 | [diff] [blame] | 1882 | } |
| 1883 | |
| tzik | efea4f5 | 2018-08-02 15:20:46 | [diff] [blame] | 1884 | TEST(BindDeathTest, BanFirstOwnerOfRefCountedType) { |
| 1885 | StrictMock<HasRef> has_ref; |
| 1886 | EXPECT_DCHECK_DEATH({ |
| 1887 | EXPECT_CALL(has_ref, HasAtLeastOneRef()).WillOnce(Return(false)); |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 1888 | BindOnce(&HasRef::VoidMethod0, &has_ref); |
| tzik | efea4f5 | 2018-08-02 15:20:46 | [diff] [blame] | 1889 | }); |
| Daniel Cheng | 00c072a | 2022-06-11 18:50:37 | [diff] [blame] | 1890 | |
| 1891 | EXPECT_DCHECK_DEATH({ |
| 1892 | raw_ptr<HasRef> rawptr(&has_ref); |
| 1893 | EXPECT_CALL(has_ref, HasAtLeastOneRef()).WillOnce(Return(false)); |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 1894 | BindOnce(&HasRef::VoidMethod0, rawptr); |
| Daniel Cheng | 00c072a | 2022-06-11 18:50:37 | [diff] [blame] | 1895 | }); |
| tzik | efea4f5 | 2018-08-02 15:20:46 | [diff] [blame] | 1896 | } |
| 1897 | |
| Arthur Sonzogni | 62e877a | 2024-04-30 16:09:43 | [diff] [blame] | 1898 | #if PA_BUILDFLAG(ENABLE_BACKUP_REF_PTR_SUPPORT) && \ |
| 1899 | PA_BUILDFLAG(USE_RAW_PTR_BACKUP_REF_IMPL) |
| Paul Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 1900 | |
| 1901 | void HandleOOM(size_t unused_size) { |
| 1902 | LOG(FATAL) << "Out of memory"; |
| 1903 | } |
| 1904 | |
| 1905 | // Basic set of options to mostly only enable `BackupRefPtr::kEnabled`. |
| 1906 | // This avoids the boilerplate of having too much options enabled for simple |
| 1907 | // testing purpose. |
| Sorin Jianu | ad4cc623 | 2024-10-08 19:06:01 | [diff] [blame] | 1908 | static constexpr auto kOnlyEnableBackupRefPtrOptions = [] { |
| mikt | a205f9e6 | 2023-11-21 11:52:01 | [diff] [blame] | 1909 | partition_alloc::PartitionOptions opts; |
| 1910 | opts.backup_ref_ptr = partition_alloc::PartitionOptions::kEnabled; |
| 1911 | return opts; |
| 1912 | }(); |
| Paul Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 1913 | |
| 1914 | class BindUnretainedDanglingInternalFixture : public BindTest { |
| 1915 | public: |
| 1916 | void SetUp() override { |
| 1917 | partition_alloc::PartitionAllocGlobalInit(HandleOOM); |
| Paul Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 1918 | enabled_feature_list_.InitWithFeaturesAndParameters( |
| 1919 | {{features::kPartitionAllocUnretainedDanglingPtr, {{"mode", "crash"}}}}, |
| 1920 | {/* disabled_features */}); |
| 1921 | allocator::InstallUnretainedDanglingRawPtrChecks(); |
| 1922 | } |
| Takashi Sakamoto | bf13812 | 2023-02-10 02:41:38 | [diff] [blame] | 1923 | |
| Paul Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 1924 | void TearDown() override { |
| 1925 | enabled_feature_list_.Reset(); |
| 1926 | allocator::InstallUnretainedDanglingRawPtrChecks(); |
| Paul Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 1927 | } |
| 1928 | |
| 1929 | // In unit tests, allocations being tested need to live in a separate PA |
| 1930 | // root so the test code doesn't interfere with various counters. Following |
| 1931 | // methods are helpers for managing allocations inside the separate allocator |
| 1932 | // root. |
| Paul Semel | 6cef46d | 2023-02-28 10:02:21 | [diff] [blame] | 1933 | template <typename T, |
| 1934 | RawPtrTraits Traits = RawPtrTraits::kEmpty, |
| 1935 | typename... Args> |
| 1936 | raw_ptr<T, Traits> Alloc(Args&&... args) { |
| Paul Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 1937 | void* ptr = allocator_.root()->Alloc(sizeof(T), ""); |
| 1938 | T* p = new (reinterpret_cast<T*>(ptr)) T(std::forward<Args>(args)...); |
| Paul Semel | 6cef46d | 2023-02-28 10:02:21 | [diff] [blame] | 1939 | return raw_ptr<T, Traits>(p); |
| Paul Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 1940 | } |
| Paul Semel | 6cef46d | 2023-02-28 10:02:21 | [diff] [blame] | 1941 | template <typename T, RawPtrTraits Traits> |
| 1942 | void Free(raw_ptr<T, Traits>& ptr) { |
| Arthur Sonzogni | 994b4d9 | 2023-05-31 07:49:35 | [diff] [blame] | 1943 | allocator_.root()->Free(ptr.ExtractAsDangling()); |
| Paul Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 1944 | } |
| 1945 | |
| 1946 | private: |
| 1947 | test::ScopedFeatureList enabled_feature_list_; |
| Takashi Sakamoto | bf13812 | 2023-02-10 02:41:38 | [diff] [blame] | 1948 | partition_alloc::PartitionAllocatorForTesting allocator_{ |
| 1949 | kOnlyEnableBackupRefPtrOptions}; |
| Paul Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 1950 | }; |
| 1951 | |
| 1952 | class BindUnretainedDanglingTest |
| 1953 | : public BindUnretainedDanglingInternalFixture {}; |
| 1954 | class BindUnretainedDanglingDeathTest |
| 1955 | : public BindUnretainedDanglingInternalFixture {}; |
| 1956 | |
| 1957 | bool PtrCheckFn(int* p) { |
| 1958 | return p != nullptr; |
| 1959 | } |
| 1960 | |
| Paul Semel | 9d2f0697 | 2022-12-12 16:50:44 | [diff] [blame] | 1961 | bool RefCheckFn(const int& p) { |
| 1962 | return true; |
| 1963 | } |
| 1964 | |
| Paul Semel | ff2ca67b | 2022-12-21 15:46:02 | [diff] [blame] | 1965 | bool MayBeDanglingCheckFn(MayBeDangling<int> p) { |
| 1966 | return p != nullptr; |
| 1967 | } |
| 1968 | |
| Paul Semel | 6cef46d | 2023-02-28 10:02:21 | [diff] [blame] | 1969 | bool MayBeDanglingAndDummyTraitCheckFn( |
| 1970 | MayBeDangling<int, RawPtrTraits::kDummyForTest> p) { |
| 1971 | return p != nullptr; |
| 1972 | } |
| 1973 | |
| Paul Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 1974 | class ClassWithWeakPtr { |
| 1975 | public: |
| 1976 | ClassWithWeakPtr() = default; |
| 1977 | void RawPtrArg(int* p) { *p = 123; } |
| Paul Semel | 9d2f0697 | 2022-12-12 16:50:44 | [diff] [blame] | 1978 | void RawRefArg(int& p) { p = 123; } |
| Paul Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 1979 | WeakPtr<ClassWithWeakPtr> GetWeakPtr() { return weak_factory_.GetWeakPtr(); } |
| 1980 | |
| 1981 | private: |
| 1982 | WeakPtrFactory<ClassWithWeakPtr> weak_factory_{this}; |
| 1983 | }; |
| 1984 | |
| 1985 | TEST_F(BindUnretainedDanglingTest, UnretainedNoDanglingPtr) { |
| 1986 | raw_ptr<int> p = Alloc<int>(3); |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 1987 | auto callback = BindOnce(PingPong, Unretained(p)); |
| Paul Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 1988 | EXPECT_EQ(std::move(callback).Run(), 3); |
| 1989 | Free(p); |
| 1990 | } |
| 1991 | |
| 1992 | TEST_F(BindUnretainedDanglingTest, UnsafeDanglingPtr) { |
| 1993 | raw_ptr<int> p = Alloc<int>(3); |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 1994 | auto callback = BindOnce(MayBeDanglingCheckFn, UnsafeDangling(p)); |
| Paul Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 1995 | Free(p); |
| 1996 | EXPECT_EQ(std::move(callback).Run(), true); |
| 1997 | } |
| 1998 | |
| Paul Semel | 6cef46d | 2023-02-28 10:02:21 | [diff] [blame] | 1999 | TEST_F(BindUnretainedDanglingTest, UnsafeDanglingPtrWithDummyTrait) { |
| 2000 | raw_ptr<int, RawPtrTraits::kDummyForTest> p = |
| 2001 | Alloc<int, RawPtrTraits::kDummyForTest>(3); |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 2002 | auto callback = |
| 2003 | BindOnce(MayBeDanglingAndDummyTraitCheckFn, UnsafeDangling(p)); |
| Paul Semel | 6cef46d | 2023-02-28 10:02:21 | [diff] [blame] | 2004 | Free(p); |
| 2005 | EXPECT_EQ(std::move(callback).Run(), true); |
| 2006 | } |
| 2007 | |
| 2008 | TEST_F(BindUnretainedDanglingTest, |
| 2009 | UnsafeDanglingPtrWithDummyAndDanglingTraits) { |
| 2010 | raw_ptr<int, RawPtrTraits::kDummyForTest | RawPtrTraits::kMayDangle> p = |
| 2011 | Alloc<int, RawPtrTraits::kDummyForTest | RawPtrTraits::kMayDangle>(3); |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 2012 | auto callback = |
| 2013 | BindOnce(MayBeDanglingAndDummyTraitCheckFn, UnsafeDangling(p)); |
| Paul Semel | 6cef46d | 2023-02-28 10:02:21 | [diff] [blame] | 2014 | Free(p); |
| 2015 | EXPECT_EQ(std::move(callback).Run(), true); |
| 2016 | } |
| 2017 | |
| Paul Semel | ff2ca67b | 2022-12-21 15:46:02 | [diff] [blame] | 2018 | TEST_F(BindUnretainedDanglingTest, UnsafeDanglingPtrNoRawPtrReceiver) { |
| 2019 | std::unique_ptr<ClassWithWeakPtr> r = std::make_unique<ClassWithWeakPtr>(); |
| 2020 | int val = 0; |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 2021 | auto callback = BindOnce(&ClassWithWeakPtr::RawPtrArg, |
| 2022 | UnsafeDangling(r.get()), Unretained(&val)); |
| Paul Semel | ff2ca67b | 2022-12-21 15:46:02 | [diff] [blame] | 2023 | std::move(callback).Run(); |
| 2024 | EXPECT_EQ(val, 123); |
| 2025 | } |
| 2026 | |
| Paul Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 2027 | TEST_F(BindUnretainedDanglingTest, UnsafeDanglingUntriagedPtr) { |
| 2028 | raw_ptr<int> p = Alloc<int>(3); |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 2029 | auto callback = BindOnce(PtrCheckFn, UnsafeDanglingUntriaged(p)); |
| Paul Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 2030 | Free(p); |
| 2031 | EXPECT_EQ(std::move(callback).Run(), true); |
| 2032 | } |
| 2033 | |
| 2034 | TEST_F(BindUnretainedDanglingTest, UnretainedWeakReceiverValidNoDangling) { |
| 2035 | raw_ptr<int> p = Alloc<int>(3); |
| 2036 | std::unique_ptr<ClassWithWeakPtr> r = std::make_unique<ClassWithWeakPtr>(); |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 2037 | auto callback = |
| 2038 | BindOnce(&ClassWithWeakPtr::RawPtrArg, r->GetWeakPtr(), Unretained(p)); |
| Paul Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 2039 | std::move(callback).Run(); |
| 2040 | EXPECT_EQ(*p, 123); |
| 2041 | Free(p); |
| 2042 | } |
| 2043 | |
| Paul Semel | 9d2f0697 | 2022-12-12 16:50:44 | [diff] [blame] | 2044 | TEST_F(BindUnretainedDanglingTest, UnretainedRefWeakReceiverValidNoDangling) { |
| 2045 | raw_ptr<int> p = Alloc<int>(3); |
| 2046 | int& ref = *p; |
| 2047 | std::unique_ptr<ClassWithWeakPtr> r = std::make_unique<ClassWithWeakPtr>(); |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 2048 | auto callback = |
| 2049 | BindOnce(&ClassWithWeakPtr::RawRefArg, r->GetWeakPtr(), std::ref(ref)); |
| Paul Semel | 9d2f0697 | 2022-12-12 16:50:44 | [diff] [blame] | 2050 | std::move(callback).Run(); |
| 2051 | EXPECT_EQ(*p, 123); |
| 2052 | Free(p); |
| 2053 | } |
| Paul Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 2054 | |
| 2055 | TEST_F(BindUnretainedDanglingTest, UnretainedWeakReceiverInvalidNoDangling) { |
| 2056 | raw_ptr<int> p = Alloc<int>(3); |
| 2057 | std::unique_ptr<ClassWithWeakPtr> r = std::make_unique<ClassWithWeakPtr>(); |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 2058 | auto callback = |
| 2059 | BindOnce(&ClassWithWeakPtr::RawPtrArg, r->GetWeakPtr(), Unretained(p)); |
| Paul Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 2060 | r.reset(); |
| 2061 | Free(p); |
| 2062 | std::move(callback).Run(); |
| 2063 | // Should reach this point without crashing; there is a dangling pointer, but |
| 2064 | // the callback is cancelled because the WeakPtr is already invalidated. |
| 2065 | } |
| 2066 | |
| Paul Semel | 9d2f0697 | 2022-12-12 16:50:44 | [diff] [blame] | 2067 | TEST_F(BindUnretainedDanglingTest, UnretainedRefWeakReceiverInvalidNoDangling) { |
| 2068 | raw_ptr<int> p = Alloc<int>(3); |
| 2069 | int& ref = *p; |
| 2070 | std::unique_ptr<ClassWithWeakPtr> r = std::make_unique<ClassWithWeakPtr>(); |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 2071 | auto callback = |
| 2072 | BindOnce(&ClassWithWeakPtr::RawRefArg, r->GetWeakPtr(), std::ref(ref)); |
| Paul Semel | 9d2f0697 | 2022-12-12 16:50:44 | [diff] [blame] | 2073 | r.reset(); |
| 2074 | Free(p); |
| 2075 | std::move(callback).Run(); |
| 2076 | // Should reach this point without crashing; there is a dangling pointer, but |
| 2077 | // the callback is cancelled because the WeakPtr is already invalidated. |
| 2078 | } |
| 2079 | |
| 2080 | TEST_F(BindUnretainedDanglingTest, UnretainedRefUnsafeDangling) { |
| 2081 | raw_ptr<int> p = Alloc<int>(3); |
| 2082 | int& ref = *p; |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 2083 | auto callback = BindOnce(RefCheckFn, UnsafeDangling(raw_ref<int>(ref))); |
| Paul Semel | 9d2f0697 | 2022-12-12 16:50:44 | [diff] [blame] | 2084 | Free(p); |
| 2085 | EXPECT_EQ(std::move(callback).Run(), true); |
| 2086 | // Should reach this point without crashing; there is a dangling pointer, but |
| 2087 | // the we marked the reference as `UnsafeDangling`. |
| 2088 | } |
| 2089 | |
| 2090 | TEST_F(BindUnretainedDanglingTest, UnretainedRefUnsafeDanglingUntriaged) { |
| 2091 | raw_ptr<int> p = Alloc<int>(3); |
| 2092 | int& ref = *p; |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 2093 | auto callback = |
| 2094 | BindOnce(RefCheckFn, UnsafeDanglingUntriaged(raw_ref<const int>(ref))); |
| Paul Semel | 9d2f0697 | 2022-12-12 16:50:44 | [diff] [blame] | 2095 | Free(p); |
| 2096 | EXPECT_EQ(std::move(callback).Run(), true); |
| 2097 | // Should reach this point without crashing; there is a dangling pointer, but |
| 2098 | // the we marked the reference as `UnsafeDanglingUntriaged`. |
| 2099 | } |
| 2100 | |
| 2101 | // Death tests misbehave on Android, http://crbug.com/643760. |
| 2102 | #if defined(GTEST_HAS_DEATH_TEST) && !BUILDFLAG(IS_ANDROID) |
| 2103 | |
| 2104 | int FuncWithRefArgument(int& i_ptr) { |
| 2105 | return i_ptr; |
| 2106 | } |
| 2107 | |
| Paul Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 2108 | TEST_F(BindUnretainedDanglingDeathTest, UnretainedDanglingPtr) { |
| 2109 | raw_ptr<int> p = Alloc<int>(3); |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 2110 | auto callback = BindOnce(PingPong, Unretained(p)); |
| Paul Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 2111 | Free(p); |
| 2112 | EXPECT_DEATH(std::move(callback).Run(), ""); |
| 2113 | } |
| 2114 | |
| Paul Semel | 9d2f0697 | 2022-12-12 16:50:44 | [diff] [blame] | 2115 | TEST_F(BindUnretainedDanglingDeathTest, UnretainedRefDanglingPtr) { |
| 2116 | raw_ptr<int> p = Alloc<int>(3); |
| 2117 | int& ref = *p; |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 2118 | auto callback = BindOnce(FuncWithRefArgument, std::ref(ref)); |
| Paul Semel | 9d2f0697 | 2022-12-12 16:50:44 | [diff] [blame] | 2119 | Free(p); |
| 2120 | EXPECT_DEATH(std::move(callback).Run(), ""); |
| 2121 | } |
| 2122 | |
| 2123 | TEST_F(BindUnretainedDanglingDeathTest, |
| 2124 | UnretainedRefWithManualUnretainedDanglingPtr) { |
| 2125 | raw_ptr<int> p = Alloc<int>(3); |
| 2126 | int& ref = *p; |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 2127 | auto callback = BindOnce(FuncWithRefArgument, Unretained(raw_ref<int>(ref))); |
| Paul Semel | 9d2f0697 | 2022-12-12 16:50:44 | [diff] [blame] | 2128 | Free(p); |
| 2129 | EXPECT_DEATH(std::move(callback).Run(), ""); |
| 2130 | } |
| 2131 | |
| Paul Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 2132 | TEST_F(BindUnretainedDanglingDeathTest, UnretainedWeakReceiverDangling) { |
| 2133 | raw_ptr<int> p = Alloc<int>(3); |
| 2134 | std::unique_ptr<ClassWithWeakPtr> r = std::make_unique<ClassWithWeakPtr>(); |
| Peter Kasting | 8e4901d | 2024-02-16 16:34:05 | [diff] [blame] | 2135 | auto callback = |
| 2136 | BindOnce(&ClassWithWeakPtr::RawPtrArg, r->GetWeakPtr(), Unretained(p)); |
| Paul Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 2137 | Free(p); |
| 2138 | EXPECT_DEATH(std::move(callback).Run(), ""); |
| 2139 | } |
| 2140 | |
| 2141 | #endif // defined(GTEST_HAS_DEATH_TEST) && !BUILDFLAG(IS_ANDROID) |
| 2142 | |
| Arthur Sonzogni | 62e877a | 2024-04-30 16:09:43 | [diff] [blame] | 2143 | #endif // PA_BUILDFLAG(ENABLE_BACKUP_REF_PTR_SUPPORT) && |
| 2144 | // PA_BUILDFLAG(USE_RAW_PTR_BACKUP_REF_IMPL) |
| Paul Semel | b0ce484 | 2022-09-28 08:09:53 | [diff] [blame] | 2145 | |
| [email protected] | b38d357 | 2011-02-15 01:27:38 | [diff] [blame] | 2146 | } // namespace |
| 2147 | } // namespace base |