aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools/singlejar/zlib_interface_test.cc
diff options
context:
space:
mode:
authorGravatar Sasha Smundak <asmundak@google.com>2016-06-30 23:00:01 +0000
committerGravatar Lukacs Berki <lberki@google.com>2016-07-01 07:12:12 +0000
commitf667aa54f4fcc2c04182de9bc267a7ee469f6445 (patch)
tree8521560d5eb01be7a05fa0d711e96223557b0b64 /src/tools/singlejar/zlib_interface_test.cc
parent583f9e9d5a847affde188f0059b398fa016394d2 (diff)
C++ reimplementation of singlejar tool: first checkin.
-- MOS_MIGRATED_REVID=126354275
Diffstat (limited to 'src/tools/singlejar/zlib_interface_test.cc')
-rw-r--r--src/tools/singlejar/zlib_interface_test.cc100
1 files changed, 100 insertions, 0 deletions
diff --git a/src/tools/singlejar/zlib_interface_test.cc b/src/tools/singlejar/zlib_interface_test.cc
new file mode 100644
index 0000000000..802d15350e
--- /dev/null
+++ b/src/tools/singlejar/zlib_interface_test.cc
@@ -0,0 +1,100 @@
+// 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 <memory>
+
+#include "src/tools/singlejar/zlib_interface.h"
+
+#include "gtest/gtest.h"
+
+namespace {
+
+class ZlibInterfaceTest : public ::testing::Test {
+ protected:
+ void SetUp() override {
+ inflater_.reset(new Inflater);
+ deflater_.reset(new Deflater);
+ }
+
+ std::unique_ptr<Inflater> inflater_;
+ std::unique_ptr<Deflater> deflater_;
+};
+
+static const uint8_t bytes[] = {1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4};
+
+TEST_F(ZlibInterfaceTest, DeflateFully) {
+ uint8_t compressed[256];
+ deflater_.get()->next_out = compressed;
+ deflater_.get()->avail_out = sizeof(compressed);
+ EXPECT_EQ(Z_STREAM_END, deflater_->Deflate(bytes, sizeof(bytes), Z_FINISH));
+}
+
+TEST_F(ZlibInterfaceTest, DeflateIntoChunks) {
+ uint8_t compressed[256];
+ deflater_.get()->next_out = compressed;
+ deflater_.get()->avail_out = 2;
+ EXPECT_EQ(Z_OK, deflater_->Deflate(bytes, sizeof(bytes), Z_FINISH));
+ EXPECT_EQ(0, deflater_.get()->avail_out);
+ deflater_.get()->next_out = compressed + 2;
+ deflater_.get()->avail_out = sizeof(compressed) - 2;
+ EXPECT_EQ(Z_STREAM_END,
+ deflater_->Deflate(deflater_.get()->next_in,
+ deflater_.get()->avail_in, Z_FINISH));
+}
+
+TEST_F(ZlibInterfaceTest, DeflateChunks) {
+ uint8_t compressed[256];
+ deflater_.get()->next_out = compressed;
+ deflater_.get()->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_F(ZlibInterfaceTest, InflateFully) {
+ uint8_t compressed[256];
+ deflater_.get()->next_out = compressed;
+ deflater_.get()->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_.get()->avail_out;
+ 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_F(ZlibInterfaceTest, InflateToChunks) {
+ uint8_t compressed[256];
+ deflater_.get()->next_out = compressed;
+ deflater_.get()->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_.get()->avail_out;
+ 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