1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 // Author: kenton@google.com (Kenton Varda)
9
10 #include "google/protobuf/stubs/common.h"
11
12 #include <gtest/gtest.h>
13
14 #include <vector>
15
16 #include "absl/log/absl_log.h"
17 #include "absl/strings/ascii.h"
18 #include "absl/strings/substitute.h"
19 #include "google/protobuf/stubs/callback.h"
20 #include "google/protobuf/testing/googletest.h"
21
22 namespace google {
23 namespace protobuf {
24 namespace {
25
26 // TODO: More tests.
27
28 #ifdef PACKAGE_VERSION // only defined when using automake, not MSVC
29
TEST(VersionTest,VersionMatchesConfig)30 TEST(VersionTest, VersionMatchesConfig) {
31 // Verify that the version string specified in config.h matches the one
32 // in common.h. The config.h version is a string which may have a suffix
33 // like "beta" or "rc1", so we remove that.
34 std::string version = PACKAGE_VERSION;
35 int pos = 0;
36 while (pos < version.size() &&
37 (absl::ascii_isdigit(version[pos]) || version[pos] == '.')) {
38 ++pos;
39 }
40 version.erase(pos);
41
42 EXPECT_EQ(version, internal::VersionString(GOOGLE_PROTOBUF_VERSION));
43 }
44
45 #endif // PACKAGE_VERSION
46
TEST(CommonTest,IntMinMaxConstants)47 TEST(CommonTest, IntMinMaxConstants) {
48 // kint32min was declared incorrectly in the first release of protobufs.
49 // Ugh.
50 EXPECT_LT(kint32min, kint32max);
51 EXPECT_EQ(static_cast<uint32>(kint32min), static_cast<uint32>(kint32max) + 1);
52 EXPECT_LT(kint64min, kint64max);
53 EXPECT_EQ(static_cast<uint64>(kint64min), static_cast<uint64>(kint64max) + 1);
54 EXPECT_EQ(0, kuint32max + 1);
55 EXPECT_EQ(0, kuint64max + 1);
56 }
57
58 class ClosureTest : public testing::Test {
59 public:
SetA123Method()60 void SetA123Method() { a_ = 123; }
SetA123Function()61 static void SetA123Function() { current_instance_->a_ = 123; }
62
SetAMethod(int a)63 void SetAMethod(int a) { a_ = a; }
SetCMethod(std::string c)64 void SetCMethod(std::string c) { c_ = c; }
65
SetAFunction(int a)66 static void SetAFunction(int a) { current_instance_->a_ = a; }
SetCFunction(std::string c)67 static void SetCFunction(std::string c) { current_instance_->c_ = c; }
68
SetABMethod(int a,const char * b)69 void SetABMethod(int a, const char* b) { a_ = a; b_ = b; }
SetABFunction(int a,const char * b)70 static void SetABFunction(int a, const char* b) {
71 current_instance_->a_ = a;
72 current_instance_->b_ = b;
73 }
74
SetUp()75 virtual void SetUp() {
76 current_instance_ = this;
77 a_ = 0;
78 b_ = nullptr;
79 c_.clear();
80 permanent_closure_ = nullptr;
81 }
82
DeleteClosureInCallback()83 void DeleteClosureInCallback() {
84 delete permanent_closure_;
85 }
86
87 int a_;
88 const char* b_;
89 std::string c_;
90 Closure* permanent_closure_;
91
92 static ClosureTest* current_instance_;
93 };
94
95 ClosureTest* ClosureTest::current_instance_ = nullptr;
96
TEST_F(ClosureTest,TestClosureFunction0)97 TEST_F(ClosureTest, TestClosureFunction0) {
98 Closure* closure = NewCallback(&SetA123Function);
99 EXPECT_NE(123, a_);
100 closure->Run();
101 EXPECT_EQ(123, a_);
102 }
103
TEST_F(ClosureTest,TestClosureMethod0)104 TEST_F(ClosureTest, TestClosureMethod0) {
105 Closure* closure = NewCallback(current_instance_,
106 &ClosureTest::SetA123Method);
107 EXPECT_NE(123, a_);
108 closure->Run();
109 EXPECT_EQ(123, a_);
110 }
111
TEST_F(ClosureTest,TestClosureFunction1)112 TEST_F(ClosureTest, TestClosureFunction1) {
113 Closure* closure = NewCallback(&SetAFunction, 456);
114 EXPECT_NE(456, a_);
115 closure->Run();
116 EXPECT_EQ(456, a_);
117 }
118
TEST_F(ClosureTest,TestClosureMethod1)119 TEST_F(ClosureTest, TestClosureMethod1) {
120 Closure* closure = NewCallback(current_instance_,
121 &ClosureTest::SetAMethod, 456);
122 EXPECT_NE(456, a_);
123 closure->Run();
124 EXPECT_EQ(456, a_);
125 }
126
TEST_F(ClosureTest,TestClosureFunction1String)127 TEST_F(ClosureTest, TestClosureFunction1String) {
128 Closure* closure = NewCallback(&SetCFunction, std::string("test"));
129 EXPECT_NE("test", c_);
130 closure->Run();
131 EXPECT_EQ("test", c_);
132 }
133
TEST_F(ClosureTest,TestClosureMethod1String)134 TEST_F(ClosureTest, TestClosureMethod1String) {
135 Closure* closure = NewCallback(current_instance_, &ClosureTest::SetCMethod,
136 std::string("test"));
137 EXPECT_NE("test", c_);
138 closure->Run();
139 EXPECT_EQ("test", c_);
140 }
141
TEST_F(ClosureTest,TestClosureFunction2)142 TEST_F(ClosureTest, TestClosureFunction2) {
143 const char* cstr = "hello";
144 Closure* closure = NewCallback(&SetABFunction, 789, cstr);
145 EXPECT_NE(789, a_);
146 EXPECT_NE(cstr, b_);
147 closure->Run();
148 EXPECT_EQ(789, a_);
149 EXPECT_EQ(cstr, b_);
150 }
151
TEST_F(ClosureTest,TestClosureMethod2)152 TEST_F(ClosureTest, TestClosureMethod2) {
153 const char* cstr = "hello";
154 Closure* closure = NewCallback(current_instance_,
155 &ClosureTest::SetABMethod, 789, cstr);
156 EXPECT_NE(789, a_);
157 EXPECT_NE(cstr, b_);
158 closure->Run();
159 EXPECT_EQ(789, a_);
160 EXPECT_EQ(cstr, b_);
161 }
162
163 // Repeat all of the above with NewPermanentCallback()
164
TEST_F(ClosureTest,TestPermanentClosureFunction0)165 TEST_F(ClosureTest, TestPermanentClosureFunction0) {
166 Closure* closure = NewPermanentCallback(&SetA123Function);
167 EXPECT_NE(123, a_);
168 closure->Run();
169 EXPECT_EQ(123, a_);
170 a_ = 0;
171 closure->Run();
172 EXPECT_EQ(123, a_);
173 delete closure;
174 }
175
TEST_F(ClosureTest,TestPermanentClosureMethod0)176 TEST_F(ClosureTest, TestPermanentClosureMethod0) {
177 Closure* closure = NewPermanentCallback(current_instance_,
178 &ClosureTest::SetA123Method);
179 EXPECT_NE(123, a_);
180 closure->Run();
181 EXPECT_EQ(123, a_);
182 a_ = 0;
183 closure->Run();
184 EXPECT_EQ(123, a_);
185 delete closure;
186 }
187
TEST_F(ClosureTest,TestPermanentClosureFunction1)188 TEST_F(ClosureTest, TestPermanentClosureFunction1) {
189 Closure* closure = NewPermanentCallback(&SetAFunction, 456);
190 EXPECT_NE(456, a_);
191 closure->Run();
192 EXPECT_EQ(456, a_);
193 a_ = 0;
194 closure->Run();
195 EXPECT_EQ(456, a_);
196 delete closure;
197 }
198
TEST_F(ClosureTest,TestPermanentClosureMethod1)199 TEST_F(ClosureTest, TestPermanentClosureMethod1) {
200 Closure* closure = NewPermanentCallback(current_instance_,
201 &ClosureTest::SetAMethod, 456);
202 EXPECT_NE(456, a_);
203 closure->Run();
204 EXPECT_EQ(456, a_);
205 a_ = 0;
206 closure->Run();
207 EXPECT_EQ(456, a_);
208 delete closure;
209 }
210
TEST_F(ClosureTest,TestPermanentClosureFunction2)211 TEST_F(ClosureTest, TestPermanentClosureFunction2) {
212 const char* cstr = "hello";
213 Closure* closure = NewPermanentCallback(&SetABFunction, 789, cstr);
214 EXPECT_NE(789, a_);
215 EXPECT_NE(cstr, b_);
216 closure->Run();
217 EXPECT_EQ(789, a_);
218 EXPECT_EQ(cstr, b_);
219 a_ = 0;
220 b_ = nullptr;
221 closure->Run();
222 EXPECT_EQ(789, a_);
223 EXPECT_EQ(cstr, b_);
224 delete closure;
225 }
226
TEST_F(ClosureTest,TestPermanentClosureMethod2)227 TEST_F(ClosureTest, TestPermanentClosureMethod2) {
228 const char* cstr = "hello";
229 Closure* closure = NewPermanentCallback(current_instance_,
230 &ClosureTest::SetABMethod, 789, cstr);
231 EXPECT_NE(789, a_);
232 EXPECT_NE(cstr, b_);
233 closure->Run();
234 EXPECT_EQ(789, a_);
235 EXPECT_EQ(cstr, b_);
236 a_ = 0;
237 b_ = nullptr;
238 closure->Run();
239 EXPECT_EQ(789, a_);
240 EXPECT_EQ(cstr, b_);
241 delete closure;
242 }
243
TEST_F(ClosureTest,TestPermanentClosureDeleteInCallback)244 TEST_F(ClosureTest, TestPermanentClosureDeleteInCallback) {
245 permanent_closure_ = NewPermanentCallback((ClosureTest*) this,
246 &ClosureTest::DeleteClosureInCallback);
247 permanent_closure_->Run();
248 }
249
250 } // anonymous namespace
251 } // namespace protobuf
252 } // namespace google
253