Skip to content

Commit

Permalink
Refactor unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Bill-hbrhbr committed Dec 3, 2024
1 parent 1519c21 commit 655bb46
Showing 1 changed file with 64 additions and 58 deletions.
122 changes: 64 additions & 58 deletions components/core/tests/test-StreamingCompression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
#include <memory>
#include <numeric>
#include <string>
#include <string_view>
#include <utility>

#include <boost/filesystem/operations.hpp>
#include <boost/pointer_cast.hpp>
#include <Catch2/single_include/catch2/catch.hpp>
#include <zstd.h>

Expand All @@ -28,69 +28,39 @@ using clp::ErrorCode_Success;
using clp::FileWriter;
using clp::streaming_compression::Compressor;
using clp::streaming_compression::Decompressor;

TEST_CASE("StreamingCompression", "[StreamingCompression]") {
// Initialize constants
constexpr size_t cBufferSize{128L * 1024 * 1024}; // 128MB
constexpr auto cCompressionChunkSizes = std::to_array<size_t>(
{cBufferSize / 100,
cBufferSize / 50,
cBufferSize / 25,
cBufferSize / 10,
cBufferSize / 5,
cBufferSize / 2,
cBufferSize}
);
constexpr size_t cAlphabetLength{26};
std::string const compressed_file_path{"test_streaming_compressed_file.bin"};

// Initialize compression devices
std::unique_ptr<Compressor> compressor;
std::unique_ptr<Decompressor> decompressor;

SECTION("ZStd single phase compression") {
compressor = std::make_unique<clp::streaming_compression::zstd::Compressor>();
decompressor = std::make_unique<clp::streaming_compression::zstd::Decompressor>();
}

SECTION("Passthrough compression") {
compressor = std::make_unique<clp::streaming_compression::passthrough::Compressor>();
decompressor = std::make_unique<clp::streaming_compression::passthrough::Decompressor>();
}

SECTION("LZMA compression") {
compressor = std::make_unique<clp::streaming_compression::lzma::Compressor>();
}

// Initialize buffers
Array<char> uncompressed_buffer{cBufferSize};
for (size_t i{0}; i < cBufferSize; ++i) {
uncompressed_buffer.at(i) = static_cast<char>(('a' + (i % cAlphabetLength)));
}

Array<char> decompressed_buffer{cBufferSize};

// Compress
using std::string;
using std::string_view;

namespace {
constexpr string_view cCompressedFilePath{"test_streaming_compressed_file.bin"};
constexpr size_t cBufferSize{128L * 1024 * 1024}; // 128MB
constexpr auto cCompressionChunkSizes = std::to_array<size_t>(
{cBufferSize / 100,
cBufferSize / 50,
cBufferSize / 25,
cBufferSize / 10,
cBufferSize / 5,
cBufferSize / 2,
cBufferSize}
);

auto compress(std::unique_ptr<Compressor> compressor, char const* const src) -> void {
FileWriter file_writer;
file_writer.open(compressed_file_path, FileWriter::OpenMode::CREATE_FOR_WRITING);
file_writer.open(string(cCompressedFilePath), FileWriter::OpenMode::CREATE_FOR_WRITING);
compressor->open(file_writer);
for (auto const chunk_size : cCompressionChunkSizes) {
compressor->write(uncompressed_buffer.data(), chunk_size);
compressor->write(src, chunk_size);
}
compressor->close();
file_writer.close();
}

if (boost::dynamic_pointer_cast<clp::streaming_compression::lzma::Compressor>(
std::move(compressor)
))
{
// TODO: remove this LZMA testing early termination
boost::filesystem::remove(compressed_file_path);
return;
}

// Decompress and compare
clp::ReadOnlyMemoryMappedFile const memory_mapped_compressed_file{compressed_file_path};
auto decompress_and_compare(
std::unique_ptr<Decompressor> decompressor,
Array<char> const& uncompressed_buffer,
Array<char>& decompressed_buffer
) -> void {
clp::ReadOnlyMemoryMappedFile const memory_mapped_compressed_file{string(cCompressedFilePath)};
auto const compressed_file_view{memory_mapped_compressed_file.get_view()};
decompressor->open(compressed_file_view.data(), compressed_file_view.size());

Expand Down Expand Up @@ -123,7 +93,43 @@ TEST_CASE("StreamingCompression", "[StreamingCompression]") {
)
== num_uncompressed_bytes)
);
}
} // namespace

TEST_CASE("StreamingCompression", "[StreamingCompression]") {
// Initialize constants
constexpr size_t cAlphabetLength{26};

// Initialize compression devices
std::unique_ptr<Compressor> compressor;
std::unique_ptr<Decompressor> decompressor;

// Initialize buffers
Array<char> decompressed_buffer{cBufferSize};
Array<char> uncompressed_buffer{cBufferSize};
for (size_t i{0}; i < cBufferSize; ++i) {
uncompressed_buffer.at(i) = static_cast<char>(('a' + (i % cAlphabetLength)));
}

SECTION("ZStd single phase compression") {
compressor = std::make_unique<clp::streaming_compression::zstd::Compressor>();
compress(std::move(compressor), uncompressed_buffer.data());
decompressor = std::make_unique<clp::streaming_compression::zstd::Decompressor>();
decompress_and_compare(std::move(decompressor), uncompressed_buffer, decompressed_buffer);
}

SECTION("Passthrough compression") {
compressor = std::make_unique<clp::streaming_compression::passthrough::Compressor>();
compress(std::move(compressor), uncompressed_buffer.data());
decompressor = std::make_unique<clp::streaming_compression::passthrough::Decompressor>();
decompress_and_compare(std::move(decompressor), uncompressed_buffer, decompressed_buffer);
}

SECTION("LZMA compression") {
compressor = std::make_unique<clp::streaming_compression::lzma::Compressor>();
compress(std::move(compressor), uncompressed_buffer.data());
}

// Cleanup
boost::filesystem::remove(compressed_file_path);
boost::filesystem::remove(string(cCompressedFilePath));
}

0 comments on commit 655bb46

Please sign in to comment.