blob: 5e1ea8d6c92cc73a4485ba1c37a8621d8be7995d [file] [log] [blame]
Julian Halld635ad72022-09-20 15:50:35 +01001/*
2 * Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <common/crc32/crc32.h>
8#include <CppUTest/TestHarness.h>
9
10TEST_GROUP(Crc32Tests)
11{
Balint Dobszay550ce872022-12-15 15:28:40 +010012 TEST_SETUP()
13 {
14 crc32_init();
15 }
Julian Halld635ad72022-09-20 15:50:35 +010016};
17
18/*
19 * Expected results obtained from: https://crc32.online/
20 */
21TEST(Crc32Tests, shortString)
22{
23 const unsigned char test_input[] = "Hello";
24 uint32_t expected_result = 0xf7d18982;
25 uint32_t input_crc = 0;
26
27 uint32_t result = crc32(input_crc, test_input, sizeof(test_input) - 1);
28
29 UNSIGNED_LONGS_EQUAL(expected_result, result);
30}
31
32TEST(Crc32Tests, longString)
33{
34 const unsigned char test_input[] =
35 "The boy stood on the burning deck Whence all but he had fled";
36 uint32_t expected_result = 0x1f11704c;
37 uint32_t input_crc = 0;
38
39 uint32_t result = crc32(input_crc, test_input, sizeof(test_input) - 1);
40
41 UNSIGNED_LONGS_EQUAL(expected_result, result);
42}
43
44TEST(Crc32Tests, multiPart)
45{
46 const unsigned char test_input_1[] =
47 "The boy stood on the burning deck ";
48 const unsigned char test_input_2[] =
49 "Whence all but he had fled";
50
51 uint32_t expected_result = 0x1f11704c;
52 uint32_t result = 0;
53
54 result = crc32(result, test_input_1, sizeof(test_input_1) - 1);
55 result = crc32(result, test_input_2, sizeof(test_input_2) - 1);
56
57 UNSIGNED_LONGS_EQUAL(expected_result, result);
58}