18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Test cases for bitfield helpers.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <kunit/test.h>
98c2ecf20Sopenharmony_ci#include <linux/bitfield.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#define CHECK_ENC_GET_U(tp, v, field, res) do {				\
128c2ecf20Sopenharmony_ci		{							\
138c2ecf20Sopenharmony_ci			u##tp _res;					\
148c2ecf20Sopenharmony_ci									\
158c2ecf20Sopenharmony_ci			_res = u##tp##_encode_bits(v, field);		\
168c2ecf20Sopenharmony_ci			KUNIT_ASSERT_FALSE_MSG(context, _res != res,	\
178c2ecf20Sopenharmony_ci				       "u" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != " #res "\n",	\
188c2ecf20Sopenharmony_ci				       (u64)_res);			\
198c2ecf20Sopenharmony_ci			KUNIT_ASSERT_FALSE(context,			\
208c2ecf20Sopenharmony_ci				   u##tp##_get_bits(_res, field) != v);	\
218c2ecf20Sopenharmony_ci		}							\
228c2ecf20Sopenharmony_ci	} while (0)
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#define CHECK_ENC_GET_LE(tp, v, field, res) do {			\
258c2ecf20Sopenharmony_ci		{							\
268c2ecf20Sopenharmony_ci			__le##tp _res;					\
278c2ecf20Sopenharmony_ci									\
288c2ecf20Sopenharmony_ci			_res = le##tp##_encode_bits(v, field);		\
298c2ecf20Sopenharmony_ci			KUNIT_ASSERT_FALSE_MSG(context,			\
308c2ecf20Sopenharmony_ci				       _res != cpu_to_le##tp(res),	\
318c2ecf20Sopenharmony_ci				       "le" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != 0x%llx",\
328c2ecf20Sopenharmony_ci				       (u64)le##tp##_to_cpu(_res),	\
338c2ecf20Sopenharmony_ci				       (u64)(res));			\
348c2ecf20Sopenharmony_ci			KUNIT_ASSERT_FALSE(context,			\
358c2ecf20Sopenharmony_ci				   le##tp##_get_bits(_res, field) != v);\
368c2ecf20Sopenharmony_ci		}							\
378c2ecf20Sopenharmony_ci	} while (0)
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#define CHECK_ENC_GET_BE(tp, v, field, res) do {			\
408c2ecf20Sopenharmony_ci		{							\
418c2ecf20Sopenharmony_ci			__be##tp _res;					\
428c2ecf20Sopenharmony_ci									\
438c2ecf20Sopenharmony_ci			_res = be##tp##_encode_bits(v, field);		\
448c2ecf20Sopenharmony_ci			KUNIT_ASSERT_FALSE_MSG(context,			\
458c2ecf20Sopenharmony_ci				       _res != cpu_to_be##tp(res),	\
468c2ecf20Sopenharmony_ci				       "be" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != 0x%llx",	\
478c2ecf20Sopenharmony_ci				       (u64)be##tp##_to_cpu(_res),	\
488c2ecf20Sopenharmony_ci				       (u64)(res));			\
498c2ecf20Sopenharmony_ci			KUNIT_ASSERT_FALSE(context,			\
508c2ecf20Sopenharmony_ci				   be##tp##_get_bits(_res, field) != v);\
518c2ecf20Sopenharmony_ci		}							\
528c2ecf20Sopenharmony_ci	} while (0)
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define CHECK_ENC_GET(tp, v, field, res) do {				\
558c2ecf20Sopenharmony_ci		CHECK_ENC_GET_U(tp, v, field, res);			\
568c2ecf20Sopenharmony_ci		CHECK_ENC_GET_LE(tp, v, field, res);			\
578c2ecf20Sopenharmony_ci		CHECK_ENC_GET_BE(tp, v, field, res);			\
588c2ecf20Sopenharmony_ci	} while (0)
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic void __init test_bitfields_constants(struct kunit *context)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	/*
638c2ecf20Sopenharmony_ci	 * NOTE
648c2ecf20Sopenharmony_ci	 * This whole function compiles (or at least should, if everything
658c2ecf20Sopenharmony_ci	 * is going according to plan) to nothing after optimisation.
668c2ecf20Sopenharmony_ci	 */
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	CHECK_ENC_GET(16,  1, 0x000f, 0x0001);
698c2ecf20Sopenharmony_ci	CHECK_ENC_GET(16,  3, 0x00f0, 0x0030);
708c2ecf20Sopenharmony_ci	CHECK_ENC_GET(16,  5, 0x0f00, 0x0500);
718c2ecf20Sopenharmony_ci	CHECK_ENC_GET(16,  7, 0xf000, 0x7000);
728c2ecf20Sopenharmony_ci	CHECK_ENC_GET(16, 14, 0x000f, 0x000e);
738c2ecf20Sopenharmony_ci	CHECK_ENC_GET(16, 15, 0x00f0, 0x00f0);
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	CHECK_ENC_GET_U(8,  1, 0x0f, 0x01);
768c2ecf20Sopenharmony_ci	CHECK_ENC_GET_U(8,  3, 0xf0, 0x30);
778c2ecf20Sopenharmony_ci	CHECK_ENC_GET_U(8, 14, 0x0f, 0x0e);
788c2ecf20Sopenharmony_ci	CHECK_ENC_GET_U(8, 15, 0xf0, 0xf0);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	CHECK_ENC_GET(32,  1, 0x00000f00, 0x00000100);
818c2ecf20Sopenharmony_ci	CHECK_ENC_GET(32,  3, 0x0000f000, 0x00003000);
828c2ecf20Sopenharmony_ci	CHECK_ENC_GET(32,  5, 0x000f0000, 0x00050000);
838c2ecf20Sopenharmony_ci	CHECK_ENC_GET(32,  7, 0x00f00000, 0x00700000);
848c2ecf20Sopenharmony_ci	CHECK_ENC_GET(32, 14, 0x0f000000, 0x0e000000);
858c2ecf20Sopenharmony_ci	CHECK_ENC_GET(32, 15, 0xf0000000, 0xf0000000);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	CHECK_ENC_GET(64,  1, 0x00000f0000000000ull, 0x0000010000000000ull);
888c2ecf20Sopenharmony_ci	CHECK_ENC_GET(64,  3, 0x0000f00000000000ull, 0x0000300000000000ull);
898c2ecf20Sopenharmony_ci	CHECK_ENC_GET(64,  5, 0x000f000000000000ull, 0x0005000000000000ull);
908c2ecf20Sopenharmony_ci	CHECK_ENC_GET(64,  7, 0x00f0000000000000ull, 0x0070000000000000ull);
918c2ecf20Sopenharmony_ci	CHECK_ENC_GET(64, 14, 0x0f00000000000000ull, 0x0e00000000000000ull);
928c2ecf20Sopenharmony_ci	CHECK_ENC_GET(64, 15, 0xf000000000000000ull, 0xf000000000000000ull);
938c2ecf20Sopenharmony_ci}
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci#define CHECK(tp, mask) do {						\
968c2ecf20Sopenharmony_ci		u64 v;							\
978c2ecf20Sopenharmony_ci									\
988c2ecf20Sopenharmony_ci		for (v = 0; v < 1 << hweight32(mask); v++)		\
998c2ecf20Sopenharmony_ci			KUNIT_ASSERT_FALSE(context,			\
1008c2ecf20Sopenharmony_ci				tp##_encode_bits(v, mask) != v << __ffs64(mask));\
1018c2ecf20Sopenharmony_ci	} while (0)
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cistatic void __init test_bitfields_variables(struct kunit *context)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	CHECK(u8, 0x0f);
1068c2ecf20Sopenharmony_ci	CHECK(u8, 0xf0);
1078c2ecf20Sopenharmony_ci	CHECK(u8, 0x38);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	CHECK(u16, 0x0038);
1108c2ecf20Sopenharmony_ci	CHECK(u16, 0x0380);
1118c2ecf20Sopenharmony_ci	CHECK(u16, 0x3800);
1128c2ecf20Sopenharmony_ci	CHECK(u16, 0x8000);
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	CHECK(u32, 0x80000000);
1158c2ecf20Sopenharmony_ci	CHECK(u32, 0x7f000000);
1168c2ecf20Sopenharmony_ci	CHECK(u32, 0x07e00000);
1178c2ecf20Sopenharmony_ci	CHECK(u32, 0x00018000);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	CHECK(u64, 0x8000000000000000ull);
1208c2ecf20Sopenharmony_ci	CHECK(u64, 0x7f00000000000000ull);
1218c2ecf20Sopenharmony_ci	CHECK(u64, 0x0001800000000000ull);
1228c2ecf20Sopenharmony_ci	CHECK(u64, 0x0000000080000000ull);
1238c2ecf20Sopenharmony_ci	CHECK(u64, 0x000000007f000000ull);
1248c2ecf20Sopenharmony_ci	CHECK(u64, 0x0000000018000000ull);
1258c2ecf20Sopenharmony_ci	CHECK(u64, 0x0000001f8000000ull);
1268c2ecf20Sopenharmony_ci}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci#ifdef TEST_BITFIELD_COMPILE
1298c2ecf20Sopenharmony_cistatic void __init test_bitfields_compile(struct kunit *context)
1308c2ecf20Sopenharmony_ci{
1318c2ecf20Sopenharmony_ci	/* these should fail compilation */
1328c2ecf20Sopenharmony_ci	CHECK_ENC_GET(16, 16, 0x0f00, 0x1000);
1338c2ecf20Sopenharmony_ci	u32_encode_bits(7, 0x06000000);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	/* this should at least give a warning */
1368c2ecf20Sopenharmony_ci	u16_encode_bits(0, 0x60000);
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci#endif
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cistatic struct kunit_case __refdata bitfields_test_cases[] = {
1418c2ecf20Sopenharmony_ci	KUNIT_CASE(test_bitfields_constants),
1428c2ecf20Sopenharmony_ci	KUNIT_CASE(test_bitfields_variables),
1438c2ecf20Sopenharmony_ci	{}
1448c2ecf20Sopenharmony_ci};
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistatic struct kunit_suite bitfields_test_suite = {
1478c2ecf20Sopenharmony_ci	.name = "bitfields",
1488c2ecf20Sopenharmony_ci	.test_cases = bitfields_test_cases,
1498c2ecf20Sopenharmony_ci};
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_cikunit_test_suites(&bitfields_test_suite);
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ciMODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
1548c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
155