Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

添加test

  • Loading branch information...
commit 40971dda0b08fb99ac4e29b6bf2492667391758c 1 parent ffbe165
@believe3301 authored
View
1  .gitignore
@@ -3,3 +3,4 @@
cscope.*
*.so
testapp
+bench
View
30 Makefile
@@ -1,19 +1,25 @@
CC = gcc
-DEBUG = -g -DERROR
-CFLAGS += -W -Wall -Werror $(DEBUG)
+CFLAGS = -W -Wall -Werror -g -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls
+DEBUG_CFLAGS = -fprofile-arcs -ftest-coverage
+DEBUG_LDFLAGS = -lgcov
LIB_OBJS = \
./hash.o\
./lru.o
-TEST_OBJS = \
+TESTAPP_OBJS = \
./test/test.o
+BENCH_OBJS = \
+ ./test/bench.o
+
LIBRARY = liblru.so
-TEST = testapp
+TESTAPP = testapp
+
+BENCH = bench
-all: $(LIBRARY) $(LIBRARY_S) $(TEST)
+all: $(LIBRARY) $(TESTAPP) $(BENCH)
$(LIBRARY): $(LIB_OBJS)
$(CC) -fPIC -shared $^ -o $@
@@ -21,12 +27,22 @@ $(LIBRARY): $(LIB_OBJS)
$(LIB_OBJS): %.o: %.c
$(CC) -fPIC $(CFLAGS) $^ -c -o $@
-$(TEST): $(TEST_OBJS)
+$(TESTAPP): $(LIBRARY) $(TESTAPP_OBJS)
$(CC) -L. $^ -o $@ -llru
-$(TEST_OBJS): %.o: %.c
+$(TESTAPP_OBJS): %.o: %.c
$(CC) $(CFLAGS) -I. $^ -c -o $@
+$(BENCH): $(LIBRARY) $(BENCH_OBJS)
+ $(CC) -L. $^ -o $@ -llru
+
+$(BENCH_OBJS): %.o: %.c
+ $(CC) $(CFLAGS) -I. $^ -c -o $@
+
+test: $(TESTAPP) $(BENCH)
+ ./$(TESTAPP)
+ ./$(BENCH)
+
clean:
-rm -f $(LIB_OBJS) $(TEST_OBJS)
-rm -f $(LIBRARY) $(TEST)
View
12 hash.c
@@ -17,12 +17,6 @@ hash_init(const int hashpower_init)
table = calloc(hashsize(hashpower), sizeof(void *));
}
-void
-hash_free()
-{
- free(table);
-}
-
lru_item*
hash_find(const char *key, const size_t nkey, const uint32_t hv)
{
@@ -73,3 +67,9 @@ hash_delete(const char *key, const size_t nkey, const uint32_t hv)
*before = nxt;
}
}
+
+void
+hash_free(void)
+{
+ free(table);
+}
View
2  hash.h
@@ -6,4 +6,4 @@ int hash_insert(lru_item *it, const uint32_t hv);
void hash_delete(const char *key, const size_t nkey, const uint32_t hv);
-void hash_free();
+void hash_free(void);
View
12 lru.c
@@ -19,7 +19,7 @@ lru_init(const size_t maxbytes)
memset(stat, 0, sizeof(lru_stat));
}
- if (maxbytes < 1 * 1024 * 1024) {
+ if (maxbytes <= 0) {
stat->max_bytes = MAXBYTE_DEDAULT;
} else {
stat->max_bytes = maxbytes;
@@ -28,7 +28,7 @@ lru_init(const size_t maxbytes)
hash_init(0);
}
-//TODO
+/* TODO */
static uint32_t
hash(const char *key, const int nkey)
{
@@ -175,7 +175,7 @@ item_set(const char *key, const size_t nkey, const char *value, const size_t nva
return 0;
}
-void
+int
item_delete(const char *key, const size_t nkey)
{
stat->del_cmds++;
@@ -185,8 +185,10 @@ item_delete(const char *key, const size_t nkey)
if (it != NULL) {
do_item_remove_hv(it, hv);
stat->del_hits++;
+ return 0;
} else {
stat->del_misses++;
+ return 1;
}
}
@@ -211,7 +213,7 @@ append_stat(char **buf, int *nbuf, char *name, const char *fmt, ...)
}
void
-print_stat(char *buf, const int nbuf)
+stat_print(char *buf, const int nbuf)
{
int remaining = nbuf;
@@ -239,7 +241,7 @@ print_stat(char *buf, const int nbuf)
append_stat(&buf, &remaining, "evictions","%llu",stat->evictions);
}
-void reset_stat()
+void stat_reset(void)
{
assert(stat != NULL);
memset(stat, 0, sizeof(struct lru_stat));
View
7 lru.h
@@ -66,10 +66,11 @@ int item_get(const char *key, const size_t nkey, char *buf, const size_t nbuf, s
/* 0 success , 1 failed */
int item_set(const char *key, const size_t nkey, const char *value, const size_t nvalue);
-void item_delete(const char *key, const size_t nkey);
+/* 0 hit, 1 miss */
+int item_delete(const char *key, const size_t nkey);
-void reset_stat();
+void stat_reset(void);
-void print_stat(char *buf, const int nbuf);
+void stat_print(char *buf, const int nbuf);
void lru_free(void);
View
5 test/bench.c
@@ -0,0 +1,5 @@
+#include <stdlib.h>
+
+int main(void) {
+ return 0;
+}
View
182 test/test.c
@@ -2,34 +2,176 @@
#include <assert.h>
#include "lru.h"
-int main(void)
-{
- lru_init(0);
+enum test_return { TEST_SKIP, TEST_PASS, TEST_FAIL };
+
+typedef enum test_return (*test_func)(void);
+
+struct testcase {
+ const char *description;
+ test_func function;
+};
+
+static enum test_return lru_normal_op(int new, size_t mb, char *key, size_t nkey, char *value, size_t nvalue) {
+ char buf[64];
+ size_t sz;
+
+ if (new) {
+ lru_init(mb);
+ }
+
+ int r = item_set(key, nkey, value, nvalue);
+
+ if (r) {
+ return TEST_FAIL;
+ }
+
+ r = item_get(key, nkey, buf, sizeof(buf), &sz);
+ assert(r == 0);
+ assert(sz == nvalue);
+ assert(memcmp(value, buf, sz) == 0);
+
+ r = item_delete(key, nkey);
+ assert(r == 0);
+
+ if (new) {
+ lru_free();
+ stat_reset();
+ }
+ return TEST_PASS;
+}
+
+static enum test_return lru_normal_test(void) {
+ char *key = "key1";
+ char *value = "value1";
+
+ return lru_normal_op(1, 0, key, strlen(key), value, strlen(value) + 1);
+}
+
+static enum test_return lru_overwrite_test(void) {
+ return TEST_SKIP;
+}
+
+static enum test_return lru_big_set_test(void) {
+ char *key = "key1";
+ char *value = "value1";
+
+ int malloc = ITEM_size + strlen(key) + 1 + strlen(value) + 1;
+
+ enum test_return r = lru_normal_op(1, malloc, key, strlen(key), value, strlen(value) + 1);
- item_set("key1", 4, "abcd", 5);
- item_set("key2", 4, "abcd", 5);
- item_set("key3", 4, "abcd", 5);
+ if ( r != TEST_PASS) {
+ return r;
+ }
- char value[256];
- size_t vsz;
+ r = lru_normal_op(1, malloc - 1, key, strlen(key), value, strlen(value) + 1);
- item_get("key1", 4, value, 256, &vsz);
+ if ( r == TEST_FAIL) {
+ return TEST_PASS;
+ } else {
+ return TEST_FAIL;
+ }
+}
+
+static enum test_return lru_evict_test(void) {
+ char key[5][5] = {"key1", "key2", "key3", "key4", "key5"};
+ char *value = "value1";
+
+ size_t nkey = 4;
+ size_t nvalue = strlen(value);
+
+ int malloc = ITEM_size + nkey + nvalue + 1;
+ int r, i = 0, loop;
+
+ loop = sizeof(key) / (nkey + 1);
+
+ lru_init(malloc * loop);
+
+ size_t sz;
+ char buf[256];
+
+
+ for (i = 0; i < loop; i++) {
+ r = item_set(key[i], nkey, value, nvalue);
+ assert(r == 0);
+ }
+
+ for (i = 0; i < loop; i++) {
+ r = item_get(key[i], nkey, buf, sizeof(buf), &sz);
+ assert(r == 0);
+ assert(sz == nvalue);
+ assert(memcmp(value, buf, sz) == 0);
+ memset(buf, 0, sizeof(buf));
+ }
- printf("key: %s, value: %s\n", "key1", value);
+ //evict 3
+ char *key2 = "key6";
+ size_t nvalue2 = malloc * 2 + 2;
+ char value2[nvalue2];
- item_delete("key1", 4);
- int r = item_get("key1", 4, value, 256, &vsz);
- assert(r == 1);
+ memset(value2, (int)'0', nvalue2);
- char buf[1024];
+ value2[nvalue2 -1] = '\0';
+ value2[0] = 'a';
+
+ r = item_set(key2, nkey, value2, nvalue2);
+ assert(r == 0);
+
+ for (i = 0; i < 3; i++) {
+ r = item_get(key[i], nkey, buf, sizeof(buf), &sz);
+ assert(r == 1);
+ }
+ for (i = 3; i < loop; i++) {
+ r = item_get(key[i], nkey, buf, sizeof(buf), &sz);
+ assert(r == 0);
+ assert(sz == nvalue);
+ assert(memcmp(value, buf, sz) == 0);
+ memset(buf, 0, sizeof(buf));
+ }
+
+ r = item_get(key2, nkey, buf, sizeof(buf), &sz);
+ assert(r == 0);
+ assert(sz == nvalue2);
+ assert(memcmp(value2, buf, sz) == 0);
+ memset(buf, 0, sizeof(buf));
+
+ return TEST_PASS;
+}
+
+static enum test_return lru_stat_test(void) {
+ return TEST_SKIP;
+}
+
+struct testcase testcases[] = {
+ { "normal_test", lru_normal_test },
+ { "overwrite_test", lru_overwrite_test },
+ { "big_test", lru_big_set_test },
+ { "evict_test", lru_evict_test },
+ { "stat_test", lru_stat_test },
+ { NULL, NULL }
+};
+
+int main(void)
+{
+ int exitcode = 0;
+ int ii = 0, num_cases = 0;
- print_stat(buf, sizeof(buf));
+ for (num_cases = 0; testcases[num_cases].description; num_cases++);
- printf("%s", buf);
+ printf("1..%d\n", num_cases);
- lru_free();
- print_stat(buf, sizeof(buf));
- printf("%s", buf);
+ for (ii = 0; testcases[ii].description != NULL; ++ii) {
+ fflush(stdout);
+ enum test_return ret = testcases[ii].function();
+ if (ret == TEST_SKIP) {
+ fprintf(stdout, "ok # SKIP %d - %s\n", ii + 1, testcases[ii].description);
+ } else if (ret == TEST_PASS) {
+ fprintf(stdout, "ok %d - %s\n", ii + 1, testcases[ii].description);
+ } else {
+ fprintf(stdout, "not ok %d - %s\n", ii + 1, testcases[ii].description);
+ exitcode = 1;
+ }
+ fflush(stdout);
+ }
- return 0;
+ return exitcode;
}

0 comments on commit 40971dd

Please sign in to comment.
Something went wrong with that request. Please try again.