OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include <webrtc/base/keep_ref_until_done.h> | |
magjed_webrtc
2015/11/30 15:44:34
This is not sorted anymore. Did you pass presubmit
perkj_webrtc
2015/11/30 16:41:31
Done.
| |
11 #include "webrtc/base/bind.h" | 12 #include "webrtc/base/bind.h" |
12 #include "webrtc/base/callback.h" | 13 #include "webrtc/base/callback.h" |
13 #include "webrtc/base/gunit.h" | 14 #include "webrtc/base/gunit.h" |
15 #include "webrtc/base/refcount.h" | |
14 | 16 |
15 namespace rtc { | 17 namespace rtc { |
16 | 18 |
17 namespace { | 19 namespace { |
18 | 20 |
19 void f() {} | 21 void f() {} |
20 int g() { return 42; } | 22 int g() { return 42; } |
21 int h(int x) { return x * x; } | 23 int h(int x) { return x * x; } |
22 void i(int& x) { x *= x; } // NOLINT: Testing refs | 24 void i(int& x) { x *= x; } // NOLINT: Testing refs |
23 | 25 |
24 struct BindTester { | 26 struct BindTester { |
25 int a() { return 24; } | 27 int a() { return 24; } |
26 int b(int x) const { return x * x; } | 28 int b(int x) const { return x * x; } |
27 }; | 29 }; |
28 | 30 |
31 class RefCountedBindTester : public RefCountInterface { | |
32 public: | |
33 RefCountedBindTester() : count_(0) {} | |
34 int AddRef() const override { | |
35 return ++count_; | |
36 } | |
37 int Release() const { | |
38 return --count_; | |
39 } | |
40 int RefCount() const { return count_; } | |
41 | |
42 private: | |
43 mutable int count_; | |
44 }; | |
45 | |
29 } // namespace | 46 } // namespace |
30 | 47 |
31 TEST(CallbackTest, VoidReturn) { | 48 TEST(CallbackTest, VoidReturn) { |
32 Callback0<void> cb; | 49 Callback0<void> cb; |
33 EXPECT_TRUE(cb.empty()); | 50 EXPECT_TRUE(cb.empty()); |
34 cb(); // Executing an empty callback should not crash. | 51 cb(); // Executing an empty callback should not crash. |
35 cb = Callback0<void>(&f); | 52 cb = Callback0<void>(&f); |
36 EXPECT_FALSE(cb.empty()); | 53 EXPECT_FALSE(cb.empty()); |
37 cb(); | 54 cb(); |
38 } | 55 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
71 EXPECT_EQ(24, cb1()); | 88 EXPECT_EQ(24, cb1()); |
72 EXPECT_EQ(24, cb1()); | 89 EXPECT_EQ(24, cb1()); |
73 cb1 = Bind(&BindTester::b, &t, 10); | 90 cb1 = Bind(&BindTester::b, &t, 10); |
74 EXPECT_EQ(100, cb1()); | 91 EXPECT_EQ(100, cb1()); |
75 EXPECT_EQ(100, cb1()); | 92 EXPECT_EQ(100, cb1()); |
76 cb1 = Bind(&BindTester::b, &t, 5); | 93 cb1 = Bind(&BindTester::b, &t, 5); |
77 EXPECT_EQ(25, cb1()); | 94 EXPECT_EQ(25, cb1()); |
78 EXPECT_EQ(25, cb1()); | 95 EXPECT_EQ(25, cb1()); |
79 } | 96 } |
80 | 97 |
98 TEST(KeepRefUntilDoneTest, simple) { | |
99 RefCountedBindTester t; | |
100 EXPECT_EQ(0, t.RefCount()); | |
101 { | |
102 Callback0<void> cb = KeepRefUntilDone(&t); | |
103 EXPECT_EQ(1, t.RefCount()); | |
104 cb(); | |
105 EXPECT_EQ(1, t.RefCount()); | |
106 cb(); | |
107 EXPECT_EQ(1, t.RefCount()); | |
108 } | |
109 EXPECT_EQ(0, t.RefCount()); | |
110 } | |
111 | |
112 TEST(KeepRefUntilDoneTest, copy) { | |
113 RefCountedBindTester t; | |
114 EXPECT_EQ(0, t.RefCount()); | |
115 Callback0<void> cb2; | |
116 { | |
117 Callback0<void> cb = KeepRefUntilDone(&t); | |
118 EXPECT_EQ(1, t.RefCount()); | |
119 cb2 = cb; | |
120 } | |
121 EXPECT_EQ(1, t.RefCount()); | |
122 cb2 = Callback0<void>(); | |
123 EXPECT_EQ(0, t.RefCount()); | |
124 } | |
125 | |
81 } // namespace rtc | 126 } // namespace rtc |
OLD | NEW |