xref: /linux/lib/test_hash.c (revision 0acc968f)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2468a9428SGeorge Spelvin /*
3468a9428SGeorge Spelvin  * Test cases for <linux/hash.h> and <linux/stringhash.h>
4468a9428SGeorge Spelvin  * This just verifies that various ways of computing a hash
5468a9428SGeorge Spelvin  * produce the same thing and, for cases where a k-bit hash
6468a9428SGeorge Spelvin  * value is requested, is of the requested size.
7468a9428SGeorge Spelvin  *
8468a9428SGeorge Spelvin  * We fill a buffer with a 255-byte null-terminated string,
9468a9428SGeorge Spelvin  * and use both full_name_hash() and hashlen_string() to hash the
10468a9428SGeorge Spelvin  * substrings from i to j, where 0 <= i < j < 256.
11468a9428SGeorge Spelvin  *
12468a9428SGeorge Spelvin  * The returned values are used to check that __hash_32() and
13468a9428SGeorge Spelvin  * __hash_32_generic() compute the same thing.  Likewise hash_32()
14468a9428SGeorge Spelvin  * and hash_64().
15468a9428SGeorge Spelvin  */
16468a9428SGeorge Spelvin 
17468a9428SGeorge Spelvin #include <linux/compiler.h>
18468a9428SGeorge Spelvin #include <linux/types.h>
19468a9428SGeorge Spelvin #include <linux/module.h>
20468a9428SGeorge Spelvin #include <linux/hash.h>
21468a9428SGeorge Spelvin #include <linux/stringhash.h>
22*0acc968fSIsabella Basso #include <kunit/test.h>
23468a9428SGeorge Spelvin 
24468a9428SGeorge Spelvin /* 32-bit XORSHIFT generator.  Seed must not be zero. */
25*0acc968fSIsabella Basso static u32 __attribute_const__
xorshift(u32 seed)26468a9428SGeorge Spelvin xorshift(u32 seed)
27468a9428SGeorge Spelvin {
28468a9428SGeorge Spelvin 	seed ^= seed << 13;
29468a9428SGeorge Spelvin 	seed ^= seed >> 17;
30468a9428SGeorge Spelvin 	seed ^= seed << 5;
31468a9428SGeorge Spelvin 	return seed;
32468a9428SGeorge Spelvin }
33468a9428SGeorge Spelvin 
34468a9428SGeorge Spelvin /* Given a non-zero x, returns a non-zero byte. */
35*0acc968fSIsabella Basso static u8 __attribute_const__
mod255(u32 x)36468a9428SGeorge Spelvin mod255(u32 x)
37468a9428SGeorge Spelvin {
38468a9428SGeorge Spelvin 	x = (x & 0xffff) + (x >> 16);	/* 1 <= x <= 0x1fffe */
39468a9428SGeorge Spelvin 	x = (x & 0xff) + (x >> 8);	/* 1 <= x <= 0x2fd */
40468a9428SGeorge Spelvin 	x = (x & 0xff) + (x >> 8);	/* 1 <= x <= 0x100 */
41468a9428SGeorge Spelvin 	x = (x & 0xff) + (x >> 8);	/* 1 <= x <= 0xff */
42468a9428SGeorge Spelvin 	return x;
43468a9428SGeorge Spelvin }
44468a9428SGeorge Spelvin 
45468a9428SGeorge Spelvin /* Fill the buffer with non-zero bytes. */
fill_buf(char * buf,size_t len,u32 seed)46*0acc968fSIsabella Basso static void fill_buf(char *buf, size_t len, u32 seed)
47468a9428SGeorge Spelvin {
48468a9428SGeorge Spelvin 	size_t i;
49468a9428SGeorge Spelvin 
50468a9428SGeorge Spelvin 	for (i = 0; i < len; i++) {
51468a9428SGeorge Spelvin 		seed = xorshift(seed);
52468a9428SGeorge Spelvin 		buf[i] = mod255(seed);
53468a9428SGeorge Spelvin 	}
54468a9428SGeorge Spelvin }
55468a9428SGeorge Spelvin 
56ae788067SIsabella Basso /* Holds most testing variables for the int test. */
57ae788067SIsabella Basso struct test_hash_params {
58ae788067SIsabella Basso         /* Pointer to integer to be hashed. */
59ae788067SIsabella Basso 	unsigned long long *h64;
60ae788067SIsabella Basso         /* Low 32-bits of integer to be hashed. */
61ae788067SIsabella Basso 	u32 h0;
62ae788067SIsabella Basso         /* Arch-specific hash result. */
63ae788067SIsabella Basso 	u32 h1;
64ae788067SIsabella Basso         /* Generic hash result. */
65ae788067SIsabella Basso 	u32 h2;
66ae788067SIsabella Basso         /* ORed hashes of given size (in bits). */
67ae788067SIsabella Basso 	u32 (*hash_or)[33];
68ae788067SIsabella Basso };
69ae788067SIsabella Basso 
70ae788067SIsabella Basso #ifdef HAVE_ARCH__HASH_32
71*0acc968fSIsabella Basso static void
test_int__hash_32(struct kunit * test,struct test_hash_params * params)72*0acc968fSIsabella Basso test_int__hash_32(struct kunit *test, struct test_hash_params *params)
73ae788067SIsabella Basso {
74ae788067SIsabella Basso 	params->hash_or[1][0] |= params->h2 = __hash_32_generic(params->h0);
75ae788067SIsabella Basso #if HAVE_ARCH__HASH_32 == 1
76*0acc968fSIsabella Basso 	KUNIT_EXPECT_EQ_MSG(test, params->h1, params->h2,
77*0acc968fSIsabella Basso 			    "__hash_32(%#x) = %#x != __hash_32_generic() = %#x",
78ae788067SIsabella Basso 			    params->h0, params->h1, params->h2);
79ae788067SIsabella Basso #endif
80ae788067SIsabella Basso }
81ae788067SIsabella Basso #endif
82ae788067SIsabella Basso 
83ae788067SIsabella Basso #ifdef HAVE_ARCH_HASH_64
84*0acc968fSIsabella Basso static void
test_int_hash_64(struct kunit * test,struct test_hash_params * params,u32 const * m,int * k)85*0acc968fSIsabella Basso test_int_hash_64(struct kunit *test, struct test_hash_params *params, u32 const *m, int *k)
86ae788067SIsabella Basso {
87ae788067SIsabella Basso 	params->h2 = hash_64_generic(*params->h64, *k);
88ae788067SIsabella Basso #if HAVE_ARCH_HASH_64 == 1
89*0acc968fSIsabella Basso 	KUNIT_EXPECT_EQ_MSG(test, params->h1, params->h2,
90*0acc968fSIsabella Basso 			    "hash_64(%#llx, %d) = %#x != hash_64_generic() = %#x",
91ae788067SIsabella Basso 			    *params->h64, *k, params->h1, params->h2);
92ae788067SIsabella Basso #else
93*0acc968fSIsabella Basso 	KUNIT_EXPECT_LE_MSG(test, params->h1, params->h2,
94*0acc968fSIsabella Basso 			    "hash_64_generic(%#llx, %d) = %#x > %#x",
95ae788067SIsabella Basso 			    *params->h64, *k, params->h1, *m);
96ae788067SIsabella Basso #endif
97ae788067SIsabella Basso }
98ae788067SIsabella Basso #endif
99ae788067SIsabella Basso 
100468a9428SGeorge Spelvin /*
101468a9428SGeorge Spelvin  * Test the various integer hash functions.  h64 (or its low-order bits)
102468a9428SGeorge Spelvin  * is the integer to hash.  hash_or accumulates the OR of the hash values,
103468a9428SGeorge Spelvin  * which are later checked to see that they cover all the requested bits.
104468a9428SGeorge Spelvin  *
105468a9428SGeorge Spelvin  * Because these functions (as opposed to the string hashes) are all
106468a9428SGeorge Spelvin  * inline, the code being tested is actually in the module, and you can
107468a9428SGeorge Spelvin  * recompile and re-test the module without rebooting.
108468a9428SGeorge Spelvin  */
109*0acc968fSIsabella Basso static void
test_int_hash(struct kunit * test,unsigned long long h64,u32 hash_or[2][33])110*0acc968fSIsabella Basso test_int_hash(struct kunit *test, unsigned long long h64, u32 hash_or[2][33])
111468a9428SGeorge Spelvin {
112468a9428SGeorge Spelvin 	int k;
113ae788067SIsabella Basso 	struct test_hash_params params = { &h64, (u32)h64, 0, 0, hash_or };
114468a9428SGeorge Spelvin 
115468a9428SGeorge Spelvin 	/* Test __hash32 */
116ae788067SIsabella Basso 	hash_or[0][0] |= params.h1 = __hash_32(params.h0);
117468a9428SGeorge Spelvin #ifdef HAVE_ARCH__HASH_32
118*0acc968fSIsabella Basso 	test_int__hash_32(test, &params);
119468a9428SGeorge Spelvin #endif
120468a9428SGeorge Spelvin 
121468a9428SGeorge Spelvin 	/* Test k = 1..32 bits */
122468a9428SGeorge Spelvin 	for (k = 1; k <= 32; k++) {
123468a9428SGeorge Spelvin 		u32 const m = ((u32)2 << (k-1)) - 1;	/* Low k bits set */
124468a9428SGeorge Spelvin 
125468a9428SGeorge Spelvin 		/* Test hash_32 */
126ae788067SIsabella Basso 		hash_or[0][k] |= params.h1 = hash_32(params.h0, k);
127*0acc968fSIsabella Basso 		KUNIT_EXPECT_LE_MSG(test, params.h1, m,
128*0acc968fSIsabella Basso 				    "hash_32(%#x, %d) = %#x > %#x",
129*0acc968fSIsabella Basso 				    params.h0, k, params.h1, m);
130fd0a1462SIsabella Basso 
131468a9428SGeorge Spelvin 		/* Test hash_64 */
132ae788067SIsabella Basso 		hash_or[1][k] |= params.h1 = hash_64(h64, k);
133*0acc968fSIsabella Basso 		KUNIT_EXPECT_LE_MSG(test, params.h1, m,
134*0acc968fSIsabella Basso 				    "hash_64(%#llx, %d) = %#x > %#x",
135*0acc968fSIsabella Basso 				    h64, k, params.h1, m);
136468a9428SGeorge Spelvin #ifdef HAVE_ARCH_HASH_64
137*0acc968fSIsabella Basso 		test_int_hash_64(test, &params, &m, &k);
138468a9428SGeorge Spelvin #endif
139468a9428SGeorge Spelvin 	}
140468a9428SGeorge Spelvin }
141468a9428SGeorge Spelvin 
142468a9428SGeorge Spelvin #define SIZE 256	/* Run time is cubic in SIZE */
143468a9428SGeorge Spelvin 
test_string_or(struct kunit * test)144*0acc968fSIsabella Basso static void test_string_or(struct kunit *test)
145468a9428SGeorge Spelvin {
146468a9428SGeorge Spelvin 	char buf[SIZE+1];
1475427d3d7SIsabella Basso 	u32 string_or = 0;
1485427d3d7SIsabella Basso 	int i, j;
1495427d3d7SIsabella Basso 
1505427d3d7SIsabella Basso 	fill_buf(buf, SIZE, 1);
1515427d3d7SIsabella Basso 
1525427d3d7SIsabella Basso 	/* Test every possible non-empty substring in the buffer. */
1535427d3d7SIsabella Basso 	for (j = SIZE; j > 0; --j) {
1545427d3d7SIsabella Basso 		buf[j] = '\0';
1555427d3d7SIsabella Basso 
1565427d3d7SIsabella Basso 		for (i = 0; i <= j; i++) {
1575427d3d7SIsabella Basso 			u32 h0 = full_name_hash(buf+i, buf+i, j-i);
1585427d3d7SIsabella Basso 
1595427d3d7SIsabella Basso 			string_or |= h0;
1605427d3d7SIsabella Basso 		} /* i */
1615427d3d7SIsabella Basso 	} /* j */
1625427d3d7SIsabella Basso 
1635427d3d7SIsabella Basso 	/* The OR of all the hash values should cover all the bits */
164*0acc968fSIsabella Basso 	KUNIT_EXPECT_EQ_MSG(test, string_or, -1u,
165*0acc968fSIsabella Basso 			    "OR of all string hash results = %#x != %#x",
1665427d3d7SIsabella Basso 			    string_or, -1u);
1675427d3d7SIsabella Basso }
1685427d3d7SIsabella Basso 
test_hash_or(struct kunit * test)169*0acc968fSIsabella Basso static void test_hash_or(struct kunit *test)
1705427d3d7SIsabella Basso {
1715427d3d7SIsabella Basso 	char buf[SIZE+1];
1725427d3d7SIsabella Basso 	u32 hash_or[2][33] = { { 0, } };
173468a9428SGeorge Spelvin 	unsigned long long h64 = 0;
174468a9428SGeorge Spelvin 	int i, j;
175468a9428SGeorge Spelvin 
176468a9428SGeorge Spelvin 	fill_buf(buf, SIZE, 1);
177468a9428SGeorge Spelvin 
178468a9428SGeorge Spelvin 	/* Test every possible non-empty substring in the buffer. */
179468a9428SGeorge Spelvin 	for (j = SIZE; j > 0; --j) {
180468a9428SGeorge Spelvin 		buf[j] = '\0';
181468a9428SGeorge Spelvin 
182468a9428SGeorge Spelvin 		for (i = 0; i <= j; i++) {
1838387ff25SLinus Torvalds 			u64 hashlen = hashlen_string(buf+i, buf+i);
1848387ff25SLinus Torvalds 			u32 h0 = full_name_hash(buf+i, buf+i, j-i);
185468a9428SGeorge Spelvin 
186468a9428SGeorge Spelvin 			/* Check that hashlen_string gets the length right */
187*0acc968fSIsabella Basso 			KUNIT_EXPECT_EQ_MSG(test, hashlen_len(hashlen), j-i,
188*0acc968fSIsabella Basso 					    "hashlen_string(%d..%d) returned length %u, expected %d",
189468a9428SGeorge Spelvin 					    i, j, hashlen_len(hashlen), j-i);
190468a9428SGeorge Spelvin 			/* Check that the hashes match */
191*0acc968fSIsabella Basso 			KUNIT_EXPECT_EQ_MSG(test, hashlen_hash(hashlen), h0,
192*0acc968fSIsabella Basso 					    "hashlen_string(%d..%d) = %08x != full_name_hash() = %08x",
193468a9428SGeorge Spelvin 					    i, j, hashlen_hash(hashlen), h0);
194468a9428SGeorge Spelvin 
195468a9428SGeorge Spelvin 			h64 = h64 << 32 | h0;	/* For use with hash_64 */
196*0acc968fSIsabella Basso 			test_int_hash(test, h64, hash_or);
197468a9428SGeorge Spelvin 		} /* i */
198468a9428SGeorge Spelvin 	} /* j */
199468a9428SGeorge Spelvin 
200*0acc968fSIsabella Basso 	KUNIT_EXPECT_EQ_MSG(test, hash_or[0][0], -1u,
201*0acc968fSIsabella Basso 			    "OR of all __hash_32 results = %#x != %#x",
202468a9428SGeorge Spelvin 			    hash_or[0][0], -1u);
203468a9428SGeorge Spelvin #ifdef HAVE_ARCH__HASH_32
204468a9428SGeorge Spelvin #if HAVE_ARCH__HASH_32 != 1	/* Test is pointless if results match */
205*0acc968fSIsabella Basso 	KUNIT_EXPECT_EQ_MSG(test, hash_or[1][0], -1u,
206*0acc968fSIsabella Basso 			    "OR of all __hash_32_generic results = %#x != %#x",
207468a9428SGeorge Spelvin 			    hash_or[1][0], -1u);
208468a9428SGeorge Spelvin #endif
209468a9428SGeorge Spelvin #endif
210468a9428SGeorge Spelvin 
211468a9428SGeorge Spelvin 	/* Likewise for all the i-bit hash values */
212468a9428SGeorge Spelvin 	for (i = 1; i <= 32; i++) {
213468a9428SGeorge Spelvin 		u32 const m = ((u32)2 << (i-1)) - 1;	/* Low i bits set */
214468a9428SGeorge Spelvin 
215*0acc968fSIsabella Basso 		KUNIT_EXPECT_EQ_MSG(test, hash_or[0][i], m,
216*0acc968fSIsabella Basso 				    "OR of all hash_32(%d) results = %#x (%#x expected)",
217*0acc968fSIsabella Basso 				    i, hash_or[0][i], m);
218*0acc968fSIsabella Basso 		KUNIT_EXPECT_EQ_MSG(test, hash_or[1][i], m,
219*0acc968fSIsabella Basso 				    "OR of all hash_64(%d) results = %#x (%#x expected)",
220*0acc968fSIsabella Basso 				    i, hash_or[1][i], m);
221468a9428SGeorge Spelvin 	}
222468a9428SGeorge Spelvin }
223468a9428SGeorge Spelvin 
224*0acc968fSIsabella Basso static struct kunit_case hash_test_cases[] __refdata = {
225*0acc968fSIsabella Basso 	KUNIT_CASE(test_string_or),
226*0acc968fSIsabella Basso 	KUNIT_CASE(test_hash_or),
227*0acc968fSIsabella Basso 	{}
228*0acc968fSIsabella Basso };
2295427d3d7SIsabella Basso 
230*0acc968fSIsabella Basso static struct kunit_suite hash_test_suite = {
231*0acc968fSIsabella Basso 	.name = "hash",
232*0acc968fSIsabella Basso 	.test_cases = hash_test_cases,
233*0acc968fSIsabella Basso };
2345427d3d7SIsabella Basso 
235468a9428SGeorge Spelvin 
236*0acc968fSIsabella Basso kunit_test_suite(hash_test_suite);
237468a9428SGeorge Spelvin 
238468a9428SGeorge Spelvin MODULE_LICENSE("GPL");
239