Files
Aaru.Compression.Native/tests/zstd.cpp

132 lines
3.4 KiB
C++
Raw Permalink Normal View History

2021-10-19 03:44:20 +01:00
/*
* This file is part of the Aaru Data Preservation Suite.
2025-12-23 08:21:08 +00:00
* Copyright (c) 2019-2026 Natalia Portillo.
2021-10-19 03:44:20 +01:00
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include <climits>
#include <cstddef>
2021-10-19 03:44:20 +01:00
#include <cstdint>
#include "../library.h"
#include "crc32.h"
#include "gtest/gtest.h"
#define EXPECTED_CRC32 0xc64059c0
2024-04-30 15:37:29 +01:00
static const uint8_t *buffer;
2021-10-19 03:44:20 +01:00
class zstdFixture : public ::testing::Test
{
2024-04-30 15:37:29 +01:00
public:
2021-10-19 03:44:20 +01:00
zstdFixture()
{
// initialization;
// can also be done in SetUp()
}
2024-04-30 15:37:29 +01:00
protected:
2021-10-19 03:44:20 +01:00
void SetUp()
{
char path[PATH_MAX];
char filename[PATH_MAX];
getcwd(path, PATH_MAX);
snprintf(filename, PATH_MAX, "%s/data/zstd.zst", path);
2024-04-30 15:37:29 +01:00
FILE *file = fopen(filename, "rb");
buffer = (const uint8_t *)malloc(1048613);
fread((void *)buffer, 1, 1048613, file);
2021-10-19 03:44:20 +01:00
fclose(file);
}
2024-04-30 15:37:29 +01:00
void TearDown() { free((void *)buffer); }
2021-10-19 03:44:20 +01:00
~zstdFixture()
{
// resources cleanup, no exceptions allowed
}
// shared user data
};
TEST_F(zstdFixture, zstd)
{
2024-04-30 15:37:29 +01:00
auto *outBuf = (uint8_t *)malloc(1048576);
2021-10-19 03:44:20 +01:00
Unify external API signatures across all buffer transforms Every exported buffer transform now has the same shape: int32_t AARU_xxx(const uint8_t *src_buffer, size_t src_size, uint8_t *dst_buffer, size_t *dst_size, ...extras); On entry *dst_size is the capacity of dst_buffer; on success it holds the number of bytes written. The return value is 0 (AARU_ERROR_NONE) on success and non-zero on error, either an AARU_ERROR_* constant or the underlying library's own error code when it is propagated. Changes by category: - Parameter reorder only: all AARU_zip_*, AARU_stuffit*, AARU_dd_*, AARU_cpt_*, AARU_lzo_*, AARU_xz_*, AARU_lzma2_*, AARU_kencode_*, AARU_apple_lzh_* and ace_decompress_*. - Byte count return replaced by an out-parameter plus a status code: AARU_adc_*, AARU_apple_rle_*, AARU_lz4_*, AARU_lzip_*, AARU_lzfse_*, AARU_lzvn_*, AARU_zstd_* and AARU_flac_*. Their algorithm bodies are kept as static internals behind a thin exported wrapper. - Type normalisation: bzip2 uint32_t* -> size_t*, zstd void* -> uint8_t*, ZIP PPMd size_t -> size_t*, and int/unsigned char -> int32_t/uint8_t throughout the header. - AARU_lzma_decode_buffer no longer takes src_size as an in/out pointer; the consumed length is tracked internally. - Already conforming decompressors (arc_*, pak_*, ha_*, lha_*, larc_*, pmarc_*, arj*, rar*, lh5_decompress) only had their declarations normalised. The streaming LZD context API and AARU_get_acn_version are left alone, as they are not buffer transforms. Tests updated to the new call convention; all 78 pass.
2026-08-31 12:26:05 +01:00
size_t decoded = 1048576;
2021-10-19 03:44:20 +01:00
Unify external API signatures across all buffer transforms Every exported buffer transform now has the same shape: int32_t AARU_xxx(const uint8_t *src_buffer, size_t src_size, uint8_t *dst_buffer, size_t *dst_size, ...extras); On entry *dst_size is the capacity of dst_buffer; on success it holds the number of bytes written. The return value is 0 (AARU_ERROR_NONE) on success and non-zero on error, either an AARU_ERROR_* constant or the underlying library's own error code when it is propagated. Changes by category: - Parameter reorder only: all AARU_zip_*, AARU_stuffit*, AARU_dd_*, AARU_cpt_*, AARU_lzo_*, AARU_xz_*, AARU_lzma2_*, AARU_kencode_*, AARU_apple_lzh_* and ace_decompress_*. - Byte count return replaced by an out-parameter plus a status code: AARU_adc_*, AARU_apple_rle_*, AARU_lz4_*, AARU_lzip_*, AARU_lzfse_*, AARU_lzvn_*, AARU_zstd_* and AARU_flac_*. Their algorithm bodies are kept as static internals behind a thin exported wrapper. - Type normalisation: bzip2 uint32_t* -> size_t*, zstd void* -> uint8_t*, ZIP PPMd size_t -> size_t*, and int/unsigned char -> int32_t/uint8_t throughout the header. - AARU_lzma_decode_buffer no longer takes src_size as an in/out pointer; the consumed length is tracked internally. - Already conforming decompressors (arc_*, pak_*, ha_*, lha_*, larc_*, pmarc_*, arj*, rar*, lh5_decompress) only had their declarations normalised. The streaming LZD context API and AARU_get_acn_version are left alone, as they are not buffer transforms. Tests updated to the new call convention; all 78 pass.
2026-08-31 12:26:05 +01:00
EXPECT_EQ(AARU_zstd_decode_buffer(buffer, 1048613, outBuf, &decoded), 0);
2021-10-19 03:44:20 +01:00
EXPECT_EQ(decoded, 1048576);
auto crc = crc32_data(outBuf, 1048576);
free(outBuf);
EXPECT_EQ(crc, EXPECTED_CRC32);
}
2021-10-21 05:02:52 +01:00
TEST_F(zstdFixture, zstdCompress)
{
size_t original_len = 8388608;
Unify external API signatures across all buffer transforms Every exported buffer transform now has the same shape: int32_t AARU_xxx(const uint8_t *src_buffer, size_t src_size, uint8_t *dst_buffer, size_t *dst_size, ...extras); On entry *dst_size is the capacity of dst_buffer; on success it holds the number of bytes written. The return value is 0 (AARU_ERROR_NONE) on success and non-zero on error, either an AARU_ERROR_* constant or the underlying library's own error code when it is propagated. Changes by category: - Parameter reorder only: all AARU_zip_*, AARU_stuffit*, AARU_dd_*, AARU_cpt_*, AARU_lzo_*, AARU_xz_*, AARU_lzma2_*, AARU_kencode_*, AARU_apple_lzh_* and ace_decompress_*. - Byte count return replaced by an out-parameter plus a status code: AARU_adc_*, AARU_apple_rle_*, AARU_lz4_*, AARU_lzip_*, AARU_lzfse_*, AARU_lzvn_*, AARU_zstd_* and AARU_flac_*. Their algorithm bodies are kept as static internals behind a thin exported wrapper. - Type normalisation: bzip2 uint32_t* -> size_t*, zstd void* -> uint8_t*, ZIP PPMd size_t -> size_t*, and int/unsigned char -> int32_t/uint8_t throughout the header. - AARU_lzma_decode_buffer no longer takes src_size as an in/out pointer; the consumed length is tracked internally. - Already conforming decompressors (arc_*, pak_*, ha_*, lha_*, larc_*, pmarc_*, arj*, rar*, lh5_decompress) only had their declarations normalised. The streaming LZD context API and AARU_get_acn_version are left alone, as they are not buffer transforms. Tests updated to the new call convention; all 78 pass.
2026-08-31 12:26:05 +01:00
size_t cmp_len = original_len;
size_t decmp_len = original_len;
2021-10-21 05:02:52 +01:00
char path[PATH_MAX];
char filename[PATH_MAX * 2];
2024-04-30 15:37:29 +01:00
FILE *file;
2021-10-21 05:02:52 +01:00
uint32_t original_crc, decmp_crc;
2024-04-30 15:37:29 +01:00
const uint8_t *original;
uint8_t *cmp_buffer;
uint8_t *decmp_buffer;
2021-10-21 05:02:52 +01:00
size_t newSize;
// Allocate buffers
2024-04-30 15:37:29 +01:00
original = (const uint8_t *)malloc(original_len);
cmp_buffer = (uint8_t *)malloc(cmp_len);
decmp_buffer = (uint8_t *)malloc(decmp_len);
2021-10-21 05:02:52 +01:00
// Read the file
getcwd(path, PATH_MAX);
snprintf(filename, PATH_MAX, "%s/data/data.bin", path);
file = fopen(filename, "rb");
2024-04-30 15:37:29 +01:00
fread((void *)original, 1, original_len, file);
2021-10-21 05:02:52 +01:00
fclose(file);
// Calculate the CRC
original_crc = crc32_data(original, original_len);
// Compress
Unify external API signatures across all buffer transforms Every exported buffer transform now has the same shape: int32_t AARU_xxx(const uint8_t *src_buffer, size_t src_size, uint8_t *dst_buffer, size_t *dst_size, ...extras); On entry *dst_size is the capacity of dst_buffer; on success it holds the number of bytes written. The return value is 0 (AARU_ERROR_NONE) on success and non-zero on error, either an AARU_ERROR_* constant or the underlying library's own error code when it is propagated. Changes by category: - Parameter reorder only: all AARU_zip_*, AARU_stuffit*, AARU_dd_*, AARU_cpt_*, AARU_lzo_*, AARU_xz_*, AARU_lzma2_*, AARU_kencode_*, AARU_apple_lzh_* and ace_decompress_*. - Byte count return replaced by an out-parameter plus a status code: AARU_adc_*, AARU_apple_rle_*, AARU_lz4_*, AARU_lzip_*, AARU_lzfse_*, AARU_lzvn_*, AARU_zstd_* and AARU_flac_*. Their algorithm bodies are kept as static internals behind a thin exported wrapper. - Type normalisation: bzip2 uint32_t* -> size_t*, zstd void* -> uint8_t*, ZIP PPMd size_t -> size_t*, and int/unsigned char -> int32_t/uint8_t throughout the header. - AARU_lzma_decode_buffer no longer takes src_size as an in/out pointer; the consumed length is tracked internally. - Already conforming decompressors (arc_*, pak_*, ha_*, lha_*, larc_*, pmarc_*, arj*, rar*, lh5_decompress) only had their declarations normalised. The streaming LZD context API and AARU_get_acn_version are left alone, as they are not buffer transforms. Tests updated to the new call convention; all 78 pass.
2026-08-31 12:26:05 +01:00
newSize = cmp_len;
EXPECT_EQ(AARU_zstd_encode_buffer(original, original_len, cmp_buffer, &newSize, 22), 0);
2021-10-21 05:02:52 +01:00
cmp_len = newSize;
// Decompress
Unify external API signatures across all buffer transforms Every exported buffer transform now has the same shape: int32_t AARU_xxx(const uint8_t *src_buffer, size_t src_size, uint8_t *dst_buffer, size_t *dst_size, ...extras); On entry *dst_size is the capacity of dst_buffer; on success it holds the number of bytes written. The return value is 0 (AARU_ERROR_NONE) on success and non-zero on error, either an AARU_ERROR_* constant or the underlying library's own error code when it is propagated. Changes by category: - Parameter reorder only: all AARU_zip_*, AARU_stuffit*, AARU_dd_*, AARU_cpt_*, AARU_lzo_*, AARU_xz_*, AARU_lzma2_*, AARU_kencode_*, AARU_apple_lzh_* and ace_decompress_*. - Byte count return replaced by an out-parameter plus a status code: AARU_adc_*, AARU_apple_rle_*, AARU_lz4_*, AARU_lzip_*, AARU_lzfse_*, AARU_lzvn_*, AARU_zstd_* and AARU_flac_*. Their algorithm bodies are kept as static internals behind a thin exported wrapper. - Type normalisation: bzip2 uint32_t* -> size_t*, zstd void* -> uint8_t*, ZIP PPMd size_t -> size_t*, and int/unsigned char -> int32_t/uint8_t throughout the header. - AARU_lzma_decode_buffer no longer takes src_size as an in/out pointer; the consumed length is tracked internally. - Already conforming decompressors (arc_*, pak_*, ha_*, lha_*, larc_*, pmarc_*, arj*, rar*, lh5_decompress) only had their declarations normalised. The streaming LZD context API and AARU_get_acn_version are left alone, as they are not buffer transforms. Tests updated to the new call convention; all 78 pass.
2026-08-31 12:26:05 +01:00
newSize = decmp_len;
EXPECT_EQ(AARU_zstd_decode_buffer(cmp_buffer, cmp_len, decmp_buffer, &newSize), 0);
2021-10-21 05:02:52 +01:00
decmp_len = newSize;
EXPECT_EQ(decmp_len, original_len);
decmp_crc = crc32_data(decmp_buffer, decmp_len);
// Free buffers
2024-04-30 15:37:29 +01:00
free((void *)original);
2021-10-21 05:02:52 +01:00
free(cmp_buffer);
free(decmp_buffer);
EXPECT_EQ(decmp_crc, original_crc);
}