#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "strint_ht.h"
#define TYPE_DEFINITIONS
#define FUNCTION_DEFINITIONS
#define FUNCTION_LINKAGE static inline
#include "./../../arena/arena_template.h"
static unsigned char buf[4096];
void str_int_ht_test_alt(void)
{
struct arena arena;
if (!ht) {
assert(false);
}
assert(ht->count == 0);
{
strcpy(egg_char_ptr, "egg");
strint_ht_insert(ht, egg_char_ptr, 1);
strint_ht_update(ht, egg_char_ptr, 2);
strcpy(milk_char_ptr, "milk");
strint_ht_update(ht, milk_char_ptr, 3);
assert(ht->count == 2);
assert(strint_ht_contains_key(ht, "milk"));
assert(!strint_ht_contains_key(ht, "chocolate"));
assert(strint_ht_get_value(ht, "egg", -1) == 2);
int *res = strint_ht_get_value_mut(ht, "milk");
if (res == NULL) {
assert(false);
}
assert(*res == 3);
*res = 4;
assert(strint_ht_get_value(ht, "milk", -1) == 4);
assert(strint_ht_delete(ht, "milk"));
assert(!strint_ht_delete(ht, "chocolate"));
assert(!strint_ht_contains_key(ht, "milk"));
assert(strint_ht_search(ht, "milk") == NULL);
assert(ht->count == 1);
strint_ht_clear(ht);
assert(ht->count == 0);
assert(!strint_ht_contains_key(ht, "egg"));
};
}
#include "murmurhash.h"
#define NAME int_to_int_hashtable
#define KEY_TYPE int
#define VALUE_TYPE int
#define KEY_IS_EQUAL(a, b) ((a) == (b))
#define HASH_FUNCTION(key) (murmur3_32((uint8_t *)&(key), sizeof(int), 0))
#define TYPE_DEFINITIONS
#define FUNCTION_DEFINITIONS
#define FUNCTION_LINKAGE static inline
#define LIM ((int)(1e+6))
void int_to_int_hashtable_test(void)
{
struct int_to_int_hashtable *ht =
int_to_int_hashtable_create(LIM * 4 / 3);
if (!ht) {
assert(false);
}
for (int i = 0; i < LIM; i++) {
int_to_int_hashtable_insert(ht, i, LIM - i);
}
for (int i = 0; i < LIM; i++) {
assert(int_to_int_hashtable_get_value(ht, i, -1) == LIM - i);
}
struct int_to_int_hashtable *ht_copy = int_to_int_hashtable_create(ht->capacity);
if (!ht_copy) {
assert(false);
}
int_to_int_hashtable_copy(ht_copy, ht);
int_to_int_hashtable_destroy(ht);
bool table[LIM] = {false};
{
int key;
int value;
(void)(value);
size_t tempi;
{
const bool inside_range = 0 <= key && key < LIM;
const bool already_counted = table[key];
if (!inside_range || already_counted) {
assert(false);
}
table[key] = true;
}
}
for (size_t i = 0; i < LIM; i++) {
assert(table[i] == true);
}
assert(ht_copy->count == (size_t)LIM);
int_to_int_hashtable_clear(ht_copy);
assert(ht_copy->count == 0);
int_to_int_hashtable_destroy(ht_copy);
}
int main(void)
{
str_int_ht_test_alt();
int_to_int_hashtable_test();
return 0;
}
void arena_deallocate(void *self_, void *mem)
Dummy no-op deallocate function.
struct arena_state arena_state_save(struct arena *arena_ptr)
Save the arena state temporarily.
void arena_state_restore(struct arena_state prev_state)
Restore the arena state.
void * arena_allocate(void *self_, const size_t size)
Get the pointer to a chunk of the arena.
void * arena_allocate_aligned(void *self_, const size_t alignment, const size_t size)
Get the pointer to a chunk of the arena. With specific alignment.
void arena_init(void *self_, const size_t len, unsigned char *backing_buf)
Initialize the arena.
Fixed-size open-adressing hashtable (robin hood hashing).
#define FHASHTABLE_FOR_EACH(self, index, key_, value_)
Iterate over the non-empty slots in the hashtable in arbitary order.
Definition fhashtable_template.h:111