Skip to content

Commit 7715f8c

Browse files
committed
lib/crc_kunit.c: add test and benchmark for crc7_be()
Wire up crc7_be() to crc_kunit. Previously it had no test. Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Eric Biggers <[email protected]>
1 parent 5aebe00 commit 7715f8c

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

lib/Kconfig.debug

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2852,6 +2852,7 @@ config CRC_KUNIT_TEST
28522852
tristate "KUnit tests for CRC functions" if !KUNIT_ALL_TESTS
28532853
depends on KUNIT
28542854
default KUNIT_ALL_TESTS
2855+
select CRC7
28552856
select CRC16
28562857
select CRC_T10DIF
28572858
select CRC32

lib/crc_kunit.c

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Author: Eric Biggers <[email protected]>
88
*/
99
#include <kunit/test.h>
10+
#include <linux/crc7.h>
1011
#include <linux/crc16.h>
1112
#include <linux/crc-t10dif.h>
1213
#include <linux/crc32.h>
@@ -32,8 +33,9 @@ static size_t test_buflen;
3233
* @poly: The generator polynomial with the highest-order term omitted.
3334
* Bit-reversed if @le is true.
3435
* @func: The function to compute a CRC. The type signature uses u64 so that it
35-
* can fit any CRC up to CRC-64. The function is expected to *not*
36-
* invert the CRC at the beginning and end.
36+
* can fit any CRC up to CRC-64. The CRC is passed in, and is expected
37+
* to be returned in, the least significant bits of the u64. The
38+
* function is expected to *not* invert the CRC at the beginning and end.
3739
* @combine_func: Optional function to combine two CRCs.
3840
*/
3941
struct crc_variant {
@@ -252,6 +254,33 @@ crc_benchmark(struct kunit *test,
252254
}
253255
}
254256

257+
/* crc7_be */
258+
259+
static u64 crc7_be_wrapper(u64 crc, const u8 *p, size_t len)
260+
{
261+
/*
262+
* crc7_be() left-aligns the 7-bit CRC in a u8, whereas the test wants a
263+
* right-aligned CRC (in a u64). Convert between the conventions.
264+
*/
265+
return crc7_be(crc << 1, p, len) >> 1;
266+
}
267+
268+
static const struct crc_variant crc_variant_crc7_be = {
269+
.bits = 7,
270+
.poly = 0x9,
271+
.func = crc7_be_wrapper,
272+
};
273+
274+
static void crc7_be_test(struct kunit *test)
275+
{
276+
crc_test(test, &crc_variant_crc7_be);
277+
}
278+
279+
static void crc7_be_benchmark(struct kunit *test)
280+
{
281+
crc_benchmark(test, crc7_be_wrapper);
282+
}
283+
255284
/* crc16 */
256285

257286
static u64 crc16_wrapper(u64 crc, const u8 *p, size_t len)
@@ -434,6 +463,8 @@ static void crc64_nvme_benchmark(struct kunit *test)
434463
}
435464

436465
static struct kunit_case crc_test_cases[] = {
466+
KUNIT_CASE(crc7_be_test),
467+
KUNIT_CASE(crc7_be_benchmark),
437468
KUNIT_CASE(crc16_test),
438469
KUNIT_CASE(crc16_benchmark),
439470
KUNIT_CASE(crc_t10dif_test),

0 commit comments

Comments
 (0)