Skip to content

Commit 66b7660

Browse files
committed
Add compression test
1 parent c5eb667 commit 66b7660

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

include/sparrow_ipc/compression.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include "Message_generated.h"
88

9+
#include "sparrow_ipc/config/config.hpp"
10+
911
namespace sparrow_ipc
1012
{
1113
// TODO use these later if needed for wrapping purposes (flatbuffers/lz4)
@@ -18,6 +20,6 @@ namespace sparrow_ipc
1820

1921
// CompressionType to_compression_type(org::apache::arrow::flatbuf::CompressionType compression_type);
2022

21-
std::vector<std::uint8_t> compress(const org::apache::arrow::flatbuf::CompressionType compression_type, std::span<const std::uint8_t> data);
22-
std::vector<std::uint8_t> decompress(const org::apache::arrow::flatbuf::CompressionType compression_type, std::span<const std::uint8_t> data);
23+
SPARROW_IPC_API std::vector<std::uint8_t> compress(const org::apache::arrow::flatbuf::CompressionType compression_type, std::span<const std::uint8_t> data);
24+
SPARROW_IPC_API std::vector<std::uint8_t> decompress(const org::apache::arrow::flatbuf::CompressionType compression_type, std::span<const std::uint8_t> data);
2325
}

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ set(SPARROW_IPC_TESTS_SRC
1010
test_arrow_schema.cpp
1111
test_chunk_memory_output_stream.cpp
1212
test_chunk_memory_serializer.cpp
13+
test_compression.cpp
1314
test_de_serialization_with_files.cpp
1415
$<$<NOT:$<BOOL:${SPARROW_IPC_BUILD_SHARED}>>:test_flatbuffer_utils.cpp>
1516
test_memory_output_streams.cpp

tests/test_compression.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <string>
2+
#include <vector>
3+
4+
#include <doctest/doctest.h>
5+
6+
#include <sparrow_ipc/compression.hpp>
7+
8+
namespace sparrow_ipc
9+
{
10+
TEST_CASE("Compression and Decompression Round-trip")
11+
{
12+
std::string original_string = "hello world, this is a test of compression and decompression!!";
13+
std::vector<uint8_t> original_data(original_string.begin(), original_string.end());
14+
const int64_t original_size = original_data.size();
15+
16+
// Compress data
17+
auto compression_type = org::apache::arrow::flatbuf::CompressionType::LZ4_FRAME;
18+
std::vector<uint8_t> compressed_frame = compress(compression_type, original_data);
19+
20+
CHECK_GT(compressed_frame.size(), 0);
21+
CHECK_NE(compressed_frame, original_data);
22+
23+
// Manually create the IPC-formatted compressed buffer by adding the 8-byte prefix
24+
std::vector<uint8_t> ipc_formatted_buffer;
25+
ipc_formatted_buffer.reserve(sizeof(int64_t) + compressed_frame.size());
26+
ipc_formatted_buffer.insert(ipc_formatted_buffer.end(), reinterpret_cast<const uint8_t*>(&original_size), reinterpret_cast<const uint8_t*>(&original_size) + sizeof(int64_t));
27+
ipc_formatted_buffer.insert(ipc_formatted_buffer.end(), compressed_frame.begin(), compressed_frame.end());
28+
29+
// Decompress
30+
std::vector<uint8_t> decompressed_data = decompress(compression_type, ipc_formatted_buffer);
31+
32+
CHECK_EQ(decompressed_data.size(), original_data.size());
33+
CHECK_EQ(decompressed_data, original_data);
34+
}
35+
}

0 commit comments

Comments
 (0)