diff options
author | Adenilson Cavalcanti <adenilson.cavalcanti@arm.com> | 2020-04-08 23:34:58 +0000 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2020-04-08 23:34:58 +0000 |
commit | 61bddccfbf47b9e6bbce6e39d3f2458e27c58c6a (patch) | |
tree | 527bb02044c2ce7ee0dbb1bb6582450625b1e816 /contrib | |
parent | 6ba41dc1141cc6d4058f288345269607a1089501 (diff) | |
download | zlib-61bddccfbf47b9e6bbce6e39d3f2458e27c58c6a.tar.gz |
Adding a utest for small payloads
One of the optimizations (i.e. chunk_copy) will perform vector stores on
16 bytes chunks instead of the original 3 bytes scalar operations.
It is interesting to validate its safety while operating with small
payloads (i.e. data input smaller than a single load/store).
Even though it is a corner case (i.e. the payload would be smaller than
the wrapper used for the DEFLATE stream for GZIP), it is good to certify
that the optimization works as expected.
This will also add gtest as a dependency as the plan is to write some
tests to stress the optimizations we ship.
Bug: 1032721
Change-Id: Ifc6a81879e3dba6a9c4b7cfde80e7207258b934c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2128836
Commit-Queue: Adenilson Cavalcanti <cavalcantii@chromium.org>
Reviewed-by: Chris Blume <cblume@chromium.org>
Reviewed-by: Victor Costan <pwnall@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#757639}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: 272595ed5f469ee379e28dd5c40ef0230b6680a5
Diffstat (limited to 'contrib')
-rw-r--r-- | contrib/tests/utils_unittest.cc | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/contrib/tests/utils_unittest.cc b/contrib/tests/utils_unittest.cc new file mode 100644 index 0000000..6e1f5c9 --- /dev/null +++ b/contrib/tests/utils_unittest.cc @@ -0,0 +1,58 @@ +// Copyright 2020 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the Chromium source repository LICENSE file. + +#include <cstddef> +#include <vector> + +#include "testing/gtest/include/gtest/gtest.h" +#include "third_party/zlib/google/compression_utils_portable.h" + +#include "zlib.h" + +void TestPayloads(size_t input_size, zlib_internal::WrapperType type) { + std::vector<unsigned char> input; + input.reserve(input_size); + for (size_t i = 1; i <= input_size; ++i) + input.push_back(i & 0xff); + + // If it is big enough for GZIP, will work for other wrappers. + std::vector<unsigned char> compressed( + zlib_internal::GzipExpectedCompressedSize(input.size())); + std::vector<unsigned char> decompressed(input.size()); + + // Libcores's java/util/zip/Deflater default settings: ZLIB, + // DEFAULT_COMPRESSION and DEFAULT_STRATEGY. + unsigned long compressed_size = static_cast<unsigned long>(compressed.size()); + int result = zlib_internal::CompressHelper( + type, compressed.data(), &compressed_size, input.data(), input.size(), + Z_DEFAULT_COMPRESSION, nullptr, nullptr); + ASSERT_EQ(result, Z_OK); + + unsigned long decompressed_size = + static_cast<unsigned long>(decompressed.size()); + result = zlib_internal::UncompressHelper(type, decompressed.data(), + &decompressed_size, + compressed.data(), compressed_size); + ASSERT_EQ(result, Z_OK); + EXPECT_EQ(input, decompressed); +} + +TEST(ZlibTest, ZlibWrapper) { + // Minimal ZLIB wrapped short stream size is about 8 bytes. + for (size_t i = 1; i < 1024; ++i) + TestPayloads(i, zlib_internal::WrapperType::ZLIB); +} + +TEST(ZlibTest, GzipWrapper) { + // GZIP should be 12 bytes bigger than ZLIB wrapper. + for (size_t i = 1; i < 1024; ++i) + TestPayloads(i, zlib_internal::WrapperType::GZIP); +} + +TEST(ZlibTest, RawWrapper) { + // RAW has no wrapper (V8 Blobs is a known user), size + // should be payload_size + 2 for short payloads. + for (size_t i = 1; i < 1024; ++i) + TestPayloads(i, zlib_internal::WrapperType::ZRAW); +} |