linux/lib/test_hash.c

// SPDX-License-Identifier: GPL-2.0-only
/*
 * Test cases for <linux/hash.h> and <linux/stringhash.h>
 * This just verifies that various ways of computing a hash
 * produce the same thing and, for cases where a k-bit hash
 * value is requested, is of the requested size.
 *
 * We fill a buffer with a 255-byte null-terminated string,
 * and use both full_name_hash() and hashlen_string() to hash the
 * substrings from i to j, where 0 <= i < j < 256.
 *
 * The returned values are used to check that __hash_32() and
 * __hash_32_generic() compute the same thing.  Likewise hash_32()
 * and hash_64().
 */

#include <linux/compiler.h>
#include <linux/types.h>
#include <linux/module.h>
#include <linux/hash.h>
#include <linux/stringhash.h>
#include <kunit/test.h>

/* 32-bit XORSHIFT generator.  Seed must not be zero. */
static u32 __attribute_const__
xorshift(u32 seed)
{}

/* Given a non-zero x, returns a non-zero byte. */
static u8 __attribute_const__
mod255(u32 x)
{}

/* Fill the buffer with non-zero bytes. */
static void fill_buf(char *buf, size_t len, u32 seed)
{}

/* Holds most testing variables for the int test. */
struct test_hash_params {};

#ifdef HAVE_ARCH__HASH_32
static void
test_int__hash_32(struct kunit *test, struct test_hash_params *params)
{
	params->hash_or[1][0] |= params->h2 = __hash_32_generic(params->h0);
#if HAVE_ARCH__HASH_32 == 1
	KUNIT_EXPECT_EQ_MSG(test, params->h1, params->h2,
			    "__hash_32(%#x) = %#x != __hash_32_generic() = %#x",
			    params->h0, params->h1, params->h2);
#endif
}
#endif

#ifdef HAVE_ARCH_HASH_64
static void
test_int_hash_64(struct kunit *test, struct test_hash_params *params, u32 const *m, int *k)
{
	params->h2 = hash_64_generic(*params->h64, *k);
#if HAVE_ARCH_HASH_64 == 1
	KUNIT_EXPECT_EQ_MSG(test, params->h1, params->h2,
			    "hash_64(%#llx, %d) = %#x != hash_64_generic() = %#x",
			    *params->h64, *k, params->h1, params->h2);
#else
	KUNIT_EXPECT_LE_MSG(test, params->h1, params->h2,
			    "hash_64_generic(%#llx, %d) = %#x > %#x",
			    *params->h64, *k, params->h1, *m);
#endif
}
#endif

/*
 * Test the various integer hash functions.  h64 (or its low-order bits)
 * is the integer to hash.  hash_or accumulates the OR of the hash values,
 * which are later checked to see that they cover all the requested bits.
 *
 * Because these functions (as opposed to the string hashes) are all
 * inline, the code being tested is actually in the module, and you can
 * recompile and re-test the module without rebooting.
 */
static void
test_int_hash(struct kunit *test, unsigned long long h64, u32 hash_or[2][33])
{}

#define SIZE

static void test_string_or(struct kunit *test)
{}

static void test_hash_or(struct kunit *test)
{}

static struct kunit_case hash_test_cases[] __refdata =;

static struct kunit_suite hash_test_suite =;


kunit_test_suite();

MODULE_DESCRIPTION();
MODULE_LICENSE();