1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-29 08:21:18 +01:00

tests/unittests: add checksum test

This commit is contained in:
Ludwig Knüpfer 2016-01-16 21:03:03 +01:00
parent 430b4aa600
commit edbfdeb02e
5 changed files with 158 additions and 0 deletions

View File

@ -0,0 +1 @@
include $(RIOTBASE)/Makefile.base

View File

@ -0,0 +1 @@
USEMODULE += checksum

View File

@ -0,0 +1,97 @@
/*
* Copyright 2016 Ludwig Knüpfer <ludwig.knuepfer@fu-berlin.de>
*
* 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/crc16_ccitt.h"
#include "tests-checksum.h"
static int calc_and_compare_crc_with_update(const unsigned char *buf,
size_t len, size_t split, uint16_t expected)
{
uint16_t result = crc16_ccitt_calc(buf, split);
result = crc16_ccitt_update(result, buf + split, len - split);
return result == expected;
}
static int calc_and_compare_crc(const unsigned char *buf, size_t len,
uint16_t expected)
{
uint16_t result = crc16_ccitt_calc(buf, len);
return result == expected;
}
static void test_checksum_crc16_ccitt_sequence(void)
{
/* Reference values according to
* http://srecord.sourceforge.net/crc16-ccitt.html */
{
unsigned char buf[] = "";
uint16_t expect = 0x1D0F;
TEST_ASSERT(calc_and_compare_crc(buf, sizeof(buf) - 1, expect));
TEST_ASSERT(calc_and_compare_crc_with_update(buf, sizeof(buf) - 1,
(sizeof(buf) - 1) / 2, expect)); }
{
unsigned char buf[] = "A";
uint16_t expect = 0x9479;
TEST_ASSERT(calc_and_compare_crc(buf, sizeof(buf) - 1, expect));
TEST_ASSERT(calc_and_compare_crc_with_update(buf, sizeof(buf) - 1,
(sizeof(buf) - 1) / 2, expect)); }
{
unsigned char buf[] = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
"AAAA";
uint16_t expect = 0xE938;
TEST_ASSERT(calc_and_compare_crc(buf, sizeof(buf) - 1, expect));
TEST_ASSERT(calc_and_compare_crc_with_update(buf, sizeof(buf) - 1,
(sizeof(buf) - 1) / 2, expect)); }
{
unsigned char buf[] = "123456789";
uint16_t expect = 0xE5CC;
TEST_ASSERT(calc_and_compare_crc(buf, sizeof(buf) - 1, expect));
TEST_ASSERT(calc_and_compare_crc_with_update(buf, sizeof(buf)
- 1, (sizeof(buf) - 1) / 2, expect));
}
{
unsigned char buf[] = { 0x12, 0x34, 0x56, 0x78 };
uint16_t expect = 0xBA3C;
TEST_ASSERT(calc_and_compare_crc(buf, sizeof(buf), expect));
TEST_ASSERT(calc_and_compare_crc_with_update(buf, sizeof(buf),
sizeof(buf) / 2, expect));
}
}
Test *tests_checksum_crc16_ccitt_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_checksum_crc16_ccitt_sequence),
};
EMB_UNIT_TESTCALLER(checksum_crc16_ccitt_tests, NULL, NULL, fixtures);
return (Test *)&checksum_crc16_ccitt_tests;
}

View File

@ -0,0 +1,14 @@
/*
* Copyright 2016 Ludwig Knüpfer <ludwig.knuepfer@fu-berlin.de>
*
* 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 "tests-checksum.h"
void tests_checksum(void)
{
TESTS_RUN(tests_checksum_crc16_ccitt_tests());
}

View File

@ -0,0 +1,45 @@
/*
* Copyright 2016 Ludwig Knüpfer <ludwig.knuepfer@fu-berlin.de>
*
* 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.
*/
/**
* @addtogroup unittests
* @{
*
* @file
* @brief Unittests for the ``checksum`` module
*
* @author Ludwig Knüpfer <ludwig.knuepfer@fu-berlin.de>
*/
#ifndef TESTS_CHECKSUM_H
#define TESTS_CHECKSUM_H
#include "embUnit.h"
#include "kernel.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief The entry point of this test suite.
*/
void tests_checksum(void);
/**
* @brief Generates tests for checksum/crc16_ccitt.h
*
* @return embUnit tests if successful, NULL if not.
*/
Test *tests_checksum_crc16_ccitt_tests(void);
#ifdef __cplusplus
}
#endif
#endif /* TESTS_CHECKSUM_H */
/** @} */