Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(698)

Side by Side Diff: base/bind_unittest.cc

Issue 2042223002: Introduce OnceClosure and BindOnce (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/bind.h" 5 #include "base/bind.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 1050 matching lines...) Expand 10 before | Expand all | Expand 10 after
1061 1061
1062 int x = 1; 1062 int x = 1;
1063 base::Callback<void(int)> cb = 1063 base::Callback<void(int)> cb =
1064 Bind([](int* x, int i) { *x *= i; }, Unretained(&x)); 1064 Bind([](int* x, int i) { *x *= i; }, Unretained(&x));
1065 cb.Run(6); 1065 cb.Run(6);
1066 EXPECT_EQ(6, x); 1066 EXPECT_EQ(6, x);
1067 cb.Run(7); 1067 cb.Run(7);
1068 EXPECT_EQ(42, x); 1068 EXPECT_EQ(42, x);
1069 } 1069 }
1070 1070
1071 TEST_F(BindTest, OneShotCallback) {
1072 // Check if Callback variants have declarations of conversions as expected.
1073 // Copy constructor and assignment of RepeatingCallback.
1074 EXPECT_TRUE((std::is_constructible<
1075 RepeatingClosure, const RepeatingClosure&>::value));
1076 EXPECT_TRUE((is_assignable<
1077 RepeatingClosure, const RepeatingClosure&>::value));
1078
1079 // Move constructor and assignment of RepeatingCallback.
1080 EXPECT_TRUE((std::is_constructible<
1081 RepeatingClosure, RepeatingClosure&&>::value));
1082 EXPECT_TRUE((is_assignable<
1083 RepeatingClosure, RepeatingClosure&&>::value));
1084
1085 // Conversions from OneShotCallback to RepeatingCallback.
1086 EXPECT_FALSE((std::is_constructible<
1087 RepeatingClosure, const OneShotClosure&>::value));
1088 EXPECT_FALSE((is_assignable<
1089 RepeatingClosure, const OneShotClosure&>::value));
1090
1091 // Destructive conversions from OneShotCallback to RepeatingCallback.
1092 EXPECT_FALSE((std::is_constructible<
1093 RepeatingClosure, OneShotClosure&&>::value));
1094 EXPECT_FALSE((is_assignable<
1095 RepeatingClosure, OneShotClosure&&>::value));
1096
1097 // Copy constructor and assignment of OneShotCallback.
1098 EXPECT_FALSE((std::is_constructible<
1099 OneShotClosure, const OneShotClosure&>::value));
1100 EXPECT_FALSE((is_assignable<
1101 OneShotClosure, const OneShotClosure&>::value));
1102
1103 // Move constructor and assignment of OneShotCallback.
1104 EXPECT_TRUE((std::is_constructible<
1105 OneShotClosure, OneShotClosure&&>::value));
1106 EXPECT_TRUE((is_assignable<
1107 OneShotClosure, OneShotClosure&&>::value));
1108
1109 // Conversions from RepeatingCallback to OneShotCallback.
1110 EXPECT_TRUE((std::is_constructible<
1111 OneShotClosure, const RepeatingClosure&>::value));
1112 EXPECT_TRUE((is_assignable<
1113 OneShotClosure, const RepeatingClosure&>::value));
1114
1115 // Destructive conversions from RepeatingCallback to OneShotCallback.
1116 EXPECT_TRUE((std::is_constructible<
1117 OneShotClosure, RepeatingClosure&&>::value));
1118 EXPECT_TRUE((is_assignable<
1119 OneShotClosure, RepeatingClosure&&>::value));
1120
1121 OneShotClosure cb = BindOneShot(&VoidPolymorphic<>::Run);
1122 std::move(cb).Run();
1123
1124 // RepeatingCallback should be convertible to OneShotCallback.
1125 OneShotClosure cb2 = BindRepeating(&VoidPolymorphic<>::Run);
1126 std::move(cb2).Run();
1127
1128 RepeatingClosure cb3 = BindRepeating(&VoidPolymorphic<>::Run);
1129 cb = cb3;
1130 std::move(cb).Run();
1131
1132 cb = std::move(cb2);
1133
1134 OneShotCallback<void(int)> cb4 = BindOneShot(
1135 &VoidPolymorphic<std::unique_ptr<int>, int>::Run, MakeUnique<int>(0));
1136 BindOneShot(std::move(cb4), 1).Run();
1137 }
1138
1071 // Callback construction and assignment tests. 1139 // Callback construction and assignment tests.
1072 // - Construction from an InvokerStorageHolder should not cause ref/deref. 1140 // - Construction from an InvokerStorageHolder should not cause ref/deref.
1073 // - Assignment from other callback should only cause one ref 1141 // - Assignment from other callback should only cause one ref
1074 // 1142 //
1075 // TODO(ajwong): Is there actually a way to test this? 1143 // TODO(ajwong): Is there actually a way to test this?
1076 1144
1077 #if defined(OS_WIN) 1145 #if defined(OS_WIN)
1078 int __fastcall FastCallFunc(int n) { 1146 int __fastcall FastCallFunc(int n) {
1079 return n; 1147 return n;
1080 } 1148 }
(...skipping 21 matching lines...) Expand all
1102 base::Callback<void(int)> null_cb; 1170 base::Callback<void(int)> null_cb;
1103 ASSERT_TRUE(null_cb.is_null()); 1171 ASSERT_TRUE(null_cb.is_null());
1104 EXPECT_DEATH(base::Bind(null_cb, 42), ""); 1172 EXPECT_DEATH(base::Bind(null_cb, 42), "");
1105 } 1173 }
1106 1174
1107 #endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && 1175 #endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) &&
1108 // GTEST_HAS_DEATH_TEST 1176 // GTEST_HAS_DEATH_TEST
1109 1177
1110 } // namespace 1178 } // namespace
1111 } // namespace base 1179 } // namespace base
OLDNEW
« base/bind.h ('K') | « base/bind_internal.h ('k') | base/callback.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698