lib/tests/hashtable_test.c
Source file repositories/reference/linux-study-clean/lib/tests/hashtable_test.c
File Facts
- System
- Linux kernel
- Corpus path
lib/tests/hashtable_test.c- Extension
.c- Size
- 8286 bytes
- Lines
- 319
- Domain
- Kernel Services
- Bucket
- lib
- Inferred role
- Kernel Services: implementation source
- Status
- source implementation candidate
Why This File Exists
Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
kunit/test.hlinux/hashtable.h
Detected Declarations
struct hashtable_test_entryfunction hashtable_test_hash_initfunction hashtable_test_hash_emptyfunction hashtable_test_hash_hashedfunction hashtable_test_hash_addfunction hash_for_eachfunction hashtable_test_hash_delfunction hashtable_test_hash_for_eachfunction hashtable_test_hash_for_each_safefunction hashtable_test_hash_for_each_possiblefunction hashtable_test_hash_for_each_possible_safe
Annotated Snippet
struct hashtable_test_entry {
int key;
int data;
struct hlist_node node;
int visited;
};
static void hashtable_test_hash_init(struct kunit *test)
{
/* Test the different ways of initialising a hashtable. */
DEFINE_HASHTABLE(hash1, 2);
DECLARE_HASHTABLE(hash2, 3);
/* When using DECLARE_HASHTABLE, must use hash_init to
* initialize the hashtable.
*/
hash_init(hash2);
KUNIT_EXPECT_TRUE(test, hash_empty(hash1));
KUNIT_EXPECT_TRUE(test, hash_empty(hash2));
}
static void hashtable_test_hash_empty(struct kunit *test)
{
struct hashtable_test_entry a;
DEFINE_HASHTABLE(hash, 1);
KUNIT_EXPECT_TRUE(test, hash_empty(hash));
a.key = 1;
a.data = 13;
hash_add(hash, &a.node, a.key);
/* Hashtable should no longer be empty. */
KUNIT_EXPECT_FALSE(test, hash_empty(hash));
}
static void hashtable_test_hash_hashed(struct kunit *test)
{
struct hashtable_test_entry a, b;
DEFINE_HASHTABLE(hash, 4);
a.key = 1;
a.data = 13;
hash_add(hash, &a.node, a.key);
b.key = 1;
b.data = 2;
hash_add(hash, &b.node, b.key);
KUNIT_EXPECT_TRUE(test, hash_hashed(&a.node));
KUNIT_EXPECT_TRUE(test, hash_hashed(&b.node));
}
static void hashtable_test_hash_add(struct kunit *test)
{
struct hashtable_test_entry a, b, *x;
int bkt;
DEFINE_HASHTABLE(hash, 3);
a.key = 1;
a.data = 13;
a.visited = 0;
hash_add(hash, &a.node, a.key);
b.key = 2;
b.data = 10;
b.visited = 0;
hash_add(hash, &b.node, b.key);
hash_for_each(hash, bkt, x, node) {
x->visited++;
if (x->key == a.key)
KUNIT_EXPECT_EQ(test, x->data, 13);
else if (x->key == b.key)
KUNIT_EXPECT_EQ(test, x->data, 10);
else
KUNIT_FAIL(test, "Unexpected key in hashtable.");
}
/* Both entries should have been visited exactly once. */
KUNIT_EXPECT_EQ(test, a.visited, 1);
KUNIT_EXPECT_EQ(test, b.visited, 1);
}
static void hashtable_test_hash_del(struct kunit *test)
{
struct hashtable_test_entry a, b, *x;
DEFINE_HASHTABLE(hash, 6);
a.key = 1;
a.data = 13;
Annotation
- Immediate include surface: `kunit/test.h`, `linux/hashtable.h`.
- Detected declarations: `struct hashtable_test_entry`, `function hashtable_test_hash_init`, `function hashtable_test_hash_empty`, `function hashtable_test_hash_hashed`, `function hashtable_test_hash_add`, `function hash_for_each`, `function hashtable_test_hash_del`, `function hashtable_test_hash_for_each`, `function hashtable_test_hash_for_each_safe`, `function hashtable_test_hash_for_each_possible`.
- Atlas domain: Kernel Services / lib.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.