Skip to content

Commit

Permalink
Add test case for map API
Browse files Browse the repository at this point in the history
  • Loading branch information
EagleTw committed Jun 4, 2023
1 parent 830d41d commit 517af99
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 12 deletions.
59 changes: 47 additions & 12 deletions mk/tests.mk
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
CACHE_TEST_DIR := tests/cache
CACHE_BUILD_DIR := build/cache
TARGET := test-cache
CACHE_TARGET := test-cache

MAP_TEST_DIR := tests/map
MAP_BUILD_DIR:= build/map
MAP_TARGET := test-map

CACHE_OBJS := \
test-cache.o

MAP_OBJS := \
test-map.o

CACHE_OBJS := $(addprefix $(CACHE_BUILD_DIR)/, $(CACHE_OBJS))
OBJS += $(CACHE_OBJS)
deps += $(CACHE_OBJS:%.o=%.o.d)

# Check adaptive replacement cache policy is enabled or not, default is LRU
MAP_OBJS := $(addprefix $(MAP_BUILD_DIR)/, $(MAP_OBJS))
OBJS += $(MAP_OBJS)
deps += $(MAP_OBJS:%.o=%.o.d)

# Check adaptive replacement cache policy is enabled or not, default is LRU
ifeq ($(ENABLE_ARC), 1)
CACHE_CHECK_ELF_FILES := \
arc/cache-new \
Expand All @@ -18,18 +29,21 @@ CACHE_CHECK_ELF_FILES := \
arc/cache-lru-replace \
arc/cache-lfu-replace \
arc/cache-lru-ghost-replace \
arc/cache-lfu-ghost-replace
arc/cache-lfu-ghost-replace
else
CACHE_CHECK_ELF_FILES := \
lfu/cache-new \
lfu/cache-put \
lfu/cache-get \
lfu/cache-lfu-replace
lfu/cache-lfu-replace
endif

CACHE_OUT = $(addprefix $(CACHE_BUILD_DIR)/, $(CACHE_CHECK_ELF_FILES:%=%.out))
MAP_OUT = $(addprefix $(MAP_BUILD_DIR)/$(MAP_TARGET), ".out")

tests : $(CACHE_OUT)
tests : run_cache_test run_map_test

run_cache_test : $(CACHE_OUT)
$(Q)$(foreach e,$(CACHE_CHECK_ELF_FILES),\
$(PRINTF) "Running $(e) ... "; \
if cmp $(CACHE_TEST_DIR)/$(e).expect $(CACHE_BUILD_DIR)/$(e).out; then \
Expand All @@ -40,16 +54,37 @@ tests : $(CACHE_OUT)
fi; \
)

$(CACHE_OUT): $(TARGET)
run_map_test: $(MAP_OUT)
$(Q)$(PRINTF) "Running test-map ... "; \
if [ "$(shell cat $(MAP_BUILD_DIR)/test-map.out)" == "0" ]; then \
$(call notice, [OK]); \
else \
$(PRINTF) "Failed.\n"; \
fi; \

$(CACHE_OUT): $(CACHE_TARGET)
$(Q)$(foreach e,$(CACHE_CHECK_ELF_FILES),\
$(CACHE_BUILD_DIR)/$(TARGET) $(CACHE_TEST_DIR)/$(e).in > $(CACHE_BUILD_DIR)/$(e).out; \
$(CACHE_BUILD_DIR)/$(CACHE_TARGET) $(CACHE_TEST_DIR)/$(e).in > $(CACHE_BUILD_DIR)/$(e).out; \
)

$(TARGET): $(CACHE_OBJS)
$(CACHE_TARGET): $(CACHE_OBJS)
$(VECHO) " CC\t$@\n"
$(Q)$(CC) $^ build/cache.o build/mpool.o -o $(CACHE_BUILD_DIR)/$(TARGET)
$(CACHE_BUILD_DIR)/%.o: $(CACHE_TEST_DIR)/%.c
$(Q)$(CC) $^ build/cache.o build/mpool.o -o $(CACHE_BUILD_DIR)/$(CACHE_TARGET)

$(CACHE_BUILD_DIR)/%.o: $(CACHE_TEST_DIR)/%.c
$(VECHO) " CC\t$@\n"
$(Q)mkdir -p $(dir $@)/arc $(dir $@)/lfu
$(Q)$(CC) -o $@ $(CFLAGS) -I./src -c -MMD -MF $@.d $<
$(Q)$(CC) -o $@ $(CFLAGS) -I./src -c -MMD -MF $@.d $<

$(MAP_OUT): $(MAP_TARGET)
$(VECHO) " CC\t$@\n"
$(Q)$(MAP_BUILD_DIR)/$(MAP_TARGET) > $(MAP_BUILD_DIR)/test-map.out

$(MAP_TARGET): $(MAP_OBJS)
$(VECHO) " CC\t$@\n"
$(Q)$(CC) $^ build/map.o -o $(MAP_BUILD_DIR)/$(MAP_TARGET)

$(MAP_BUILD_DIR)/%.o: $(MAP_TEST_DIR)/%.c
$(VECHO) " CC\t$@\n"
$(Q)mkdir -p $(dir $@)
$(Q)$(CC) -o $@ $(CFLAGS) -I./src -c -MMD -MF $@.d $<
101 changes: 101 additions & 0 deletions tests/map/test-map.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "map.h"

static void swap(int *x, int *y)
{
int tmp = *x;
*x = *y;
*y = tmp;
}

enum { N_NODES = 10000 };

bool test_mix_insert()
{
bool failed = false;

map_t tree = map_init(int, int, map_cmp_uint);

int key[N_NODES], val[N_NODES];

/*
* Generate data for insertion
*/
for (int i = 0; i < N_NODES; i++) {
key[i] = i;
val[i] = i + 1;
}

/* TODO: This is not a reconmended way to randomize stuff, just a simple
* test. Using MT19937 might be better
*/
srand((unsigned) time(NULL));
for (int i = 0; i < N_NODES; i++) {
int pos_a = rand() % N_NODES;
int pos_b = rand() % N_NODES;
swap(&key[pos_a], &key[pos_b]);
swap(&val[pos_a], &val[pos_b]);
}

/* add first 1/2 items */
for (int i = 0; i < N_NODES / 2; i++) {
map_iter_t my_it;
map_insert(tree, key + i, val + i);
map_find(tree, &my_it, key + i);
assert(my_it.node);
assert((*(int *) (my_it.node->data)) == val[i]);
}

/* remove first 1/4 items */
for (int i = 0; i < N_NODES / 4; i++) {
map_iter_t my_it;
map_find(tree, &my_it, key + i);
if (map_at_end(tree, &my_it))
continue;
map_erase(tree, &my_it);
map_find(tree, &my_it, key + i);
assert(my_it.node);
assert(!my_it.node);
}

/* add the rest */
for (int i = N_NODES / 2 + 1; i < N_NODES; i++) {
map_iter_t my_it;
map_insert(tree, key + i, val + i);
map_find(tree, &my_it, key + i);
assert((*(int *) (my_it.node->data)) == val[i]);
}


/* remove 2nd quarter of items */
for (int i = N_NODES / 4 + 1; i < N_NODES / 2; i++) {
map_iter_t my_it;
map_find(tree, &my_it, key + i);
if (map_at_end(tree, &my_it))
continue;
map_erase(tree, &my_it);
map_find(tree, &my_it, key + i);
assert(!my_it.node);
}

map_clear(tree);
map_delete(tree);

return failed;
}

int main(int argc, char *argv[])
{
(void) argc;
(void) argv;
bool failed = test_mix_insert();
printf("%d", failed);

return 0;
}

0 comments on commit 517af99

Please sign in to comment.