Skip to content

Commit 45e09b2

Browse files
committed
[core/zip] Add test for compression buffer sizes
This would have found any of the previous three commits.
1 parent 15576ad commit 45e09b2

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

core/zip/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ target_include_directories(Core PUBLIC
2222
)
2323

2424
ROOT_INSTALL_HEADERS()
25+
26+
ROOT_ADD_TEST_SUBDIRECTORY(test)

core/zip/test/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright (C) 1995-2024, Rene Brun and Fons Rademakers.
2+
# All rights reserved.
3+
#
4+
# For the licensing terms see $ROOTSYS/LICENSE.
5+
# For the list of contributors see $ROOTSYS/README/CREDITS.
6+
7+
ROOT_ADD_GTEST(ZipTest ZipTest.cxx LIBRARIES Core)

core/zip/test/ZipTest.cxx

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <Compression.h>
2+
#include <RZip.h>
3+
4+
#include <gtest/gtest.h>
5+
6+
#include <memory>
7+
8+
static void testZipBufferSizes(ROOT::RCompressionSetting::EAlgorithm::EValues compressionAlgorithm)
9+
{
10+
static constexpr size_t BufferSize = 256;
11+
static constexpr size_t MaxBytes = 128;
12+
static_assert(MaxBytes <= BufferSize, "MaxBytes must be smaller than BufferSize");
13+
// For extra "safety", allocate the buffers on the heap to avoid corrupting the stack should anything go wrong.
14+
std::unique_ptr<char[]> source(new char[BufferSize]);
15+
std::unique_ptr<char[]> target(new char[BufferSize]);
16+
17+
// Fill the buffers with monotonically increasing numbers. This is easy to compress, but that's fine because we scan
18+
// through all possible sizes.
19+
for (size_t i = 0; i < BufferSize; i++) {
20+
source[i] = static_cast<char>(i);
21+
target[i] = static_cast<char>(i);
22+
}
23+
24+
// Now test all possible combinations of target and source sizes. The outer loop is for the target sizes because that
25+
// allows us to check that nothing got overwritten.
26+
for (size_t targetSize = 1; targetSize <= MaxBytes; targetSize++) {
27+
for (size_t sourceSize = 1; sourceSize <= MaxBytes; sourceSize++) {
28+
for (int cxlevel = 1; cxlevel <= 9; cxlevel++) {
29+
int srcsize = static_cast<int>(sourceSize);
30+
int tgtsize = static_cast<int>(targetSize);
31+
int irep = -1;
32+
R__zipMultipleAlgorithm(cxlevel, &srcsize, source.get(), &tgtsize, target.get(), &irep,
33+
compressionAlgorithm);
34+
35+
for (size_t i = targetSize + 1; i < BufferSize; i++) {
36+
EXPECT_EQ(target[i], static_cast<char>(i));
37+
}
38+
}
39+
}
40+
}
41+
}
42+
43+
TEST(RZip, ZipBufferSizesOld)
44+
{
45+
testZipBufferSizes(ROOT::RCompressionSetting::EAlgorithm::kOldCompressionAlgo);
46+
}
47+
48+
TEST(RZip, ZipBufferSizesZLIB)
49+
{
50+
testZipBufferSizes(ROOT::RCompressionSetting::EAlgorithm::kZLIB);
51+
}
52+
53+
TEST(RZip, ZipBufferSizesLZMA)
54+
{
55+
testZipBufferSizes(ROOT::RCompressionSetting::EAlgorithm::kLZMA);
56+
}
57+
58+
TEST(RZip, ZipBufferSizesLZ4)
59+
{
60+
testZipBufferSizes(ROOT::RCompressionSetting::EAlgorithm::kLZ4);
61+
}
62+
63+
TEST(RZip, ZipBufferSizesZSTD)
64+
{
65+
testZipBufferSizes(ROOT::RCompressionSetting::EAlgorithm::kZSTD);
66+
}

0 commit comments

Comments
 (0)