Skip to content

Commit 1eed0b9

Browse files
committed
Add HAL CRC test and test header file.
1 parent 835d38d commit 1eed0b9

File tree

2 files changed

+388
-0
lines changed

2 files changed

+388
-0
lines changed

TESTS/mbed_hal/crc/crc_api_tests.h

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2018 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/** \addtogroup hal_crc_tests */
18+
/** @{*/
19+
20+
#ifndef MBED_CRC_API_TESTS_H
21+
#define MBED_CRC_API_TESTS_H
22+
23+
#include "device.h"
24+
25+
#if DEVICE_CRC
26+
27+
#ifdef __cplusplus
28+
extern "C" {
29+
#endif
30+
31+
/** Test that hal_crc_is_supported() function returns true if given polynomial/width
32+
* is supported, false otherwise (at least one predefined polynomial/width must be supported).
33+
*
34+
* Given is platform with hardware CRC support.
35+
*
36+
* When given polynomial/width is supported.
37+
* Then hal_crc_is_supported() function returns true.
38+
*
39+
* When given polynomial/width is not supported.
40+
* Then hal_crc_is_supported() function returns false.
41+
*
42+
* Note:
43+
* At least one predefined polynomial/width config must be supported.
44+
*
45+
*/
46+
void crc_is_supported_test();
47+
48+
/** Test that CRC module can be successfully configured, fed with data and the result can
49+
* be successfully obtained.
50+
*
51+
* Given is platform with hardware CRC support.
52+
*
53+
* When hal_crc_compute_partial_start() function is called.
54+
* Then it configures CRC module with the given polynomial.
55+
*
56+
* When hal_crc_compute_partial() function is called with valid buffer and data length.
57+
* Then it feeds CRC module with data.
58+
*
59+
* When hal_crc_get_result() function is called.
60+
* Then CRC value for the given data is returned.
61+
*
62+
*/
63+
void crc_calc_single_test();
64+
65+
/** Test that hal_crc_compute_partial() function can be call multiple times in
66+
* succession in order to provide additional data to CRC module.
67+
*
68+
* Given is platform with hardware CRC support and CRC module is configured.
69+
* When hal_crc_compute_partial() function is called multiple times.
70+
* Then each call provides additional data to CRC module.
71+
*
72+
*/
73+
void crc_calc_multi_test();
74+
75+
/** Test that calling hal_crc_compute_partial_start() without finalising the
76+
* CRC calculation overrides the current configuration and partial result.
77+
*
78+
* Given is platform with hardware CRC support.
79+
* When CRC module has been configured and fed with data and reconfigured (without reading the result).
80+
* Then the configuration has been overwritten and the new data can be successfully processed.
81+
*
82+
*/
83+
void crc_reconfigure_test();
84+
85+
/** Test that hal_crc_compute_partial() does nothing if pointer to buffer is undefined or
86+
* data length is equal to 0.
87+
*
88+
* Given is platform with hardware CRC support.
89+
* When hal_crc_compute_partial() is called with invalid parameters.
90+
* Then no data is provided to CRC module and no exception is generated.
91+
*
92+
*/
93+
void crc_compute_partial_invalid_param_test();
94+
95+
/** Test that hal_crc_is_supported() returns false if pointer to the config structure is undefined.
96+
*
97+
* Given is platform with hardware CRC support.
98+
* When hal_crc_is_supported() is called with invalid parameter.
99+
* Then function returns false.
100+
*
101+
*/
102+
void crc_is_supported_invalid_param_test();
103+
104+
/**@}*/
105+
106+
#ifdef __cplusplus
107+
}
108+
#endif
109+
110+
#endif
111+
112+
#endif
113+
114+
/**@}*/

TESTS/mbed_hal/crc/main.cpp

Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2018 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#include "utest/utest.h"
17+
#include "unity/unity.h"
18+
#include "greentea-client/test_env.h"
19+
#include "mbed.h"
20+
#include "math.h"
21+
#include "crc_api.h"
22+
23+
#if !DEVICE_CRC
24+
#error [NOT_SUPPORTED] CRC not supported for this target
25+
#endif
26+
27+
using namespace utest::v1;
28+
29+
#define POLY_8BIT_MAXIM 0x31
30+
#define POLY_16BIT_MAXIM 0x8005
31+
#define POLY_32BIT_POSIX 0x4C11DB7
32+
33+
#define UNSUPPORTED (-1)
34+
#define POL_CNT (2)
35+
36+
const uint8_t input_data[] = "123456789";
37+
38+
typedef struct
39+
{
40+
const crc_mbed_config config_data;
41+
const char* input_data;
42+
uint32_t expected_result;
43+
44+
} TEST_CASE;
45+
46+
static TEST_CASE test_cases[] = {
47+
/* Predefined polynomials. */
48+
{ {POLY_7BIT_SD , 7, 0x00000000, 0x00000000, false, false}, "123456789", 0xEA },
49+
{ {POLY_7BIT_SD , 7, 0x0000007F, 0x00000000, false, false}, "123456789", 0xA0 },
50+
{ {POLY_7BIT_SD , 7, 0x0000002B, 0x00000000, false, false}, "123456789", 0x74 },
51+
{ {POLY_7BIT_SD , 7, 0x00000000, 0x0000007F, false, false}, "123456789", 0x95 },
52+
{ {POLY_7BIT_SD , 7, 0x00000000, 0x0000002B, false, false}, "123456789", 0xC1 },
53+
{ {POLY_7BIT_SD , 7, 0x00000000, 0x00000000, true , false}, "123456789", 0xA4 },
54+
{ {POLY_7BIT_SD , 7, 0x00000000, 0x00000000, false, true }, "123456789", 0x57 },
55+
56+
{ {POLY_8BIT_CCITT , 8, 0x00000000, 0x00000000, false, false}, "123456789", 0xF4 },
57+
{ {POLY_8BIT_CCITT , 8, 0x000000FF, 0x00000000, false, false}, "123456789", 0xFB },
58+
{ {POLY_8BIT_CCITT , 8, 0x000000AB, 0x00000000, false, false}, "123456789", 0x87 },
59+
{ {POLY_8BIT_CCITT , 8, 0x00000000, 0x000000FF, false, false}, "123456789", 0x0B },
60+
{ {POLY_8BIT_CCITT , 8, 0x00000000, 0x000000AB, false, false}, "123456789", 0x5F },
61+
{ {POLY_8BIT_CCITT , 8, 0x00000000, 0x00000000, true , false}, "123456789", 0x04 },
62+
{ {POLY_8BIT_CCITT , 8, 0x00000000, 0x00000000, false, true }, "123456789", 0x2F },
63+
64+
{ {POLY_16BIT_CCITT , 16, 0x00000000, 0x00000000, false, false}, "123456789", 0x31C3 },
65+
{ {POLY_16BIT_CCITT , 16, 0x0000FFFF, 0x00000000, false, false}, "123456789", 0x29B1 },
66+
{ {POLY_16BIT_CCITT , 16, 0x0000ABAB, 0x00000000, false, false}, "123456789", 0x7D70 },
67+
{ {POLY_16BIT_CCITT , 16, 0x00000000, 0x0000FFFF, false, false}, "123456789", 0xCE3C },
68+
{ {POLY_16BIT_CCITT , 16, 0x00000000, 0x0000ABAB, false, false}, "123456789", 0x9A68 },
69+
{ {POLY_16BIT_CCITT , 16, 0x00000000, 0x00000000, true , false}, "123456789", 0x9184 },
70+
{ {POLY_16BIT_CCITT , 16, 0x00000000, 0x00000000, false, true }, "123456789", 0xC38C },
71+
72+
{ {POLY_16BIT_IBM , 16, 0x00000000, 0x00000000, false, false}, "123456789", 0xFEE8 },
73+
{ {POLY_16BIT_IBM , 16, 0x0000FFFF, 0x00000000, false, false}, "123456789", 0xAEE7 },
74+
{ {POLY_16BIT_IBM , 16, 0x0000ABAB, 0x00000000, false, false}, "123456789", 0x0887 },
75+
{ {POLY_16BIT_IBM , 16, 0x00000000, 0x0000FFFF, false, false}, "123456789", 0x0117 },
76+
{ {POLY_16BIT_IBM , 16, 0x00000000, 0x0000ABAB, false, false}, "123456789", 0x5543 },
77+
{ {POLY_16BIT_IBM , 16, 0x00000000, 0x00000000, true , false}, "123456789", 0xBCDD },
78+
{ {POLY_16BIT_IBM , 16, 0x00000000, 0x00000000, false, true }, "123456789", 0x177F },
79+
80+
{ {POLY_32BIT_ANSI , 32, 0x00000000, 0x00000000, false, false}, "123456789", 0x89A1897F },
81+
{ {POLY_32BIT_ANSI , 32, 0xFFFFFFFF, 0x00000000, false, false}, "123456789", 0x0376E6E7 },
82+
{ {POLY_32BIT_ANSI , 32, 0xABABABAB, 0x00000000, false, false}, "123456789", 0x871A2FAA },
83+
{ {POLY_32BIT_ANSI , 32, 0x00000000, 0xFFFFFFFF, false, false}, "123456789", 0x765E7680 },
84+
{ {POLY_32BIT_ANSI , 32, 0x00000000, 0xABABABAB, false, false}, "123456789", 0x220A22D4 },
85+
{ {POLY_32BIT_ANSI , 32, 0x00000000, 0x00000000, true , false}, "123456789", 0x11B4BFB4 },
86+
{ {POLY_32BIT_ANSI , 32, 0x00000000, 0x00000000, false, true }, "123456789", 0xFE918591 },
87+
88+
/* Not-predefined polynomials. */
89+
{ {POLY_8BIT_MAXIM , 8, 0x00000000, 0x00000000, false, false}, "123456789", 0xA2 },
90+
{ {POLY_8BIT_MAXIM , 8, 0x000000FF, 0x00000000, false, false}, "123456789", 0xF7 },
91+
{ {POLY_8BIT_MAXIM , 8, 0x000000AB, 0x00000000, false, false}, "123456789", 0x71 },
92+
{ {POLY_8BIT_MAXIM , 8, 0x00000000, 0x000000FF, false, false}, "123456789", 0x5D },
93+
{ {POLY_8BIT_MAXIM , 8, 0x00000000, 0x000000AB, false, false}, "123456789", 0x09 },
94+
{ {POLY_8BIT_MAXIM , 8, 0x00000000, 0x00000000, true , false}, "123456789", 0x85 },
95+
{ {POLY_8BIT_MAXIM , 8, 0x00000000, 0x00000000, false, true }, "123456789", 0x45 },
96+
97+
{ {POLY_16BIT_MAXIM , 16, 0x00000000, 0x00000000, false, false}, "123456789", 0xFEE8 },
98+
{ {POLY_16BIT_MAXIM , 16, 0x0000FFFF, 0x00000000, false, false}, "123456789", 0xAEE7 },
99+
{ {POLY_16BIT_MAXIM , 16, 0x0000ABAB, 0x00000000, false, false}, "123456789", 0x0887 },
100+
{ {POLY_16BIT_MAXIM , 16, 0x00000000, 0x0000FFFF, false, false}, "123456789", 0x0117 },
101+
{ {POLY_16BIT_MAXIM , 16, 0x00000000, 0x0000ABAB, false, false}, "123456789", 0x5543 },
102+
{ {POLY_16BIT_MAXIM , 16, 0x00000000, 0x00000000, true , false}, "123456789", 0xBCDD },
103+
{ {POLY_16BIT_MAXIM , 16, 0x00000000, 0x00000000, false, true }, "123456789", 0x177F },
104+
105+
{ {POLY_32BIT_POSIX , 32, 0x00000000, 0x00000000, false, false}, "123456789", 0x89A1897F },
106+
{ {POLY_32BIT_POSIX , 32, 0xFFFFFFFF, 0x00000000, false, false}, "123456789", 0x0376E6E7 },
107+
{ {POLY_32BIT_POSIX , 32, 0xABABABAB, 0x00000000, false, false}, "123456789", 0x871A2FAA },
108+
{ {POLY_32BIT_POSIX , 32, 0x00000000, 0xFFFFFFFF, false, false}, "123456789", 0x765E7680 },
109+
{ {POLY_32BIT_POSIX , 32, 0x00000000, 0xABABABAB, false, false}, "123456789", 0x220A22D4 },
110+
{ {POLY_32BIT_POSIX , 32, 0x00000000, 0x00000000, true , false}, "123456789", 0x11B4BFB4 },
111+
{ {POLY_32BIT_POSIX , 32, 0x00000000, 0x00000000, false, true }, "123456789", 0xFE918591 },
112+
};
113+
114+
/* Test that hal_crc_is_supported() function returns true if given polynomial
115+
* is supported, false otherwise (at least one polynomial from the predefined list must be supported). */
116+
void crc_is_supported_test()
117+
{
118+
/* Check if at least one crc polynomial/config is supported. */
119+
uint32_t num_of_supported_polynomials = 0;
120+
121+
for (unsigned int i = 0; i < (sizeof(test_cases) / sizeof(TEST_CASE)); i++) {
122+
if (hal_crc_is_supported(&test_cases[i].config_data) == true) {
123+
num_of_supported_polynomials++;
124+
}
125+
}
126+
127+
TEST_ASSERT(num_of_supported_polynomials > 0);
128+
}
129+
130+
/* Test that CRC module can be successfully configured, fed with data and the result can
131+
* be successfully obtained. */
132+
void crc_calc_single_test()
133+
{
134+
for (unsigned int i = 0; i < (sizeof(test_cases) / sizeof(TEST_CASE)); i++) {
135+
if (hal_crc_is_supported(&test_cases[i].config_data) == true) {
136+
137+
hal_crc_compute_partial_start(&test_cases[i].config_data);
138+
hal_crc_compute_partial((uint8_t*) test_cases[i].input_data, strlen((const char*) test_cases[i].input_data));
139+
const uint32_t crc = hal_crc_get_result();
140+
141+
TEST_ASSERT_EQUAL(test_cases[i].expected_result, crc);
142+
}
143+
}
144+
}
145+
146+
/* Test that hal_crc_compute_partial() function can be call multiple times in
147+
* succession in order to provide additional data to CRC module. */
148+
void crc_calc_multi_test()
149+
{
150+
for (unsigned int i = 0; i < (sizeof(test_cases) / sizeof(TEST_CASE)); i++) {
151+
if (hal_crc_is_supported(&test_cases[i].config_data) == true) {
152+
153+
const uint32_t first_part_bytes = 3;
154+
const uint32_t second_part_bytes = 1;
155+
const uint32_t third_part_bytes = strlen((const char*) test_cases[i].input_data) - first_part_bytes
156+
- second_part_bytes;
157+
158+
hal_crc_compute_partial_start(&test_cases[i].config_data);
159+
hal_crc_compute_partial((uint8_t*) test_cases[i].input_data, first_part_bytes);
160+
hal_crc_compute_partial((uint8_t*) (test_cases[i].input_data + first_part_bytes), second_part_bytes);
161+
hal_crc_compute_partial((uint8_t*) (test_cases[i].input_data + first_part_bytes + second_part_bytes),
162+
third_part_bytes);
163+
const uint32_t crc = hal_crc_get_result();
164+
165+
TEST_ASSERT_EQUAL(test_cases[i].expected_result, crc);
166+
}
167+
}
168+
}
169+
170+
/* Test that calling hal_crc_compute_partial_start() without finalising the
171+
* CRC calculation overrides the current configuration. */
172+
void crc_reconfigure_test()
173+
{
174+
int pol_idx[POL_CNT] =
175+
{ UNSUPPORTED, UNSUPPORTED };
176+
int pol_cnt = 0;
177+
const uint8_t dummy_input_data[] = "abcdefghijklmnopqrstuvwxyz";
178+
179+
/* At least one configuration must be supported. If two are supported, then
180+
* re-initialize CRC module using different config. */
181+
for (unsigned int i = 0; i < (sizeof(test_cases) / sizeof(TEST_CASE)); i++) {
182+
183+
/* Find two supported polynomials if possible. */
184+
if (hal_crc_is_supported(&test_cases[i].config_data) == true) {
185+
if (pol_cnt == 0) {
186+
pol_idx[pol_cnt] = i;
187+
pol_cnt++;
188+
} else if (test_cases[pol_idx[0]].config_data.polynomial != test_cases[i].config_data.polynomial) {
189+
pol_idx[pol_cnt] = i;
190+
pol_cnt++;
191+
}
192+
193+
if (pol_cnt == POL_CNT) {
194+
break;
195+
}
196+
}
197+
}
198+
199+
pol_cnt = 0;
200+
201+
/* Init CRC module and provide some data, but do not read the result. */
202+
hal_crc_compute_partial_start(&test_cases[pol_idx[pol_cnt]].config_data);
203+
hal_crc_compute_partial((uint8_t*) dummy_input_data, strlen((const char*) dummy_input_data));
204+
205+
/* Change index only if more than one supported polynomial has been found. */
206+
if (pol_idx[POL_CNT - 1] != UNSUPPORTED) {
207+
pol_cnt++;
208+
}
209+
210+
/* Now re-init CRC module and provide new data and check the result. */
211+
hal_crc_compute_partial_start(&test_cases[pol_idx[pol_cnt]].config_data);
212+
hal_crc_compute_partial((uint8_t*) test_cases[pol_idx[pol_cnt]].input_data,
213+
strlen((const char*) test_cases[pol_idx[pol_cnt]].input_data));
214+
const uint32_t crc = hal_crc_get_result();
215+
216+
TEST_ASSERT_EQUAL(test_cases[pol_idx[pol_cnt]].expected_result, crc);
217+
}
218+
219+
/* Test that hal_crc_compute_partial() does nothing if pointer to buffer is undefined or
220+
* data length is equal to 0. */
221+
void crc_compute_partial_invalid_param_test()
222+
{
223+
uint32_t crc = 0;
224+
225+
/* At least one polynomial must be supported. */
226+
for (unsigned int i = 0; i < (sizeof(test_cases) / sizeof(TEST_CASE)); i++) {
227+
if (hal_crc_is_supported(&test_cases[i].config_data) == true) {
228+
229+
hal_crc_compute_partial_start(&test_cases[i].config_data);
230+
231+
/* Call hal_crc_compute_partial() with invalid parameters. */
232+
hal_crc_compute_partial((uint8_t*) NULL, strlen((const char*) test_cases[i].input_data));
233+
hal_crc_compute_partial((uint8_t*) test_cases[i].input_data, 0);
234+
235+
/* Now use valid parameters. */
236+
hal_crc_compute_partial((uint8_t*) test_cases[i].input_data,
237+
strlen((const char*) test_cases[i].input_data));
238+
239+
crc = hal_crc_get_result();
240+
241+
TEST_ASSERT_EQUAL(test_cases[i].expected_result, crc);
242+
243+
break;
244+
}
245+
}
246+
}
247+
248+
/* Test that hal_crc_is_supported() returns false if pointer to the config structure is undefined. */
249+
void crc_is_supported_invalid_param_test()
250+
{
251+
TEST_ASSERT_EQUAL(false, hal_crc_is_supported(NULL));
252+
}
253+
254+
Case cases[] = {
255+
Case("test: supported polynomials.", crc_is_supported_test),
256+
Case("test: CRC calculation - single input.", crc_calc_single_test),
257+
Case("test: CRC calculation - multi input.", crc_calc_multi_test),
258+
Case("test: re-configure without getting the result.", crc_reconfigure_test),
259+
Case("test: hal_crc_compute_partial() - invalid parameters.", crc_compute_partial_invalid_param_test),
260+
Case("test: hal_crc_is_supported() - invalid parameter.", crc_is_supported_invalid_param_test),
261+
};
262+
263+
utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
264+
{
265+
GREENTEA_SETUP(30, "default_auto");
266+
return greentea_test_setup_handler(number_of_cases);
267+
}
268+
269+
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
270+
271+
int main()
272+
{
273+
Harness::run(specification);
274+
}

0 commit comments

Comments
 (0)