some cleanup and extended insertion in rbtree, updated rbtree test
This commit is contained in:
parent
401b11fdcf
commit
f630cbf979
8 changed files with 382 additions and 310 deletions
2
Makefile
2
Makefile
|
|
@ -24,7 +24,7 @@ all :
|
||||||
|
|
||||||
me :
|
me :
|
||||||
mkdir -p $(BUILD)
|
mkdir -p $(BUILD)
|
||||||
$(CC) $(CFLAGS) $(SOURCES) -o $(BUILD)/arch
|
$(CC) $(CFLAGS) $(DEFS) $(SOURCES) -o $(BUILD)/arch
|
||||||
|
|
||||||
clean :
|
clean :
|
||||||
rm -fr $(BUILD)
|
rm -fr $(BUILD)
|
||||||
|
|
|
||||||
|
|
@ -1,24 +1,16 @@
|
||||||
/*
|
/* common/rbtree.c - red black tree implementation
|
||||||
* Modified version of Julienne Walker's implementation
|
|
||||||
* http://www.eternallyconfuzzled.com/tuts/datastructures/jsw_tut_rbtree.aspx
|
|
||||||
*
|
*
|
||||||
* Copyright (C) 2010 Archived
|
* Copyright (C) 2010 Hernrik Hautakoski <henrik.hautakoski@gmail.com>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
* (at your option) any later version.
|
* (at your option) any later version.
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful,
|
* Based on the work of Julienne Walker's rbtree implementation
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* http://www.eternallyconfuzzled.com/tuts/datastructures/jsw_tut_rbtree.aspx
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*
|
|
||||||
* Do not touch anything in this file. it's perfect ;)
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
|
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
|
|
@ -27,50 +19,6 @@
|
||||||
#define is_red(n) (n != NULL && n->color == RB_RED)
|
#define is_red(n) (n != NULL && n->color == RB_RED)
|
||||||
#define swap(n,d,q) n->child[n->child[d] == q]
|
#define swap(n,d,q) n->child[n->child[d] == q]
|
||||||
|
|
||||||
#ifdef __DEBUG__
|
|
||||||
int rb_assert(rbnode *node) {
|
|
||||||
|
|
||||||
int rh, lh;
|
|
||||||
rbnode *ln;
|
|
||||||
rbnode *rn;
|
|
||||||
|
|
||||||
if (node == NULL) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ln = node->child[0];
|
|
||||||
rn = node->child[1];
|
|
||||||
|
|
||||||
if (is_red(node)) {
|
|
||||||
if (is_red(ln) || is_red(rn)) {
|
|
||||||
die("Double red");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
lh = rb_assert(ln);
|
|
||||||
rh = rb_assert(rn);
|
|
||||||
|
|
||||||
if ( (ln != NULL && ln->key >= node->key) &&
|
|
||||||
(rn != NULL && rn->key <= node->key) ) {
|
|
||||||
die("BST violation");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (rh != 0 && lh != 0) {
|
|
||||||
|
|
||||||
if (rh != lh) {
|
|
||||||
die("Black height violation");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (is_red(node)) ? lh : lh+1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static rbnode* node_alloc(uint key, void *ptr) {
|
static rbnode* node_alloc(uint key, void *ptr) {
|
||||||
|
|
||||||
rbnode *n = malloc(sizeof(rbnode));
|
rbnode *n = malloc(sizeof(rbnode));
|
||||||
|
|
@ -87,52 +35,74 @@ static rbnode* node_alloc(uint key, void *ptr) {
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void node_dealloc(rbnode *n, void (*a)(rbnode *)) {
|
/*
|
||||||
|
* Recursivly deallocate a tree.
|
||||||
|
*/
|
||||||
|
static void node_dealloc(rbnode *n, void (*action)(rbnode *)) {
|
||||||
|
|
||||||
if (n == NULL)
|
if (n == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (a != NULL)
|
if (action != NULL) {
|
||||||
a(n);
|
action(n);
|
||||||
else
|
} else if (n->data != NULL) {
|
||||||
free(n->data);
|
free(n->data);
|
||||||
|
n->data = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
node_dealloc(n->child[0], a);
|
node_dealloc(n->child[0], action);
|
||||||
node_dealloc(n->child[1], a);
|
node_dealloc(n->child[1], action);
|
||||||
|
|
||||||
free(n);
|
free(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _rbwalk(rbnode *n, void (*a)(rbnode *)) {
|
/*
|
||||||
|
* Recursivly walks a tree, applying action function on every node
|
||||||
|
*/
|
||||||
|
static void rbwalk(rbnode *n, void (*action)(rbnode *)) {
|
||||||
|
|
||||||
if (n == NULL)
|
if (n == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
a(n);
|
action(n);
|
||||||
|
|
||||||
_rbwalk(n->child[0], a);
|
rbwalk(n->child[0], action);
|
||||||
_rbwalk(n->child[1], a);
|
rbwalk(n->child[1], action);
|
||||||
}
|
}
|
||||||
|
|
||||||
static rbnode* _rbcmp(rbnode *n, void *d, size_t l) {
|
static inline int datacmp(void *d1, void *d2, size_t l) {
|
||||||
|
|
||||||
rbnode* r;
|
if (d1 == NULL || d2 == NULL)
|
||||||
|
return d1 == d2;
|
||||||
|
|
||||||
|
return !memcmp(d1, d2, l);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Compares every node's data member with cmpdata along the
|
||||||
|
* path. comparison is done at memory level and returns the first node that match.
|
||||||
|
*/
|
||||||
|
static rbnode* rbcmp(rbnode *n, void *cmpdata, size_t len) {
|
||||||
|
|
||||||
|
rbnode *r;
|
||||||
|
|
||||||
if (n == NULL)
|
if (n == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (memcmp(n->data, d, l) == 0)
|
dprint("CMP %s - %s\n", (char*)n->data, (char*)cmpdata);
|
||||||
|
|
||||||
|
if (datacmp(n->data, cmpdata, len))
|
||||||
return n;
|
return n;
|
||||||
|
|
||||||
r = _rbcmp(n->child[0], d, l);
|
r = rbcmp(n->child[0], cmpdata, len);
|
||||||
|
|
||||||
if (r == NULL)
|
if (r == NULL)
|
||||||
r = _rbcmp(n->child[1], d, l);
|
r = rbcmp(n->child[1], cmpdata, len);
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
static rbnode* rotate_single(rbnode *root, unsigned char dir) {
|
static inline rbnode* rotate_single(rbnode *root, unsigned char dir) {
|
||||||
|
|
||||||
rbnode *save = root->child[!dir];
|
rbnode *save = root->child[!dir];
|
||||||
|
|
||||||
|
|
@ -145,11 +115,76 @@ static rbnode* rotate_single(rbnode *root, unsigned char dir) {
|
||||||
return save;
|
return save;
|
||||||
}
|
}
|
||||||
|
|
||||||
static rbnode* rotate_double(rbnode *root, unsigned char dir) {
|
static inline rbnode* rotate_double(rbnode *root, unsigned char dir) {
|
||||||
root->child[!dir] = rotate_single(root->child[!dir], !dir);
|
root->child[!dir] = rotate_single(root->child[!dir], !dir);
|
||||||
return rotate_single(root, dir);
|
return rotate_single(root, dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline int rbtree_is_empty(rbtree *tree) {
|
||||||
|
|
||||||
|
return tree == NULL || tree->root == NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Searches a tree by key.
|
||||||
|
*/
|
||||||
|
rbnode* rbtree_search(rbtree *tree, uint key) {
|
||||||
|
|
||||||
|
rbnode *n;
|
||||||
|
|
||||||
|
if (tree == NULL || tree->root == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
n = tree->root;
|
||||||
|
|
||||||
|
while(n != NULL) {
|
||||||
|
|
||||||
|
dprint("SEARCH: check %u\n", n->key);
|
||||||
|
|
||||||
|
if (n->key == key)
|
||||||
|
break;
|
||||||
|
|
||||||
|
n = n->child[n->key < key];
|
||||||
|
}
|
||||||
|
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
rbnode* rbtree_cmp_search(rbtree *tree, void *cmpdata, size_t len) {
|
||||||
|
|
||||||
|
if (tree == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
return rbcmp(tree->root, cmpdata, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
void rbtree_walk(rbtree *tree, void (*action)(rbnode *)) {
|
||||||
|
|
||||||
|
if (tree == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
rbwalk(tree->root, action);
|
||||||
|
}
|
||||||
|
|
||||||
|
void rbtree_free(rbtree *tree, void (*action)(rbnode *)) {
|
||||||
|
|
||||||
|
if (tree == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
node_dealloc(tree->root, action);
|
||||||
|
tree->root = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* duplicate keys result in the tree remains unchanged
|
||||||
|
* this can cause memory leaks as data fields can (should)
|
||||||
|
* be heap allocated, and the client expects us to keep track of it.
|
||||||
|
*
|
||||||
|
* for general purposes, we should notify client about it so
|
||||||
|
* then they can chose what to do
|
||||||
|
*
|
||||||
|
* the function now returns -1 in that situation // H Hautakoski
|
||||||
|
*/
|
||||||
int rbtree_insert(rbtree *tree, uint key, void *data) {
|
int rbtree_insert(rbtree *tree, uint key, void *data) {
|
||||||
|
|
||||||
rbnode head = {0};
|
rbnode head = {0};
|
||||||
|
|
@ -160,7 +195,7 @@ int rbtree_insert(rbtree *tree, uint key, void *data) {
|
||||||
/* iterator and parent */
|
/* iterator and parent */
|
||||||
rbnode *p, *q;
|
rbnode *p, *q;
|
||||||
|
|
||||||
unsigned char dir = 0, dir2, last;
|
unsigned char dir = 0, dir2, last, inserted = 0;
|
||||||
|
|
||||||
/* somewhere in here, there should be dragons */
|
/* somewhere in here, there should be dragons */
|
||||||
|
|
||||||
|
|
@ -168,7 +203,8 @@ int rbtree_insert(rbtree *tree, uint key, void *data) {
|
||||||
tree->root = node_alloc(key, data);
|
tree->root = node_alloc(key, data);
|
||||||
if (tree->root == NULL)
|
if (tree->root == NULL)
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
t = &head;
|
t = &head;
|
||||||
g = p = NULL;
|
g = p = NULL;
|
||||||
|
|
@ -179,6 +215,7 @@ int rbtree_insert(rbtree *tree, uint key, void *data) {
|
||||||
p->child[dir] = q = node_alloc(key, data);
|
p->child[dir] = q = node_alloc(key, data);
|
||||||
if (q == NULL)
|
if (q == NULL)
|
||||||
return 0;
|
return 0;
|
||||||
|
inserted = 1;
|
||||||
} else if (is_red(q->child[0]) && is_red(q->child[1])) {
|
} else if (is_red(q->child[0]) && is_red(q->child[1])) {
|
||||||
/* color flip case */
|
/* color flip case */
|
||||||
q->color = RB_RED;
|
q->color = RB_RED;
|
||||||
|
|
@ -208,11 +245,14 @@ int rbtree_insert(rbtree *tree, uint key, void *data) {
|
||||||
}
|
}
|
||||||
|
|
||||||
tree->root = head.child[1];
|
tree->root = head.child[1];
|
||||||
}
|
|
||||||
|
|
||||||
|
done:
|
||||||
/* root should be black */
|
/* root should be black */
|
||||||
tree->root->color = RB_BLACK;
|
tree->root->color = RB_BLACK;
|
||||||
|
|
||||||
|
if (!inserted)
|
||||||
|
return -1;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -226,7 +266,8 @@ void* rbtree_delete(rbtree *tree, uint key) {
|
||||||
/* found item */
|
/* found item */
|
||||||
rbnode *f = NULL;
|
rbnode *f = NULL;
|
||||||
|
|
||||||
/* pointer to the data member of the node we delete */
|
/* pointer to the data member of the node we delete,
|
||||||
|
returned so it can be free'd */
|
||||||
void *ret = NULL;
|
void *ret = NULL;
|
||||||
|
|
||||||
unsigned char dir = 1, dir2, last;
|
unsigned char dir = 1, dir2, last;
|
||||||
|
|
@ -238,7 +279,7 @@ void* rbtree_delete(rbtree *tree, uint key) {
|
||||||
g = p = NULL;
|
g = p = NULL;
|
||||||
q->child[1] = tree->root;
|
q->child[1] = tree->root;
|
||||||
|
|
||||||
/* more dragons */
|
/* more dragons, TODO: reduce indentation */
|
||||||
|
|
||||||
while(q->child[dir] != NULL) {
|
while(q->child[dir] != NULL) {
|
||||||
last = dir;
|
last = dir;
|
||||||
|
|
@ -292,64 +333,10 @@ void* rbtree_delete(rbtree *tree, uint key) {
|
||||||
f->key = q->key;
|
f->key = q->key;
|
||||||
f->data = q->data;
|
f->data = q->data;
|
||||||
}
|
}
|
||||||
swap(p,1,q) = swap(q, 0, NULL);
|
swap(p, 1, q) = swap(q, 0, NULL);
|
||||||
}
|
}
|
||||||
free(q);
|
free(q);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
rbnode* rbtree_search(rbtree *tree, uint key) {
|
|
||||||
|
|
||||||
rbnode *n;
|
|
||||||
|
|
||||||
if (tree == NULL || tree->root == NULL)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
n = tree->root;
|
|
||||||
|
|
||||||
while(n != NULL) {
|
|
||||||
|
|
||||||
#ifdef __DEBUG__
|
|
||||||
printf("SEARCH: check %u\n", n->key);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (n->key == key)
|
|
||||||
break;
|
|
||||||
|
|
||||||
n = n->child[n->key < key];
|
|
||||||
}
|
|
||||||
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
rbnode* rbtree_cmp_search(rbtree *tree, void *cmpdata, size_t len) {
|
|
||||||
|
|
||||||
if (tree == NULL)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
return _rbcmp(tree->root, cmpdata, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
void rbtree_walk(rbtree *tree, void (*action)(rbnode *)) {
|
|
||||||
|
|
||||||
if (tree == NULL)
|
|
||||||
return;
|
|
||||||
|
|
||||||
_rbwalk(tree->root, action);
|
|
||||||
}
|
|
||||||
|
|
||||||
void rbtree_free(rbtree *tree, void (*action)(rbnode *)) {
|
|
||||||
|
|
||||||
if (tree == NULL)
|
|
||||||
return;
|
|
||||||
|
|
||||||
node_dealloc(tree->root, action);
|
|
||||||
tree->root = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline int rbtree_is_empty(rbtree *tree) {
|
|
||||||
|
|
||||||
return tree == NULL || tree->root == NULL;
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,24 +1,7 @@
|
||||||
|
|
||||||
/*
|
#ifndef __COMMON_RBTREE_H
|
||||||
* Copyright (C) 2010 Archived
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef _COMMON_RBTREE_H
|
#define __COMMON_RBTREE_H
|
||||||
|
|
||||||
#define _COMMON_RBTREE_H
|
|
||||||
|
|
||||||
#define RB_RED 0
|
#define RB_RED 0
|
||||||
#define RB_BLACK 1
|
#define RB_BLACK 1
|
||||||
|
|
@ -30,6 +13,7 @@ typedef unsigned int uint;
|
||||||
typedef struct _rbn {
|
typedef struct _rbn {
|
||||||
uint key;
|
uint key;
|
||||||
void *data;
|
void *data;
|
||||||
|
size_t len;
|
||||||
struct _rbn *child[2];
|
struct _rbn *child[2];
|
||||||
color_t color;
|
color_t color;
|
||||||
} rbnode;
|
} rbnode;
|
||||||
|
|
@ -38,13 +22,7 @@ typedef struct {
|
||||||
rbnode *root;
|
rbnode *root;
|
||||||
} rbtree;
|
} rbtree;
|
||||||
|
|
||||||
#ifdef RB_DEBUG
|
int rbtree_is_empty(rbtree *tree);
|
||||||
int rb_assert(rbnode *node);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
int rbtree_insert(rbtree *tree, uint key, void *data);
|
|
||||||
|
|
||||||
void* rbtree_delete(rbtree *tree, uint key);
|
|
||||||
|
|
||||||
rbnode* rbtree_search(rbtree *tree, uint key);
|
rbnode* rbtree_search(rbtree *tree, uint key);
|
||||||
|
|
||||||
|
|
@ -54,6 +32,8 @@ void rbtree_walk(rbtree *tree, void (*action)(rbnode *));
|
||||||
|
|
||||||
void rbtree_free(rbtree *tree, void (*action)(rbnode *));
|
void rbtree_free(rbtree *tree, void (*action)(rbnode *));
|
||||||
|
|
||||||
inline int rbtree_is_empty(rbtree *tree);
|
int rbtree_insert(rbtree *tree, uint key, void *data);
|
||||||
|
|
||||||
|
void* rbtree_delete(rbtree *tree, uint key);
|
||||||
|
|
||||||
#endif /* _COMMON_RBTREE_H */
|
#endif /* _COMMON_RBTREE_H */
|
||||||
|
|
|
||||||
|
|
@ -109,24 +109,19 @@ static int proc_event(inoev *iev) {
|
||||||
if (iev == NULL)
|
if (iev == NULL)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
//notify_event_clear(&event);
|
/* notify_event_clear(&event); */
|
||||||
|
|
||||||
#ifdef __DEBUG__
|
|
||||||
fprintf(stderr, "RAW EVENT: %i, %x", iev->wd, iev->mask);
|
fprintf(stderr, "RAW EVENT: %i, %x", iev->wd, iev->mask);
|
||||||
if (iev->len)
|
if (iev->len)
|
||||||
fprintf(stderr, ", %s\n", iev->name);
|
fprintf(stderr, ", %s\n", iev->name);
|
||||||
else
|
else
|
||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
#endif
|
|
||||||
|
|
||||||
/* lookup the watch descriptor in rbtree */
|
/* lookup the watch descriptor in rbtree */
|
||||||
node = rbtree_search(&tree, iev->wd);
|
node = rbtree_search(&tree, iev->wd);
|
||||||
|
|
||||||
if (node == NULL) {
|
if (node == NULL) {
|
||||||
#ifdef __DEBUG__
|
dprint("-- IGNORING EVENT -- invalid watchdescriptor %i\n", iev->wd);
|
||||||
fprintf(stderr, "-- IGNORING EVENT -- invalid watchdescriptor %i\n", iev->wd);
|
|
||||||
rb_assert(tree.root);
|
|
||||||
#endif
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -152,7 +147,7 @@ static int proc_event(inoev *iev) {
|
||||||
case IN_MOVED_TO :
|
case IN_MOVED_TO :
|
||||||
|
|
||||||
if (event.dir) {
|
if (event.dir) {
|
||||||
dprint("IN_CREATE on directory, adding\n");
|
dprint("IN_MOVED_TO on directory, adding\n");
|
||||||
addwatch(event.path, event.filename);
|
addwatch(event.path, event.filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ path2 :
|
||||||
$(CC) -D__DEBUG__ $(CFLAGS) ../src/common/path.c t_path2.c -o test_path2
|
$(CC) -D__DEBUG__ $(CFLAGS) ../src/common/path.c t_path2.c -o test_path2
|
||||||
|
|
||||||
rbtree :
|
rbtree :
|
||||||
$(CC) -D RB_DEBUG $(CFLAGS) ../src/common/rbtree.c t_rbtree.c -o test_rbtree
|
$(CC) $(DEFS) $(CFLAGS) unit.c ../src/common/rbtree.c t_rbtree.c -o test_rbtree
|
||||||
|
|
||||||
inotify :
|
inotify :
|
||||||
$(CC) -lpthread -D INOTIFY_DEBUG -D RB_DEBUG $(CFLAGS) \
|
$(CC) -lpthread -D INOTIFY_DEBUG -D RB_DEBUG $(CFLAGS) \
|
||||||
|
|
|
||||||
208
test/t_rbtree.c
208
test/t_rbtree.c
|
|
@ -1,91 +1,169 @@
|
||||||
#ifndef RB_DEBUG
|
|
||||||
#define RB_DEBUG 1
|
/*
|
||||||
#endif
|
* rb_assert should be called after every rbtree_* operation (not just only the one's
|
||||||
|
* that actualy performs changes to the tree) to ensure the tree is valid after the
|
||||||
|
* function is done.
|
||||||
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <assert.h>
|
#include "unit.h"
|
||||||
#include "../src/common/rbtree.h"
|
#include "../src/common/rbtree.h"
|
||||||
|
#include "../src/common/debug.h"
|
||||||
|
|
||||||
#define MAX_VAL 12500
|
#define MAX_VAL 500
|
||||||
#define NODES 5000
|
#define NODES 20
|
||||||
|
|
||||||
uint get_random_key(uint m) {
|
#define is_red(n) (n != NULL && n->color == RB_RED)
|
||||||
return (uint) (rand() % m);
|
|
||||||
|
static int rb_assert(rbnode *node) {
|
||||||
|
|
||||||
|
int rh, lh;
|
||||||
|
rbnode *ln, *rn;
|
||||||
|
|
||||||
|
if (node == NULL) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ln = node->child[0];
|
||||||
|
rn = node->child[1];
|
||||||
|
|
||||||
|
if (is_red(node)) {
|
||||||
|
/* double red violation */
|
||||||
|
if (is_red(ln) || is_red(rn)) {
|
||||||
|
die("Double red");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lh = rb_assert(ln);
|
||||||
|
rh = rb_assert(rn);
|
||||||
|
|
||||||
|
if ( (ln != NULL && ln->key >= node->key) &&
|
||||||
|
(rn != NULL && rn->key <= node->key) ) {
|
||||||
|
die("BST violation");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rh != 0 && lh != 0) {
|
||||||
|
|
||||||
|
if (rh != lh) {
|
||||||
|
die("Black height violation");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (is_red(node)) ? lh : lh+1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void printer(rbnode *node) {
|
/* data */
|
||||||
|
static rbtree tree;
|
||||||
|
static uint keyref[NODES];
|
||||||
|
static uint search_key = -1;
|
||||||
|
static char search_data[32];
|
||||||
|
|
||||||
printf("node: %u (%p)\n", node->key, node);
|
void test_insert() {
|
||||||
|
|
||||||
|
int i = 0, ret;
|
||||||
|
uint ckey;
|
||||||
|
char *data;
|
||||||
|
|
||||||
|
utest_init_RNG();
|
||||||
|
|
||||||
|
/* insert values */
|
||||||
|
while(i < NODES) {
|
||||||
|
|
||||||
|
ckey = (uint) (rand() % MAX_VAL);
|
||||||
|
data = utest_ran_string(32);
|
||||||
|
|
||||||
|
/* insert into rbtree and assert it */
|
||||||
|
ret = rbtree_insert(&tree, ckey, data);
|
||||||
|
rb_assert(tree.root);
|
||||||
|
|
||||||
|
dprint("INSERT: %i %s\n", ckey, data);
|
||||||
|
|
||||||
|
/* ignore duplicate key */
|
||||||
|
if (ret == -1)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
keyref[i] = ckey;
|
||||||
|
|
||||||
|
if (i == ((NODES/2))) {
|
||||||
|
search_key = ckey;
|
||||||
|
memcpy(&search_data, data, 32);
|
||||||
|
}
|
||||||
|
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* insert duplicate key */
|
||||||
|
assert(rbtree_insert(&tree, search_key, "duplicate") == -1);
|
||||||
|
rb_assert(tree.root);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_delete() {
|
||||||
|
|
||||||
|
int i;
|
||||||
|
uint key;
|
||||||
|
|
||||||
|
/* remove some values */
|
||||||
|
for(i=0; i < 10; i++) {
|
||||||
|
|
||||||
|
key = keyref[(NODES/2)+i];
|
||||||
|
|
||||||
|
rbtree_delete(&tree, key);
|
||||||
|
rb_assert(tree.root);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_search() {
|
||||||
|
|
||||||
|
rbnode *n;
|
||||||
|
|
||||||
|
/* search for a key we know exists */
|
||||||
|
n = rbtree_search(&tree, search_key);
|
||||||
|
|
||||||
|
assert(n != NULL);
|
||||||
|
assert(n->key == search_key);
|
||||||
|
|
||||||
|
/* search for a key we now don't exist */
|
||||||
|
n = rbtree_search(&tree, MAX_VAL+512);
|
||||||
|
|
||||||
|
assert(n == NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_cmp_search() {
|
||||||
|
|
||||||
|
rbnode *n;
|
||||||
|
|
||||||
|
n = rbtree_cmp_search(&tree, &search_data, 32);
|
||||||
|
|
||||||
|
assert(n != NULL);
|
||||||
|
assert_string(n->data, search_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
|
|
||||||
uint keyref[NODES];
|
|
||||||
uint i, spos, search_key, ckey;
|
|
||||||
rbtree tree;
|
|
||||||
rbnode *snode;
|
|
||||||
|
|
||||||
tree.root = NULL;
|
tree.root = NULL;
|
||||||
|
|
||||||
srand(time(NULL));
|
/* a new tree is empty */
|
||||||
|
|
||||||
/* rbtree should be empty */
|
|
||||||
assert(rbtree_is_empty(&tree) == 1);
|
assert(rbtree_is_empty(&tree) == 1);
|
||||||
|
|
||||||
/* get a random node position */
|
test_insert();
|
||||||
spos = get_random_key(NODES);
|
test_search();
|
||||||
|
test_cmp_search();
|
||||||
|
test_delete();
|
||||||
|
|
||||||
/* insert values */
|
|
||||||
for(i=0; i < NODES; i++) {
|
|
||||||
|
|
||||||
ckey = get_random_key(MAX_VAL);
|
|
||||||
|
|
||||||
if(i == spos)
|
|
||||||
search_key = ckey;
|
|
||||||
|
|
||||||
//printf("insert: %u\n", ckey);
|
|
||||||
rbtree_insert(&tree, ckey, NULL);
|
|
||||||
keyref[i] = ckey;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* validate tree */
|
|
||||||
rb_assert(tree.root);
|
|
||||||
|
|
||||||
/* search the random key */
|
|
||||||
snode = rbtree_search(&tree, search_key);
|
|
||||||
|
|
||||||
assert(snode->key == search_key);
|
|
||||||
|
|
||||||
/* remove some values */
|
|
||||||
for(i=0; i < NODES; i++) {
|
|
||||||
|
|
||||||
//printf("removing: %u\n", ckey);
|
|
||||||
|
|
||||||
rbtree_delete(&tree, ckey);
|
|
||||||
|
|
||||||
ckey = get_random_key(MAX_VAL);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* assert again */
|
|
||||||
rb_assert(tree.root);
|
|
||||||
|
|
||||||
if(argc == 1) {
|
|
||||||
/* free the tree */
|
/* free the tree */
|
||||||
rbtree_free(&tree, NULL);
|
rbtree_free(&tree, NULL);
|
||||||
} else {
|
|
||||||
printf("---\n");
|
|
||||||
for(i=0; i < NODES; i++) {
|
|
||||||
//printf("removing: %u\n", keyref[i]);
|
|
||||||
rbtree_delete(&tree, keyref[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
rbtree_walk(&tree, printer);
|
/* tree is now empty again */
|
||||||
|
|
||||||
/* tree should now be empty */
|
|
||||||
assert(rbtree_is_empty(&tree) == 1);
|
assert(rbtree_is_empty(&tree) == 1);
|
||||||
|
|
||||||
|
printf("-- TEST PASS --\n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
53
test/unit.c
53
test/unit.c
|
|
@ -1,16 +1,51 @@
|
||||||
|
|
||||||
#include "unit.h"
|
#include "unit.h"
|
||||||
|
#include <malloc.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
int utest_string(const char *a, const char *b) {
|
static inline char ranchr() {
|
||||||
|
|
||||||
while(*a == *b) {
|
char ch;
|
||||||
|
|
||||||
if (*a == 0)
|
do {
|
||||||
return 1;
|
ch = rand() % 0x7A;
|
||||||
|
} while(ch < 0x20);
|
||||||
|
|
||||||
a++;
|
return ch;
|
||||||
b++;
|
}
|
||||||
}
|
|
||||||
|
void utest_init_RNG() {
|
||||||
return 0;
|
|
||||||
|
static unsigned char init = 0;
|
||||||
|
|
||||||
|
if (init)
|
||||||
|
return;
|
||||||
|
|
||||||
|
srand(time(NULL));
|
||||||
|
init = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Helper function for generating random strings
|
||||||
|
*/
|
||||||
|
char* utest_ran_string(size_t size) {
|
||||||
|
|
||||||
|
int i, r;
|
||||||
|
char *str;
|
||||||
|
|
||||||
|
if (size < 1)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
str = malloc(size+1);
|
||||||
|
|
||||||
|
if (str == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
utest_init_RNG();
|
||||||
|
|
||||||
|
for(i=0; i < size; i++)
|
||||||
|
str[i] = ranchr();
|
||||||
|
str[i] = 0;
|
||||||
|
|
||||||
|
return str;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
11
test/unit.h
11
test/unit.h
|
|
@ -8,14 +8,11 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
#define utest_exit(fmt, ...) \
|
#define assert_string(a, b) \
|
||||||
fprintf(stderr, fmt, __VA_ARGS__); \
|
assert(strcmp((char*)a, (char*)b) == 0)
|
||||||
exit(1)
|
|
||||||
|
|
||||||
#define utest_string(a, b) \
|
void utest_init_RNG();
|
||||||
(strcmp(a, b) == 0 ? \
|
|
||||||
(void) 0 : \
|
|
||||||
utest_exit("ASSERT: \"" #expr "\" at %s:%i\n", __FILE__, __LINE__))
|
|
||||||
|
|
||||||
|
char* utest_ran_string(size_t size);
|
||||||
|
|
||||||
#endif /* _UNIT_H */
|
#endif /* _UNIT_H */
|
||||||
|
|
|
||||||
Reference in a new issue