| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stdlib.h> | 5 #include <stdlib.h> |
| 6 | 6 |
| 7 #include "remoting/base/compressor_zlib.h" | 7 #include "remoting/base/compressor_zlib.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 9 |
| 10 static void GenerateTestData(uint8* data, int size, int seed) { | 10 static void GenerateTestData(uint8* data, int size, int seed) { |
| 11 srand(seed); | 11 srand(seed); |
| 12 for (int i = 0; i < size; ++i) | 12 for (int i = 0; i < size; ++i) |
| 13 data[i] = rand() % 256; | 13 data[i] = rand() % 256; |
| 14 } | 14 } |
| 15 | 15 |
| 16 // Keep compressing |input_data| into |output_data| until the last | 16 // Keep compressing |input_data| into |output_data| until the last |
| 17 // bytes is consumed. | 17 // bytes is consumed. |
| 18 static void Compress(remoting::Compressor* compressor, | 18 static void Compress(remoting::Compressor* compressor, |
| 19 const uint8* input_data, int input_size, | 19 const uint8* input_data, int input_size, |
| 20 uint8* output_data, int output_size) { | 20 uint8* output_data, int output_size) { |
| 21 | 21 |
| 22 // Feed data into the compress until the end. | 22 // Feed data into the compress until the end. |
| 23 // This loop will rewrite |output_data| continuously. | 23 // This loop will rewrite |output_data| continuously. |
| 24 while (input_size) { | 24 int consumed = 0; |
| 25 int consumed, written; | 25 int written = 0; |
| 26 compressor->Write(input_data, input_size, | 26 while (compressor->Process(input_data, input_size, |
| 27 output_data, output_size, | 27 output_data, output_size, |
| 28 &consumed, &written); | 28 &consumed, &written)) { |
| 29 input_data += consumed; | 29 input_data += consumed; |
| 30 input_size -= consumed; | 30 input_size -= consumed; |
| 31 } | 31 } |
| 32 | |
| 33 // And then flush the remaining data from the compressor. | |
| 34 int written; | |
| 35 while (compressor->Flush(output_data, output_size, &written)) { | |
| 36 } | |
| 37 } | 32 } |
| 38 | 33 |
| 39 TEST(CompressorZlibTest, SimpleCompress) { | 34 TEST(CompressorZlibTest, Compress) { |
| 40 static const int kRawDataSize = 1024 * 128; | 35 static const int kRawDataSize = 1024 * 128; |
| 41 static const int kCompressedDataSize = 256; | 36 static const int kCompressedDataSize = 256; |
| 42 uint8 raw_data[kRawDataSize]; | 37 uint8 raw_data[kRawDataSize]; |
| 43 uint8 compressed_data[kCompressedDataSize]; | 38 uint8 compressed_data[kCompressedDataSize]; |
| 44 | 39 |
| 45 // Generate the test data.g | 40 // Generate the test data.g |
| 46 GenerateTestData(raw_data, kRawDataSize, 99); | 41 GenerateTestData(raw_data, kRawDataSize, 99); |
| 47 | 42 |
| 48 // Then use the compressor to compress. | 43 // Then use the compressor to compress. |
| 49 remoting::CompressorZlib compressor; | 44 remoting::CompressorZlib compressor; |
| 50 Compress(&compressor, raw_data, kRawDataSize, | 45 Compress(&compressor, raw_data, kRawDataSize, |
| 51 compressed_data, kCompressedDataSize); | 46 compressed_data, kCompressedDataSize); |
| 52 } | 47 } |
| 48 |
| 49 // Checks that zlib can work with a small output buffer by reading |
| 50 // less from the input. |
| 51 TEST(CompressorZlibTest, SmallOutputBuffer) { |
| 52 static const int kRawDataSize = 1024 * 128; |
| 53 static const int kCompressedDataSize = 1; |
| 54 uint8 raw_data[kRawDataSize]; |
| 55 uint8 compressed_data[kCompressedDataSize]; |
| 56 |
| 57 // Generate the test data.g |
| 58 GenerateTestData(raw_data, kRawDataSize, 99); |
| 59 |
| 60 // Then use the compressor to compress. |
| 61 remoting::CompressorZlib compressor; |
| 62 Compress(&compressor, raw_data, kRawDataSize, |
| 63 compressed_data, kCompressedDataSize); |
| 64 } |
| OLD | NEW |