|
| 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