|
| 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