This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git
The following commit(s) were added to refs/heads/master by this push:
new 34a4fc864 testing/crctest: Cover SMBus PEC and CRC8 compatibility.
34a4fc864 is described below
commit 34a4fc864c50ce58093cbfcad8ff05bbb034b4bc
Author: jsanchez-2g <[email protected]>
AuthorDate: Fri Oct 9 09:25:12 2026 -0500
testing/crctest: Cover SMBus PEC and CRC8 compatibility.
Exercise crc8smbus() and crc8smbuspart() using known PEC vectors,
all split points of the standard check string, empty buffers and all
256 seed values against an independent bitwise reference.
Verify the existing complemented crc8ccitt() behavior is preserved.
Retain the existing CRC variant tests. All nine cases pass in a native
build with real CMocka 1.1.7. Requires the companion NuttX SMBus helpers.
Assisted-by: Codex:GPT-6
Signed-off-by: jsanchez-2g <[email protected]>
---
testing/libc/crctest/crctest_main.c | 115 ++++++++++++++++++++++++++++++++++++
1 file changed, 115 insertions(+)
diff --git a/testing/libc/crctest/crctest_main.c
b/testing/libc/crctest/crctest_main.c
index 7ae966f91..6bc05ba94 100644
--- a/testing/libc/crctest/crctest_main.c
+++ b/testing/libc/crctest/crctest_main.c
@@ -126,6 +126,119 @@ static uint64_t crc64emac_expected_crc[] =
* Private Functions
****************************************************************************/
+/****************************************************************************
+ * Name: crc8smbus_reference
+ ****************************************************************************/
+
+static uint8_t crc8smbus_reference(FAR const uint8_t *src, size_t len,
+ uint8_t crc)
+{
+ size_t i;
+ unsigned int bit;
+
+ for (i = 0; i < len; i++)
+ {
+ crc ^= src[i];
+ for (bit = 0; bit < 8; bit++)
+ {
+ crc = (crc << 1) ^ ((crc & 0x80) ? 0x07 : 0);
+ }
+ }
+
+ return crc;
+}
+
+/****************************************************************************
+ * Name: test_case_crc8smbus
+ ****************************************************************************/
+
+static void test_case_crc8smbus(void **state)
+{
+ static const uint8_t check[] = "123456789";
+ static const uint8_t frame[] =
+ {
+ 0x11, 0x00
+ };
+
+ uint8_t data[256];
+ uint8_t crc;
+ size_t split;
+ unsigned int seed;
+ unsigned int i;
+
+ /* Known CRC-8/SMBUS check value and a BQ769x0 address/data frame. */
+
+ assert_int_equal(crc8smbus(check, sizeof(check) - 1), 0xf4);
+ assert_int_equal(crc8smbus(frame, sizeof(frame)), 0x42);
+ assert_int_equal(crc8smbus(check, 0), 0);
+
+ for (split = 0; split <= sizeof(check) - 1; split++)
+ {
+ crc = crc8smbuspart(check, split, 0);
+ crc = crc8smbuspart(check + split, sizeof(check) - 1 - split, crc);
+ assert_int_equal(crc, 0xf4);
+ }
+
+ for (i = 0; i < sizeof(data); i++)
+ {
+ data[i] = i;
+ }
+
+ /* Verify all seed values against an independent bitwise algorithm. */
+
+ for (seed = 0; seed <= UINT8_MAX; seed++)
+ {
+ assert_int_equal(crc8smbuspart(data, 0, seed), seed);
+ assert_int_equal(crc8smbuspart(data, sizeof(data), seed),
+ crc8smbus_reference(data, sizeof(data), seed));
+ }
+
+ crc = 0;
+ for (i = 0; i < sizeof(frame); i++)
+ {
+ crc = crc8smbuspart(&frame[i], 1, crc);
+ }
+
+ assert_int_equal(crc, 0x42);
+}
+
+/****************************************************************************
+ * Name: test_case_crc8ccitt_compatibility
+ ****************************************************************************/
+
+static void test_case_crc8ccitt_compatibility(void **state)
+{
+ static const uint8_t frame[] =
+ {
+ 0x11, 0x00
+ };
+
+ uint8_t data[256];
+ uint8_t expected;
+ uint8_t crc;
+ unsigned int seed;
+ unsigned int i;
+
+ /* Preserve the existing complemented API, including incremental use. */
+
+ assert_int_equal(crc8ccitt(frame, sizeof(frame)), 0x6a);
+ crc = crc8ccittpart(frame, 1, 0);
+ assert_int_equal(crc8ccittpart(frame + 1, 1, crc), 0x6a);
+
+ for (i = 0; i < sizeof(data); i++)
+ {
+ data[i] = i;
+ }
+
+ for (seed = 0; seed <= UINT8_MAX; seed++)
+ {
+ expected = crc8smbus_reference(data, sizeof(data), seed ^ 0xff);
+ assert_int_equal(crc8ccittpart(data, sizeof(data), seed),
+ expected ^ 0xff);
+ assert_int_equal(crc8ccittpart(data, 0, seed), seed);
+ }
+}
+
/****************************************************************************
* Name: test_case_crc8h1d
****************************************************************************/
@@ -320,6 +433,8 @@ int main(int argc, FAR char *argv[])
{
const struct CMUnitTest tests[] =
{
+ cmocka_unit_test(test_case_crc8smbus),
+ cmocka_unit_test(test_case_crc8ccitt_compatibility),
cmocka_unit_test(test_case_crc8h1d),
cmocka_unit_test(test_case_crc8h2f),
cmocka_unit_test(test_case_crc16h1021),