snappy 0.2.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -31,161 +31,91 @@
31
31
  #ifndef THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_TEST_H_
32
32
  #define THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_TEST_H_
33
33
 
34
- #include <iostream>
35
- #include <string>
34
+ #if HAVE_CONFIG_H
35
+ #include "config.h"
36
+ #endif
36
37
 
37
38
  #include "snappy-stubs-internal.h"
38
39
 
39
- #include <stdio.h>
40
- #include <stdarg.h>
41
-
42
- #ifdef HAVE_SYS_MMAN_H
40
+ #if HAVE_SYS_MMAN_H
43
41
  #include <sys/mman.h>
44
42
  #endif
45
43
 
46
- #ifdef HAVE_SYS_RESOURCE_H
44
+ #if HAVE_SYS_RESOURCE_H
47
45
  #include <sys/resource.h>
48
46
  #endif
49
47
 
50
- #ifdef HAVE_SYS_TIME_H
48
+ #if HAVE_SYS_TIME_H
51
49
  #include <sys/time.h>
52
50
  #endif
53
51
 
54
- #ifdef HAVE_WINDOWS_H
52
+ #if HAVE_WINDOWS_H
53
+ // Needed to be able to use std::max without workarounds in the source code.
54
+ // https://support.microsoft.com/en-us/help/143208/prb-using-stl-in-windows-program-can-cause-min-max-conflicts
55
+ #define NOMINMAX
55
56
  #include <windows.h>
56
57
  #endif
57
58
 
58
- #ifdef HAVE_GTEST
59
-
60
- #include <gtest/gtest.h>
61
- #undef TYPED_TEST
62
- #define TYPED_TEST TEST
63
- #define INIT_GTEST(argc, argv) ::testing::InitGoogleTest(argc, *argv)
64
-
65
- #else
66
-
67
- // Stubs for if the user doesn't have Google Test installed.
68
-
69
- #define TEST(test_case, test_subcase) \
70
- void Test_ ## test_case ## _ ## test_subcase()
71
- #define INIT_GTEST(argc, argv)
72
-
73
- #define TYPED_TEST TEST
74
- #define EXPECT_EQ CHECK_EQ
75
- #define EXPECT_NE CHECK_NE
76
- #define EXPECT_FALSE(cond) CHECK(!(cond))
77
-
78
- #endif
79
-
80
- #ifdef HAVE_GFLAGS
81
-
82
- #include <gflags/gflags.h>
83
-
84
- // This is tricky; both gflags and Google Test want to look at the command line
85
- // arguments. Google Test seems to be the most happy with unknown arguments,
86
- // though, so we call it first and hope for the best.
87
- #define InitGoogle(argv0, argc, argv, remove_flags) \
88
- INIT_GTEST(argc, argv); \
89
- google::ParseCommandLineFlags(argc, argv, remove_flags);
90
-
91
- #else
59
+ #define InitGoogle(argv0, argc, argv, remove_flags) ((void)(0))
92
60
 
93
- // If we don't have the gflags package installed, these can only be
94
- // changed at compile time.
95
- #define DEFINE_int32(flag_name, default_value, description) \
96
- static int FLAGS_ ## flag_name = default_value;
97
-
98
- #define InitGoogle(argv0, argc, argv, remove_flags) \
99
- INIT_GTEST(argc, argv)
100
-
101
- #endif
102
-
103
- #ifdef HAVE_LIBZ
61
+ #if HAVE_LIBZ
104
62
  #include "zlib.h"
105
63
  #endif
106
64
 
107
- #ifdef HAVE_LIBLZO2
65
+ #if HAVE_LIBLZO2
108
66
  #include "lzo/lzo1x.h"
109
67
  #endif
110
68
 
111
- namespace {
69
+ #if HAVE_LIBLZ4
70
+ #include "lz4.h"
71
+ #endif
112
72
 
113
73
  namespace file {
114
- int Defaults() { return 0; }
115
-
116
- class DummyStatus {
117
- public:
118
- void CheckSuccess() { }
119
- };
120
-
121
- DummyStatus GetContents(
122
- const std::string& filename, std::string* data, int unused) {
123
- FILE* fp = fopen(filename.c_str(), "rb");
124
- if (fp == NULL) {
125
- perror(filename.c_str());
126
- exit(1);
127
- }
128
-
129
- data->clear();
130
- while (!feof(fp)) {
131
- char buf[4096];
132
- size_t ret = fread(buf, 1, 4096, fp);
133
- if (ret == 0 && ferror(fp)) {
134
- perror("fread");
135
- exit(1);
136
- }
137
- data->append(std::string(buf, ret));
138
- }
139
-
140
- fclose(fp);
141
-
142
- return DummyStatus();
143
- }
144
74
 
145
- inline DummyStatus SetContents(
146
- const std::string& filename, const std::string& str, int unused) {
147
- FILE* fp = fopen(filename.c_str(), "wb");
148
- if (fp == NULL) {
149
- perror(filename.c_str());
150
- exit(1);
151
- }
75
+ // Stubs the class file::Options.
76
+ //
77
+ // This class should not be instantiated explicitly. It should only be used by
78
+ // passing file::Defaults() to file::GetContents() / file::SetContents().
79
+ class OptionsStub {
80
+ public:
81
+ OptionsStub();
82
+ OptionsStub(const OptionsStub &) = delete;
83
+ OptionsStub &operator=(const OptionsStub &) = delete;
84
+ ~OptionsStub();
85
+ };
152
86
 
153
- int ret = fwrite(str.data(), str.size(), 1, fp);
154
- if (ret != 1) {
155
- perror("fwrite");
156
- exit(1);
157
- }
87
+ const OptionsStub &Defaults();
158
88
 
159
- fclose(fp);
89
+ // Stubs the class absl::Status.
90
+ //
91
+ // This class should not be instantiated explicitly. It should only be used by
92
+ // passing the result of file::GetContents() / file::SetContents() to
93
+ // CHECK_OK().
94
+ class StatusStub {
95
+ public:
96
+ StatusStub();
97
+ StatusStub(const StatusStub &);
98
+ StatusStub &operator=(const StatusStub &);
99
+ ~StatusStub();
160
100
 
161
- return DummyStatus();
162
- }
163
- } // namespace file
101
+ bool ok();
102
+ };
164
103
 
165
- } // namespace
104
+ StatusStub GetContents(const std::string &file_name, std::string *output,
105
+ const OptionsStub & /* options */);
106
+
107
+ StatusStub SetContents(const std::string &file_name, const std::string &content,
108
+ const OptionsStub & /* options */);
109
+
110
+ } // namespace file
166
111
 
167
112
  namespace snappy {
168
113
 
169
114
  #define FLAGS_test_random_seed 301
170
- using TypeParam = std::string;
171
-
172
- void Test_CorruptedTest_VerifyCorrupted();
173
- void Test_Snappy_SimpleTests();
174
- void Test_Snappy_MaxBlowup();
175
- void Test_Snappy_RandomData();
176
- void Test_Snappy_FourByteOffset();
177
- void Test_SnappyCorruption_TruncatedVarint();
178
- void Test_SnappyCorruption_UnterminatedVarint();
179
- void Test_SnappyCorruption_OverflowingVarint();
180
- void Test_Snappy_ReadPastEndOfBuffer();
181
- void Test_Snappy_FindMatchLength();
182
- void Test_Snappy_FindMatchLengthRandom();
183
115
 
184
116
  std::string ReadTestDataFile(const std::string& base, size_t size_limit);
185
117
 
186
- std::string ReadTestDataFile(const std::string& base);
187
-
188
- // A sprintf() variant that returns a std::string.
118
+ // A std::sprintf() variant that returns a std::string.
189
119
  // Not safe for general use due to truncation issues.
190
120
  std::string StrFormat(const char* format, ...);
191
121
 
@@ -193,17 +123,18 @@ std::string StrFormat(const char* format, ...);
193
123
  // system time changing.
194
124
  class CycleTimer {
195
125
  public:
196
- CycleTimer() : real_time_us_(0) {}
126
+ inline CycleTimer() : real_time_us_(0) {}
127
+ inline ~CycleTimer() = default;
197
128
 
198
- void Start() {
129
+ inline void Start() {
199
130
  #ifdef WIN32
200
131
  QueryPerformanceCounter(&start_);
201
132
  #else
202
- gettimeofday(&start_, NULL);
133
+ ::gettimeofday(&start_, nullptr);
203
134
  #endif
204
135
  }
205
136
 
206
- void Stop() {
137
+ inline void Stop() {
207
138
  #ifdef WIN32
208
139
  LARGE_INTEGER stop;
209
140
  LARGE_INTEGER frequency;
@@ -214,67 +145,78 @@ class CycleTimer {
214
145
  frequency.QuadPart;
215
146
  real_time_us_ += elapsed * 1e6 + 0.5;
216
147
  #else
217
- struct timeval stop;
218
- gettimeofday(&stop, NULL);
148
+ struct ::timeval stop;
149
+ ::gettimeofday(&stop, nullptr);
219
150
 
220
151
  real_time_us_ += 1000000 * (stop.tv_sec - start_.tv_sec);
221
152
  real_time_us_ += (stop.tv_usec - start_.tv_usec);
222
153
  #endif
223
154
  }
224
155
 
225
- double Get() {
226
- return real_time_us_ * 1e-6;
227
- }
156
+ inline double Get() { return real_time_us_ * 1e-6; }
228
157
 
229
158
  private:
230
- int64 real_time_us_;
159
+ int64_t real_time_us_;
231
160
  #ifdef WIN32
232
161
  LARGE_INTEGER start_;
233
162
  #else
234
- struct timeval start_;
163
+ struct ::timeval start_;
235
164
  #endif
236
165
  };
237
166
 
238
- // Minimalistic microbenchmark framework.
167
+ // Logging.
168
+
169
+ class LogMessage {
170
+ public:
171
+ inline LogMessage() = default;
172
+ ~LogMessage();
239
173
 
240
- typedef void (*BenchmarkFunction)(int, int);
174
+ LogMessage &operator<<(const std::string &message);
175
+ LogMessage &operator<<(int number);
176
+ };
241
177
 
242
- class Benchmark {
178
+ class LogMessageCrash : public LogMessage {
243
179
  public:
244
- Benchmark(const std::string& name, BenchmarkFunction function)
245
- : name_(name), function_(function) {}
180
+ inline LogMessageCrash() = default;
181
+ ~LogMessageCrash();
182
+ };
246
183
 
247
- Benchmark* DenseRange(int start, int stop) {
248
- start_ = start;
249
- stop_ = stop;
250
- return this;
251
- }
184
+ // This class is used to explicitly ignore values in the conditional
185
+ // logging macros. This avoids compiler warnings like "value computed
186
+ // is not used" and "statement has no effect".
252
187
 
253
- void Run();
188
+ class LogMessageVoidify {
189
+ public:
190
+ inline LogMessageVoidify() = default;
191
+ inline ~LogMessageVoidify() = default;
254
192
 
255
- private:
256
- const std::string name_;
257
- const BenchmarkFunction function_;
258
- int start_, stop_;
193
+ // This has to be an operator with a precedence lower than << but
194
+ // higher than ?:
195
+ inline void operator&(const LogMessage &) {}
259
196
  };
260
- #define BENCHMARK(benchmark_name) \
261
- Benchmark* Benchmark_ ## benchmark_name = \
262
- (new Benchmark(#benchmark_name, benchmark_name))
263
197
 
264
- extern Benchmark* Benchmark_BM_UFlat;
265
- extern Benchmark* Benchmark_BM_UIOVec;
266
- extern Benchmark* Benchmark_BM_UValidate;
267
- extern Benchmark* Benchmark_BM_ZFlat;
268
- extern Benchmark* Benchmark_BM_ZFlatAll;
269
- extern Benchmark* Benchmark_BM_ZFlatIncreasingTableSize;
198
+ // Asserts, both versions activated in debug mode only,
199
+ // and ones that are always active.
270
200
 
271
- void ResetBenchmarkTiming();
272
- void StartBenchmarkTiming();
273
- void StopBenchmarkTiming();
274
- void SetBenchmarkLabel(const std::string& str);
275
- void SetBenchmarkBytesProcessed(int64 bytes);
201
+ #define CRASH_UNLESS(condition) \
202
+ SNAPPY_PREDICT_TRUE(condition) \
203
+ ? (void)0 \
204
+ : snappy::LogMessageVoidify() & snappy::LogMessageCrash()
205
+
206
+ #define LOG(level) LogMessage()
207
+ #define VLOG(level) \
208
+ true ? (void)0 : snappy::LogMessageVoidify() & snappy::LogMessage()
209
+
210
+ #define CHECK(cond) CRASH_UNLESS(cond)
211
+ #define CHECK_LE(a, b) CRASH_UNLESS((a) <= (b))
212
+ #define CHECK_GE(a, b) CRASH_UNLESS((a) >= (b))
213
+ #define CHECK_EQ(a, b) CRASH_UNLESS((a) == (b))
214
+ #define CHECK_NE(a, b) CRASH_UNLESS((a) != (b))
215
+ #define CHECK_LT(a, b) CRASH_UNLESS((a) < (b))
216
+ #define CHECK_GT(a, b) CRASH_UNLESS((a) > (b))
217
+ #define CHECK_OK(cond) (cond).ok()
276
218
 
277
- #ifdef HAVE_LIBZ
219
+ #if HAVE_LIBZ
278
220
 
279
221
  // Object-oriented wrapper around zlib.
280
222
  class ZLib {
@@ -397,129 +339,4 @@ class ZLib {
397
339
 
398
340
  } // namespace snappy
399
341
 
400
- DECLARE_bool(run_microbenchmarks);
401
-
402
- static inline void RunSpecifiedBenchmarks() {
403
- if (!FLAGS_run_microbenchmarks) {
404
- return;
405
- }
406
-
407
- fprintf(stderr, "Running microbenchmarks.\n");
408
- #ifndef NDEBUG
409
- fprintf(stderr, "WARNING: Compiled with assertions enabled, will be slow.\n");
410
- #endif
411
- #ifndef __OPTIMIZE__
412
- fprintf(stderr, "WARNING: Compiled without optimization, will be slow.\n");
413
- #endif
414
- fprintf(stderr, "Benchmark Time(ns) CPU(ns) Iterations\n");
415
- fprintf(stderr, "---------------------------------------------------\n");
416
-
417
- snappy::Benchmark_BM_UFlat->Run();
418
- snappy::Benchmark_BM_UIOVec->Run();
419
- snappy::Benchmark_BM_UValidate->Run();
420
- snappy::Benchmark_BM_ZFlat->Run();
421
- snappy::Benchmark_BM_ZFlatAll->Run();
422
- snappy::Benchmark_BM_ZFlatIncreasingTableSize->Run();
423
-
424
- fprintf(stderr, "\n");
425
- }
426
-
427
- #ifndef HAVE_GTEST
428
-
429
- static inline int RUN_ALL_TESTS() {
430
- fprintf(stderr, "Running correctness tests.\n");
431
- snappy::Test_CorruptedTest_VerifyCorrupted();
432
- snappy::Test_Snappy_SimpleTests();
433
- snappy::Test_Snappy_MaxBlowup();
434
- snappy::Test_Snappy_RandomData();
435
- snappy::Test_Snappy_FourByteOffset();
436
- snappy::Test_SnappyCorruption_TruncatedVarint();
437
- snappy::Test_SnappyCorruption_UnterminatedVarint();
438
- snappy::Test_SnappyCorruption_OverflowingVarint();
439
- snappy::Test_Snappy_ReadPastEndOfBuffer();
440
- snappy::Test_Snappy_FindMatchLength();
441
- snappy::Test_Snappy_FindMatchLengthRandom();
442
- fprintf(stderr, "All tests passed.\n");
443
-
444
- return 0;
445
- }
446
-
447
- #endif // HAVE_GTEST
448
-
449
- // For main().
450
- namespace snappy {
451
-
452
- // Logging.
453
-
454
- #define LOG(level) LogMessage()
455
- #define VLOG(level) true ? (void)0 : \
456
- snappy::LogMessageVoidify() & snappy::LogMessage()
457
-
458
- class LogMessage {
459
- public:
460
- LogMessage() { }
461
- ~LogMessage() {
462
- std::cerr << std::endl;
463
- }
464
-
465
- LogMessage& operator<<(const std::string& msg) {
466
- std::cerr << msg;
467
- return *this;
468
- }
469
- LogMessage& operator<<(int x) {
470
- std::cerr << x;
471
- return *this;
472
- }
473
- };
474
-
475
- // Asserts, both versions activated in debug mode only,
476
- // and ones that are always active.
477
-
478
- #define CRASH_UNLESS(condition) \
479
- SNAPPY_PREDICT_TRUE(condition) ? (void)0 : \
480
- snappy::LogMessageVoidify() & snappy::LogMessageCrash()
481
-
482
- #ifdef _MSC_VER
483
- // ~LogMessageCrash calls abort() and therefore never exits. This is by design
484
- // so temporarily disable warning C4722.
485
- #pragma warning(push)
486
- #pragma warning(disable:4722)
487
- #endif
488
-
489
- class LogMessageCrash : public LogMessage {
490
- public:
491
- LogMessageCrash() { }
492
- ~LogMessageCrash() {
493
- std::cerr << std::endl;
494
- abort();
495
- }
496
- };
497
-
498
- #ifdef _MSC_VER
499
- #pragma warning(pop)
500
- #endif
501
-
502
- // This class is used to explicitly ignore values in the conditional
503
- // logging macros. This avoids compiler warnings like "value computed
504
- // is not used" and "statement has no effect".
505
-
506
- class LogMessageVoidify {
507
- public:
508
- LogMessageVoidify() { }
509
- // This has to be an operator with a precedence lower than << but
510
- // higher than ?:
511
- void operator&(const LogMessage&) { }
512
- };
513
-
514
- #define CHECK(cond) CRASH_UNLESS(cond)
515
- #define CHECK_LE(a, b) CRASH_UNLESS((a) <= (b))
516
- #define CHECK_GE(a, b) CRASH_UNLESS((a) >= (b))
517
- #define CHECK_EQ(a, b) CRASH_UNLESS((a) == (b))
518
- #define CHECK_NE(a, b) CRASH_UNLESS((a) != (b))
519
- #define CHECK_LT(a, b) CRASH_UNLESS((a) < (b))
520
- #define CHECK_GT(a, b) CRASH_UNLESS((a) > (b))
521
- #define CHECK_OK(cond) (cond).CheckSuccess()
522
-
523
- } // namespace snappy
524
-
525
342
  #endif // THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_TEST_H_