OLD | NEW |
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 1020 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1031 move_assigns = 0; | 1031 move_assigns = 0; |
1032 Bind(&VoidPolymorphic<CopyMoveCounter>::Run) | 1032 Bind(&VoidPolymorphic<CopyMoveCounter>::Run) |
1033 .Run(CopyMoveCounter(DerivedCopyMoveCounter( | 1033 .Run(CopyMoveCounter(DerivedCopyMoveCounter( |
1034 &copies, &assigns, &move_constructs, &move_assigns))); | 1034 &copies, &assigns, &move_constructs, &move_assigns))); |
1035 EXPECT_EQ(0, copies); | 1035 EXPECT_EQ(0, copies); |
1036 EXPECT_EQ(0, assigns); | 1036 EXPECT_EQ(0, assigns); |
1037 EXPECT_EQ(2, move_constructs); | 1037 EXPECT_EQ(2, move_constructs); |
1038 EXPECT_EQ(0, move_assigns); | 1038 EXPECT_EQ(0, move_assigns); |
1039 } | 1039 } |
1040 | 1040 |
| 1041 TEST_F(BindTest, CapturelessLambda) { |
| 1042 EXPECT_FALSE(internal::IsConvertibleToRunType<void>::value); |
| 1043 EXPECT_FALSE(internal::IsConvertibleToRunType<int>::value); |
| 1044 EXPECT_FALSE(internal::IsConvertibleToRunType<void(*)()>::value); |
| 1045 EXPECT_FALSE(internal::IsConvertibleToRunType<void(NoRef::*)()>::value); |
| 1046 |
| 1047 auto f = []() {}; |
| 1048 EXPECT_TRUE(internal::IsConvertibleToRunType<decltype(f)>::value); |
| 1049 |
| 1050 int i = 0; |
| 1051 auto g = [i]() {}; |
| 1052 EXPECT_FALSE(internal::IsConvertibleToRunType<decltype(g)>::value); |
| 1053 |
| 1054 auto h = [](int, double) { return 'k'; }; |
| 1055 EXPECT_TRUE((std::is_same< |
| 1056 char(int, double), |
| 1057 internal::ExtractCallableRunType<decltype(h)>>::value)); |
| 1058 |
| 1059 EXPECT_EQ(42, Bind([] { return 42; }).Run()); |
| 1060 EXPECT_EQ(42, Bind([](int i) { return i * 7; }, 6).Run()); |
| 1061 |
| 1062 int x = 1; |
| 1063 base::Callback<void(int)> cb = |
| 1064 Bind([](int* x, int i) { *x *= i; }, Unretained(&x)); |
| 1065 cb.Run(6); |
| 1066 EXPECT_EQ(6, x); |
| 1067 cb.Run(7); |
| 1068 EXPECT_EQ(42, x); |
| 1069 } |
| 1070 |
1041 // Callback construction and assignment tests. | 1071 // Callback construction and assignment tests. |
1042 // - Construction from an InvokerStorageHolder should not cause ref/deref. | 1072 // - Construction from an InvokerStorageHolder should not cause ref/deref. |
1043 // - Assignment from other callback should only cause one ref | 1073 // - Assignment from other callback should only cause one ref |
1044 // | 1074 // |
1045 // TODO(ajwong): Is there actually a way to test this? | 1075 // TODO(ajwong): Is there actually a way to test this? |
1046 | 1076 |
1047 #if defined(OS_WIN) | 1077 #if defined(OS_WIN) |
1048 int __fastcall FastCallFunc(int n) { | 1078 int __fastcall FastCallFunc(int n) { |
1049 return n; | 1079 return n; |
1050 } | 1080 } |
(...skipping 21 matching lines...) Expand all Loading... |
1072 base::Callback<void(int)> null_cb; | 1102 base::Callback<void(int)> null_cb; |
1073 ASSERT_TRUE(null_cb.is_null()); | 1103 ASSERT_TRUE(null_cb.is_null()); |
1074 EXPECT_DEATH(base::Bind(null_cb, 42), ""); | 1104 EXPECT_DEATH(base::Bind(null_cb, 42), ""); |
1075 } | 1105 } |
1076 | 1106 |
1077 #endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && | 1107 #endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && |
1078 // GTEST_HAS_DEATH_TEST | 1108 // GTEST_HAS_DEATH_TEST |
1079 | 1109 |
1080 } // namespace | 1110 } // namespace |
1081 } // namespace base | 1111 } // namespace base |
OLD | NEW |