开始设计
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
#include "c_HashMap.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct {
|
||||
char username[32];
|
||||
} UserKey_t;
|
||||
|
||||
typedef struct {
|
||||
int user_uid;
|
||||
int permissions_mask;
|
||||
} ProfileValue_t;
|
||||
|
||||
// Standard high-distribution DJB2 Hash function implementation
|
||||
uint32_t hash_djb2(const void* key, int key_size) {
|
||||
const char* str = ((UserKey_t*)key)->username;
|
||||
uint32_t hash = 5381;
|
||||
int c;
|
||||
while ((c = (unsigned char)*str++)) {
|
||||
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
int compare_user_keys(const void* a, const void* b, int key_size) {
|
||||
return strcmp(((UserKey_t*)a)->username, ((UserKey_t*)b)->username);
|
||||
}
|
||||
|
||||
void test_log(const char* name) {
|
||||
printf("[PASS] %s\n", name);
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
printf("==================================================\n");
|
||||
printf(" Starting Generic Hash Map Unit Testing Suite\n");
|
||||
printf("==================================================\n\n");
|
||||
|
||||
c_HashMap_t map;
|
||||
// Set a small capacity of 4 to test scaling re-hashing logic mid-flight
|
||||
c_err_t err = c_HashMap_Init(&map, sizeof(UserKey_t), sizeof(ProfileValue_t), 4, hash_djb2, compare_user_keys);
|
||||
assert(err == C_ERR_SUCCESS);
|
||||
assert(map.size == 0);
|
||||
test_log("1. Hash Map allocation and callback structures ready");
|
||||
|
||||
UserKey_t k1 = {"admin"}; ProfileValue_t v1 = {9001, 0xFF};
|
||||
UserKey_t k2 = {"developer"}; ProfileValue_t v2 = {9002, 0x0F};
|
||||
UserKey_t k3 = {"guest"}; ProfileValue_t v3 = {9003, 0x01};
|
||||
|
||||
// ==========================================
|
||||
// 2. Testing Insertions & Resizing
|
||||
// ==========================================
|
||||
c_HashMap_Put(&map, &k1, &v1);
|
||||
c_HashMap_Put(&map, &k2, &v2);
|
||||
|
||||
// Inserting 3rd item triggers 3/4 = 0.75 load factor threshold, forcing scale-out to capacity 8
|
||||
err = c_HashMap_Put(&map, &k3, &v3);
|
||||
assert(err == C_ERR_SUCCESS);
|
||||
assert(map.size == 3);
|
||||
assert(map.capacity == 8); // Capacity successfully doubled automatically
|
||||
test_log("2. Mapping assertions and dynamic scale-out rehashing verified");
|
||||
|
||||
// ==========================================
|
||||
// 3. Testing Lookups (Get)
|
||||
// ==========================================
|
||||
ProfileValue_t out_buf;
|
||||
err = c_HashMap_Get(&map, &k2, &out_buf);
|
||||
assert(err == C_ERR_SUCCESS);
|
||||
assert(out_buf.user_uid == 9002);
|
||||
assert(out_buf.permissions_mask == 0x0F);
|
||||
|
||||
// Overwrite test validation
|
||||
ProfileValue_t v1_updated = {9001, 0xAA};
|
||||
c_HashMap_Put(&map, &k1, &v1_updated);
|
||||
c_HashMap_Get(&map, &k1, &out_buf);
|
||||
assert(out_buf.permissions_mask == 0xAA);
|
||||
test_log("3. Mapping value extractions and key overrides verified");
|
||||
|
||||
// ==========================================
|
||||
// 4. Testing Deletions (Remove)
|
||||
// ==========================================
|
||||
assert(c_HashMap_Contains(&map, &k3) == C_TRUE);
|
||||
err = c_HashMap_Remove(&map, &k3);
|
||||
assert(err == C_ERR_SUCCESS);
|
||||
assert(map.size == 2);
|
||||
assert(c_HashMap_Contains(&map, &k3) == C_FALSE);
|
||||
|
||||
// Not found tracking assertions checks
|
||||
assert(c_HashMap_Get(&map, &k3, &out_buf) == C_ERR_NOT_FOUND);
|
||||
test_log("4. Chain unlinking and item deletions verified");
|
||||
|
||||
c_HashMap_Destroy(&map);
|
||||
test_log("5. Resource pool cleanup teardown success");
|
||||
|
||||
printf("\n==================================================\n");
|
||||
printf(" Success! Universal defensive Hash Map behaves correctly!\n");
|
||||
printf("==================================================\n");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user