|
| 1 | +/* |
| 2 | + * Copyright 2026-present Alibaba Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "paimon/common/compression/block_compression_factory.h" |
| 18 | + |
| 19 | +#include <cstdint> |
| 20 | +#include <string> |
| 21 | +#include <utility> |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +#include "gtest/gtest.h" |
| 25 | +#include "paimon/defs.h" |
| 26 | +#include "paimon/memory/memory_pool.h" |
| 27 | +#include "paimon/status.h" |
| 28 | +#include "paimon/testing/mock/mock_file_batch_reader.h" |
| 29 | +#include "paimon/testing/utils/read_result_collector.h" |
| 30 | +#include "paimon/testing/utils/testharness.h" |
| 31 | + |
| 32 | +namespace paimon { |
| 33 | +class Predicate; |
| 34 | +} // namespace paimon |
| 35 | + |
| 36 | +namespace paimon::test { |
| 37 | +class CompressionFactoryTest : public ::testing::TestWithParam<BlockCompressionType> {}; |
| 38 | + |
| 39 | +TEST_P(CompressionFactoryTest, TESTCompressThenDecompress) { |
| 40 | + int32_t original_len = 16; |
| 41 | + BlockCompressionType type = GetParam(); |
| 42 | + |
| 43 | + std::string data(original_len, '\0'); |
| 44 | + for (int32_t i = 0; i < original_len; i++) { |
| 45 | + data[i] = static_cast<char>(i); |
| 46 | + } |
| 47 | + |
| 48 | + ASSERT_OK_AND_ASSIGN(auto factory, BlockCompressionFactory::Create(type)); |
| 49 | + ASSERT_EQ(type, factory->GetCompressionType()); |
| 50 | + |
| 51 | + // compress |
| 52 | + auto compressor = factory->GetCompressor(); |
| 53 | + auto max_len = compressor->GetMaxCompressedSize(data.size()); |
| 54 | + std::string compressed_data(max_len, '\0'); |
| 55 | + auto compressed_size = |
| 56 | + compressor->Compress(data.data(), data.size(), compressed_data.data(), max_len); |
| 57 | + ASSERT_OK(compressed_size); |
| 58 | + ASSERT_GT(compressed_size.value(), 0); |
| 59 | + compressed_data.resize(compressed_size.value()); |
| 60 | + |
| 61 | + // decompress |
| 62 | + auto decompressor = factory->GetDecompressor(); |
| 63 | + std::string decompressed_data(original_len, '\0'); |
| 64 | + auto decompressed_size = |
| 65 | + decompressor->Decompress(compressed_data.data(), compressed_data.size(), |
| 66 | + decompressed_data.data(), decompressed_data.size()); |
| 67 | + ASSERT_OK(decompressed_size); |
| 68 | + ASSERT_GT(decompressed_size.value(), 0); |
| 69 | + ASSERT_EQ(data, decompressed_data); |
| 70 | + |
| 71 | + std::string read_write_le{4, '\0'}; |
| 72 | + compressor->WriteIntLE(123, read_write_le.data()); |
| 73 | + ASSERT_EQ(123, decompressor->ReadIntLE(read_write_le.data())); |
| 74 | + compressor->WriteIntLE(100000, read_write_le.data()); |
| 75 | + ASSERT_EQ(100000, decompressor->ReadIntLE(read_write_le.data())); |
| 76 | + compressor->WriteIntLE(-6555, read_write_le.data()); |
| 77 | + ASSERT_EQ(-6555, decompressor->ReadIntLE(read_write_le.data())); |
| 78 | + compressor->WriteIntLE(0, read_write_le.data()); |
| 79 | + ASSERT_EQ(0, decompressor->ReadIntLE(read_write_le.data())); |
| 80 | +} |
| 81 | + |
| 82 | +INSTANTIATE_TEST_SUITE_P(BlockCompressionTypeGroup, CompressionFactoryTest, |
| 83 | + ::testing::Values(BlockCompressionType::LZ4, BlockCompressionType::ZSTD)); |
| 84 | + |
| 85 | +} // namespace paimon::test |
0 commit comments