~ubuntu-branches/ubuntu/oneiric/protobuf/oneiric

« back to all changes in this revision

Viewing changes to src/google/protobuf/stubs/common_unittest.cc

  • Committer: Bazaar Package Importer
  • Author(s): Iustin Pop
  • Date: 2008-08-03 11:01:44 UTC
  • Revision ID: james.westby@ubuntu.com-20080803110144-uyiw41bf1m2oe17t
Tags: upstream-2.0.0~b
ImportĀ upstreamĀ versionĀ 2.0.0~b

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Protocol Buffers - Google's data interchange format
 
2
// Copyright 2008 Google Inc.
 
3
// http://code.google.com/p/protobuf/
 
4
//
 
5
// Licensed under the Apache License, Version 2.0 (the "License");
 
6
// you may not use this file except in compliance with the License.
 
7
// 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, software
 
12
// distributed under the License is distributed on an "AS IS" BASIS,
 
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
14
// See the License for the specific language governing permissions and
 
15
// limitations under the License.
 
16
 
 
17
// Author: kenton@google.com (Kenton Varda)
 
18
 
 
19
#include <vector>
 
20
#include <google/protobuf/stubs/common.h>
 
21
#include <google/protobuf/stubs/strutil.h>
 
22
#include <google/protobuf/stubs/substitute.h>
 
23
 
 
24
#include <google/protobuf/testing/googletest.h>
 
25
#include <gtest/gtest.h>
 
26
 
 
27
#include "config.h"
 
28
 
 
29
namespace google {
 
30
namespace protobuf {
 
31
namespace {
 
32
 
 
33
// TODO(kenton):  More tests.
 
34
 
 
35
#ifdef PACKAGE_VERSION  // only defined when using automake, not MSVC
 
36
 
 
37
TEST(VersionTest, VersionMatchesConfig) {
 
38
  // Verify that the version string specified in config.h matches the one
 
39
  // in common.h.  The config.h version is a string which may have a suffix
 
40
  // like "beta", so we remove that.
 
41
  string version = PACKAGE_VERSION;
 
42
  int pos = version.size();
 
43
  while (pos > 0 && !ascii_isdigit(version[pos-1])) {
 
44
    --pos;
 
45
  }
 
46
  version.erase(pos);
 
47
 
 
48
  EXPECT_EQ(version, internal::VersionString(GOOGLE_PROTOBUF_VERSION));
 
49
}
 
50
 
 
51
#endif  // PACKAGE_VERSION
 
52
 
 
53
vector<string> captured_messages_;
 
54
 
 
55
void CaptureLog(LogLevel level, const char* filename, int line,
 
56
                const string& message) {
 
57
  captured_messages_.push_back(
 
58
    strings::Substitute("$0 $1:$2: $3",
 
59
      implicit_cast<int>(level), filename, line, message));
 
60
}
 
61
 
 
62
TEST(LoggingTest, DefaultLogging) {
 
63
  CaptureTestStderr();
 
64
  int line = __LINE__;
 
65
  GOOGLE_LOG(INFO   ) << "A message.";
 
66
  GOOGLE_LOG(WARNING) << "A warning.";
 
67
  GOOGLE_LOG(ERROR  ) << "An error.";
 
68
 
 
69
  string text = GetCapturedTestStderr();
 
70
  EXPECT_EQ(
 
71
    "libprotobuf INFO "__FILE__":" + SimpleItoa(line + 1) + "] A message.\n"
 
72
    "libprotobuf WARNING "__FILE__":" + SimpleItoa(line + 2) + "] A warning.\n"
 
73
    "libprotobuf ERROR "__FILE__":" + SimpleItoa(line + 3) + "] An error.\n",
 
74
    text);
 
75
}
 
76
 
 
77
TEST(LoggingTest, NullLogging) {
 
78
  LogHandler* old_handler = SetLogHandler(NULL);
 
79
 
 
80
  CaptureTestStderr();
 
81
  GOOGLE_LOG(INFO   ) << "A message.";
 
82
  GOOGLE_LOG(WARNING) << "A warning.";
 
83
  GOOGLE_LOG(ERROR  ) << "An error.";
 
84
 
 
85
  EXPECT_TRUE(SetLogHandler(old_handler) == NULL);
 
86
 
 
87
  string text = GetCapturedTestStderr();
 
88
  EXPECT_EQ("", text);
 
89
}
 
90
 
 
91
TEST(LoggingTest, CaptureLogging) {
 
92
  captured_messages_.clear();
 
93
 
 
94
  LogHandler* old_handler = SetLogHandler(&CaptureLog);
 
95
 
 
96
  int start_line = __LINE__;
 
97
  GOOGLE_LOG(ERROR) << "An error.";
 
98
  GOOGLE_LOG(WARNING) << "A warning.";
 
99
 
 
100
  EXPECT_TRUE(SetLogHandler(old_handler) == &CaptureLog);
 
101
 
 
102
  ASSERT_EQ(2, captured_messages_.size());
 
103
  EXPECT_EQ(
 
104
    "2 "__FILE__":" + SimpleItoa(start_line + 1) + ": An error.",
 
105
    captured_messages_[0]);
 
106
  EXPECT_EQ(
 
107
    "1 "__FILE__":" + SimpleItoa(start_line + 2) + ": A warning.",
 
108
    captured_messages_[1]);
 
109
}
 
110
 
 
111
TEST(LoggingTest, SilenceLogging) {
 
112
  captured_messages_.clear();
 
113
 
 
114
  LogHandler* old_handler = SetLogHandler(&CaptureLog);
 
115
 
 
116
  int line1 = __LINE__; GOOGLE_LOG(INFO) << "Visible1";
 
117
  LogSilencer* silencer1 = new LogSilencer;
 
118
  GOOGLE_LOG(INFO) << "Not visible.";
 
119
  LogSilencer* silencer2 = new LogSilencer;
 
120
  GOOGLE_LOG(INFO) << "Not visible.";
 
121
  delete silencer1;
 
122
  GOOGLE_LOG(INFO) << "Not visible.";
 
123
  delete silencer2;
 
124
  int line2 = __LINE__; GOOGLE_LOG(INFO) << "Visible2";
 
125
 
 
126
  EXPECT_TRUE(SetLogHandler(old_handler) == &CaptureLog);
 
127
 
 
128
  ASSERT_EQ(2, captured_messages_.size());
 
129
  EXPECT_EQ(
 
130
    "0 "__FILE__":" + SimpleItoa(line1) + ": Visible1",
 
131
    captured_messages_[0]);
 
132
  EXPECT_EQ(
 
133
    "0 "__FILE__":" + SimpleItoa(line2) + ": Visible2",
 
134
    captured_messages_[1]);
 
135
}
 
136
 
 
137
class ClosureTest : public testing::Test {
 
138
 public:
 
139
  void SetA123Method()   { a_ = 123; }
 
140
  static void SetA123Function() { current_instance_->a_ = 123; }
 
141
 
 
142
  void SetAMethod(int a)         { a_ = a; }
 
143
  void SetCMethod(string c)      { c_ = c; }
 
144
 
 
145
  static void SetAFunction(int a)         { current_instance_->a_ = a; }
 
146
  static void SetCFunction(string c)      { current_instance_->c_ = c; }
 
147
 
 
148
  void SetABMethod(int a, const char* b)  { a_ = a; b_ = b; }
 
149
  static void SetABFunction(int a, const char* b) {
 
150
    current_instance_->a_ = a;
 
151
    current_instance_->b_ = b;
 
152
  }
 
153
 
 
154
  virtual void SetUp() {
 
155
    current_instance_ = this;
 
156
    a_ = 0;
 
157
    b_ = NULL;
 
158
    c_.clear();
 
159
  }
 
160
 
 
161
  int a_;
 
162
  const char* b_;
 
163
  string c_;
 
164
 
 
165
  static ClosureTest* current_instance_;
 
166
};
 
167
 
 
168
ClosureTest* ClosureTest::current_instance_ = NULL;
 
169
 
 
170
TEST_F(ClosureTest, TestClosureFunction0) {
 
171
  Closure* closure = NewCallback(&SetA123Function);
 
172
  EXPECT_NE(123, a_);
 
173
  closure->Run();
 
174
  EXPECT_EQ(123, a_);
 
175
}
 
176
 
 
177
TEST_F(ClosureTest, TestClosureMethod0) {
 
178
  Closure* closure = NewCallback(current_instance_,
 
179
                                 &ClosureTest::SetA123Method);
 
180
  EXPECT_NE(123, a_);
 
181
  closure->Run();
 
182
  EXPECT_EQ(123, a_);
 
183
}
 
184
 
 
185
TEST_F(ClosureTest, TestClosureFunction1) {
 
186
  Closure* closure = NewCallback(&SetAFunction, 456);
 
187
  EXPECT_NE(456, a_);
 
188
  closure->Run();
 
189
  EXPECT_EQ(456, a_);
 
190
}
 
191
 
 
192
TEST_F(ClosureTest, TestClosureMethod1) {
 
193
  Closure* closure = NewCallback(current_instance_,
 
194
                                 &ClosureTest::SetAMethod, 456);
 
195
  EXPECT_NE(456, a_);
 
196
  closure->Run();
 
197
  EXPECT_EQ(456, a_);
 
198
}
 
199
 
 
200
TEST_F(ClosureTest, TestClosureFunction1String) {
 
201
  Closure* closure = NewCallback(&SetCFunction, string("test"));
 
202
  EXPECT_NE("test", c_);
 
203
  closure->Run();
 
204
  EXPECT_EQ("test", c_);
 
205
}
 
206
 
 
207
TEST_F(ClosureTest, TestClosureMethod1String) {
 
208
  Closure* closure = NewCallback(current_instance_,
 
209
                                 &ClosureTest::SetCMethod, string("test"));
 
210
  EXPECT_NE("test", c_);
 
211
  closure->Run();
 
212
  EXPECT_EQ("test", c_);
 
213
}
 
214
 
 
215
TEST_F(ClosureTest, TestClosureFunction2) {
 
216
  const char* cstr = "hello";
 
217
  Closure* closure = NewCallback(&SetABFunction, 789, cstr);
 
218
  EXPECT_NE(789, a_);
 
219
  EXPECT_NE(cstr, b_);
 
220
  closure->Run();
 
221
  EXPECT_EQ(789, a_);
 
222
  EXPECT_EQ(cstr, b_);
 
223
}
 
224
 
 
225
TEST_F(ClosureTest, TestClosureMethod2) {
 
226
  const char* cstr = "hello";
 
227
  Closure* closure = NewCallback(current_instance_,
 
228
                                 &ClosureTest::SetABMethod, 789, cstr);
 
229
  EXPECT_NE(789, a_);
 
230
  EXPECT_NE(cstr, b_);
 
231
  closure->Run();
 
232
  EXPECT_EQ(789, a_);
 
233
  EXPECT_EQ(cstr, b_);
 
234
}
 
235
 
 
236
// Repeat all of the above with NewPermanentCallback()
 
237
 
 
238
TEST_F(ClosureTest, TestPermanentClosureFunction0) {
 
239
  Closure* closure = NewPermanentCallback(&SetA123Function);
 
240
  EXPECT_NE(123, a_);
 
241
  closure->Run();
 
242
  EXPECT_EQ(123, a_);
 
243
  a_ = 0;
 
244
  closure->Run();
 
245
  EXPECT_EQ(123, a_);
 
246
  delete closure;
 
247
}
 
248
 
 
249
TEST_F(ClosureTest, TestPermanentClosureMethod0) {
 
250
  Closure* closure = NewPermanentCallback(current_instance_,
 
251
                                          &ClosureTest::SetA123Method);
 
252
  EXPECT_NE(123, a_);
 
253
  closure->Run();
 
254
  EXPECT_EQ(123, a_);
 
255
  a_ = 0;
 
256
  closure->Run();
 
257
  EXPECT_EQ(123, a_);
 
258
  delete closure;
 
259
}
 
260
 
 
261
TEST_F(ClosureTest, TestPermanentClosureFunction1) {
 
262
  Closure* closure = NewPermanentCallback(&SetAFunction, 456);
 
263
  EXPECT_NE(456, a_);
 
264
  closure->Run();
 
265
  EXPECT_EQ(456, a_);
 
266
  a_ = 0;
 
267
  closure->Run();
 
268
  EXPECT_EQ(456, a_);
 
269
  delete closure;
 
270
}
 
271
 
 
272
TEST_F(ClosureTest, TestPermanentClosureMethod1) {
 
273
  Closure* closure = NewPermanentCallback(current_instance_,
 
274
                                          &ClosureTest::SetAMethod, 456);
 
275
  EXPECT_NE(456, a_);
 
276
  closure->Run();
 
277
  EXPECT_EQ(456, a_);
 
278
  a_ = 0;
 
279
  closure->Run();
 
280
  EXPECT_EQ(456, a_);
 
281
  delete closure;
 
282
}
 
283
 
 
284
TEST_F(ClosureTest, TestPermanentClosureFunction2) {
 
285
  const char* cstr = "hello";
 
286
  Closure* closure = NewPermanentCallback(&SetABFunction, 789, cstr);
 
287
  EXPECT_NE(789, a_);
 
288
  EXPECT_NE(cstr, b_);
 
289
  closure->Run();
 
290
  EXPECT_EQ(789, a_);
 
291
  EXPECT_EQ(cstr, b_);
 
292
  a_ = 0;
 
293
  b_ = NULL;
 
294
  closure->Run();
 
295
  EXPECT_EQ(789, a_);
 
296
  EXPECT_EQ(cstr, b_);
 
297
  delete closure;
 
298
}
 
299
 
 
300
TEST_F(ClosureTest, TestPermanentClosureMethod2) {
 
301
  const char* cstr = "hello";
 
302
  Closure* closure = NewPermanentCallback(current_instance_,
 
303
                                          &ClosureTest::SetABMethod, 789, cstr);
 
304
  EXPECT_NE(789, a_);
 
305
  EXPECT_NE(cstr, b_);
 
306
  closure->Run();
 
307
  EXPECT_EQ(789, a_);
 
308
  EXPECT_EQ(cstr, b_);
 
309
  a_ = 0;
 
310
  b_ = NULL;
 
311
  closure->Run();
 
312
  EXPECT_EQ(789, a_);
 
313
  EXPECT_EQ(cstr, b_);
 
314
  delete closure;
 
315
}
 
316
 
 
317
}  // anonymous namespace
 
318
}  // namespace protobuf
 
319
}  // namespace google