Blame view
lib/bitfield_kunit.c
4.41 KB
0e2dc70e3 bitfield: add tests |
1 2 3 4 5 6 |
// SPDX-License-Identifier: GPL-2.0+ /* * Test cases for bitfield helpers. */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
d2585f516 lib: kunit: add b... |
7 |
#include <kunit/test.h> |
0e2dc70e3 bitfield: add tests |
8 9 10 11 12 13 14 |
#include <linux/bitfield.h> #define CHECK_ENC_GET_U(tp, v, field, res) do { \ { \ u##tp _res; \ \ _res = u##tp##_encode_bits(v, field); \ |
d2585f516 lib: kunit: add b... |
15 16 17 18 19 20 |
KUNIT_ASSERT_FALSE_MSG(context, _res != res, \ "u" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != " #res " ", \ (u64)_res); \ KUNIT_ASSERT_FALSE(context, \ u##tp##_get_bits(_res, field) != v); \ |
0e2dc70e3 bitfield: add tests |
21 22 23 24 25 26 27 28 |
} \ } while (0) #define CHECK_ENC_GET_LE(tp, v, field, res) do { \ { \ __le##tp _res; \ \ _res = le##tp##_encode_bits(v, field); \ |
d2585f516 lib: kunit: add b... |
29 30 31 32 33 34 35 |
KUNIT_ASSERT_FALSE_MSG(context, \ _res != cpu_to_le##tp(res), \ "le" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != 0x%llx",\ (u64)le##tp##_to_cpu(_res), \ (u64)(res)); \ KUNIT_ASSERT_FALSE(context, \ le##tp##_get_bits(_res, field) != v);\ |
0e2dc70e3 bitfield: add tests |
36 37 38 39 40 41 42 43 |
} \ } while (0) #define CHECK_ENC_GET_BE(tp, v, field, res) do { \ { \ __be##tp _res; \ \ _res = be##tp##_encode_bits(v, field); \ |
d2585f516 lib: kunit: add b... |
44 45 46 47 48 49 50 |
KUNIT_ASSERT_FALSE_MSG(context, \ _res != cpu_to_be##tp(res), \ "be" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != 0x%llx", \ (u64)be##tp##_to_cpu(_res), \ (u64)(res)); \ KUNIT_ASSERT_FALSE(context, \ be##tp##_get_bits(_res, field) != v);\ |
0e2dc70e3 bitfield: add tests |
51 52 53 54 55 56 57 58 |
} \ } while (0) #define CHECK_ENC_GET(tp, v, field, res) do { \ CHECK_ENC_GET_U(tp, v, field, res); \ CHECK_ENC_GET_LE(tp, v, field, res); \ CHECK_ENC_GET_BE(tp, v, field, res); \ } while (0) |
d2585f516 lib: kunit: add b... |
59 |
static void __init test_bitfields_constants(struct kunit *context) |
0e2dc70e3 bitfield: add tests |
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
{ /* * NOTE * This whole function compiles (or at least should, if everything * is going according to plan) to nothing after optimisation. */ CHECK_ENC_GET(16, 1, 0x000f, 0x0001); CHECK_ENC_GET(16, 3, 0x00f0, 0x0030); CHECK_ENC_GET(16, 5, 0x0f00, 0x0500); CHECK_ENC_GET(16, 7, 0xf000, 0x7000); CHECK_ENC_GET(16, 14, 0x000f, 0x000e); CHECK_ENC_GET(16, 15, 0x00f0, 0x00f0); CHECK_ENC_GET_U(8, 1, 0x0f, 0x01); CHECK_ENC_GET_U(8, 3, 0xf0, 0x30); CHECK_ENC_GET_U(8, 14, 0x0f, 0x0e); CHECK_ENC_GET_U(8, 15, 0xf0, 0xf0); CHECK_ENC_GET(32, 1, 0x00000f00, 0x00000100); CHECK_ENC_GET(32, 3, 0x0000f000, 0x00003000); CHECK_ENC_GET(32, 5, 0x000f0000, 0x00050000); CHECK_ENC_GET(32, 7, 0x00f00000, 0x00700000); CHECK_ENC_GET(32, 14, 0x0f000000, 0x0e000000); CHECK_ENC_GET(32, 15, 0xf0000000, 0xf0000000); CHECK_ENC_GET(64, 1, 0x00000f0000000000ull, 0x0000010000000000ull); CHECK_ENC_GET(64, 3, 0x0000f00000000000ull, 0x0000300000000000ull); CHECK_ENC_GET(64, 5, 0x000f000000000000ull, 0x0005000000000000ull); CHECK_ENC_GET(64, 7, 0x00f0000000000000ull, 0x0070000000000000ull); CHECK_ENC_GET(64, 14, 0x0f00000000000000ull, 0x0e00000000000000ull); CHECK_ENC_GET(64, 15, 0xf000000000000000ull, 0xf000000000000000ull); |
0e2dc70e3 bitfield: add tests |
92 93 94 95 96 97 |
} #define CHECK(tp, mask) do { \ u64 v; \ \ for (v = 0; v < 1 << hweight32(mask); v++) \ |
d2585f516 lib: kunit: add b... |
98 99 |
KUNIT_ASSERT_FALSE(context, \ tp##_encode_bits(v, mask) != v << __ffs64(mask));\ |
0e2dc70e3 bitfield: add tests |
100 |
} while (0) |
d2585f516 lib: kunit: add b... |
101 |
static void __init test_bitfields_variables(struct kunit *context) |
0e2dc70e3 bitfield: add tests |
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
{ CHECK(u8, 0x0f); CHECK(u8, 0xf0); CHECK(u8, 0x38); CHECK(u16, 0x0038); CHECK(u16, 0x0380); CHECK(u16, 0x3800); CHECK(u16, 0x8000); CHECK(u32, 0x80000000); CHECK(u32, 0x7f000000); CHECK(u32, 0x07e00000); CHECK(u32, 0x00018000); CHECK(u64, 0x8000000000000000ull); CHECK(u64, 0x7f00000000000000ull); CHECK(u64, 0x0001800000000000ull); CHECK(u64, 0x0000000080000000ull); CHECK(u64, 0x000000007f000000ull); CHECK(u64, 0x0000000018000000ull); CHECK(u64, 0x0000001f8000000ull); |
0e2dc70e3 bitfield: add tests |
124 |
} |
294a7f161 lib: kunit: Fix c... |
125 |
#ifdef TEST_BITFIELD_COMPILE |
d2585f516 lib: kunit: add b... |
126 127 |
static void __init test_bitfields_compile(struct kunit *context) { |
0e2dc70e3 bitfield: add tests |
128 129 130 131 132 133 |
/* these should fail compilation */ CHECK_ENC_GET(16, 16, 0x0f00, 0x1000); u32_encode_bits(7, 0x06000000); /* this should at least give a warning */ u16_encode_bits(0, 0x60000); |
d2585f516 lib: kunit: add b... |
134 |
} |
294a7f161 lib: kunit: Fix c... |
135 |
#endif |
d2585f516 lib: kunit: add b... |
136 137 138 139 |
static struct kunit_case __refdata bitfields_test_cases[] = { KUNIT_CASE(test_bitfields_constants), KUNIT_CASE(test_bitfields_variables), |
d2585f516 lib: kunit: add b... |
140 141 |
{} }; |
0e2dc70e3 bitfield: add tests |
142 |
|
d2585f516 lib: kunit: add b... |
143 144 145 146 |
static struct kunit_suite bitfields_test_suite = { .name = "bitfields", .test_cases = bitfields_test_cases, }; |
0e2dc70e3 bitfield: add tests |
147 |
|
d2585f516 lib: kunit: add b... |
148 |
kunit_test_suites(&bitfields_test_suite); |
0e2dc70e3 bitfield: add tests |
149 150 151 |
MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>"); MODULE_LICENSE("GPL"); |