/Users/deen/code/yugabyte-db/src/yb/util/callback_bind-test.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | |
33 | | #include <glog/logging.h> |
34 | | #include <gtest/gtest.h> |
35 | | |
36 | | #include "yb/gutil/bind.h" |
37 | | #include "yb/gutil/callback.h" |
38 | | #include "yb/gutil/macros.h" |
39 | | |
40 | | namespace yb { |
41 | | |
42 | | using std::string; |
43 | | |
44 | 1 | static int Return5() { |
45 | 1 | return 5; |
46 | 1 | } |
47 | | |
48 | 1 | TEST(CallbackBindTest, TestFreeFunction) { |
49 | 1 | Callback<int(void)> func_cb = Bind(&Return5); |
50 | 1 | ASSERT_EQ(5, func_cb.Run()); |
51 | 1 | } |
52 | | |
53 | | class Ref : public RefCountedThreadSafe<Ref> { |
54 | | public: |
55 | 1 | int Foo() { return 3; } |
56 | | }; |
57 | | |
58 | | // Simple class that helps with verifying ref counting. |
59 | | // Not thread-safe. |
60 | | struct RefCountable { |
61 | | RefCountable() |
62 | 1 | : refs(0) { |
63 | 1 | } |
64 | 1 | void AddRef() const { |
65 | 1 | refs++; |
66 | 1 | } |
67 | 1 | void Release() const { |
68 | 1 | refs--; |
69 | 1 | } |
70 | 1 | void Print() const { |
71 | 1 | LOG(INFO) << "Hello. Refs: " << refs; |
72 | 1 | } |
73 | | |
74 | | mutable int refs; |
75 | | |
76 | | private: |
77 | | DISALLOW_COPY_AND_ASSIGN(RefCountable); |
78 | | }; |
79 | | |
80 | 1 | TEST(CallbackBindTest, TestClassMethod) { |
81 | 1 | scoped_refptr<Ref> ref = new Ref(); |
82 | 1 | Callback<int(void)> ref_cb = Bind(&Ref::Foo, ref); |
83 | 1 | ref = nullptr; |
84 | 1 | ASSERT_EQ(3, ref_cb.Run()); |
85 | 1 | } |
86 | | |
87 | 1 | int ReturnI(int i, const char* str) { |
88 | 1 | return i; |
89 | 1 | } |
90 | | |
91 | 1 | TEST(CallbackBindTest, TestPartialBind) { |
92 | 1 | Callback<int(const char*)> cb = Bind(&ReturnI, 23); |
93 | 1 | ASSERT_EQ(23, cb.Run("hello world")); |
94 | 1 | } |
95 | | |
96 | 0 | char IncrementChar(std::unique_ptr<char> in) { |
97 | 0 | return *in + 1; |
98 | 0 | } |
99 | | |
100 | | // Test that the ref counting functionality works. |
101 | 1 | TEST(CallbackBindTest, TestRefCounting) { |
102 | 1 | RefCountable countable; |
103 | 1 | { |
104 | 1 | ASSERT_EQ(0, countable.refs); |
105 | 1 | Closure cb = Bind(&RefCountable::Print, &countable); |
106 | 1 | ASSERT_EQ(1, countable.refs); |
107 | 1 | cb.Run(); |
108 | 1 | ASSERT_EQ(1, countable.refs); |
109 | 1 | } |
110 | 1 | ASSERT_EQ(0, countable.refs); |
111 | 1 | } |
112 | | |
113 | | } // namespace yb |