Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
michael@0 | 2 | // |
michael@0 | 3 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 4 | // modification, are permitted provided that the following conditions are |
michael@0 | 5 | // met: |
michael@0 | 6 | // |
michael@0 | 7 | // * Redistributions of source code must retain the above copyright |
michael@0 | 8 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 9 | // * Redistributions in binary form must reproduce the above |
michael@0 | 10 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 11 | // in the documentation and/or other materials provided with the |
michael@0 | 12 | // distribution. |
michael@0 | 13 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 14 | // contributors may be used to endorse or promote products derived from |
michael@0 | 15 | // this software without specific prior written permission. |
michael@0 | 16 | // |
michael@0 | 17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 18 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 19 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 20 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 21 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 22 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 23 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 28 | // |
michael@0 | 29 | // Various stubs for the unit tests for the open-source version of Snappy. |
michael@0 | 30 | |
michael@0 | 31 | #ifndef UTIL_SNAPPY_OPENSOURCE_SNAPPY_TEST_H_ |
michael@0 | 32 | #define UTIL_SNAPPY_OPENSOURCE_SNAPPY_TEST_H_ |
michael@0 | 33 | |
michael@0 | 34 | #include "snappy-stubs-internal.h" |
michael@0 | 35 | |
michael@0 | 36 | #include <stdio.h> |
michael@0 | 37 | #include <stdarg.h> |
michael@0 | 38 | |
michael@0 | 39 | #ifdef HAVE_SYS_MMAN_H |
michael@0 | 40 | #include <sys/mman.h> |
michael@0 | 41 | #endif |
michael@0 | 42 | |
michael@0 | 43 | #ifdef HAVE_SYS_RESOURCE_H |
michael@0 | 44 | #include <sys/resource.h> |
michael@0 | 45 | #endif |
michael@0 | 46 | |
michael@0 | 47 | #include <sys/time.h> |
michael@0 | 48 | |
michael@0 | 49 | #ifdef HAVE_WINDOWS_H |
michael@0 | 50 | #define WIN32_LEAN_AND_MEAN |
michael@0 | 51 | #include <windows.h> |
michael@0 | 52 | #endif |
michael@0 | 53 | |
michael@0 | 54 | #include <string> |
michael@0 | 55 | |
michael@0 | 56 | #ifdef HAVE_GTEST |
michael@0 | 57 | |
michael@0 | 58 | #include <gtest/gtest.h> |
michael@0 | 59 | #undef TYPED_TEST |
michael@0 | 60 | #define TYPED_TEST TEST |
michael@0 | 61 | #define INIT_GTEST(argc, argv) ::testing::InitGoogleTest(argc, *argv) |
michael@0 | 62 | |
michael@0 | 63 | #else |
michael@0 | 64 | |
michael@0 | 65 | // Stubs for if the user doesn't have Google Test installed. |
michael@0 | 66 | |
michael@0 | 67 | #define TEST(test_case, test_subcase) \ |
michael@0 | 68 | void Test_ ## test_case ## _ ## test_subcase() |
michael@0 | 69 | #define INIT_GTEST(argc, argv) |
michael@0 | 70 | |
michael@0 | 71 | #define TYPED_TEST TEST |
michael@0 | 72 | #define EXPECT_EQ CHECK_EQ |
michael@0 | 73 | #define EXPECT_NE CHECK_NE |
michael@0 | 74 | #define EXPECT_FALSE(cond) CHECK(!(cond)) |
michael@0 | 75 | |
michael@0 | 76 | #endif |
michael@0 | 77 | |
michael@0 | 78 | #ifdef HAVE_GFLAGS |
michael@0 | 79 | |
michael@0 | 80 | #include <gflags/gflags.h> |
michael@0 | 81 | |
michael@0 | 82 | // This is tricky; both gflags and Google Test want to look at the command line |
michael@0 | 83 | // arguments. Google Test seems to be the most happy with unknown arguments, |
michael@0 | 84 | // though, so we call it first and hope for the best. |
michael@0 | 85 | #define InitGoogle(argv0, argc, argv, remove_flags) \ |
michael@0 | 86 | INIT_GTEST(argc, argv); \ |
michael@0 | 87 | google::ParseCommandLineFlags(argc, argv, remove_flags); |
michael@0 | 88 | |
michael@0 | 89 | #else |
michael@0 | 90 | |
michael@0 | 91 | // If we don't have the gflags package installed, these can only be |
michael@0 | 92 | // changed at compile time. |
michael@0 | 93 | #define DEFINE_int32(flag_name, default_value, description) \ |
michael@0 | 94 | static int FLAGS_ ## flag_name = default_value; |
michael@0 | 95 | |
michael@0 | 96 | #define InitGoogle(argv0, argc, argv, remove_flags) \ |
michael@0 | 97 | INIT_GTEST(argc, argv) |
michael@0 | 98 | |
michael@0 | 99 | #endif |
michael@0 | 100 | |
michael@0 | 101 | #ifdef HAVE_LIBZ |
michael@0 | 102 | #include "zlib.h" |
michael@0 | 103 | #endif |
michael@0 | 104 | |
michael@0 | 105 | #ifdef HAVE_LIBLZO2 |
michael@0 | 106 | #include "lzo/lzo1x.h" |
michael@0 | 107 | #endif |
michael@0 | 108 | |
michael@0 | 109 | #ifdef HAVE_LIBLZF |
michael@0 | 110 | extern "C" { |
michael@0 | 111 | #include "lzf.h" |
michael@0 | 112 | } |
michael@0 | 113 | #endif |
michael@0 | 114 | |
michael@0 | 115 | #ifdef HAVE_LIBFASTLZ |
michael@0 | 116 | #include "fastlz.h" |
michael@0 | 117 | #endif |
michael@0 | 118 | |
michael@0 | 119 | #ifdef HAVE_LIBQUICKLZ |
michael@0 | 120 | #include "quicklz.h" |
michael@0 | 121 | #endif |
michael@0 | 122 | |
michael@0 | 123 | namespace { |
michael@0 | 124 | namespace File { |
michael@0 | 125 | void Init() { } |
michael@0 | 126 | |
michael@0 | 127 | void ReadFileToStringOrDie(const char* filename, string* data) { |
michael@0 | 128 | FILE* fp = fopen(filename, "rb"); |
michael@0 | 129 | if (fp == NULL) { |
michael@0 | 130 | perror(filename); |
michael@0 | 131 | exit(1); |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | data->clear(); |
michael@0 | 135 | while (!feof(fp)) { |
michael@0 | 136 | char buf[4096]; |
michael@0 | 137 | size_t ret = fread(buf, 1, 4096, fp); |
michael@0 | 138 | if (ret == 0 && ferror(fp)) { |
michael@0 | 139 | perror("fread"); |
michael@0 | 140 | exit(1); |
michael@0 | 141 | } |
michael@0 | 142 | data->append(string(buf, ret)); |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | fclose(fp); |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | void ReadFileToStringOrDie(const string& filename, string* data) { |
michael@0 | 149 | ReadFileToStringOrDie(filename.c_str(), data); |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | void WriteStringToFileOrDie(const string& str, const char* filename) { |
michael@0 | 153 | FILE* fp = fopen(filename, "wb"); |
michael@0 | 154 | if (fp == NULL) { |
michael@0 | 155 | perror(filename); |
michael@0 | 156 | exit(1); |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | int ret = fwrite(str.data(), str.size(), 1, fp); |
michael@0 | 160 | if (ret != 1) { |
michael@0 | 161 | perror("fwrite"); |
michael@0 | 162 | exit(1); |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | fclose(fp); |
michael@0 | 166 | } |
michael@0 | 167 | } // namespace File |
michael@0 | 168 | } // namespace |
michael@0 | 169 | |
michael@0 | 170 | namespace snappy { |
michael@0 | 171 | |
michael@0 | 172 | #define FLAGS_test_random_seed 301 |
michael@0 | 173 | typedef string TypeParam; |
michael@0 | 174 | |
michael@0 | 175 | void Test_CorruptedTest_VerifyCorrupted(); |
michael@0 | 176 | void Test_Snappy_SimpleTests(); |
michael@0 | 177 | void Test_Snappy_MaxBlowup(); |
michael@0 | 178 | void Test_Snappy_RandomData(); |
michael@0 | 179 | void Test_Snappy_FourByteOffset(); |
michael@0 | 180 | void Test_SnappyCorruption_TruncatedVarint(); |
michael@0 | 181 | void Test_SnappyCorruption_UnterminatedVarint(); |
michael@0 | 182 | void Test_Snappy_ReadPastEndOfBuffer(); |
michael@0 | 183 | void Test_Snappy_FindMatchLength(); |
michael@0 | 184 | void Test_Snappy_FindMatchLengthRandom(); |
michael@0 | 185 | |
michael@0 | 186 | string ReadTestDataFile(const string& base); |
michael@0 | 187 | |
michael@0 | 188 | // A sprintf() variant that returns a std::string. |
michael@0 | 189 | // Not safe for general use due to truncation issues. |
michael@0 | 190 | string StringPrintf(const char* format, ...); |
michael@0 | 191 | |
michael@0 | 192 | // A simple, non-cryptographically-secure random generator. |
michael@0 | 193 | class ACMRandom { |
michael@0 | 194 | public: |
michael@0 | 195 | explicit ACMRandom(uint32 seed) : seed_(seed) {} |
michael@0 | 196 | |
michael@0 | 197 | int32 Next(); |
michael@0 | 198 | |
michael@0 | 199 | int32 Uniform(int32 n) { |
michael@0 | 200 | return Next() % n; |
michael@0 | 201 | } |
michael@0 | 202 | uint8 Rand8() { |
michael@0 | 203 | return static_cast<uint8>((Next() >> 1) & 0x000000ff); |
michael@0 | 204 | } |
michael@0 | 205 | bool OneIn(int X) { return Uniform(X) == 0; } |
michael@0 | 206 | |
michael@0 | 207 | // Skewed: pick "base" uniformly from range [0,max_log] and then |
michael@0 | 208 | // return "base" random bits. The effect is to pick a number in the |
michael@0 | 209 | // range [0,2^max_log-1] with bias towards smaller numbers. |
michael@0 | 210 | int32 Skewed(int max_log); |
michael@0 | 211 | |
michael@0 | 212 | private: |
michael@0 | 213 | static const uint32 M = 2147483647L; // 2^31-1 |
michael@0 | 214 | uint32 seed_; |
michael@0 | 215 | }; |
michael@0 | 216 | |
michael@0 | 217 | inline int32 ACMRandom::Next() { |
michael@0 | 218 | static const uint64 A = 16807; // bits 14, 8, 7, 5, 2, 1, 0 |
michael@0 | 219 | // We are computing |
michael@0 | 220 | // seed_ = (seed_ * A) % M, where M = 2^31-1 |
michael@0 | 221 | // |
michael@0 | 222 | // seed_ must not be zero or M, or else all subsequent computed values |
michael@0 | 223 | // will be zero or M respectively. For all other values, seed_ will end |
michael@0 | 224 | // up cycling through every number in [1,M-1] |
michael@0 | 225 | uint64 product = seed_ * A; |
michael@0 | 226 | |
michael@0 | 227 | // Compute (product % M) using the fact that ((x << 31) % M) == x. |
michael@0 | 228 | seed_ = (product >> 31) + (product & M); |
michael@0 | 229 | // The first reduction may overflow by 1 bit, so we may need to repeat. |
michael@0 | 230 | // mod == M is not possible; using > allows the faster sign-bit-based test. |
michael@0 | 231 | if (seed_ > M) { |
michael@0 | 232 | seed_ -= M; |
michael@0 | 233 | } |
michael@0 | 234 | return seed_; |
michael@0 | 235 | } |
michael@0 | 236 | |
michael@0 | 237 | inline int32 ACMRandom::Skewed(int max_log) { |
michael@0 | 238 | const int32 base = (Next() - 1) % (max_log+1); |
michael@0 | 239 | return (Next() - 1) & ((1u << base)-1); |
michael@0 | 240 | } |
michael@0 | 241 | |
michael@0 | 242 | // A wall-time clock. This stub is not super-accurate, nor resistant to the |
michael@0 | 243 | // system time changing. |
michael@0 | 244 | class CycleTimer { |
michael@0 | 245 | public: |
michael@0 | 246 | CycleTimer() : real_time_us_(0) {} |
michael@0 | 247 | |
michael@0 | 248 | void Start() { |
michael@0 | 249 | #ifdef WIN32 |
michael@0 | 250 | QueryPerformanceCounter(&start_); |
michael@0 | 251 | #else |
michael@0 | 252 | gettimeofday(&start_, NULL); |
michael@0 | 253 | #endif |
michael@0 | 254 | } |
michael@0 | 255 | |
michael@0 | 256 | void Stop() { |
michael@0 | 257 | #ifdef WIN32 |
michael@0 | 258 | LARGE_INTEGER stop; |
michael@0 | 259 | LARGE_INTEGER frequency; |
michael@0 | 260 | QueryPerformanceCounter(&stop); |
michael@0 | 261 | QueryPerformanceFrequency(&frequency); |
michael@0 | 262 | |
michael@0 | 263 | double elapsed = static_cast<double>(stop.QuadPart - start_.QuadPart) / |
michael@0 | 264 | frequency.QuadPart; |
michael@0 | 265 | real_time_us_ += elapsed * 1e6 + 0.5; |
michael@0 | 266 | #else |
michael@0 | 267 | struct timeval stop; |
michael@0 | 268 | gettimeofday(&stop, NULL); |
michael@0 | 269 | |
michael@0 | 270 | real_time_us_ += 1000000 * (stop.tv_sec - start_.tv_sec); |
michael@0 | 271 | real_time_us_ += (stop.tv_usec - start_.tv_usec); |
michael@0 | 272 | #endif |
michael@0 | 273 | } |
michael@0 | 274 | |
michael@0 | 275 | double Get() { |
michael@0 | 276 | return real_time_us_ * 1e-6; |
michael@0 | 277 | } |
michael@0 | 278 | |
michael@0 | 279 | private: |
michael@0 | 280 | int64 real_time_us_; |
michael@0 | 281 | #ifdef WIN32 |
michael@0 | 282 | LARGE_INTEGER start_; |
michael@0 | 283 | #else |
michael@0 | 284 | struct timeval start_; |
michael@0 | 285 | #endif |
michael@0 | 286 | }; |
michael@0 | 287 | |
michael@0 | 288 | // Minimalistic microbenchmark framework. |
michael@0 | 289 | |
michael@0 | 290 | typedef void (*BenchmarkFunction)(int, int); |
michael@0 | 291 | |
michael@0 | 292 | class Benchmark { |
michael@0 | 293 | public: |
michael@0 | 294 | Benchmark(const string& name, BenchmarkFunction function) : |
michael@0 | 295 | name_(name), function_(function) {} |
michael@0 | 296 | |
michael@0 | 297 | Benchmark* DenseRange(int start, int stop) { |
michael@0 | 298 | start_ = start; |
michael@0 | 299 | stop_ = stop; |
michael@0 | 300 | return this; |
michael@0 | 301 | } |
michael@0 | 302 | |
michael@0 | 303 | void Run(); |
michael@0 | 304 | |
michael@0 | 305 | private: |
michael@0 | 306 | const string name_; |
michael@0 | 307 | const BenchmarkFunction function_; |
michael@0 | 308 | int start_, stop_; |
michael@0 | 309 | }; |
michael@0 | 310 | #define BENCHMARK(benchmark_name) \ |
michael@0 | 311 | Benchmark* Benchmark_ ## benchmark_name = \ |
michael@0 | 312 | (new Benchmark(#benchmark_name, benchmark_name)) |
michael@0 | 313 | |
michael@0 | 314 | extern Benchmark* Benchmark_BM_UFlat; |
michael@0 | 315 | extern Benchmark* Benchmark_BM_UValidate; |
michael@0 | 316 | extern Benchmark* Benchmark_BM_ZFlat; |
michael@0 | 317 | |
michael@0 | 318 | void ResetBenchmarkTiming(); |
michael@0 | 319 | void StartBenchmarkTiming(); |
michael@0 | 320 | void StopBenchmarkTiming(); |
michael@0 | 321 | void SetBenchmarkLabel(const string& str); |
michael@0 | 322 | void SetBenchmarkBytesProcessed(int64 bytes); |
michael@0 | 323 | |
michael@0 | 324 | #ifdef HAVE_LIBZ |
michael@0 | 325 | |
michael@0 | 326 | // Object-oriented wrapper around zlib. |
michael@0 | 327 | class ZLib { |
michael@0 | 328 | public: |
michael@0 | 329 | ZLib(); |
michael@0 | 330 | ~ZLib(); |
michael@0 | 331 | |
michael@0 | 332 | // Wipe a ZLib object to a virgin state. This differs from Reset() |
michael@0 | 333 | // in that it also breaks any state. |
michael@0 | 334 | void Reinit(); |
michael@0 | 335 | |
michael@0 | 336 | // Call this to make a zlib buffer as good as new. Here's the only |
michael@0 | 337 | // case where they differ: |
michael@0 | 338 | // CompressChunk(a); CompressChunk(b); CompressChunkDone(); vs |
michael@0 | 339 | // CompressChunk(a); Reset(); CompressChunk(b); CompressChunkDone(); |
michael@0 | 340 | // You'll want to use Reset(), then, when you interrupt a compress |
michael@0 | 341 | // (or uncompress) in the middle of a chunk and want to start over. |
michael@0 | 342 | void Reset(); |
michael@0 | 343 | |
michael@0 | 344 | // According to the zlib manual, when you Compress, the destination |
michael@0 | 345 | // buffer must have size at least src + .1%*src + 12. This function |
michael@0 | 346 | // helps you calculate that. Augment this to account for a potential |
michael@0 | 347 | // gzip header and footer, plus a few bytes of slack. |
michael@0 | 348 | static int MinCompressbufSize(int uncompress_size) { |
michael@0 | 349 | return uncompress_size + uncompress_size/1000 + 40; |
michael@0 | 350 | } |
michael@0 | 351 | |
michael@0 | 352 | // Compresses the source buffer into the destination buffer. |
michael@0 | 353 | // sourceLen is the byte length of the source buffer. |
michael@0 | 354 | // Upon entry, destLen is the total size of the destination buffer, |
michael@0 | 355 | // which must be of size at least MinCompressbufSize(sourceLen). |
michael@0 | 356 | // Upon exit, destLen is the actual size of the compressed buffer. |
michael@0 | 357 | // |
michael@0 | 358 | // This function can be used to compress a whole file at once if the |
michael@0 | 359 | // input file is mmap'ed. |
michael@0 | 360 | // |
michael@0 | 361 | // Returns Z_OK if success, Z_MEM_ERROR if there was not |
michael@0 | 362 | // enough memory, Z_BUF_ERROR if there was not enough room in the |
michael@0 | 363 | // output buffer. Note that if the output buffer is exactly the same |
michael@0 | 364 | // size as the compressed result, we still return Z_BUF_ERROR. |
michael@0 | 365 | // (check CL#1936076) |
michael@0 | 366 | int Compress(Bytef *dest, uLongf *destLen, |
michael@0 | 367 | const Bytef *source, uLong sourceLen); |
michael@0 | 368 | |
michael@0 | 369 | // Uncompresses the source buffer into the destination buffer. |
michael@0 | 370 | // The destination buffer must be long enough to hold the entire |
michael@0 | 371 | // decompressed contents. |
michael@0 | 372 | // |
michael@0 | 373 | // Returns Z_OK on success, otherwise, it returns a zlib error code. |
michael@0 | 374 | int Uncompress(Bytef *dest, uLongf *destLen, |
michael@0 | 375 | const Bytef *source, uLong sourceLen); |
michael@0 | 376 | |
michael@0 | 377 | // Uncompress data one chunk at a time -- ie you can call this |
michael@0 | 378 | // more than once. To get this to work you need to call per-chunk |
michael@0 | 379 | // and "done" routines. |
michael@0 | 380 | // |
michael@0 | 381 | // Returns Z_OK if success, Z_MEM_ERROR if there was not |
michael@0 | 382 | // enough memory, Z_BUF_ERROR if there was not enough room in the |
michael@0 | 383 | // output buffer. |
michael@0 | 384 | |
michael@0 | 385 | int UncompressAtMost(Bytef *dest, uLongf *destLen, |
michael@0 | 386 | const Bytef *source, uLong *sourceLen); |
michael@0 | 387 | |
michael@0 | 388 | // Checks gzip footer information, as needed. Mostly this just |
michael@0 | 389 | // makes sure the checksums match. Whenever you call this, it |
michael@0 | 390 | // will assume the last 8 bytes from the previous UncompressChunk |
michael@0 | 391 | // call are the footer. Returns true iff everything looks ok. |
michael@0 | 392 | bool UncompressChunkDone(); |
michael@0 | 393 | |
michael@0 | 394 | private: |
michael@0 | 395 | int InflateInit(); // sets up the zlib inflate structure |
michael@0 | 396 | int DeflateInit(); // sets up the zlib deflate structure |
michael@0 | 397 | |
michael@0 | 398 | // These init the zlib data structures for compressing/uncompressing |
michael@0 | 399 | int CompressInit(Bytef *dest, uLongf *destLen, |
michael@0 | 400 | const Bytef *source, uLong *sourceLen); |
michael@0 | 401 | int UncompressInit(Bytef *dest, uLongf *destLen, |
michael@0 | 402 | const Bytef *source, uLong *sourceLen); |
michael@0 | 403 | // Initialization method to be called if we hit an error while |
michael@0 | 404 | // uncompressing. On hitting an error, call this method before |
michael@0 | 405 | // returning the error. |
michael@0 | 406 | void UncompressErrorInit(); |
michael@0 | 407 | |
michael@0 | 408 | // Helper function for Compress |
michael@0 | 409 | int CompressChunkOrAll(Bytef *dest, uLongf *destLen, |
michael@0 | 410 | const Bytef *source, uLong sourceLen, |
michael@0 | 411 | int flush_mode); |
michael@0 | 412 | int CompressAtMostOrAll(Bytef *dest, uLongf *destLen, |
michael@0 | 413 | const Bytef *source, uLong *sourceLen, |
michael@0 | 414 | int flush_mode); |
michael@0 | 415 | |
michael@0 | 416 | // Likewise for UncompressAndUncompressChunk |
michael@0 | 417 | int UncompressChunkOrAll(Bytef *dest, uLongf *destLen, |
michael@0 | 418 | const Bytef *source, uLong sourceLen, |
michael@0 | 419 | int flush_mode); |
michael@0 | 420 | |
michael@0 | 421 | int UncompressAtMostOrAll(Bytef *dest, uLongf *destLen, |
michael@0 | 422 | const Bytef *source, uLong *sourceLen, |
michael@0 | 423 | int flush_mode); |
michael@0 | 424 | |
michael@0 | 425 | // Initialization method to be called if we hit an error while |
michael@0 | 426 | // compressing. On hitting an error, call this method before |
michael@0 | 427 | // returning the error. |
michael@0 | 428 | void CompressErrorInit(); |
michael@0 | 429 | |
michael@0 | 430 | int compression_level_; // compression level |
michael@0 | 431 | int window_bits_; // log base 2 of the window size used in compression |
michael@0 | 432 | int mem_level_; // specifies the amount of memory to be used by |
michael@0 | 433 | // compressor (1-9) |
michael@0 | 434 | z_stream comp_stream_; // Zlib stream data structure |
michael@0 | 435 | bool comp_init_; // True if we have initialized comp_stream_ |
michael@0 | 436 | z_stream uncomp_stream_; // Zlib stream data structure |
michael@0 | 437 | bool uncomp_init_; // True if we have initialized uncomp_stream_ |
michael@0 | 438 | |
michael@0 | 439 | // These are used only with chunked compression. |
michael@0 | 440 | bool first_chunk_; // true if we need to emit headers with this chunk |
michael@0 | 441 | }; |
michael@0 | 442 | |
michael@0 | 443 | #endif // HAVE_LIBZ |
michael@0 | 444 | |
michael@0 | 445 | } // namespace snappy |
michael@0 | 446 | |
michael@0 | 447 | DECLARE_bool(run_microbenchmarks); |
michael@0 | 448 | |
michael@0 | 449 | static void RunSpecifiedBenchmarks() { |
michael@0 | 450 | if (!FLAGS_run_microbenchmarks) { |
michael@0 | 451 | return; |
michael@0 | 452 | } |
michael@0 | 453 | |
michael@0 | 454 | fprintf(stderr, "Running microbenchmarks.\n"); |
michael@0 | 455 | #ifndef NDEBUG |
michael@0 | 456 | fprintf(stderr, "WARNING: Compiled with assertions enabled, will be slow.\n"); |
michael@0 | 457 | #endif |
michael@0 | 458 | #ifndef __OPTIMIZE__ |
michael@0 | 459 | fprintf(stderr, "WARNING: Compiled without optimization, will be slow.\n"); |
michael@0 | 460 | #endif |
michael@0 | 461 | fprintf(stderr, "Benchmark Time(ns) CPU(ns) Iterations\n"); |
michael@0 | 462 | fprintf(stderr, "---------------------------------------------------\n"); |
michael@0 | 463 | |
michael@0 | 464 | snappy::Benchmark_BM_UFlat->Run(); |
michael@0 | 465 | snappy::Benchmark_BM_UValidate->Run(); |
michael@0 | 466 | snappy::Benchmark_BM_ZFlat->Run(); |
michael@0 | 467 | |
michael@0 | 468 | fprintf(stderr, "\n"); |
michael@0 | 469 | } |
michael@0 | 470 | |
michael@0 | 471 | #ifndef HAVE_GTEST |
michael@0 | 472 | |
michael@0 | 473 | static inline int RUN_ALL_TESTS() { |
michael@0 | 474 | fprintf(stderr, "Running correctness tests.\n"); |
michael@0 | 475 | snappy::Test_CorruptedTest_VerifyCorrupted(); |
michael@0 | 476 | snappy::Test_Snappy_SimpleTests(); |
michael@0 | 477 | snappy::Test_Snappy_MaxBlowup(); |
michael@0 | 478 | snappy::Test_Snappy_RandomData(); |
michael@0 | 479 | snappy::Test_Snappy_FourByteOffset(); |
michael@0 | 480 | snappy::Test_SnappyCorruption_TruncatedVarint(); |
michael@0 | 481 | snappy::Test_SnappyCorruption_UnterminatedVarint(); |
michael@0 | 482 | snappy::Test_Snappy_ReadPastEndOfBuffer(); |
michael@0 | 483 | snappy::Test_Snappy_FindMatchLength(); |
michael@0 | 484 | snappy::Test_Snappy_FindMatchLengthRandom(); |
michael@0 | 485 | fprintf(stderr, "All tests passed.\n"); |
michael@0 | 486 | |
michael@0 | 487 | return 0; |
michael@0 | 488 | } |
michael@0 | 489 | |
michael@0 | 490 | #endif // HAVE_GTEST |
michael@0 | 491 | |
michael@0 | 492 | // For main(). |
michael@0 | 493 | namespace snappy { |
michael@0 | 494 | |
michael@0 | 495 | static void CompressFile(const char* fname); |
michael@0 | 496 | static void UncompressFile(const char* fname); |
michael@0 | 497 | static void MeasureFile(const char* fname); |
michael@0 | 498 | |
michael@0 | 499 | } // namespace |
michael@0 | 500 | |
michael@0 | 501 | using snappy::CompressFile; |
michael@0 | 502 | using snappy::UncompressFile; |
michael@0 | 503 | using snappy::MeasureFile; |
michael@0 | 504 | |
michael@0 | 505 | #endif // UTIL_SNAPPY_OPENSOURCE_SNAPPY_TEST_H_ |