Skip to content

Commit

Permalink
add test for reentrancy
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzprix committed May 28, 2016
1 parent ccf5963 commit a0be292
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 4 deletions.
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ REL_DYNAMIC_TARGET=libcdsl.so

VPATH=$(SRC-y)
INCS=$(INC-y:%=-I%)
LIBS=$(LIB-y:%=-l%)

DBG_OBJS=$(OBJ-y:%=$(DBG_CACHE_DIR)/%.do)
REL_OBJS=$(OBJ-y:%=$(REL_CACHE_DIR)/%.o)
Expand Down Expand Up @@ -115,16 +116,16 @@ endif

$(TEST_TARGET) : $(REL_CACHE_DIR)/main.o $(REL_OBJS)
@echo 'Building unit-test executable... for $(ARCH) $@'
$(CXX) -o $@ $(REL_CFLAG) $< $(REL_OBJS)
$(CXX) -o $@ $(REL_CFLAG) $< $(REL_OBJS) $(LIBS)


$(DEV_TEST_TARGET) : $(DBG_CACHE_DIR)/main.do $(DBG_OBJS)
@echo 'Building unit-test executable... for $(ARCH) $@'
$(CXX) -o $@ $(DBG_CFLAG) $< $(DBG_OBJS)
$(CXX) -o $@ $(DBG_CFLAG) $< $(DBG_OBJS) $(LIBS)

$(DBG_CACHE_DIR)/%.do : %.c
@echo '$(ARCH) compile...$@'
$(CC) -c -o $@ $(DBG_CFLAG) $< $(INCS)
$(CC) -c -o $@ $(DBG_CFLAG) $< $(INCS)

$(REL_CACHE_DIR)/%.o : %.c
@echo '$(ARCH) compile...$@'
Expand Down
54 changes: 53 additions & 1 deletion source/test/cdsl_bstree_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,25 @@
#include "cdsl_bstree_test.h"
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

struct compete_req {
bstreeRoot_t* root;
trkey_t base;
pthread_mutex_t* lock;
};


static bstreeNode_t bst_nodepool[TEST_SIZE];
static bstreeNode_t replace;
static trkey_t repl_key;
static trkey_t keys[TEST_SIZE];
static pthread_t threads[100];

static int cb_count;
static void* compete_rbtree(void* com_req);


BOOL cdsl_bstreeDoTest(void)
{
bstreeRoot_t root;
Expand Down Expand Up @@ -76,7 +88,47 @@ BOOL cdsl_bstreeDoTest(void)

if(cdsl_bstreeSize(&root) > 0) // size should be zero
return FALSE;


pthread_mutex_t lock;
pthread_mutex_init(&lock, NULL);
int idx;
struct compete_req* req = NULL;
for(idx = 0;idx < 100; idx++) {
req = malloc(sizeof(struct compete_req));
req->base = idx * 100;
req->root = &root;
req->lock = &lock;
pthread_create(&threads[idx], NULL, compete_rbtree, req);
}

for(idx = 0;idx < 100; idx++) {
pthread_join(threads[idx], (void**) &req);
free(req);
}
return TRUE;
}


static void* compete_rbtree(void* com_req) {
struct compete_req* req = (struct compete_req*) com_req;
printf("Start Job : %lu \n", req->base);
trkey_t end = req->base + 100;
trkey_t start = req->base;
bstreeNode_t* node = NULL;
for(; start < end; start++) {
cdsl_bstreeNodeInit(&bst_nodepool[start],start);
cdsl_bstreeInsert(req->root, &bst_nodepool[start]);
}
for(start = req->base; start < end; start++) {
node = cdsl_bstreeDelete(req->root, start);
if(!node) {
fprintf(stderr, "Reentrant Test Delete Fail %lu / %lu\n",start,req->base);
exit(-1);
}
if((node->key < req->base) || (node->key >= end)) {
fprintf(stderr, "Reentrant Test Fail\n");
exit(-1);
}
}
return com_req;
}
58 changes: 58 additions & 0 deletions source/test/cdsl_nrbtree_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,22 @@
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>


static nrbtreeNode_t node_pool[TEST_SIZE];
static nrbtreeNode_t replace;
static int keys[TEST_SIZE];
static pthread_t threads[100];

struct compete_req {
nrbtreeRoot_t* root;
trkey_t base;
pthread_mutex_t* lock;
};


static void* compete_rbtree(void* com_req);

BOOL cdsl_nrbtreeDoTest(void)
{
Expand Down Expand Up @@ -78,8 +89,55 @@ BOOL cdsl_nrbtreeDoTest(void)
return FALSE;
}
}


if(cdsl_nrbtreeSize(&root) > 0)
return FALSE;

pthread_mutex_t lock;
pthread_mutex_init(&lock, NULL);
int idx;
struct compete_req* req = NULL;
for(idx = 0;idx < 100; idx++) {
req = malloc(sizeof(struct compete_req));
req->base = idx * 100;
req->root = &root;
req->lock = &lock;
pthread_create(&threads[idx], NULL, compete_rbtree, req);
}

for(idx = 0;idx < 100; idx++) {
pthread_join(threads[idx], (void**) &req);
free(req);
}

return TRUE;
}

static void* compete_rbtree(void* com_req) {
struct compete_req* req = (struct compete_req*) com_req;
printf("Start Job : %lu \n", req->base);
trkey_t end = req->base + 100;
trkey_t start = req->base;
nrbtreeNode_t* node = NULL;
for(; start < end; start++) {
cdsl_nrbtreeNodeInit(&node_pool[start],start);
pthread_mutex_lock(req->lock);
cdsl_nrbtreeInsert(req->root, &node_pool[start]);
pthread_mutex_unlock(req->lock);
}
for(start = req->base; start < end; start++) {
pthread_mutex_lock(req->lock);
node = cdsl_nrbtreeDelete(req->root, start);
pthread_mutex_unlock(req->lock);
if(!node) {
fprintf(stderr, "Reentrant Test Delete Fail %lu / %lu\n",start,req->base);
exit(-1);
}
if((node->key < req->base) || (node->key >= end)) {
fprintf(stderr, "Reentrant Test Fail\n");
exit(-1);
}
}
return com_req;
}
1 change: 1 addition & 0 deletions source/test/recipe.mk
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
INC-y+=./include/test
LIB-y+=pthread
SRC-y+=./source/test
OBJ-y+=cdsl_bstree_test\
cdsl_hash_test\
Expand Down

0 comments on commit a0be292

Please sign in to comment.