| 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 namespace remoting { |
| 11 |
| 10 static void GenerateTestData(uint8* data, int size, int seed) { | 12 static void GenerateTestData(uint8* data, int size, int seed) { |
| 11 srand(seed); | 13 srand(seed); |
| 12 for (int i = 0; i < size; ++i) | 14 for (int i = 0; i < size; ++i) |
| 13 data[i] = rand() % 256; | 15 data[i] = rand() % 256; |
| 14 } | 16 } |
| 15 | 17 |
| 16 // Keep compressing |input_data| into |output_data| until the last | 18 // Keep compressing |input_data| into |output_data| until the last |
| 17 // bytes is consumed. | 19 // bytes is consumed. |
| 18 static void Compress(remoting::Compressor* compressor, | 20 static void Compress(remoting::Compressor* compressor, |
| 19 const uint8* input_data, int input_size, | 21 const uint8* input_data, int input_size, |
| 20 uint8* output_data, int output_size) { | 22 uint8* output_data, int output_size) { |
| 21 | 23 |
| 22 // Feed data into the compress until the end. | 24 // Feed data into the compress until the end. |
| 23 // This loop will rewrite |output_data| continuously. | 25 // This loop will rewrite |output_data| continuously. |
| 24 int consumed = 0; | 26 int consumed = 0; |
| 25 int written = 0; | 27 int written = 0; |
| 26 while (compressor->Process(input_data, input_size, | 28 while (compressor->Process( |
| 27 output_data, output_size, | 29 input_data, input_size, output_data, output_size, |
| 28 &consumed, &written)) { | 30 input_size == 0 ? |
| 31 Compressor::CompressorFinish : Compressor::CompressorNoFlush, |
| 32 &consumed, &written)) { |
| 29 input_data += consumed; | 33 input_data += consumed; |
| 30 input_size -= consumed; | 34 input_size -= consumed; |
| 31 } | 35 } |
| 32 } | 36 } |
| 33 | 37 |
| 34 TEST(CompressorZlibTest, Compress) { | 38 TEST(CompressorZlibTest, Compress) { |
| 35 static const int kRawDataSize = 1024 * 128; | 39 static const int kRawDataSize = 1024 * 128; |
| 36 static const int kCompressedDataSize = 256; | 40 static const int kCompressedDataSize = 256; |
| 37 uint8 raw_data[kRawDataSize]; | 41 uint8 raw_data[kRawDataSize]; |
| 38 uint8 compressed_data[kCompressedDataSize]; | 42 uint8 compressed_data[kCompressedDataSize]; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 55 uint8 compressed_data[kCompressedDataSize]; | 59 uint8 compressed_data[kCompressedDataSize]; |
| 56 | 60 |
| 57 // Generate the test data.g | 61 // Generate the test data.g |
| 58 GenerateTestData(raw_data, kRawDataSize, 99); | 62 GenerateTestData(raw_data, kRawDataSize, 99); |
| 59 | 63 |
| 60 // Then use the compressor to compress. | 64 // Then use the compressor to compress. |
| 61 remoting::CompressorZlib compressor; | 65 remoting::CompressorZlib compressor; |
| 62 Compress(&compressor, raw_data, kRawDataSize, | 66 Compress(&compressor, raw_data, kRawDataSize, |
| 63 compressed_data, kCompressedDataSize); | 67 compressed_data, kCompressedDataSize); |
| 64 } | 68 } |
| 69 |
| 70 } // namespace remoting |
| OLD | NEW |