mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-01-18 12:52:44 +01:00
tests/checksum: add tests for crc8
Taken from tests-checksum-crc16-ccitt.c
This commit is contained in:
parent
22c2dbbbe5
commit
5d8aa6ad9a
82
tests/unittests/tests-checksum/tests-checksum-crc8.c
Normal file
82
tests/unittests/tests-checksum/tests-checksum-crc8.c
Normal file
@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright 2019 Benjamin Valentin <benpicco@googlemail.com>
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser
|
||||
* General Public License v2.1. See the file LICENSE in the top level
|
||||
* directory for more details.
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "embUnit/embUnit.h"
|
||||
|
||||
#include "checksum/crc8.h"
|
||||
|
||||
#include "tests-checksum.h"
|
||||
|
||||
#define CRC8_POLY 0x31
|
||||
#define CRC8_INIT 0xff
|
||||
|
||||
static void test_checksum_crc8_sequence_empty(void)
|
||||
{
|
||||
unsigned char buf[] = "";
|
||||
uint8_t expect = 0xFF;
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(expect, crc8(buf, sizeof(buf) - 1, CRC8_POLY, CRC8_INIT));
|
||||
}
|
||||
|
||||
static void test_checksum_crc8_sequence_1a(void)
|
||||
{
|
||||
unsigned char buf[] = "A";
|
||||
uint8_t expect = 0xA0;
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(expect, crc8(buf, sizeof(buf) - 1, CRC8_POLY, CRC8_INIT));
|
||||
}
|
||||
|
||||
static void test_checksum_crc8_sequence_256a(void)
|
||||
{
|
||||
unsigned char buf[] = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
|
||||
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
|
||||
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
|
||||
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
|
||||
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
|
||||
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
|
||||
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
|
||||
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
|
||||
uint8_t expect = 0xF0;
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(expect, crc8(buf, sizeof(buf) - 1, CRC8_POLY, CRC8_INIT));
|
||||
}
|
||||
|
||||
static void test_checksum_crc8_sequence_1to9(void)
|
||||
{
|
||||
unsigned char buf[] = "123456789";
|
||||
uint8_t expect = 0xF7;
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(expect, crc8(buf, sizeof(buf) - 1, CRC8_POLY, CRC8_INIT));
|
||||
}
|
||||
|
||||
static void test_checksum_crc8_sequence_4bytes(void)
|
||||
{
|
||||
unsigned char buf[] = { 0x12, 0x34, 0x56, 0x78 };
|
||||
uint8_t expect = 0xE0;
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(expect, crc8(buf, sizeof(buf), CRC8_POLY, CRC8_INIT));
|
||||
}
|
||||
|
||||
Test *tests_checksum_crc8_tests(void)
|
||||
{
|
||||
EMB_UNIT_TESTFIXTURES(fixtures) {
|
||||
/* Reference values according to
|
||||
* http://srecord.sourceforge.net/crc16-ccitt.html */
|
||||
new_TestFixture(test_checksum_crc8_sequence_empty),
|
||||
new_TestFixture(test_checksum_crc8_sequence_1a),
|
||||
new_TestFixture(test_checksum_crc8_sequence_256a),
|
||||
new_TestFixture(test_checksum_crc8_sequence_1to9),
|
||||
new_TestFixture(test_checksum_crc8_sequence_4bytes),
|
||||
};
|
||||
|
||||
EMB_UNIT_TESTCALLER(checksum_crc8_tests, NULL, NULL, fixtures);
|
||||
|
||||
return (Test *)&checksum_crc8_tests;
|
||||
}
|
@ -10,6 +10,7 @@
|
||||
|
||||
void tests_checksum(void)
|
||||
{
|
||||
TESTS_RUN(tests_checksum_crc8_tests());
|
||||
TESTS_RUN(tests_checksum_crc16_ccitt_tests());
|
||||
TESTS_RUN(tests_checksum_fletcher16_tests());
|
||||
TESTS_RUN(tests_checksum_fletcher32_tests());
|
||||
|
@ -29,6 +29,13 @@ extern "C" {
|
||||
*/
|
||||
void tests_checksum(void);
|
||||
|
||||
/**
|
||||
* @brief Generates tests for checksum/crc8.h
|
||||
*
|
||||
* @return embUnit tests if successful, NULL if not.
|
||||
*/
|
||||
Test *tests_checksum_crc8_tests(void);
|
||||
|
||||
/**
|
||||
* @brief Generates tests for checksum/crc16_ccitt.h
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user