summaryrefslogtreecommitdiffstats
path: root/remoting/base/compressor_zlib_unittest.cc
blob: 8127350f29c1879197ec15863c7a2526af3c452b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stdlib.h>

#include "remoting/base/compressor_zlib.h"
#include "testing/gtest/include/gtest/gtest.h"

static void GenerateTestData(uint8* data, int size, int seed) {
  srand(seed);
  for (int i = 0; i < size; ++i)
    data[i] = rand() % 256;
}

// Keep compressing |input_data| into |output_data| until the last
// bytes is consumed.
static void Compress(remoting::Compressor* compressor,
                     const uint8* input_data, int input_size,
                     uint8* output_data, int output_size) {

  // Feed data into the compress until the end.
  // This loop will rewrite |output_data| continuously.
  int consumed = 0;
  int written = 0;
  while (compressor->Process(input_data, input_size,
                             output_data, output_size,
                             &consumed, &written)) {
    input_data += consumed;
    input_size -= consumed;
  }
}

TEST(CompressorZlibTest, Compress) {
  static const int kRawDataSize = 1024 * 128;
  static const int kCompressedDataSize = 256;
  uint8 raw_data[kRawDataSize];
  uint8 compressed_data[kCompressedDataSize];

  // Generate the test data.g
  GenerateTestData(raw_data, kRawDataSize, 99);

  // Then use the compressor to compress.
  remoting::CompressorZlib compressor;
  Compress(&compressor, raw_data, kRawDataSize,
           compressed_data, kCompressedDataSize);
}

// Checks that zlib can work with a small output buffer by reading
// less from the input.
TEST(CompressorZlibTest, SmallOutputBuffer) {
  static const int kRawDataSize = 1024 * 128;
  static const int kCompressedDataSize = 1;
  uint8 raw_data[kRawDataSize];
  uint8 compressed_data[kCompressedDataSize];

  // Generate the test data.g
  GenerateTestData(raw_data, kRawDataSize, 99);

  // Then use the compressor to compress.
  remoting::CompressorZlib compressor;
  Compress(&compressor, raw_data, kRawDataSize,
           compressed_data, kCompressedDataSize);
}