OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <stdlib.h> |
| 6 |
| 7 #include "remoting/base/compressor_zlib.h" |
| 8 #include "remoting/base/decompressor_zlib.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 static void GenerateTestData(uint8* data, int size, int seed) { |
| 12 srand(seed); |
| 13 for (int i = 0; i < size; ++i) |
| 14 data[i] = rand() % 256; |
| 15 } |
| 16 |
| 17 // Keep compressing |input_data| into |output_data| until the last |
| 18 // bytes is consumed. |
| 19 // |
| 20 // The compressed size is written to |compressed_size|. |
| 21 static void Compress(remoting::Compressor* compressor, |
| 22 const uint8* input_data, int input_size, |
| 23 uint8* output_data, int output_size, |
| 24 int* compressed_size) { |
| 25 *compressed_size = 0; |
| 26 while (true) { |
| 27 int consumed, written; |
| 28 bool ret = compressor->Process(input_data, input_size, |
| 29 output_data, output_size, |
| 30 &consumed, &written); |
| 31 input_data += consumed; |
| 32 input_size -= consumed; |
| 33 output_data += written; |
| 34 output_size -= written; |
| 35 *compressed_size += written; |
| 36 |
| 37 if (!ret) |
| 38 break; |
| 39 } |
| 40 } |
| 41 |
| 42 // The decompressed size is written to |decompressed_size|. |
| 43 static void Decompress(remoting::Decompressor* decompressor, |
| 44 const uint8* input_data, int input_size, |
| 45 uint8* output_data, int output_size, |
| 46 int* decompressed_size) { |
| 47 *decompressed_size = 0; |
| 48 while (true) { |
| 49 int consumed, written; |
| 50 bool ret = decompressor->Process(input_data, input_size, |
| 51 output_data, output_size, |
| 52 &consumed, &written); |
| 53 input_data += consumed; |
| 54 input_size -= consumed; |
| 55 output_data += written; |
| 56 output_size -= written; |
| 57 *decompressed_size += written; |
| 58 |
| 59 if (!ret) |
| 60 break; |
| 61 } |
| 62 } |
| 63 |
| 64 TEST(DecompressorZlibTest, CompressAndDecompress) { |
| 65 static const int kRawDataSize = 1024 * 128; |
| 66 static const int kCompressedDataSize = 2 * kRawDataSize; |
| 67 static const int kDecompressedDataSize = kRawDataSize; |
| 68 |
| 69 uint8 raw_data[kRawDataSize]; |
| 70 uint8 compressed_data[kCompressedDataSize]; |
| 71 uint8 decompressed_data[kDecompressedDataSize]; |
| 72 |
| 73 // Generate the test data.g |
| 74 GenerateTestData(raw_data, kRawDataSize, 99); |
| 75 |
| 76 // Then use the compressor. |
| 77 remoting::CompressorZlib compressor; |
| 78 int compressed_size = 0; |
| 79 Compress(&compressor, raw_data, kRawDataSize, |
| 80 compressed_data, kCompressedDataSize, |
| 81 &compressed_size); |
| 82 |
| 83 // Then use the decompressor. |
| 84 remoting::DecompressorZlib decompressor; |
| 85 int decompressed_size = 0; |
| 86 Decompress(&decompressor, compressed_data, compressed_size, |
| 87 decompressed_data, kDecompressedDataSize, |
| 88 &decompressed_size); |
| 89 |
| 90 EXPECT_EQ(kRawDataSize, decompressed_size); |
| 91 EXPECT_EQ(0, memcmp(raw_data, decompressed_data, decompressed_size)); |
| 92 } |
| 93 |
| 94 // Checks that zlib can work with a small output buffer by limiting |
| 95 // number of bytes it gets from the input. |
| 96 TEST(DecompressorZlibTest, SmallOutputBuffer) { |
| 97 static const int kRawDataSize = 1024 * 128; |
| 98 static const int kCompressedDataSize = 2 * kRawDataSize; |
| 99 |
| 100 uint8 raw_data[kRawDataSize]; |
| 101 uint8 compressed_data[kCompressedDataSize]; |
| 102 |
| 103 // Generate the test data. |
| 104 GenerateTestData(raw_data, kRawDataSize, 99); |
| 105 |
| 106 // Then use the compressor to compress. |
| 107 remoting::CompressorZlib compressor; |
| 108 int compressed_size = 0; |
| 109 Compress(&compressor, raw_data, kRawDataSize, |
| 110 compressed_data, kCompressedDataSize, |
| 111 &compressed_size); |
| 112 |
| 113 // Then use the decompressor. We decompress into a 1 byte buffer |
| 114 // every time. |
| 115 remoting::DecompressorZlib decompressor; |
| 116 uint8* input_data = compressed_data; |
| 117 int input_size = compressed_size; |
| 118 int decompressed_size = 0; |
| 119 while (true) { |
| 120 int consumed, written; |
| 121 uint8 output_data; |
| 122 bool ret = decompressor.Process(input_data, input_size, |
| 123 &output_data, 1, |
| 124 &consumed, &written); |
| 125 input_data += consumed; |
| 126 input_size -= consumed; |
| 127 |
| 128 // Expect that there's only one byte written. |
| 129 EXPECT_EQ(1, written); |
| 130 decompressed_size += written; |
| 131 |
| 132 if (!ret) |
| 133 break; |
| 134 } |
| 135 EXPECT_EQ(kRawDataSize, decompressed_size); |
| 136 } |
OLD | NEW |