diff options
author | kenton@google.com <kenton@google.com@630680e5-0e50-0410-840e-4b1c322b438d> | 2009-08-01 00:38:45 +0000 |
---|---|---|
committer | kenton@google.com <kenton@google.com@630680e5-0e50-0410-840e-4b1c322b438d> | 2009-08-01 00:38:45 +0000 |
commit | 253a8508047a23e06d46a276480360f76b3cb219 (patch) | |
tree | 40d621129b60773a84799edc2d4c34af6d7c675c /src/google/protobuf/io/zero_copy_stream_unittest.cc | |
parent | 1900c536cd91bae783e6766d3fc016e75061d17c (diff) | |
download | protobuf-253a8508047a23e06d46a276480360f76b3cb219.tar.gz protobuf-253a8508047a23e06d46a276480360f76b3cb219.tar.bz2 protobuf-253a8508047a23e06d46a276480360f76b3cb219.zip |
Allow compression level of GzipOutputStream to be configured.
Diffstat (limited to 'src/google/protobuf/io/zero_copy_stream_unittest.cc')
-rw-r--r-- | src/google/protobuf/io/zero_copy_stream_unittest.cc | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/google/protobuf/io/zero_copy_stream_unittest.cc b/src/google/protobuf/io/zero_copy_stream_unittest.cc index 5e3310ac..c8f669a0 100644 --- a/src/google/protobuf/io/zero_copy_stream_unittest.cc +++ b/src/google/protobuf/io/zero_copy_stream_unittest.cc @@ -68,6 +68,7 @@ #include <google/protobuf/stubs/common.h> #include <google/protobuf/testing/googletest.h> +#include <google/protobuf/testing/file.h> #include <gtest/gtest.h> namespace google { @@ -114,6 +115,11 @@ class IoTest : public testing::Test { // via WriteStuffLarge(). void ReadStuffLarge(ZeroCopyInputStream* input); +#if HAVE_ZLIB + string Compress(const string& data, const GzipOutputStream::Options& options); + string Uncompress(const string& data); +#endif + static const int kBlockSizes[]; static const int kBlockSizeCount; }; @@ -366,6 +372,65 @@ TEST_F(IoTest, ZlibIoInputAutodetect) { } delete [] buffer; } + +string IoTest::Compress(const string& data, + const GzipOutputStream::Options& options) { + string result; + { + StringOutputStream output(&result); + GzipOutputStream gzout(&output, options); + WriteToOutput(&gzout, data.data(), data.size()); + } + return result; +} + +string IoTest::Uncompress(const string& data) { + string result; + { + ArrayInputStream input(data.data(), data.size()); + GzipInputStream gzin(&input); + const void* buffer; + int size; + while (gzin.Next(&buffer, &size)) { + result.append(reinterpret_cast<const char*>(buffer), size); + } + } + return result; +} + +TEST_F(IoTest, CompressionOptions) { + // Some ad-hoc testing of compression options. + + string golden; + File::ReadFileToStringOrDie( + TestSourceDir() + "/google/protobuf/testdata/golden_message", &golden); + + GzipOutputStream::Options options; + string gzip_compressed = Compress(golden, options); + + options.compression_level = 0; + string not_compressed = Compress(golden, options); + + // Try zlib compression for fun. + options = GzipOutputStream::Options(); + options.format = GzipOutputStream::ZLIB; + string zlib_compressed = Compress(golden, options); + + // Uncompressed should be bigger than the original since it should have some + // sort of header. + EXPECT_GT(not_compressed.size(), golden.size()); + + // Higher compression levels should result in smaller sizes. + EXPECT_LT(zlib_compressed.size(), not_compressed.size()); + + // ZLIB format should differ from GZIP format. + EXPECT_TRUE(zlib_compressed != gzip_compressed); + + // Everything should decompress correctly. + EXPECT_TRUE(Uncompress(not_compressed) == golden); + EXPECT_TRUE(Uncompress(gzip_compressed) == golden); + EXPECT_TRUE(Uncompress(zlib_compressed) == golden); +} #endif // There is no string input, only string output. Also, it doesn't support |