]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
lib/crc_kunit.c: add test and benchmark for crc7_be()
authorEric Biggers <ebiggers@google.com>
Tue, 4 Mar 2025 22:39:43 +0000 (14:39 -0800)
committerEric Biggers <ebiggers@google.com>
Mon, 10 Mar 2025 16:29:29 +0000 (09:29 -0700)
Wire up crc7_be() to crc_kunit.  Previously it had no test.

Link: https://lore.kernel.org/r/20250304223943.157493-1-ebiggers@kernel.org
Signed-off-by: Eric Biggers <ebiggers@google.com>
lib/Kconfig.debug
lib/crc_kunit.c

index 1af972a92d06f6e3f7beec4bd086c00b412c83ac..141855e9d8c43e3da720ab8b92dece0c307ca9d6 100644 (file)
@@ -2852,6 +2852,7 @@ config CRC_KUNIT_TEST
        tristate "KUnit tests for CRC functions" if !KUNIT_ALL_TESTS
        depends on KUNIT
        default KUNIT_ALL_TESTS
+       select CRC7
        select CRC16
        select CRC_T10DIF
        select CRC32
index 40b4b41f218474d5ae3c1dfd178242bc28e60c1e..0e15eb244b20a0813ad63df2b1413892af2cce7d 100644 (file)
@@ -7,6 +7,7 @@
  * Author: Eric Biggers <ebiggers@google.com>
  */
 #include <kunit/test.h>
+#include <linux/crc7.h>
 #include <linux/crc16.h>
 #include <linux/crc-t10dif.h>
 #include <linux/crc32.h>
@@ -32,8 +33,9 @@ static size_t test_buflen;
  * @poly: The generator polynomial with the highest-order term omitted.
  *       Bit-reversed if @le is true.
  * @func: The function to compute a CRC.  The type signature uses u64 so that it
- *       can fit any CRC up to CRC-64.  The function is expected to *not*
- *       invert the CRC at the beginning and end.
+ *       can fit any CRC up to CRC-64.  The CRC is passed in, and is expected
+ *       to be returned in, the least significant bits of the u64.  The
+ *       function is expected to *not* invert the CRC at the beginning and end.
  * @combine_func: Optional function to combine two CRCs.
  */
 struct crc_variant {
@@ -252,6 +254,33 @@ crc_benchmark(struct kunit *test,
        }
 }
 
+/* crc7_be */
+
+static u64 crc7_be_wrapper(u64 crc, const u8 *p, size_t len)
+{
+       /*
+        * crc7_be() left-aligns the 7-bit CRC in a u8, whereas the test wants a
+        * right-aligned CRC (in a u64).  Convert between the conventions.
+        */
+       return crc7_be(crc << 1, p, len) >> 1;
+}
+
+static const struct crc_variant crc_variant_crc7_be = {
+       .bits = 7,
+       .poly = 0x9,
+       .func = crc7_be_wrapper,
+};
+
+static void crc7_be_test(struct kunit *test)
+{
+       crc_test(test, &crc_variant_crc7_be);
+}
+
+static void crc7_be_benchmark(struct kunit *test)
+{
+       crc_benchmark(test, crc7_be_wrapper);
+}
+
 /* crc16 */
 
 static u64 crc16_wrapper(u64 crc, const u8 *p, size_t len)
@@ -434,6 +463,8 @@ static void crc64_nvme_benchmark(struct kunit *test)
 }
 
 static struct kunit_case crc_test_cases[] = {
+       KUNIT_CASE(crc7_be_test),
+       KUNIT_CASE(crc7_be_benchmark),
        KUNIT_CASE(crc16_test),
        KUNIT_CASE(crc16_benchmark),
        KUNIT_CASE(crc_t10dif_test),