2015-11-04 16:54:33 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Lucas Jenß
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2018-06-01 15:04:44 +02:00
|
|
|
* @ingroup unittests
|
2015-11-04 16:54:33 +01:00
|
|
|
* @brief
|
|
|
|
* @{
|
|
|
|
*
|
2018-07-26 15:39:44 +02:00
|
|
|
* @brief Tests for Error Correction Codes
|
|
|
|
*
|
2015-11-04 16:54:33 +01:00
|
|
|
* @author Lucas Jenß <lucas@x3ro.de>
|
2018-07-26 15:39:44 +02:00
|
|
|
* @author Peter Kietzmann <peter.kietzmann@haw-hamburg.de>
|
2015-11-04 16:54:33 +01:00
|
|
|
*/
|
|
|
|
#include <string.h>
|
|
|
|
#include "embUnit.h"
|
|
|
|
|
|
|
|
#include "ecc/hamming256.h"
|
|
|
|
|
2018-07-26 15:39:44 +02:00
|
|
|
static void test_hamming256_single(void)
|
2015-11-04 16:54:33 +01:00
|
|
|
{
|
|
|
|
uint8_t data[256];
|
|
|
|
uint8_t ecc[3];
|
|
|
|
uint8_t result;
|
|
|
|
|
|
|
|
memset(data, 0xAB, 256);
|
|
|
|
|
|
|
|
hamming_compute256x(data, 256, ecc);
|
|
|
|
result = hamming_verify256x(data, 256, ecc);
|
|
|
|
TEST_ASSERT_EQUAL_INT(Hamming_ERROR_NONE, result);
|
|
|
|
|
|
|
|
data[10] |= (2 << 3);
|
|
|
|
result = hamming_verify256x(data, 256, ecc);
|
|
|
|
TEST_ASSERT_EQUAL_INT(Hamming_ERROR_SINGLEBIT, result);
|
|
|
|
|
|
|
|
data[10] |= (2 << 3);
|
|
|
|
data[20] |= (2 << 5);
|
|
|
|
result = hamming_verify256x(data, 256, ecc);
|
|
|
|
TEST_ASSERT_EQUAL_INT(Hamming_ERROR_MULTIPLEBITS, result);
|
|
|
|
|
|
|
|
memset(data, 0xAB, 256);
|
2018-02-05 11:40:11 +01:00
|
|
|
ecc[1] ^= 1; /* Flip first bit, corrupting the ECC */
|
2015-11-04 16:54:33 +01:00
|
|
|
result = hamming_verify256x(data, 256, ecc);
|
|
|
|
TEST_ASSERT_EQUAL_INT(Hamming_ERROR_ECC, result);
|
|
|
|
}
|
|
|
|
|
2018-07-26 15:39:44 +02:00
|
|
|
static void test_hamming256_padding(void)
|
2015-11-04 16:54:33 +01:00
|
|
|
{
|
|
|
|
uint8_t data[203];
|
|
|
|
uint8_t ecc[3];
|
|
|
|
uint8_t result;
|
|
|
|
|
|
|
|
memset(data, 0xAB, 203);
|
|
|
|
|
|
|
|
hamming_compute256x(data, 203, ecc);
|
|
|
|
result = hamming_verify256x(data, 203, ecc);
|
|
|
|
TEST_ASSERT_EQUAL_INT(Hamming_ERROR_NONE, result);
|
|
|
|
|
|
|
|
data[10] |= (2 << 3);
|
|
|
|
result = hamming_verify256x(data, 203, ecc);
|
|
|
|
TEST_ASSERT_EQUAL_INT(Hamming_ERROR_SINGLEBIT, result);
|
|
|
|
|
|
|
|
data[10] |= (2 << 3);
|
|
|
|
data[20] |= (2 << 5);
|
|
|
|
result = hamming_verify256x(data, 203, ecc);
|
|
|
|
TEST_ASSERT_EQUAL_INT(Hamming_ERROR_MULTIPLEBITS, result);
|
|
|
|
|
|
|
|
memset(data, 0xAB, 203);
|
2018-02-05 11:40:11 +01:00
|
|
|
ecc[1] ^= 1; /* Flip first bit, corrupting the ECC */
|
2015-11-04 16:54:33 +01:00
|
|
|
result = hamming_verify256x(data, 203, ecc);
|
|
|
|
TEST_ASSERT_EQUAL_INT(Hamming_ERROR_ECC, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TestRef test_all(void)
|
|
|
|
{
|
|
|
|
EMB_UNIT_TESTFIXTURES(fixtures) {
|
2018-07-26 15:39:44 +02:00
|
|
|
new_TestFixture(test_hamming256_single),
|
|
|
|
new_TestFixture(test_hamming256_padding),
|
2015-11-04 16:54:33 +01:00
|
|
|
};
|
|
|
|
|
2018-07-26 15:39:44 +02:00
|
|
|
EMB_UNIT_TESTCALLER(EccTest, NULL, NULL, fixtures);
|
2015-11-04 16:54:33 +01:00
|
|
|
return (TestRef) & EccTest;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tests_ecc(void)
|
|
|
|
{
|
|
|
|
TESTS_RUN(test_all());
|
|
|
|
}
|