1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/other-licenses/snappy/src/snappy-test.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,505 @@ 1.4 +// Copyright 2011 Google Inc. All Rights Reserved. 1.5 +// 1.6 +// Redistribution and use in source and binary forms, with or without 1.7 +// modification, are permitted provided that the following conditions are 1.8 +// met: 1.9 +// 1.10 +// * Redistributions of source code must retain the above copyright 1.11 +// notice, this list of conditions and the following disclaimer. 1.12 +// * Redistributions in binary form must reproduce the above 1.13 +// copyright notice, this list of conditions and the following disclaimer 1.14 +// in the documentation and/or other materials provided with the 1.15 +// distribution. 1.16 +// * Neither the name of Google Inc. nor the names of its 1.17 +// contributors may be used to endorse or promote products derived from 1.18 +// this software without specific prior written permission. 1.19 +// 1.20 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.21 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.22 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.23 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.24 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.25 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.26 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.27 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.28 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.29 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.30 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.31 +// 1.32 +// Various stubs for the unit tests for the open-source version of Snappy. 1.33 + 1.34 +#ifndef UTIL_SNAPPY_OPENSOURCE_SNAPPY_TEST_H_ 1.35 +#define UTIL_SNAPPY_OPENSOURCE_SNAPPY_TEST_H_ 1.36 + 1.37 +#include "snappy-stubs-internal.h" 1.38 + 1.39 +#include <stdio.h> 1.40 +#include <stdarg.h> 1.41 + 1.42 +#ifdef HAVE_SYS_MMAN_H 1.43 +#include <sys/mman.h> 1.44 +#endif 1.45 + 1.46 +#ifdef HAVE_SYS_RESOURCE_H 1.47 +#include <sys/resource.h> 1.48 +#endif 1.49 + 1.50 +#include <sys/time.h> 1.51 + 1.52 +#ifdef HAVE_WINDOWS_H 1.53 +#define WIN32_LEAN_AND_MEAN 1.54 +#include <windows.h> 1.55 +#endif 1.56 + 1.57 +#include <string> 1.58 + 1.59 +#ifdef HAVE_GTEST 1.60 + 1.61 +#include <gtest/gtest.h> 1.62 +#undef TYPED_TEST 1.63 +#define TYPED_TEST TEST 1.64 +#define INIT_GTEST(argc, argv) ::testing::InitGoogleTest(argc, *argv) 1.65 + 1.66 +#else 1.67 + 1.68 +// Stubs for if the user doesn't have Google Test installed. 1.69 + 1.70 +#define TEST(test_case, test_subcase) \ 1.71 + void Test_ ## test_case ## _ ## test_subcase() 1.72 +#define INIT_GTEST(argc, argv) 1.73 + 1.74 +#define TYPED_TEST TEST 1.75 +#define EXPECT_EQ CHECK_EQ 1.76 +#define EXPECT_NE CHECK_NE 1.77 +#define EXPECT_FALSE(cond) CHECK(!(cond)) 1.78 + 1.79 +#endif 1.80 + 1.81 +#ifdef HAVE_GFLAGS 1.82 + 1.83 +#include <gflags/gflags.h> 1.84 + 1.85 +// This is tricky; both gflags and Google Test want to look at the command line 1.86 +// arguments. Google Test seems to be the most happy with unknown arguments, 1.87 +// though, so we call it first and hope for the best. 1.88 +#define InitGoogle(argv0, argc, argv, remove_flags) \ 1.89 + INIT_GTEST(argc, argv); \ 1.90 + google::ParseCommandLineFlags(argc, argv, remove_flags); 1.91 + 1.92 +#else 1.93 + 1.94 +// If we don't have the gflags package installed, these can only be 1.95 +// changed at compile time. 1.96 +#define DEFINE_int32(flag_name, default_value, description) \ 1.97 + static int FLAGS_ ## flag_name = default_value; 1.98 + 1.99 +#define InitGoogle(argv0, argc, argv, remove_flags) \ 1.100 + INIT_GTEST(argc, argv) 1.101 + 1.102 +#endif 1.103 + 1.104 +#ifdef HAVE_LIBZ 1.105 +#include "zlib.h" 1.106 +#endif 1.107 + 1.108 +#ifdef HAVE_LIBLZO2 1.109 +#include "lzo/lzo1x.h" 1.110 +#endif 1.111 + 1.112 +#ifdef HAVE_LIBLZF 1.113 +extern "C" { 1.114 +#include "lzf.h" 1.115 +} 1.116 +#endif 1.117 + 1.118 +#ifdef HAVE_LIBFASTLZ 1.119 +#include "fastlz.h" 1.120 +#endif 1.121 + 1.122 +#ifdef HAVE_LIBQUICKLZ 1.123 +#include "quicklz.h" 1.124 +#endif 1.125 + 1.126 +namespace { 1.127 +namespace File { 1.128 + void Init() { } 1.129 + 1.130 + void ReadFileToStringOrDie(const char* filename, string* data) { 1.131 + FILE* fp = fopen(filename, "rb"); 1.132 + if (fp == NULL) { 1.133 + perror(filename); 1.134 + exit(1); 1.135 + } 1.136 + 1.137 + data->clear(); 1.138 + while (!feof(fp)) { 1.139 + char buf[4096]; 1.140 + size_t ret = fread(buf, 1, 4096, fp); 1.141 + if (ret == 0 && ferror(fp)) { 1.142 + perror("fread"); 1.143 + exit(1); 1.144 + } 1.145 + data->append(string(buf, ret)); 1.146 + } 1.147 + 1.148 + fclose(fp); 1.149 + } 1.150 + 1.151 + void ReadFileToStringOrDie(const string& filename, string* data) { 1.152 + ReadFileToStringOrDie(filename.c_str(), data); 1.153 + } 1.154 + 1.155 + void WriteStringToFileOrDie(const string& str, const char* filename) { 1.156 + FILE* fp = fopen(filename, "wb"); 1.157 + if (fp == NULL) { 1.158 + perror(filename); 1.159 + exit(1); 1.160 + } 1.161 + 1.162 + int ret = fwrite(str.data(), str.size(), 1, fp); 1.163 + if (ret != 1) { 1.164 + perror("fwrite"); 1.165 + exit(1); 1.166 + } 1.167 + 1.168 + fclose(fp); 1.169 + } 1.170 +} // namespace File 1.171 +} // namespace 1.172 + 1.173 +namespace snappy { 1.174 + 1.175 +#define FLAGS_test_random_seed 301 1.176 +typedef string TypeParam; 1.177 + 1.178 +void Test_CorruptedTest_VerifyCorrupted(); 1.179 +void Test_Snappy_SimpleTests(); 1.180 +void Test_Snappy_MaxBlowup(); 1.181 +void Test_Snappy_RandomData(); 1.182 +void Test_Snappy_FourByteOffset(); 1.183 +void Test_SnappyCorruption_TruncatedVarint(); 1.184 +void Test_SnappyCorruption_UnterminatedVarint(); 1.185 +void Test_Snappy_ReadPastEndOfBuffer(); 1.186 +void Test_Snappy_FindMatchLength(); 1.187 +void Test_Snappy_FindMatchLengthRandom(); 1.188 + 1.189 +string ReadTestDataFile(const string& base); 1.190 + 1.191 +// A sprintf() variant that returns a std::string. 1.192 +// Not safe for general use due to truncation issues. 1.193 +string StringPrintf(const char* format, ...); 1.194 + 1.195 +// A simple, non-cryptographically-secure random generator. 1.196 +class ACMRandom { 1.197 + public: 1.198 + explicit ACMRandom(uint32 seed) : seed_(seed) {} 1.199 + 1.200 + int32 Next(); 1.201 + 1.202 + int32 Uniform(int32 n) { 1.203 + return Next() % n; 1.204 + } 1.205 + uint8 Rand8() { 1.206 + return static_cast<uint8>((Next() >> 1) & 0x000000ff); 1.207 + } 1.208 + bool OneIn(int X) { return Uniform(X) == 0; } 1.209 + 1.210 + // Skewed: pick "base" uniformly from range [0,max_log] and then 1.211 + // return "base" random bits. The effect is to pick a number in the 1.212 + // range [0,2^max_log-1] with bias towards smaller numbers. 1.213 + int32 Skewed(int max_log); 1.214 + 1.215 + private: 1.216 + static const uint32 M = 2147483647L; // 2^31-1 1.217 + uint32 seed_; 1.218 +}; 1.219 + 1.220 +inline int32 ACMRandom::Next() { 1.221 + static const uint64 A = 16807; // bits 14, 8, 7, 5, 2, 1, 0 1.222 + // We are computing 1.223 + // seed_ = (seed_ * A) % M, where M = 2^31-1 1.224 + // 1.225 + // seed_ must not be zero or M, or else all subsequent computed values 1.226 + // will be zero or M respectively. For all other values, seed_ will end 1.227 + // up cycling through every number in [1,M-1] 1.228 + uint64 product = seed_ * A; 1.229 + 1.230 + // Compute (product % M) using the fact that ((x << 31) % M) == x. 1.231 + seed_ = (product >> 31) + (product & M); 1.232 + // The first reduction may overflow by 1 bit, so we may need to repeat. 1.233 + // mod == M is not possible; using > allows the faster sign-bit-based test. 1.234 + if (seed_ > M) { 1.235 + seed_ -= M; 1.236 + } 1.237 + return seed_; 1.238 +} 1.239 + 1.240 +inline int32 ACMRandom::Skewed(int max_log) { 1.241 + const int32 base = (Next() - 1) % (max_log+1); 1.242 + return (Next() - 1) & ((1u << base)-1); 1.243 +} 1.244 + 1.245 +// A wall-time clock. This stub is not super-accurate, nor resistant to the 1.246 +// system time changing. 1.247 +class CycleTimer { 1.248 + public: 1.249 + CycleTimer() : real_time_us_(0) {} 1.250 + 1.251 + void Start() { 1.252 +#ifdef WIN32 1.253 + QueryPerformanceCounter(&start_); 1.254 +#else 1.255 + gettimeofday(&start_, NULL); 1.256 +#endif 1.257 + } 1.258 + 1.259 + void Stop() { 1.260 +#ifdef WIN32 1.261 + LARGE_INTEGER stop; 1.262 + LARGE_INTEGER frequency; 1.263 + QueryPerformanceCounter(&stop); 1.264 + QueryPerformanceFrequency(&frequency); 1.265 + 1.266 + double elapsed = static_cast<double>(stop.QuadPart - start_.QuadPart) / 1.267 + frequency.QuadPart; 1.268 + real_time_us_ += elapsed * 1e6 + 0.5; 1.269 +#else 1.270 + struct timeval stop; 1.271 + gettimeofday(&stop, NULL); 1.272 + 1.273 + real_time_us_ += 1000000 * (stop.tv_sec - start_.tv_sec); 1.274 + real_time_us_ += (stop.tv_usec - start_.tv_usec); 1.275 +#endif 1.276 + } 1.277 + 1.278 + double Get() { 1.279 + return real_time_us_ * 1e-6; 1.280 + } 1.281 + 1.282 + private: 1.283 + int64 real_time_us_; 1.284 +#ifdef WIN32 1.285 + LARGE_INTEGER start_; 1.286 +#else 1.287 + struct timeval start_; 1.288 +#endif 1.289 +}; 1.290 + 1.291 +// Minimalistic microbenchmark framework. 1.292 + 1.293 +typedef void (*BenchmarkFunction)(int, int); 1.294 + 1.295 +class Benchmark { 1.296 + public: 1.297 + Benchmark(const string& name, BenchmarkFunction function) : 1.298 + name_(name), function_(function) {} 1.299 + 1.300 + Benchmark* DenseRange(int start, int stop) { 1.301 + start_ = start; 1.302 + stop_ = stop; 1.303 + return this; 1.304 + } 1.305 + 1.306 + void Run(); 1.307 + 1.308 + private: 1.309 + const string name_; 1.310 + const BenchmarkFunction function_; 1.311 + int start_, stop_; 1.312 +}; 1.313 +#define BENCHMARK(benchmark_name) \ 1.314 + Benchmark* Benchmark_ ## benchmark_name = \ 1.315 + (new Benchmark(#benchmark_name, benchmark_name)) 1.316 + 1.317 +extern Benchmark* Benchmark_BM_UFlat; 1.318 +extern Benchmark* Benchmark_BM_UValidate; 1.319 +extern Benchmark* Benchmark_BM_ZFlat; 1.320 + 1.321 +void ResetBenchmarkTiming(); 1.322 +void StartBenchmarkTiming(); 1.323 +void StopBenchmarkTiming(); 1.324 +void SetBenchmarkLabel(const string& str); 1.325 +void SetBenchmarkBytesProcessed(int64 bytes); 1.326 + 1.327 +#ifdef HAVE_LIBZ 1.328 + 1.329 +// Object-oriented wrapper around zlib. 1.330 +class ZLib { 1.331 + public: 1.332 + ZLib(); 1.333 + ~ZLib(); 1.334 + 1.335 + // Wipe a ZLib object to a virgin state. This differs from Reset() 1.336 + // in that it also breaks any state. 1.337 + void Reinit(); 1.338 + 1.339 + // Call this to make a zlib buffer as good as new. Here's the only 1.340 + // case where they differ: 1.341 + // CompressChunk(a); CompressChunk(b); CompressChunkDone(); vs 1.342 + // CompressChunk(a); Reset(); CompressChunk(b); CompressChunkDone(); 1.343 + // You'll want to use Reset(), then, when you interrupt a compress 1.344 + // (or uncompress) in the middle of a chunk and want to start over. 1.345 + void Reset(); 1.346 + 1.347 + // According to the zlib manual, when you Compress, the destination 1.348 + // buffer must have size at least src + .1%*src + 12. This function 1.349 + // helps you calculate that. Augment this to account for a potential 1.350 + // gzip header and footer, plus a few bytes of slack. 1.351 + static int MinCompressbufSize(int uncompress_size) { 1.352 + return uncompress_size + uncompress_size/1000 + 40; 1.353 + } 1.354 + 1.355 + // Compresses the source buffer into the destination buffer. 1.356 + // sourceLen is the byte length of the source buffer. 1.357 + // Upon entry, destLen is the total size of the destination buffer, 1.358 + // which must be of size at least MinCompressbufSize(sourceLen). 1.359 + // Upon exit, destLen is the actual size of the compressed buffer. 1.360 + // 1.361 + // This function can be used to compress a whole file at once if the 1.362 + // input file is mmap'ed. 1.363 + // 1.364 + // Returns Z_OK if success, Z_MEM_ERROR if there was not 1.365 + // enough memory, Z_BUF_ERROR if there was not enough room in the 1.366 + // output buffer. Note that if the output buffer is exactly the same 1.367 + // size as the compressed result, we still return Z_BUF_ERROR. 1.368 + // (check CL#1936076) 1.369 + int Compress(Bytef *dest, uLongf *destLen, 1.370 + const Bytef *source, uLong sourceLen); 1.371 + 1.372 + // Uncompresses the source buffer into the destination buffer. 1.373 + // The destination buffer must be long enough to hold the entire 1.374 + // decompressed contents. 1.375 + // 1.376 + // Returns Z_OK on success, otherwise, it returns a zlib error code. 1.377 + int Uncompress(Bytef *dest, uLongf *destLen, 1.378 + const Bytef *source, uLong sourceLen); 1.379 + 1.380 + // Uncompress data one chunk at a time -- ie you can call this 1.381 + // more than once. To get this to work you need to call per-chunk 1.382 + // and "done" routines. 1.383 + // 1.384 + // Returns Z_OK if success, Z_MEM_ERROR if there was not 1.385 + // enough memory, Z_BUF_ERROR if there was not enough room in the 1.386 + // output buffer. 1.387 + 1.388 + int UncompressAtMost(Bytef *dest, uLongf *destLen, 1.389 + const Bytef *source, uLong *sourceLen); 1.390 + 1.391 + // Checks gzip footer information, as needed. Mostly this just 1.392 + // makes sure the checksums match. Whenever you call this, it 1.393 + // will assume the last 8 bytes from the previous UncompressChunk 1.394 + // call are the footer. Returns true iff everything looks ok. 1.395 + bool UncompressChunkDone(); 1.396 + 1.397 + private: 1.398 + int InflateInit(); // sets up the zlib inflate structure 1.399 + int DeflateInit(); // sets up the zlib deflate structure 1.400 + 1.401 + // These init the zlib data structures for compressing/uncompressing 1.402 + int CompressInit(Bytef *dest, uLongf *destLen, 1.403 + const Bytef *source, uLong *sourceLen); 1.404 + int UncompressInit(Bytef *dest, uLongf *destLen, 1.405 + const Bytef *source, uLong *sourceLen); 1.406 + // Initialization method to be called if we hit an error while 1.407 + // uncompressing. On hitting an error, call this method before 1.408 + // returning the error. 1.409 + void UncompressErrorInit(); 1.410 + 1.411 + // Helper function for Compress 1.412 + int CompressChunkOrAll(Bytef *dest, uLongf *destLen, 1.413 + const Bytef *source, uLong sourceLen, 1.414 + int flush_mode); 1.415 + int CompressAtMostOrAll(Bytef *dest, uLongf *destLen, 1.416 + const Bytef *source, uLong *sourceLen, 1.417 + int flush_mode); 1.418 + 1.419 + // Likewise for UncompressAndUncompressChunk 1.420 + int UncompressChunkOrAll(Bytef *dest, uLongf *destLen, 1.421 + const Bytef *source, uLong sourceLen, 1.422 + int flush_mode); 1.423 + 1.424 + int UncompressAtMostOrAll(Bytef *dest, uLongf *destLen, 1.425 + const Bytef *source, uLong *sourceLen, 1.426 + int flush_mode); 1.427 + 1.428 + // Initialization method to be called if we hit an error while 1.429 + // compressing. On hitting an error, call this method before 1.430 + // returning the error. 1.431 + void CompressErrorInit(); 1.432 + 1.433 + int compression_level_; // compression level 1.434 + int window_bits_; // log base 2 of the window size used in compression 1.435 + int mem_level_; // specifies the amount of memory to be used by 1.436 + // compressor (1-9) 1.437 + z_stream comp_stream_; // Zlib stream data structure 1.438 + bool comp_init_; // True if we have initialized comp_stream_ 1.439 + z_stream uncomp_stream_; // Zlib stream data structure 1.440 + bool uncomp_init_; // True if we have initialized uncomp_stream_ 1.441 + 1.442 + // These are used only with chunked compression. 1.443 + bool first_chunk_; // true if we need to emit headers with this chunk 1.444 +}; 1.445 + 1.446 +#endif // HAVE_LIBZ 1.447 + 1.448 +} // namespace snappy 1.449 + 1.450 +DECLARE_bool(run_microbenchmarks); 1.451 + 1.452 +static void RunSpecifiedBenchmarks() { 1.453 + if (!FLAGS_run_microbenchmarks) { 1.454 + return; 1.455 + } 1.456 + 1.457 + fprintf(stderr, "Running microbenchmarks.\n"); 1.458 +#ifndef NDEBUG 1.459 + fprintf(stderr, "WARNING: Compiled with assertions enabled, will be slow.\n"); 1.460 +#endif 1.461 +#ifndef __OPTIMIZE__ 1.462 + fprintf(stderr, "WARNING: Compiled without optimization, will be slow.\n"); 1.463 +#endif 1.464 + fprintf(stderr, "Benchmark Time(ns) CPU(ns) Iterations\n"); 1.465 + fprintf(stderr, "---------------------------------------------------\n"); 1.466 + 1.467 + snappy::Benchmark_BM_UFlat->Run(); 1.468 + snappy::Benchmark_BM_UValidate->Run(); 1.469 + snappy::Benchmark_BM_ZFlat->Run(); 1.470 + 1.471 + fprintf(stderr, "\n"); 1.472 +} 1.473 + 1.474 +#ifndef HAVE_GTEST 1.475 + 1.476 +static inline int RUN_ALL_TESTS() { 1.477 + fprintf(stderr, "Running correctness tests.\n"); 1.478 + snappy::Test_CorruptedTest_VerifyCorrupted(); 1.479 + snappy::Test_Snappy_SimpleTests(); 1.480 + snappy::Test_Snappy_MaxBlowup(); 1.481 + snappy::Test_Snappy_RandomData(); 1.482 + snappy::Test_Snappy_FourByteOffset(); 1.483 + snappy::Test_SnappyCorruption_TruncatedVarint(); 1.484 + snappy::Test_SnappyCorruption_UnterminatedVarint(); 1.485 + snappy::Test_Snappy_ReadPastEndOfBuffer(); 1.486 + snappy::Test_Snappy_FindMatchLength(); 1.487 + snappy::Test_Snappy_FindMatchLengthRandom(); 1.488 + fprintf(stderr, "All tests passed.\n"); 1.489 + 1.490 + return 0; 1.491 +} 1.492 + 1.493 +#endif // HAVE_GTEST 1.494 + 1.495 +// For main(). 1.496 +namespace snappy { 1.497 + 1.498 +static void CompressFile(const char* fname); 1.499 +static void UncompressFile(const char* fname); 1.500 +static void MeasureFile(const char* fname); 1.501 + 1.502 +} // namespace 1.503 + 1.504 +using snappy::CompressFile; 1.505 +using snappy::UncompressFile; 1.506 +using snappy::MeasureFile; 1.507 + 1.508 +#endif // UTIL_SNAPPY_OPENSOURCE_SNAPPY_TEST_H_