lib: kunit: add bitfield test conversion to KUnit
This adds the conversion of the runtime tests of test_bitfield, from `lib/test_bitfield.c` to KUnit tests. Code Style Documentation: [0] Signed-off-by: Vitor Massaru Iha <vitor@massaru.org> Link: [0] https://lore.kernel.org/linux-kselftest/20200620054944.167330-1-davidgow@google.com/T/#u Reviewed-by: Brendan Higgins <brendanhiggins@google.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
This commit is contained in:
parent
a82763e63e
commit
d2585f5164
@ -2037,13 +2037,6 @@ config TEST_BITMAP
|
|||||||
|
|
||||||
If unsure, say N.
|
If unsure, say N.
|
||||||
|
|
||||||
config TEST_BITFIELD
|
|
||||||
tristate "Test bitfield functions at runtime"
|
|
||||||
help
|
|
||||||
Enable this option to test the bitfield functions at boot.
|
|
||||||
|
|
||||||
If unsure, say N.
|
|
||||||
|
|
||||||
config TEST_UUID
|
config TEST_UUID
|
||||||
tristate "Test functions located in the uuid module at runtime"
|
tristate "Test functions located in the uuid module at runtime"
|
||||||
|
|
||||||
@ -2193,6 +2186,22 @@ config TEST_SYSCTL
|
|||||||
|
|
||||||
If unsure, say N.
|
If unsure, say N.
|
||||||
|
|
||||||
|
config BITFIELD_KUNIT
|
||||||
|
tristate "KUnit test bitfield functions at runtime"
|
||||||
|
depends on KUNIT
|
||||||
|
help
|
||||||
|
Enable this option to test the bitfield functions at boot.
|
||||||
|
|
||||||
|
KUnit tests run during boot and output the results to the debug log
|
||||||
|
in TAP format (http://testanything.org/). Only useful for kernel devs
|
||||||
|
running the KUnit test harness, and not intended for inclusion into a
|
||||||
|
production build.
|
||||||
|
|
||||||
|
For more information on KUnit and unit tests in general please refer
|
||||||
|
to the KUnit documentation in Documentation/dev-tools/kunit/.
|
||||||
|
|
||||||
|
If unsure, say N.
|
||||||
|
|
||||||
config SYSCTL_KUNIT_TEST
|
config SYSCTL_KUNIT_TEST
|
||||||
tristate "KUnit test for sysctl" if !KUNIT_ALL_TESTS
|
tristate "KUnit test for sysctl" if !KUNIT_ALL_TESTS
|
||||||
depends on KUNIT
|
depends on KUNIT
|
||||||
|
@ -80,7 +80,6 @@ obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_key_base.o
|
|||||||
obj-$(CONFIG_TEST_PRINTF) += test_printf.o
|
obj-$(CONFIG_TEST_PRINTF) += test_printf.o
|
||||||
obj-$(CONFIG_TEST_BITMAP) += test_bitmap.o
|
obj-$(CONFIG_TEST_BITMAP) += test_bitmap.o
|
||||||
obj-$(CONFIG_TEST_STRSCPY) += test_strscpy.o
|
obj-$(CONFIG_TEST_STRSCPY) += test_strscpy.o
|
||||||
obj-$(CONFIG_TEST_BITFIELD) += test_bitfield.o
|
|
||||||
obj-$(CONFIG_TEST_UUID) += test_uuid.o
|
obj-$(CONFIG_TEST_UUID) += test_uuid.o
|
||||||
obj-$(CONFIG_TEST_XARRAY) += test_xarray.o
|
obj-$(CONFIG_TEST_XARRAY) += test_xarray.o
|
||||||
obj-$(CONFIG_TEST_PARMAN) += test_parman.o
|
obj-$(CONFIG_TEST_PARMAN) += test_parman.o
|
||||||
@ -340,6 +339,7 @@ obj-$(CONFIG_OBJAGG) += objagg.o
|
|||||||
obj-$(CONFIG_PLDMFW) += pldmfw/
|
obj-$(CONFIG_PLDMFW) += pldmfw/
|
||||||
|
|
||||||
# KUnit tests
|
# KUnit tests
|
||||||
|
obj-$(CONFIG_BITFIELD_KUNIT) += bitfield_kunit.o
|
||||||
obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o
|
obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o
|
||||||
obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o
|
obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o
|
||||||
obj-$(CONFIG_BITS_TEST) += test_bits.o
|
obj-$(CONFIG_BITS_TEST) += test_bits.o
|
||||||
|
@ -5,8 +5,7 @@
|
|||||||
|
|
||||||
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
||||||
|
|
||||||
#include <linux/kernel.h>
|
#include <kunit/test.h>
|
||||||
#include <linux/module.h>
|
|
||||||
#include <linux/bitfield.h>
|
#include <linux/bitfield.h>
|
||||||
|
|
||||||
#define CHECK_ENC_GET_U(tp, v, field, res) do { \
|
#define CHECK_ENC_GET_U(tp, v, field, res) do { \
|
||||||
@ -14,13 +13,11 @@
|
|||||||
u##tp _res; \
|
u##tp _res; \
|
||||||
\
|
\
|
||||||
_res = u##tp##_encode_bits(v, field); \
|
_res = u##tp##_encode_bits(v, field); \
|
||||||
if (_res != res) { \
|
KUNIT_ASSERT_FALSE_MSG(context, _res != res, \
|
||||||
pr_warn("u" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != " #res "\n",\
|
"u" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != " #res "\n", \
|
||||||
(u64)_res); \
|
(u64)_res); \
|
||||||
return -EINVAL; \
|
KUNIT_ASSERT_FALSE(context, \
|
||||||
} \
|
u##tp##_get_bits(_res, field) != v); \
|
||||||
if (u##tp##_get_bits(_res, field) != v) \
|
|
||||||
return -EINVAL; \
|
|
||||||
} \
|
} \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
@ -29,14 +26,13 @@
|
|||||||
__le##tp _res; \
|
__le##tp _res; \
|
||||||
\
|
\
|
||||||
_res = le##tp##_encode_bits(v, field); \
|
_res = le##tp##_encode_bits(v, field); \
|
||||||
if (_res != cpu_to_le##tp(res)) { \
|
KUNIT_ASSERT_FALSE_MSG(context, \
|
||||||
pr_warn("le" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != 0x%llx\n",\
|
_res != cpu_to_le##tp(res), \
|
||||||
(u64)le##tp##_to_cpu(_res), \
|
"le" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != 0x%llx",\
|
||||||
(u64)(res)); \
|
(u64)le##tp##_to_cpu(_res), \
|
||||||
return -EINVAL; \
|
(u64)(res)); \
|
||||||
} \
|
KUNIT_ASSERT_FALSE(context, \
|
||||||
if (le##tp##_get_bits(_res, field) != v) \
|
le##tp##_get_bits(_res, field) != v);\
|
||||||
return -EINVAL; \
|
|
||||||
} \
|
} \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
@ -45,14 +41,13 @@
|
|||||||
__be##tp _res; \
|
__be##tp _res; \
|
||||||
\
|
\
|
||||||
_res = be##tp##_encode_bits(v, field); \
|
_res = be##tp##_encode_bits(v, field); \
|
||||||
if (_res != cpu_to_be##tp(res)) { \
|
KUNIT_ASSERT_FALSE_MSG(context, \
|
||||||
pr_warn("be" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != 0x%llx\n",\
|
_res != cpu_to_be##tp(res), \
|
||||||
(u64)be##tp##_to_cpu(_res), \
|
"be" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != 0x%llx", \
|
||||||
(u64)(res)); \
|
(u64)be##tp##_to_cpu(_res), \
|
||||||
return -EINVAL; \
|
(u64)(res)); \
|
||||||
} \
|
KUNIT_ASSERT_FALSE(context, \
|
||||||
if (be##tp##_get_bits(_res, field) != v) \
|
be##tp##_get_bits(_res, field) != v);\
|
||||||
return -EINVAL; \
|
|
||||||
} \
|
} \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
@ -62,7 +57,7 @@
|
|||||||
CHECK_ENC_GET_BE(tp, v, field, res); \
|
CHECK_ENC_GET_BE(tp, v, field, res); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
static int test_constants(void)
|
static void __init test_bitfields_constants(struct kunit *context)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* NOTE
|
* NOTE
|
||||||
@ -95,19 +90,17 @@ static int test_constants(void)
|
|||||||
CHECK_ENC_GET(64, 7, 0x00f0000000000000ull, 0x0070000000000000ull);
|
CHECK_ENC_GET(64, 7, 0x00f0000000000000ull, 0x0070000000000000ull);
|
||||||
CHECK_ENC_GET(64, 14, 0x0f00000000000000ull, 0x0e00000000000000ull);
|
CHECK_ENC_GET(64, 14, 0x0f00000000000000ull, 0x0e00000000000000ull);
|
||||||
CHECK_ENC_GET(64, 15, 0xf000000000000000ull, 0xf000000000000000ull);
|
CHECK_ENC_GET(64, 15, 0xf000000000000000ull, 0xf000000000000000ull);
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#define CHECK(tp, mask) do { \
|
#define CHECK(tp, mask) do { \
|
||||||
u64 v; \
|
u64 v; \
|
||||||
\
|
\
|
||||||
for (v = 0; v < 1 << hweight32(mask); v++) \
|
for (v = 0; v < 1 << hweight32(mask); v++) \
|
||||||
if (tp##_encode_bits(v, mask) != v << __ffs64(mask)) \
|
KUNIT_ASSERT_FALSE(context, \
|
||||||
return -EINVAL; \
|
tp##_encode_bits(v, mask) != v << __ffs64(mask));\
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
static int test_variables(void)
|
static void __init test_bitfields_variables(struct kunit *context)
|
||||||
{
|
{
|
||||||
CHECK(u8, 0x0f);
|
CHECK(u8, 0x0f);
|
||||||
CHECK(u8, 0xf0);
|
CHECK(u8, 0xf0);
|
||||||
@ -130,39 +123,34 @@ static int test_variables(void)
|
|||||||
CHECK(u64, 0x000000007f000000ull);
|
CHECK(u64, 0x000000007f000000ull);
|
||||||
CHECK(u64, 0x0000000018000000ull);
|
CHECK(u64, 0x0000000018000000ull);
|
||||||
CHECK(u64, 0x0000001f8000000ull);
|
CHECK(u64, 0x0000001f8000000ull);
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int __init test_bitfields(void)
|
|
||||||
|
static void __init test_bitfields_compile(struct kunit *context)
|
||||||
{
|
{
|
||||||
int ret = test_constants();
|
|
||||||
|
|
||||||
if (ret) {
|
|
||||||
pr_warn("constant tests failed!\n");
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = test_variables();
|
|
||||||
if (ret) {
|
|
||||||
pr_warn("variable tests failed!\n");
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef TEST_BITFIELD_COMPILE
|
|
||||||
/* these should fail compilation */
|
/* these should fail compilation */
|
||||||
CHECK_ENC_GET(16, 16, 0x0f00, 0x1000);
|
CHECK_ENC_GET(16, 16, 0x0f00, 0x1000);
|
||||||
u32_encode_bits(7, 0x06000000);
|
u32_encode_bits(7, 0x06000000);
|
||||||
|
|
||||||
/* this should at least give a warning */
|
/* this should at least give a warning */
|
||||||
u16_encode_bits(0, 0x60000);
|
u16_encode_bits(0, 0x60000);
|
||||||
#endif
|
|
||||||
|
|
||||||
pr_info("tests passed\n");
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
module_init(test_bitfields)
|
|
||||||
|
static struct kunit_case __refdata bitfields_test_cases[] = {
|
||||||
|
KUNIT_CASE(test_bitfields_constants),
|
||||||
|
KUNIT_CASE(test_bitfields_variables),
|
||||||
|
#ifdef TEST_BITFIELD_COMPILE
|
||||||
|
KUNIT_CASE(test_bitfields_compile),
|
||||||
|
#endif
|
||||||
|
{}
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct kunit_suite bitfields_test_suite = {
|
||||||
|
.name = "bitfields",
|
||||||
|
.test_cases = bitfields_test_cases,
|
||||||
|
};
|
||||||
|
|
||||||
|
kunit_test_suites(&bitfields_test_suite);
|
||||||
|
|
||||||
MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
|
MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
|
||||||
MODULE_LICENSE("GPL");
|
MODULE_LICENSE("GPL");
|
Loading…
Reference in New Issue
Block a user