aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools/singlejar/zlib_interface_test.cc
blob: 02c544aec9bdf71654b18f60aea4e2e25643ef8a (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2016 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "src/tools/singlejar/zlib_interface.h"

#include "gtest/gtest.h"

namespace {

static const uint8_t bytes[] = {1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4};

TEST(ZlibInterfaceTest, DeflateFully) {
  Deflater deflater;
  uint8_t compressed[256];
  deflater.next_out = compressed;
  deflater.avail_out = sizeof(compressed);
  EXPECT_EQ(Z_STREAM_END, deflater.Deflate(bytes, sizeof(bytes), Z_FINISH));
}

TEST(ZlibInterfaceTest, DeflateIntoChunks) {
  Deflater deflater;
  uint8_t compressed[256];
  deflater.next_out = compressed;
  deflater.avail_out = 2;
  EXPECT_EQ(Z_OK, deflater.Deflate(bytes, sizeof(bytes), Z_FINISH));
  EXPECT_EQ(0, deflater.avail_out);
  deflater.next_out = compressed + 2;
  deflater.avail_out = sizeof(compressed) - 2;
  EXPECT_EQ(Z_STREAM_END,
            deflater.Deflate(deflater.next_in,
                               deflater.avail_in, Z_FINISH));
}

TEST(ZlibInterfaceTest, DeflateChunks) {
  Deflater deflater;
  uint8_t compressed[256];
  deflater.next_out = compressed;
  deflater.avail_out = sizeof(compressed);
  EXPECT_EQ(Z_OK, deflater.Deflate(bytes, 4, Z_NO_FLUSH));
  EXPECT_EQ(Z_STREAM_END,
            deflater.Deflate(bytes + 4, sizeof(bytes) - 4, Z_FINISH));
}

TEST(ZlibInterfaceTest, InflateFully) {
  uint8_t compressed[256];
  Deflater deflater;
  deflater.next_out = compressed;
  deflater.avail_out = sizeof(compressed);
  EXPECT_EQ(Z_STREAM_END, deflater.Deflate(bytes, sizeof(bytes), Z_FINISH));

  // Now we have deflated data, inflate it back and compare.
  size_t compressed_size = sizeof(compressed) - deflater.avail_out;
  Inflater inflater;
  inflater.DataToInflate(compressed, compressed_size);

  uint8_t uncompressed[256];
  memset(uncompressed, 0, sizeof(uncompressed));
  EXPECT_EQ(Z_STREAM_END,
            inflater.Inflate(uncompressed, sizeof(uncompressed)));
  EXPECT_EQ(sizeof(bytes), sizeof(uncompressed) - inflater.available_out());
  EXPECT_EQ(0, memcmp(bytes, uncompressed, sizeof(bytes)));
}

TEST(ZlibInterfaceTest, InflateToChunks) {
  uint8_t compressed[256];
  Deflater deflater;
  deflater.next_out = compressed;
  deflater.avail_out = sizeof(compressed);
  EXPECT_EQ(Z_STREAM_END, deflater.Deflate(bytes, sizeof(bytes), Z_FINISH));

  // Now we have deflated data, inflate it back and compare.
  size_t compressed_size = sizeof(compressed) - deflater.avail_out;
  Inflater inflater;
  inflater.DataToInflate(compressed, compressed_size);
  uint8_t uncompressed[256];
  memset(uncompressed, 0, sizeof(uncompressed));
  EXPECT_EQ(Z_OK, inflater.Inflate(uncompressed, 3));
  EXPECT_EQ(Z_STREAM_END,
            inflater.Inflate(uncompressed + 3, sizeof(uncompressed) - 3));
  EXPECT_EQ(0, memcmp(bytes, uncompressed, sizeof(bytes)));
}

}  //  namespace