mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-12-27 15:31:17 +01:00
unittests: add fletcher* tests
This commit is contained in:
parent
040ad55f62
commit
889adc18b8
71
tests/unittests/tests-checksum/tests-checksum-fletcher16.c
Normal file
71
tests/unittests/tests-checksum/tests-checksum-fletcher16.c
Normal file
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* 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/fletcher16.h"
|
||||
|
||||
#include "tests-checksum.h"
|
||||
|
||||
static int calc_and_compare_checksum(const unsigned char *buf, size_t len,
|
||||
uint16_t expected)
|
||||
{
|
||||
uint16_t result = fletcher16(buf, len);
|
||||
|
||||
return result == expected;
|
||||
}
|
||||
|
||||
static void test_checksum_fletcher16(void)
|
||||
{
|
||||
{
|
||||
unsigned char buf[] = "";
|
||||
uint16_t expect = 0xFFFF;
|
||||
|
||||
TEST_ASSERT(calc_and_compare_checksum(buf, sizeof(buf) - 1, expect));
|
||||
}
|
||||
|
||||
{
|
||||
/* fletcher cannot distinguish between all 0 and all 1 segments */
|
||||
unsigned char buf0[16] = {
|
||||
0xA1, 0xA1, 0xA1, 0xA1,
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
0x1A, 0x1A, 0x1A, 0x1A,
|
||||
};
|
||||
uint32_t expect = fletcher16(buf0, sizeof(buf0));
|
||||
unsigned char buf1[16] = {
|
||||
0xA1, 0xA1, 0xA1, 0xA1,
|
||||
0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0x1A, 0x1A, 0x1A, 0x1A,
|
||||
};
|
||||
|
||||
TEST_ASSERT(calc_and_compare_checksum(buf1, sizeof(buf1), expect));
|
||||
}
|
||||
|
||||
{
|
||||
/* verified with http://www.nitrxgen.net/hashgen/ */
|
||||
unsigned char buf[] = "abcde";
|
||||
uint16_t expect = 0xc8f0;
|
||||
|
||||
TEST_ASSERT(calc_and_compare_checksum(buf, sizeof(buf) - 1, expect));
|
||||
}
|
||||
}
|
||||
|
||||
Test *tests_checksum_fletcher16_tests(void)
|
||||
{
|
||||
EMB_UNIT_TESTFIXTURES(fixtures) {
|
||||
new_TestFixture(test_checksum_fletcher16),
|
||||
};
|
||||
|
||||
EMB_UNIT_TESTCALLER(checksum_fletcher16_tests, NULL, NULL, fixtures);
|
||||
|
||||
return (Test *)&checksum_fletcher16_tests;
|
||||
}
|
||||
74
tests/unittests/tests-checksum/tests-checksum-fletcher32.c
Normal file
74
tests/unittests/tests-checksum/tests-checksum-fletcher32.c
Normal file
@ -0,0 +1,74 @@
|
||||
/*
|
||||
* 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/fletcher32.h"
|
||||
|
||||
#include "tests-checksum.h"
|
||||
|
||||
static int calc_and_compare_checksum(const unsigned char *buf, size_t len,
|
||||
uint32_t expected)
|
||||
{
|
||||
const uint16_t *buf16 = (const uint16_t *) buf;
|
||||
size_t len16 = len/2;
|
||||
uint32_t result = fletcher32(buf16, len16);
|
||||
|
||||
return result == expected;
|
||||
}
|
||||
|
||||
static void test_checksum_fletcher32(void)
|
||||
{
|
||||
{
|
||||
/* the initial checksum value is 0xFFFFFFFF */
|
||||
unsigned char buf[] = "";
|
||||
uint32_t expect = 0xFFFFFFFF;
|
||||
|
||||
TEST_ASSERT(calc_and_compare_checksum(buf, sizeof(buf) - 1, expect));
|
||||
}
|
||||
|
||||
{
|
||||
/* fletcher cannot distinguish between all 0 and all 1 segments */
|
||||
unsigned char buf0[16] = {
|
||||
0xA1, 0xA1, 0xA1, 0xA1,
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
0x1A, 0x1A, 0x1A, 0x1A,
|
||||
};
|
||||
uint32_t expect = fletcher32((const uint16_t *) buf0, sizeof(buf0)/2);
|
||||
unsigned char buf1[16] = {
|
||||
0xA1, 0xA1, 0xA1, 0xA1,
|
||||
0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0x1A, 0x1A, 0x1A, 0x1A,
|
||||
};
|
||||
|
||||
TEST_ASSERT(calc_and_compare_checksum(buf1, sizeof(buf1), expect));
|
||||
}
|
||||
|
||||
{
|
||||
/* XXX: not verified with external implementation yet */
|
||||
unsigned char buf[] = "abcdef";
|
||||
uint32_t expect = 0x56502d2a;
|
||||
|
||||
TEST_ASSERT(calc_and_compare_checksum(buf, sizeof(buf) - 1, expect));
|
||||
}
|
||||
}
|
||||
|
||||
Test *tests_checksum_fletcher32_tests(void)
|
||||
{
|
||||
EMB_UNIT_TESTFIXTURES(fixtures) {
|
||||
new_TestFixture(test_checksum_fletcher32),
|
||||
};
|
||||
|
||||
EMB_UNIT_TESTCALLER(checksum_fletcher32_tests, NULL, NULL, fixtures);
|
||||
|
||||
return (Test *)&checksum_fletcher32_tests;
|
||||
}
|
||||
@ -11,4 +11,6 @@
|
||||
void tests_checksum(void)
|
||||
{
|
||||
TESTS_RUN(tests_checksum_crc16_ccitt_tests());
|
||||
TESTS_RUN(tests_checksum_fletcher16_tests());
|
||||
TESTS_RUN(tests_checksum_fletcher32_tests());
|
||||
}
|
||||
|
||||
@ -36,6 +36,20 @@ void tests_checksum(void);
|
||||
*/
|
||||
Test *tests_checksum_crc16_ccitt_tests(void);
|
||||
|
||||
/**
|
||||
* @brief Generates tests for checksum/fletcher16.h
|
||||
*
|
||||
* @return embUnit tests if successful, NULL if not.
|
||||
*/
|
||||
Test *tests_checksum_fletcher16_tests(void);
|
||||
|
||||
/**
|
||||
* @brief Generates tests for checksum/fletcher32.h
|
||||
*
|
||||
* @return embUnit tests if successful, NULL if not.
|
||||
*/
|
||||
Test *tests_checksum_fletcher32_tests(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user