Add HAL CRC test and test header file.

pull/6831/head
Przemyslaw Stekiel 2018-05-07 15:15:58 +02:00
parent 835d38db58
commit 1eed0b960d
2 changed files with 388 additions and 0 deletions

View File

@ -0,0 +1,114 @@
/* mbed Microcontroller Library
* Copyright (c) 2018 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** \addtogroup hal_crc_tests */
/** @{*/
#ifndef MBED_CRC_API_TESTS_H
#define MBED_CRC_API_TESTS_H
#include "device.h"
#if DEVICE_CRC
#ifdef __cplusplus
extern "C" {
#endif
/** Test that hal_crc_is_supported() function returns true if given polynomial/width
* is supported, false otherwise (at least one predefined polynomial/width must be supported).
*
* Given is platform with hardware CRC support.
*
* When given polynomial/width is supported.
* Then hal_crc_is_supported() function returns true.
*
* When given polynomial/width is not supported.
* Then hal_crc_is_supported() function returns false.
*
* Note:
* At least one predefined polynomial/width config must be supported.
*
*/
void crc_is_supported_test();
/** Test that CRC module can be successfully configured, fed with data and the result can
* be successfully obtained.
*
* Given is platform with hardware CRC support.
*
* When hal_crc_compute_partial_start() function is called.
* Then it configures CRC module with the given polynomial.
*
* When hal_crc_compute_partial() function is called with valid buffer and data length.
* Then it feeds CRC module with data.
*
* When hal_crc_get_result() function is called.
* Then CRC value for the given data is returned.
*
*/
void crc_calc_single_test();
/** Test that hal_crc_compute_partial() function can be call multiple times in
* succession in order to provide additional data to CRC module.
*
* Given is platform with hardware CRC support and CRC module is configured.
* When hal_crc_compute_partial() function is called multiple times.
* Then each call provides additional data to CRC module.
*
*/
void crc_calc_multi_test();
/** Test that calling hal_crc_compute_partial_start() without finalising the
* CRC calculation overrides the current configuration and partial result.
*
* Given is platform with hardware CRC support.
* When CRC module has been configured and fed with data and reconfigured (without reading the result).
* Then the configuration has been overwritten and the new data can be successfully processed.
*
*/
void crc_reconfigure_test();
/** Test that hal_crc_compute_partial() does nothing if pointer to buffer is undefined or
* data length is equal to 0.
*
* Given is platform with hardware CRC support.
* When hal_crc_compute_partial() is called with invalid parameters.
* Then no data is provided to CRC module and no exception is generated.
*
*/
void crc_compute_partial_invalid_param_test();
/** Test that hal_crc_is_supported() returns false if pointer to the config structure is undefined.
*
* Given is platform with hardware CRC support.
* When hal_crc_is_supported() is called with invalid parameter.
* Then function returns false.
*
*/
void crc_is_supported_invalid_param_test();
/**@}*/
#ifdef __cplusplus
}
#endif
#endif
#endif
/**@}*/

274
TESTS/mbed_hal/crc/main.cpp Normal file
View File

@ -0,0 +1,274 @@
/* mbed Microcontroller Library
* Copyright (c) 2018 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "utest/utest.h"
#include "unity/unity.h"
#include "greentea-client/test_env.h"
#include "mbed.h"
#include "math.h"
#include "crc_api.h"
#if !DEVICE_CRC
#error [NOT_SUPPORTED] CRC not supported for this target
#endif
using namespace utest::v1;
#define POLY_8BIT_MAXIM 0x31
#define POLY_16BIT_MAXIM 0x8005
#define POLY_32BIT_POSIX 0x4C11DB7
#define UNSUPPORTED (-1)
#define POL_CNT (2)
const uint8_t input_data[] = "123456789";
typedef struct
{
const crc_mbed_config config_data;
const char* input_data;
uint32_t expected_result;
} TEST_CASE;
static TEST_CASE test_cases[] = {
/* Predefined polynomials. */
{ {POLY_7BIT_SD , 7, 0x00000000, 0x00000000, false, false}, "123456789", 0xEA },
{ {POLY_7BIT_SD , 7, 0x0000007F, 0x00000000, false, false}, "123456789", 0xA0 },
{ {POLY_7BIT_SD , 7, 0x0000002B, 0x00000000, false, false}, "123456789", 0x74 },
{ {POLY_7BIT_SD , 7, 0x00000000, 0x0000007F, false, false}, "123456789", 0x95 },
{ {POLY_7BIT_SD , 7, 0x00000000, 0x0000002B, false, false}, "123456789", 0xC1 },
{ {POLY_7BIT_SD , 7, 0x00000000, 0x00000000, true , false}, "123456789", 0xA4 },
{ {POLY_7BIT_SD , 7, 0x00000000, 0x00000000, false, true }, "123456789", 0x57 },
{ {POLY_8BIT_CCITT , 8, 0x00000000, 0x00000000, false, false}, "123456789", 0xF4 },
{ {POLY_8BIT_CCITT , 8, 0x000000FF, 0x00000000, false, false}, "123456789", 0xFB },
{ {POLY_8BIT_CCITT , 8, 0x000000AB, 0x00000000, false, false}, "123456789", 0x87 },
{ {POLY_8BIT_CCITT , 8, 0x00000000, 0x000000FF, false, false}, "123456789", 0x0B },
{ {POLY_8BIT_CCITT , 8, 0x00000000, 0x000000AB, false, false}, "123456789", 0x5F },
{ {POLY_8BIT_CCITT , 8, 0x00000000, 0x00000000, true , false}, "123456789", 0x04 },
{ {POLY_8BIT_CCITT , 8, 0x00000000, 0x00000000, false, true }, "123456789", 0x2F },
{ {POLY_16BIT_CCITT , 16, 0x00000000, 0x00000000, false, false}, "123456789", 0x31C3 },
{ {POLY_16BIT_CCITT , 16, 0x0000FFFF, 0x00000000, false, false}, "123456789", 0x29B1 },
{ {POLY_16BIT_CCITT , 16, 0x0000ABAB, 0x00000000, false, false}, "123456789", 0x7D70 },
{ {POLY_16BIT_CCITT , 16, 0x00000000, 0x0000FFFF, false, false}, "123456789", 0xCE3C },
{ {POLY_16BIT_CCITT , 16, 0x00000000, 0x0000ABAB, false, false}, "123456789", 0x9A68 },
{ {POLY_16BIT_CCITT , 16, 0x00000000, 0x00000000, true , false}, "123456789", 0x9184 },
{ {POLY_16BIT_CCITT , 16, 0x00000000, 0x00000000, false, true }, "123456789", 0xC38C },
{ {POLY_16BIT_IBM , 16, 0x00000000, 0x00000000, false, false}, "123456789", 0xFEE8 },
{ {POLY_16BIT_IBM , 16, 0x0000FFFF, 0x00000000, false, false}, "123456789", 0xAEE7 },
{ {POLY_16BIT_IBM , 16, 0x0000ABAB, 0x00000000, false, false}, "123456789", 0x0887 },
{ {POLY_16BIT_IBM , 16, 0x00000000, 0x0000FFFF, false, false}, "123456789", 0x0117 },
{ {POLY_16BIT_IBM , 16, 0x00000000, 0x0000ABAB, false, false}, "123456789", 0x5543 },
{ {POLY_16BIT_IBM , 16, 0x00000000, 0x00000000, true , false}, "123456789", 0xBCDD },
{ {POLY_16BIT_IBM , 16, 0x00000000, 0x00000000, false, true }, "123456789", 0x177F },
{ {POLY_32BIT_ANSI , 32, 0x00000000, 0x00000000, false, false}, "123456789", 0x89A1897F },
{ {POLY_32BIT_ANSI , 32, 0xFFFFFFFF, 0x00000000, false, false}, "123456789", 0x0376E6E7 },
{ {POLY_32BIT_ANSI , 32, 0xABABABAB, 0x00000000, false, false}, "123456789", 0x871A2FAA },
{ {POLY_32BIT_ANSI , 32, 0x00000000, 0xFFFFFFFF, false, false}, "123456789", 0x765E7680 },
{ {POLY_32BIT_ANSI , 32, 0x00000000, 0xABABABAB, false, false}, "123456789", 0x220A22D4 },
{ {POLY_32BIT_ANSI , 32, 0x00000000, 0x00000000, true , false}, "123456789", 0x11B4BFB4 },
{ {POLY_32BIT_ANSI , 32, 0x00000000, 0x00000000, false, true }, "123456789", 0xFE918591 },
/* Not-predefined polynomials. */
{ {POLY_8BIT_MAXIM , 8, 0x00000000, 0x00000000, false, false}, "123456789", 0xA2 },
{ {POLY_8BIT_MAXIM , 8, 0x000000FF, 0x00000000, false, false}, "123456789", 0xF7 },
{ {POLY_8BIT_MAXIM , 8, 0x000000AB, 0x00000000, false, false}, "123456789", 0x71 },
{ {POLY_8BIT_MAXIM , 8, 0x00000000, 0x000000FF, false, false}, "123456789", 0x5D },
{ {POLY_8BIT_MAXIM , 8, 0x00000000, 0x000000AB, false, false}, "123456789", 0x09 },
{ {POLY_8BIT_MAXIM , 8, 0x00000000, 0x00000000, true , false}, "123456789", 0x85 },
{ {POLY_8BIT_MAXIM , 8, 0x00000000, 0x00000000, false, true }, "123456789", 0x45 },
{ {POLY_16BIT_MAXIM , 16, 0x00000000, 0x00000000, false, false}, "123456789", 0xFEE8 },
{ {POLY_16BIT_MAXIM , 16, 0x0000FFFF, 0x00000000, false, false}, "123456789", 0xAEE7 },
{ {POLY_16BIT_MAXIM , 16, 0x0000ABAB, 0x00000000, false, false}, "123456789", 0x0887 },
{ {POLY_16BIT_MAXIM , 16, 0x00000000, 0x0000FFFF, false, false}, "123456789", 0x0117 },
{ {POLY_16BIT_MAXIM , 16, 0x00000000, 0x0000ABAB, false, false}, "123456789", 0x5543 },
{ {POLY_16BIT_MAXIM , 16, 0x00000000, 0x00000000, true , false}, "123456789", 0xBCDD },
{ {POLY_16BIT_MAXIM , 16, 0x00000000, 0x00000000, false, true }, "123456789", 0x177F },
{ {POLY_32BIT_POSIX , 32, 0x00000000, 0x00000000, false, false}, "123456789", 0x89A1897F },
{ {POLY_32BIT_POSIX , 32, 0xFFFFFFFF, 0x00000000, false, false}, "123456789", 0x0376E6E7 },
{ {POLY_32BIT_POSIX , 32, 0xABABABAB, 0x00000000, false, false}, "123456789", 0x871A2FAA },
{ {POLY_32BIT_POSIX , 32, 0x00000000, 0xFFFFFFFF, false, false}, "123456789", 0x765E7680 },
{ {POLY_32BIT_POSIX , 32, 0x00000000, 0xABABABAB, false, false}, "123456789", 0x220A22D4 },
{ {POLY_32BIT_POSIX , 32, 0x00000000, 0x00000000, true , false}, "123456789", 0x11B4BFB4 },
{ {POLY_32BIT_POSIX , 32, 0x00000000, 0x00000000, false, true }, "123456789", 0xFE918591 },
};
/* Test that hal_crc_is_supported() function returns true if given polynomial
* is supported, false otherwise (at least one polynomial from the predefined list must be supported). */
void crc_is_supported_test()
{
/* Check if at least one crc polynomial/config is supported. */
uint32_t num_of_supported_polynomials = 0;
for (unsigned int i = 0; i < (sizeof(test_cases) / sizeof(TEST_CASE)); i++) {
if (hal_crc_is_supported(&test_cases[i].config_data) == true) {
num_of_supported_polynomials++;
}
}
TEST_ASSERT(num_of_supported_polynomials > 0);
}
/* Test that CRC module can be successfully configured, fed with data and the result can
* be successfully obtained. */
void crc_calc_single_test()
{
for (unsigned int i = 0; i < (sizeof(test_cases) / sizeof(TEST_CASE)); i++) {
if (hal_crc_is_supported(&test_cases[i].config_data) == true) {
hal_crc_compute_partial_start(&test_cases[i].config_data);
hal_crc_compute_partial((uint8_t*) test_cases[i].input_data, strlen((const char*) test_cases[i].input_data));
const uint32_t crc = hal_crc_get_result();
TEST_ASSERT_EQUAL(test_cases[i].expected_result, crc);
}
}
}
/* Test that hal_crc_compute_partial() function can be call multiple times in
* succession in order to provide additional data to CRC module. */
void crc_calc_multi_test()
{
for (unsigned int i = 0; i < (sizeof(test_cases) / sizeof(TEST_CASE)); i++) {
if (hal_crc_is_supported(&test_cases[i].config_data) == true) {
const uint32_t first_part_bytes = 3;
const uint32_t second_part_bytes = 1;
const uint32_t third_part_bytes = strlen((const char*) test_cases[i].input_data) - first_part_bytes
- second_part_bytes;
hal_crc_compute_partial_start(&test_cases[i].config_data);
hal_crc_compute_partial((uint8_t*) test_cases[i].input_data, first_part_bytes);
hal_crc_compute_partial((uint8_t*) (test_cases[i].input_data + first_part_bytes), second_part_bytes);
hal_crc_compute_partial((uint8_t*) (test_cases[i].input_data + first_part_bytes + second_part_bytes),
third_part_bytes);
const uint32_t crc = hal_crc_get_result();
TEST_ASSERT_EQUAL(test_cases[i].expected_result, crc);
}
}
}
/* Test that calling hal_crc_compute_partial_start() without finalising the
* CRC calculation overrides the current configuration. */
void crc_reconfigure_test()
{
int pol_idx[POL_CNT] =
{ UNSUPPORTED, UNSUPPORTED };
int pol_cnt = 0;
const uint8_t dummy_input_data[] = "abcdefghijklmnopqrstuvwxyz";
/* At least one configuration must be supported. If two are supported, then
* re-initialize CRC module using different config. */
for (unsigned int i = 0; i < (sizeof(test_cases) / sizeof(TEST_CASE)); i++) {
/* Find two supported polynomials if possible. */
if (hal_crc_is_supported(&test_cases[i].config_data) == true) {
if (pol_cnt == 0) {
pol_idx[pol_cnt] = i;
pol_cnt++;
} else if (test_cases[pol_idx[0]].config_data.polynomial != test_cases[i].config_data.polynomial) {
pol_idx[pol_cnt] = i;
pol_cnt++;
}
if (pol_cnt == POL_CNT) {
break;
}
}
}
pol_cnt = 0;
/* Init CRC module and provide some data, but do not read the result. */
hal_crc_compute_partial_start(&test_cases[pol_idx[pol_cnt]].config_data);
hal_crc_compute_partial((uint8_t*) dummy_input_data, strlen((const char*) dummy_input_data));
/* Change index only if more than one supported polynomial has been found. */
if (pol_idx[POL_CNT - 1] != UNSUPPORTED) {
pol_cnt++;
}
/* Now re-init CRC module and provide new data and check the result. */
hal_crc_compute_partial_start(&test_cases[pol_idx[pol_cnt]].config_data);
hal_crc_compute_partial((uint8_t*) test_cases[pol_idx[pol_cnt]].input_data,
strlen((const char*) test_cases[pol_idx[pol_cnt]].input_data));
const uint32_t crc = hal_crc_get_result();
TEST_ASSERT_EQUAL(test_cases[pol_idx[pol_cnt]].expected_result, crc);
}
/* Test that hal_crc_compute_partial() does nothing if pointer to buffer is undefined or
* data length is equal to 0. */
void crc_compute_partial_invalid_param_test()
{
uint32_t crc = 0;
/* At least one polynomial must be supported. */
for (unsigned int i = 0; i < (sizeof(test_cases) / sizeof(TEST_CASE)); i++) {
if (hal_crc_is_supported(&test_cases[i].config_data) == true) {
hal_crc_compute_partial_start(&test_cases[i].config_data);
/* Call hal_crc_compute_partial() with invalid parameters. */
hal_crc_compute_partial((uint8_t*) NULL, strlen((const char*) test_cases[i].input_data));
hal_crc_compute_partial((uint8_t*) test_cases[i].input_data, 0);
/* Now use valid parameters. */
hal_crc_compute_partial((uint8_t*) test_cases[i].input_data,
strlen((const char*) test_cases[i].input_data));
crc = hal_crc_get_result();
TEST_ASSERT_EQUAL(test_cases[i].expected_result, crc);
break;
}
}
}
/* Test that hal_crc_is_supported() returns false if pointer to the config structure is undefined. */
void crc_is_supported_invalid_param_test()
{
TEST_ASSERT_EQUAL(false, hal_crc_is_supported(NULL));
}
Case cases[] = {
Case("test: supported polynomials.", crc_is_supported_test),
Case("test: CRC calculation - single input.", crc_calc_single_test),
Case("test: CRC calculation - multi input.", crc_calc_multi_test),
Case("test: re-configure without getting the result.", crc_reconfigure_test),
Case("test: hal_crc_compute_partial() - invalid parameters.", crc_compute_partial_invalid_param_test),
Case("test: hal_crc_is_supported() - invalid parameter.", crc_is_supported_invalid_param_test),
};
utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
{
GREENTEA_SETUP(30, "default_auto");
return greentea_test_setup_handler(number_of_cases);
}
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
int main()
{
Harness::run(specification);
}