| 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 910 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 921 | 921 |
| 922 copies = 0; | 922 copies = 0; |
| 923 assigns = 0; | 923 assigns = 0; |
| 924 Bind(&VoidPolymorphic<CopyCounter>::Run, CopyCounter(&copies, &assigns)); | 924 Bind(&VoidPolymorphic<CopyCounter>::Run, CopyCounter(&copies, &assigns)); |
| 925 EXPECT_EQ(1, copies); | 925 EXPECT_EQ(1, copies); |
| 926 EXPECT_EQ(0, assigns); | 926 EXPECT_EQ(0, assigns); |
| 927 | 927 |
| 928 copies = 0; | 928 copies = 0; |
| 929 assigns = 0; | 929 assigns = 0; |
| 930 Bind(&VoidPolymorphic<CopyCounter>::Run).Run(counter); | 930 Bind(&VoidPolymorphic<CopyCounter>::Run).Run(counter); |
| 931 EXPECT_EQ(1, copies); | 931 EXPECT_EQ(2, copies); |
| 932 EXPECT_EQ(0, assigns); | 932 EXPECT_EQ(0, assigns); |
| 933 | 933 |
| 934 copies = 0; | 934 copies = 0; |
| 935 assigns = 0; | 935 assigns = 0; |
| 936 Bind(&VoidPolymorphic<CopyCounter>::Run).Run(CopyCounter(&copies, &assigns)); | 936 Bind(&VoidPolymorphic<CopyCounter>::Run).Run(CopyCounter(&copies, &assigns)); |
| 937 EXPECT_EQ(1, copies); | 937 EXPECT_EQ(1, copies); |
| 938 EXPECT_EQ(0, assigns); | 938 EXPECT_EQ(0, assigns); |
| 939 | 939 |
| 940 copies = 0; | 940 copies = 0; |
| 941 assigns = 0; | 941 assigns = 0; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 960 int move_assigns = 0; | 960 int move_assigns = 0; |
| 961 | 961 |
| 962 Bind(&VoidPolymorphic<const MoveCounter&>::Run, | 962 Bind(&VoidPolymorphic<const MoveCounter&>::Run, |
| 963 MoveCounter(&move_constructs, &move_assigns)); | 963 MoveCounter(&move_constructs, &move_assigns)); |
| 964 EXPECT_EQ(1, move_constructs); | 964 EXPECT_EQ(1, move_constructs); |
| 965 EXPECT_EQ(0, move_assigns); | 965 EXPECT_EQ(0, move_assigns); |
| 966 | 966 |
| 967 // TODO(tzik): Support binding move-only type into a non-reference parameter | 967 // TODO(tzik): Support binding move-only type into a non-reference parameter |
| 968 // of a variant of Callback. | 968 // of a variant of Callback. |
| 969 | 969 |
| 970 // TODO(tzik): Support move-only type forwarding on Callback::Run. | 970 move_constructs = 0; |
| 971 move_assigns = 0; |
| 972 Bind(&VoidPolymorphic<MoveCounter>::Run) |
| 973 .Run(MoveCounter(&move_constructs, &move_assigns)); |
| 974 EXPECT_EQ(1, move_constructs); |
| 975 EXPECT_EQ(0, move_assigns); |
| 976 |
| 977 move_constructs = 0; |
| 978 move_assigns = 0; |
| 979 Bind(&VoidPolymorphic<MoveCounter>::Run) |
| 980 .Run(MoveCounter(DerivedCopyMoveCounter( |
| 981 nullptr, nullptr, &move_constructs, &move_assigns))); |
| 982 EXPECT_EQ(2, move_constructs); |
| 983 EXPECT_EQ(0, move_assigns); |
| 971 } | 984 } |
| 972 | 985 |
| 973 // Argument constructor usage for non-reference movable-copyable | 986 // Argument constructor usage for non-reference movable-copyable |
| 974 // parameters. | 987 // parameters. |
| 975 // - Bound arguments passed by move are not copied. | 988 // - Bound arguments passed by move are not copied. |
| 976 // - Forwarded arguments are only copied once. | 989 // - Forwarded arguments are only copied once. |
| 977 // - Forwarded arguments with coercions are only copied once and moved once. | 990 // - Forwarded arguments with coercions are only copied once and moved once. |
| 978 TEST_F(BindTest, ArgumentCopiesAndMoves) { | 991 TEST_F(BindTest, ArgumentCopiesAndMoves) { |
| 979 int copies = 0; | 992 int copies = 0; |
| 980 int assigns = 0; | 993 int assigns = 0; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 999 EXPECT_EQ(1, move_constructs); | 1012 EXPECT_EQ(1, move_constructs); |
| 1000 EXPECT_EQ(0, move_assigns); | 1013 EXPECT_EQ(0, move_assigns); |
| 1001 | 1014 |
| 1002 copies = 0; | 1015 copies = 0; |
| 1003 assigns = 0; | 1016 assigns = 0; |
| 1004 move_constructs = 0; | 1017 move_constructs = 0; |
| 1005 move_assigns = 0; | 1018 move_assigns = 0; |
| 1006 Bind(&VoidPolymorphic<CopyMoveCounter>::Run).Run(counter); | 1019 Bind(&VoidPolymorphic<CopyMoveCounter>::Run).Run(counter); |
| 1007 EXPECT_EQ(1, copies); | 1020 EXPECT_EQ(1, copies); |
| 1008 EXPECT_EQ(0, assigns); | 1021 EXPECT_EQ(0, assigns); |
| 1009 EXPECT_EQ(0, move_constructs); | 1022 EXPECT_EQ(1, move_constructs); |
| 1010 EXPECT_EQ(0, move_assigns); | 1023 EXPECT_EQ(0, move_assigns); |
| 1011 | 1024 |
| 1012 // TODO(tzik): This case should be done in no copy and one move. | |
| 1013 copies = 0; | 1025 copies = 0; |
| 1014 assigns = 0; | 1026 assigns = 0; |
| 1015 move_constructs = 0; | 1027 move_constructs = 0; |
| 1016 move_assigns = 0; | 1028 move_assigns = 0; |
| 1017 Bind(&VoidPolymorphic<CopyMoveCounter>::Run) | 1029 Bind(&VoidPolymorphic<CopyMoveCounter>::Run) |
| 1018 .Run(CopyMoveCounter(&copies, &assigns, &move_constructs, &move_assigns)); | 1030 .Run(CopyMoveCounter(&copies, &assigns, &move_constructs, &move_assigns)); |
| 1019 EXPECT_EQ(1, copies); | 1031 EXPECT_EQ(0, copies); |
| 1020 EXPECT_EQ(0, assigns); | 1032 EXPECT_EQ(0, assigns); |
| 1021 EXPECT_EQ(0, move_constructs); | 1033 EXPECT_EQ(1, move_constructs); |
| 1022 EXPECT_EQ(0, move_assigns); | 1034 EXPECT_EQ(0, move_assigns); |
| 1023 | 1035 |
| 1024 // TODO(tzik): This case should be done in one copy and one move. | |
| 1025 DerivedCopyMoveCounter derived_counter(&copies, &assigns, &move_constructs, | 1036 DerivedCopyMoveCounter derived_counter(&copies, &assigns, &move_constructs, |
| 1026 &move_assigns); | 1037 &move_assigns); |
| 1027 copies = 0; | 1038 copies = 0; |
| 1028 assigns = 0; | 1039 assigns = 0; |
| 1029 move_constructs = 0; | 1040 move_constructs = 0; |
| 1030 move_assigns = 0; | 1041 move_assigns = 0; |
| 1031 Bind(&VoidPolymorphic<CopyMoveCounter>::Run) | 1042 Bind(&VoidPolymorphic<CopyMoveCounter>::Run) |
| 1032 .Run(CopyMoveCounter(derived_counter)); | 1043 .Run(CopyMoveCounter(derived_counter)); |
| 1033 EXPECT_EQ(2, copies); | 1044 EXPECT_EQ(1, copies); |
| 1034 EXPECT_EQ(0, assigns); | 1045 EXPECT_EQ(0, assigns); |
| 1035 EXPECT_EQ(0, move_constructs); | 1046 EXPECT_EQ(1, move_constructs); |
| 1036 EXPECT_EQ(0, move_assigns); | 1047 EXPECT_EQ(0, move_assigns); |
| 1037 | 1048 |
| 1038 // TODO(tzik): This case should be done in no copy and two move. | |
| 1039 copies = 0; | 1049 copies = 0; |
| 1040 assigns = 0; | 1050 assigns = 0; |
| 1041 move_constructs = 0; | 1051 move_constructs = 0; |
| 1042 move_assigns = 0; | 1052 move_assigns = 0; |
| 1043 Bind(&VoidPolymorphic<CopyMoveCounter>::Run) | 1053 Bind(&VoidPolymorphic<CopyMoveCounter>::Run) |
| 1044 .Run(CopyMoveCounter(DerivedCopyMoveCounter( | 1054 .Run(CopyMoveCounter(DerivedCopyMoveCounter( |
| 1045 &copies, &assigns, &move_constructs, &move_assigns))); | 1055 &copies, &assigns, &move_constructs, &move_assigns))); |
| 1046 EXPECT_EQ(1, copies); | 1056 EXPECT_EQ(0, copies); |
| 1047 EXPECT_EQ(0, assigns); | 1057 EXPECT_EQ(0, assigns); |
| 1048 EXPECT_EQ(1, move_constructs); | 1058 EXPECT_EQ(2, move_constructs); |
| 1049 EXPECT_EQ(0, move_assigns); | 1059 EXPECT_EQ(0, move_assigns); |
| 1050 } | 1060 } |
| 1051 | 1061 |
| 1052 // Callback construction and assignment tests. | 1062 // Callback construction and assignment tests. |
| 1053 // - Construction from an InvokerStorageHolder should not cause ref/deref. | 1063 // - Construction from an InvokerStorageHolder should not cause ref/deref. |
| 1054 // - Assignment from other callback should only cause one ref | 1064 // - Assignment from other callback should only cause one ref |
| 1055 // | 1065 // |
| 1056 // TODO(ajwong): Is there actually a way to test this? | 1066 // TODO(ajwong): Is there actually a way to test this? |
| 1057 | 1067 |
| 1058 #if defined(OS_WIN) | 1068 #if defined(OS_WIN) |
| (...skipping 24 matching lines...) Expand all Loading... |
| 1083 base::Callback<void(int)> null_cb; | 1093 base::Callback<void(int)> null_cb; |
| 1084 ASSERT_TRUE(null_cb.is_null()); | 1094 ASSERT_TRUE(null_cb.is_null()); |
| 1085 EXPECT_DEATH(base::Bind(null_cb, 42), ""); | 1095 EXPECT_DEATH(base::Bind(null_cb, 42), ""); |
| 1086 } | 1096 } |
| 1087 | 1097 |
| 1088 #endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && | 1098 #endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && |
| 1089 // GTEST_HAS_DEATH_TEST | 1099 // GTEST_HAS_DEATH_TEST |
| 1090 | 1100 |
| 1091 } // namespace | 1101 } // namespace |
| 1092 } // namespace base | 1102 } // namespace base |
| OLD | NEW |