Files
libaaruformat/tests/flac.cpp

133 lines
3.5 KiB
C++
Raw Normal View History

2022-05-28 13:38:09 +01:00
/*
* This file is part of the Aaru Data Preservation Suite.
2025-08-01 21:19:45 +01:00
* Copyright (c) 2019-2025 Natalia Portillo.
2022-05-28 13:38:09 +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>
#include <cstdint>
#include <aaruformat.h>
#include "../include/aaruformat/flac.h"
#include "crc32.h"
#include "gtest/gtest.h"
#define EXPECTED_CRC32 0xdfbc99bb
2024-04-30 15:51:32 +01:00
static const uint8_t *buffer;
2022-05-28 13:38:09 +01:00
class flacFixture : public ::testing::Test
{
2024-04-30 15:51:32 +01:00
public:
2022-05-28 13:38:09 +01:00
flacFixture()
{
// initialization;
// can also be done in SetUp()
}
2024-04-30 15:51:32 +01:00
protected:
2022-05-28 13:38:09 +01:00
void SetUp()
{
char path[PATH_MAX];
char filename[PATH_MAX];
getcwd(path, PATH_MAX);
snprintf(filename, PATH_MAX, "%s/data/flac.flac", path);
2024-04-30 15:51:32 +01:00
FILE *file = fopen(filename, "rb");
buffer = (const uint8_t *)malloc(6534197);
fread((void *)buffer, 1, 6534197, file);
2022-05-28 13:38:09 +01:00
fclose(file);
}
2024-04-30 15:51:32 +01:00
void TearDown() { free((void *)buffer); }
2022-05-28 13:38:09 +01:00
~flacFixture()
{
// resources cleanup, no exceptions allowed
}
// shared user data
};
TEST_F(flacFixture, flac)
{
2024-04-30 15:51:32 +01:00
auto *outBuf = (uint8_t *)malloc(9633792);
2022-05-28 13:38:09 +01:00
auto decoded = aaruf_flac_decode_redbook_buffer(outBuf, 9633792, buffer, 6534197);
EXPECT_EQ(decoded, 9633792);
auto crc = crc32_data(outBuf, 9633792);
free(outBuf);
EXPECT_EQ(crc, EXPECTED_CRC32);
}
TEST_F(flacFixture, flacCompress)
{
size_t original_len = 9633792;
uint cmp_len = original_len;
uint decmp_len = original_len;
char path[PATH_MAX];
char filename[PATH_MAX * 2];
2024-04-30 15:51:32 +01:00
FILE *file;
2022-05-28 13:38:09 +01:00
uint32_t original_crc, decmp_crc;
2024-04-30 15:51:32 +01:00
const uint8_t *original;
uint8_t *cmp_buffer;
uint8_t *decmp_buffer;
2022-05-28 13:38:09 +01:00
size_t newSize;
// Allocate buffers
2024-04-30 15:51:32 +01:00
original = (const uint8_t *)malloc(original_len);
cmp_buffer = (uint8_t *)malloc(cmp_len);
decmp_buffer = (uint8_t *)malloc(decmp_len);
2022-05-28 13:38:09 +01:00
// Read the file
getcwd(path, PATH_MAX);
snprintf(filename, PATH_MAX, "%s/data/audio.bin", path);
file = fopen(filename, "rb");
2024-04-30 15:51:32 +01:00
fread((void *)original, 1, original_len, file);
2022-05-28 13:38:09 +01:00
fclose(file);
// Calculate the CRC
original_crc = crc32_data(original, original_len);
// Compress
2024-04-30 15:51:32 +01:00
newSize = aaruf_flac_encode_redbook_buffer(
cmp_buffer, cmp_len, original, original_len, 4608, 1, 0, "partial_tukey(0/1.0/1.0)", 12, 0, 1, false, 0, 8,
"Aaru.Compression.Native.Tests", strlen("Aaru.Compression.Native.Tests"));
2022-05-28 13:38:09 +01:00
cmp_len = newSize;
// Decompress
newSize = aaruf_flac_decode_redbook_buffer(decmp_buffer, decmp_len, cmp_buffer, cmp_len);
decmp_len = newSize;
EXPECT_EQ(decmp_len, original_len);
decmp_crc = crc32_data(decmp_buffer, decmp_len);
// Free buffers
2024-04-30 15:51:32 +01:00
free((void *)original);
2022-05-28 13:38:09 +01:00
free(cmp_buffer);
free(decmp_buffer);
EXPECT_EQ(decmp_crc, original_crc);
}