2021-10-05 02:29:12 +01:00
|
|
|
//
|
|
|
|
|
// Created by claunia on 5/10/21.
|
|
|
|
|
//
|
|
|
|
|
|
2021-10-05 04:38:55 +01:00
|
|
|
#include <climits>
|
2021-10-05 02:29:12 +01:00
|
|
|
#include <cstdint>
|
2021-10-06 04:28:26 +01:00
|
|
|
#include <cstring>
|
2021-10-05 02:29:12 +01:00
|
|
|
|
|
|
|
|
#include "../library.h"
|
2021-10-05 04:38:55 +01:00
|
|
|
#include "../crc16_ccitt.h"
|
2021-10-05 02:29:12 +01:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
2024-04-30 15:12:48 +01:00
|
|
|
#define EXPECTED_CRC16_CCITT 0x3640
|
|
|
|
|
#define EXPECTED_CRC16_CCITT_15BYTES 0x166e
|
|
|
|
|
#define EXPECTED_CRC16_CCITT_31BYTES 0xd016
|
|
|
|
|
#define EXPECTED_CRC16_CCITT_63BYTES 0x73c4
|
2021-10-06 03:50:24 +01:00
|
|
|
#define EXPECTED_CRC16_CCITT_2352BYTES 0x1946
|
2021-10-05 02:29:12 +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:29:12 +01:00
|
|
|
|
|
|
|
|
class crc16_ccittFixture : public ::testing::Test
|
|
|
|
|
{
|
2023-09-23 18:55:52 +01:00
|
|
|
public:
|
2021-10-05 02:29:12 +01:00
|
|
|
crc16_ccittFixture()
|
|
|
|
|
{
|
|
|
|
|
// initialization;
|
|
|
|
|
// can also be done in SetUp()
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-23 18:55:52 +01:00
|
|
|
protected:
|
2021-10-05 02:29:12 +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:29:12 +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:29:12 +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:29:12 +01:00
|
|
|
|
|
|
|
|
~crc16_ccittFixture()
|
|
|
|
|
{
|
|
|
|
|
// resources cleanup, no exceptions allowed
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// shared user data
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
TEST_F(crc16_ccittFixture, crc16_ccitt_auto)
|
|
|
|
|
{
|
2023-09-23 18:55:52 +01:00
|
|
|
crc16_ccitt_ctx *ctx = crc16_ccitt_init();
|
2024-04-30 15:12:48 +01:00
|
|
|
uint16_t crc;
|
2021-10-05 02:29:12 +01:00
|
|
|
|
|
|
|
|
EXPECT_NE(ctx, nullptr);
|
|
|
|
|
|
|
|
|
|
crc16_ccitt_update(ctx, buffer, 1048576);
|
|
|
|
|
crc16_ccitt_final(ctx, &crc);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(crc, EXPECTED_CRC16_CCITT);
|
|
|
|
|
}
|
2021-10-06 03:50:24 +01:00
|
|
|
|
2021-10-06 04:28:26 +01:00
|
|
|
TEST_F(crc16_ccittFixture, crc16_ccitt_auto_misaligned)
|
|
|
|
|
{
|
2023-09-23 18:55:52 +01:00
|
|
|
crc16_ccitt_ctx *ctx = crc16_ccitt_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_ccitt_update(ctx, buffer_misaligned + 1, 1048576);
|
2021-10-06 04:28:26 +01:00
|
|
|
crc16_ccitt_final(ctx, &crc);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(crc, EXPECTED_CRC16_CCITT);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 03:50:24 +01:00
|
|
|
TEST_F(crc16_ccittFixture, crc16_ccitt_auto_15bytes)
|
|
|
|
|
{
|
2023-09-23 18:55:52 +01:00
|
|
|
crc16_ccitt_ctx *ctx = crc16_ccitt_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_ccitt_update(ctx, buffer, 15);
|
|
|
|
|
crc16_ccitt_final(ctx, &crc);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(crc, EXPECTED_CRC16_CCITT_15BYTES);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(crc16_ccittFixture, crc16_ccitt_auto_31bytes)
|
|
|
|
|
{
|
2023-09-23 18:55:52 +01:00
|
|
|
crc16_ccitt_ctx *ctx = crc16_ccitt_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_ccitt_update(ctx, buffer, 31);
|
|
|
|
|
crc16_ccitt_final(ctx, &crc);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(crc, EXPECTED_CRC16_CCITT_31BYTES);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(crc16_ccittFixture, crc16_ccitt_auto_63bytes)
|
|
|
|
|
{
|
2023-09-23 18:55:52 +01:00
|
|
|
crc16_ccitt_ctx *ctx = crc16_ccitt_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_ccitt_update(ctx, buffer, 63);
|
|
|
|
|
crc16_ccitt_final(ctx, &crc);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(crc, EXPECTED_CRC16_CCITT_63BYTES);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(crc16_ccittFixture, crc16_ccitt_auto_2352bytes)
|
|
|
|
|
{
|
2023-09-23 18:55:52 +01:00
|
|
|
crc16_ccitt_ctx *ctx = crc16_ccitt_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_ccitt_update(ctx, buffer, 2352);
|
|
|
|
|
crc16_ccitt_final(ctx, &crc);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(crc, EXPECTED_CRC16_CCITT_2352BYTES);
|
|
|
|
|
}
|
2025-08-21 00:07:21 +01:00
|
|
|
|
|
|
|
|
#if defined(__x86_64__) || defined(__amd64) || defined(_M_AMD64) || defined(_M_X64) || defined(__I386__) || \
|
|
|
|
|
defined(__i386__) || defined(__THW_INTEL) || defined(_M_IX86)
|
|
|
|
|
|
|
|
|
|
TEST_F(crc16_ccittFixture, crc16_ccitt_clmul)
|
|
|
|
|
{
|
|
|
|
|
if(!have_clmul()) return;
|
|
|
|
|
|
|
|
|
|
crc16_ccitt_ctx *ctx = crc16_ccitt_init();
|
|
|
|
|
uint16_t crc;
|
|
|
|
|
|
|
|
|
|
EXPECT_NE(ctx, nullptr);
|
|
|
|
|
|
|
|
|
|
crc16_ccitt_update_clmul(ctx, buffer, 1048576);
|
|
|
|
|
crc16_ccitt_final(ctx, &crc);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(crc, EXPECTED_CRC16_CCITT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(crc16_ccittFixture, crc16_ccitt_clmul_misaligned)
|
|
|
|
|
{
|
|
|
|
|
if(!have_clmul()) return;
|
|
|
|
|
|
|
|
|
|
crc16_ccitt_ctx *ctx = crc16_ccitt_init();
|
|
|
|
|
uint16_t crc;
|
|
|
|
|
|
|
|
|
|
EXPECT_NE(ctx, nullptr);
|
|
|
|
|
|
|
|
|
|
crc16_ccitt_update_clmul(ctx, buffer_misaligned + 1, 1048576);
|
|
|
|
|
crc16_ccitt_final(ctx, &crc);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(crc, EXPECTED_CRC16_CCITT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(crc16_ccittFixture, crc16_ccitt_clmul_15bytes)
|
|
|
|
|
{
|
|
|
|
|
if(!have_clmul()) return;
|
|
|
|
|
|
|
|
|
|
crc16_ccitt_ctx *ctx = crc16_ccitt_init();
|
|
|
|
|
uint16_t crc;
|
|
|
|
|
|
|
|
|
|
EXPECT_NE(ctx, nullptr);
|
|
|
|
|
|
|
|
|
|
crc16_ccitt_update_clmul(ctx, buffer, 15);
|
|
|
|
|
crc16_ccitt_final(ctx, &crc);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(crc, EXPECTED_CRC16_CCITT_15BYTES);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(crc16_ccittFixture, crc16_ccitt_clmul_31bytes)
|
|
|
|
|
{
|
|
|
|
|
if(!have_clmul()) return;
|
|
|
|
|
|
|
|
|
|
crc16_ccitt_ctx *ctx = crc16_ccitt_init();
|
|
|
|
|
uint16_t crc;
|
|
|
|
|
|
|
|
|
|
EXPECT_NE(ctx, nullptr);
|
|
|
|
|
|
|
|
|
|
crc16_ccitt_update_clmul(ctx, buffer, 31);
|
|
|
|
|
crc16_ccitt_final(ctx, &crc);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(crc, EXPECTED_CRC16_CCITT_31BYTES);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(crc16_ccittFixture, crc16_ccitt_clmul_63bytes)
|
|
|
|
|
{
|
|
|
|
|
if(!have_clmul()) return;
|
|
|
|
|
|
|
|
|
|
crc16_ccitt_ctx *ctx = crc16_ccitt_init();
|
|
|
|
|
uint16_t crc;
|
|
|
|
|
|
|
|
|
|
EXPECT_NE(ctx, nullptr);
|
|
|
|
|
|
|
|
|
|
crc16_ccitt_update_clmul(ctx, buffer, 63);
|
|
|
|
|
crc16_ccitt_final(ctx, &crc);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(crc, EXPECTED_CRC16_CCITT_63BYTES);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(crc16_ccittFixture, crc16_ccitt_clmul_2352bytes)
|
|
|
|
|
{
|
|
|
|
|
if(!have_clmul()) return;
|
|
|
|
|
|
|
|
|
|
crc16_ccitt_ctx *ctx = crc16_ccitt_init();
|
|
|
|
|
uint16_t crc;
|
|
|
|
|
|
|
|
|
|
EXPECT_NE(ctx, nullptr);
|
|
|
|
|
|
|
|
|
|
crc16_ccitt_update_clmul(ctx, buffer, 2352);
|
|
|
|
|
crc16_ccitt_final(ctx, &crc);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(crc, EXPECTED_CRC16_CCITT_2352BYTES);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|