Skip to content

Commit 0a7e671

Browse files
committed
ztest: add tests for crc32
Coverage analysis showed crc32 in src/math/numbers.c was not exercised by either the CMock or the Ztest arithmetic test suites. Add test_crc32_ztest.c to the sof.unit.math.basic.arithmetic suite with six test cases covering all meaningful code paths: - empty buffer (zero-iteration outer loop) - single 0x00 byte (inner loop, XOR-with-polynomial branch taken) - single 0xFF byte (inner loop, shift-only branch taken) - standard check vector "123456789" → 0xCBF43926 (ISO 3309 / ITU-T V.42) - non-zero base value (different initial CRC register, verifies chaining) - four-byte all-zeros buffer (multiple outer-loop iterations) Signed-off-by: Tomasz Leman <tomasz.m.leman@intel.com>
1 parent 2129278 commit 0a7e671

2 files changed

Lines changed: 105 additions & 0 deletions

File tree

test/ztest/unit/math/basic/arithmetic/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ target_sources(app PRIVATE
2626
test_find_min_int16_ztest.c
2727
test_find_max_abs_int32_ztest.c
2828
test_norm_int32_ztest.c
29+
test_crc32_ztest.c
2930
${SOF_ROOT}/src/math/numbers.c
3031
)
3132

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
//
3+
// Copyright(c) 2026 Intel Corporation. All rights reserved.
4+
//
5+
// Author: Tomasz Leman <tomasz.m.leman@intel.com>
6+
7+
#include <zephyr/ztest.h>
8+
#include <sof/math/numbers.h>
9+
#include <stdint.h>
10+
11+
/**
12+
* @brief Test crc32 with an empty buffer
13+
*
14+
* With zero bytes the outer loop does not execute and the function returns
15+
* ~(~base) == 0 for base == 0.
16+
*/
17+
ZTEST(math_arithmetic_suite, test_math_numbers_crc32_empty_buffer)
18+
{
19+
uint8_t data[1] = {0};
20+
uint32_t result = crc32(0, data, 0);
21+
22+
zassert_equal(result, 0x00000000U,
23+
"crc32 of empty buffer with base=0 should be 0x00000000");
24+
}
25+
26+
/**
27+
* @brief Test crc32 with a single zero byte
28+
*
29+
* Exercises one iteration of the outer loop and all 8 iterations of the
30+
* inner bit-processing loop.
31+
*/
32+
ZTEST(math_arithmetic_suite, test_math_numbers_crc32_single_zero_byte)
33+
{
34+
uint8_t data[] = {0x00};
35+
uint32_t result = crc32(0, data, sizeof(data));
36+
37+
zassert_equal(result, 0xD202EF8DU,
38+
"crc32({0x00}, base=0) should be 0xD202EF8D");
39+
}
40+
41+
/**
42+
* @brief Test crc32 with a single 0xFF byte
43+
*
44+
* Exercises the branch where (cur & 1) is false in the inner loop.
45+
*/
46+
ZTEST(math_arithmetic_suite, test_math_numbers_crc32_single_ff_byte)
47+
{
48+
uint8_t data[] = {0xFF};
49+
uint32_t result = crc32(0, data, sizeof(data));
50+
51+
zassert_equal(result, 0xFF000000U,
52+
"crc32({0xFF}, base=0) should be 0xFF000000");
53+
}
54+
55+
/**
56+
* @brief Test crc32 against the well-known CRC-32 check value
57+
*
58+
* The string "123456789" must produce 0xCBF43926 — the standard reference
59+
* value for CRC-32 (ISO 3309 / ITU-T V.42).
60+
*/
61+
ZTEST(math_arithmetic_suite, test_math_numbers_crc32_known_vector_123456789)
62+
{
63+
const uint8_t data[] = "123456789";
64+
uint32_t result = crc32(0, data, sizeof(data) - 1); /* exclude NUL */
65+
66+
zassert_equal(result, 0xCBF43926U,
67+
"crc32(\"123456789\", base=0) should be 0xCBF43926");
68+
}
69+
70+
/**
71+
* @brief Test crc32 with a non-zero base
72+
*
73+
* A non-zero base changes the initial CRC register value (~base), allowing
74+
* incremental / chained CRC computation. Verifies the result differs from the
75+
* base=0 case and matches the expected precomputed value.
76+
*/
77+
ZTEST(math_arithmetic_suite, test_math_numbers_crc32_nonzero_base)
78+
{
79+
uint8_t data[] = {0xAB, 0xCD, 0xEF};
80+
uint32_t result_base0 = crc32(0x00000000U, data, sizeof(data));
81+
uint32_t result_nonzero = crc32(0xDEADBEEFU, data, sizeof(data));
82+
83+
zassert_equal(result_base0, 0x648D3D79U,
84+
"crc32({0xAB,0xCD,0xEF}, base=0) should be 0x648D3D79");
85+
zassert_equal(result_nonzero, 0x1412F659U,
86+
"crc32({0xAB,0xCD,0xEF}, base=0xDEADBEEF) should be 0x1412F659");
87+
zassert_not_equal(result_base0, result_nonzero,
88+
"Different bases must yield different CRC values");
89+
}
90+
91+
/**
92+
* @brief Test crc32 over a four-byte all-zeros buffer
93+
*
94+
* Exercises multiple iterations of the outer byte loop, producing a result
95+
* that differs from the single-byte zero-byte case.
96+
*/
97+
ZTEST(math_arithmetic_suite, test_math_numbers_crc32_four_zero_bytes)
98+
{
99+
uint8_t data[] = {0x00, 0x00, 0x00, 0x00};
100+
uint32_t result = crc32(0, data, sizeof(data));
101+
102+
zassert_equal(result, 0x2144DF1CU,
103+
"crc32({0,0,0,0}, base=0) should be 0x2144DF1C");
104+
}

0 commit comments

Comments
 (0)