Files
Aaru.Checksums.Native/tests/crc16.cpp

140 lines
2.7 KiB
C++
Raw Normal View History

2021-10-05 02:26:18 +01:00
//
// Created by claunia on 5/10/21.
//
2021-10-05 04:38:55 +01:00
#include <climits>
2021-10-05 02:26:18 +01:00
#include <cstdint>
2021-10-06 04:28:26 +01:00
#include <cstring>
2021-10-05 02:26:18 +01:00
#include "../library.h"
#include "../crc16.h"
#include "gtest/gtest.h"
2024-04-30 15:12:48 +01:00
#define EXPECTED_CRC16 0x2d6d
#define EXPECTED_CRC16_15BYTES 0x72a6
#define EXPECTED_CRC16_31BYTES 0xF49E
#define EXPECTED_CRC16_63BYTES 0xFBD9
2021-10-06 03:50:24 +01:00
#define EXPECTED_CRC16_2352BYTES 0x23F4
2021-10-05 02:26:18 +01:00
2023-09-23 18:55:52 +01:00
static const uint8_t *buffer;
static const uint8_t *buffer_misaligned;
2021-10-05 02:26:18 +01:00
class crc16Fixture : public ::testing::Test
{
2023-09-23 18:55:52 +01:00
public:
2021-10-05 02:26:18 +01:00
crc16Fixture()
{
// initialization;
// can also be done in SetUp()
}
2023-09-23 18:55:52 +01:00
protected:
2021-10-05 02:26:18 +01:00
void SetUp()
{
2021-10-05 04:38:55 +01:00
char path[PATH_MAX];
char filename[PATH_MAX];
getcwd(path, PATH_MAX);
snprintf(filename, PATH_MAX, "%s/data/random", path);
2023-09-23 18:55:52 +01:00
FILE *file = fopen(filename, "rb");
2024-04-30 15:12:48 +01:00
buffer = (const uint8_t *)malloc(1048576);
2023-09-23 18:55:52 +01:00
fread((void *)buffer, 1, 1048576, file);
2021-10-05 02:26:18 +01:00
fclose(file);
2021-10-06 04:28:26 +01:00
2023-09-23 18:55:52 +01:00
buffer_misaligned = (const uint8_t *)malloc(1048577);
memcpy((void *)(buffer_misaligned + 1), buffer, 1048576);
2021-10-05 02:26:18 +01:00
}
2023-09-23 18:55:52 +01:00
void TearDown()
{
free((void *)buffer);
free((void *)buffer_misaligned);
2021-10-06 04:28:26 +01:00
}
2021-10-05 02:26:18 +01:00
~crc16Fixture()
{
// resources cleanup, no exceptions allowed
}
// shared user data
};
TEST_F(crc16Fixture, crc16_auto)
{
2023-09-23 18:55:52 +01:00
crc16_ctx *ctx = crc16_init();
2024-04-30 15:12:48 +01:00
uint16_t crc;
2021-10-05 02:26:18 +01:00
EXPECT_NE(ctx, nullptr);
crc16_update(ctx, buffer, 1048576);
crc16_final(ctx, &crc);
2021-10-06 04:28:26 +01:00
EXPECT_EQ(crc, EXPECTED_CRC16);
}
TEST_F(crc16Fixture, crc16_auto_misaligned)
{
2023-09-23 18:55:52 +01:00
crc16_ctx *ctx = crc16_init();
2024-04-30 15:12:48 +01:00
uint16_t crc;
2021-10-06 04:28:26 +01:00
EXPECT_NE(ctx, nullptr);
2023-09-23 18:55:52 +01:00
crc16_update(ctx, buffer_misaligned + 1, 1048576);
2021-10-06 04:28:26 +01:00
crc16_final(ctx, &crc);
2021-10-05 02:26:18 +01:00
EXPECT_EQ(crc, EXPECTED_CRC16);
}
2021-10-06 03:50:24 +01:00
TEST_F(crc16Fixture, crc16_auto_15bytes)
{
2023-09-23 18:55:52 +01:00
crc16_ctx *ctx = crc16_init();
2024-04-30 15:12:48 +01:00
uint16_t crc;
2021-10-06 03:50:24 +01:00
EXPECT_NE(ctx, nullptr);
crc16_update(ctx, buffer, 15);
crc16_final(ctx, &crc);
EXPECT_EQ(crc, EXPECTED_CRC16_15BYTES);
}
TEST_F(crc16Fixture, crc16_auto_31bytes)
{
2023-09-23 18:55:52 +01:00
crc16_ctx *ctx = crc16_init();
2024-04-30 15:12:48 +01:00
uint16_t crc;
2021-10-06 03:50:24 +01:00
EXPECT_NE(ctx, nullptr);
crc16_update(ctx, buffer, 31);
crc16_final(ctx, &crc);
EXPECT_EQ(crc, EXPECTED_CRC16_31BYTES);
}
TEST_F(crc16Fixture, crc16_auto_63bytes)
{
2023-09-23 18:55:52 +01:00
crc16_ctx *ctx = crc16_init();
2024-04-30 15:12:48 +01:00
uint16_t crc;
2021-10-06 03:50:24 +01:00
EXPECT_NE(ctx, nullptr);
crc16_update(ctx, buffer, 63);
crc16_final(ctx, &crc);
EXPECT_EQ(crc, EXPECTED_CRC16_63BYTES);
}
TEST_F(crc16Fixture, crc16_auto_2352bytes)
{
2023-09-23 18:55:52 +01:00
crc16_ctx *ctx = crc16_init();
2024-04-30 15:12:48 +01:00
uint16_t crc;
2021-10-06 03:50:24 +01:00
EXPECT_NE(ctx, nullptr);
crc16_update(ctx, buffer, 2352);
crc16_final(ctx, &crc);
EXPECT_EQ(crc, EXPECTED_CRC16_2352BYTES);
}