API rename & cleanup

This commit is contained in:
c9s 2014-05-16 15:29:25 +08:00
parent cc3f5024e8
commit 0e488403a9
4 changed files with 235 additions and 164 deletions

View file

@ -13,6 +13,35 @@ Pattern Syntax
/blog/post/{id:\d+} use `\d+` regular expression instead of default. /blog/post/{id:\d+} use `\d+` regular expression instead of default.
C API
------------------------
```c
// create a router tree with 10 children capacity (this capacity can grow dynamically)
n = rtree_create(10);
int route_data = 3;
// insert the route path into the router tree
rtree_insert_pathn(n , "/zoo" , strlen("/zoo") , &route_data );
rtree_insert_pathn(n , "/foo/bar" , strlen("/foo/bar") , &route_data );
rtree_insert_pathn(n , "/bar" , strlen("/bar") , &route_data );
rtree_insert_pathn(n , "/post/{id}" , strlen("/post/{id}") , &route_data );
// let's compile the tree!
rtree_compile(n);
// dump the compiled tree
rtree_dump(n, 0);
// match a route
node *matched_node = rtree_match(n, "/foo/bar", strlen("/foo/bar") );
matched_node->endpoint; // make sure there is a route end at here.
int ret = *( (*int) matched_node->route_ptr );
```
Use case in PHP Use case in PHP
----------------------- -----------------------

View file

@ -16,14 +16,14 @@
#include "token.h" #include "token.h"
struct _redge; struct _edge;
struct _rnode; struct _node;
typedef struct _redge redge; typedef struct _edge edge;
typedef struct _rnode rnode; typedef struct _node node;
struct _rnode { struct _node {
redge ** edges; edge ** edges;
int edge_len; int edge_len;
int edge_cap; int edge_cap;
@ -31,57 +31,65 @@ struct _rnode {
char * combined_pattern; char * combined_pattern;
int combined_pattern_len; int combined_pattern_len;
pcre * pcre_pattern; pcre * pcre_pattern;
pcre_extra * pcre_extra;
/**
* the pointer of route structure
*/
void * route_ptr;
int endpoint; int endpoint;
}; };
struct _redge { struct _edge {
char * pattern; char * pattern;
int pattern_len; int pattern_len;
bool has_slug; bool has_slug;
rnode * child; node * child;
}; };
rnode * rnode_create(int cap); node * rtree_create(int cap);
void rnode_free(rnode * tree); node * node_create();
void redge_free(redge * edge); void rtree_free(node * tree);
redge * rnode_add_child(rnode * n, char * pat , rnode *child); void edge_free(edge * edge);
redge * rnode_find_edge(rnode * n, char * pat); edge * rtree_add_child(node * n, char * pat , node *child);
void rnode_append_edge(rnode *n, redge *child); edge * node_find_edge(node * n, char * pat);
rnode * rnode_insert_tokens(rnode * tree, token_array * tokens); void rtree_append_edge(node *n, edge *child);
rnode * rnode_insert_route(rnode *tree, char *route); node * rtree_insert_tokens(node * tree, token_array * tokens);
rnode * rnode_insert_routel(rnode *tree, char *route, int route_len); node * rtree_insert_path(node *tree, char *route, void * route_ptr);
void rnode_dump(rnode * n, int level); node * rtree_insert_pathn(node *tree, char *route, int route_len, void * route_ptr);
redge * rnode_find_edge_str(rnode * n, char * str, int str_len); void rtree_dump(node * n, int level);
edge * node_find_edge_str(node * n, char * str, int str_len);
void rnode_compile(rnode *n); void rtree_compile(node *n);
void rnode_compile_patterns(rnode * n); void rtree_compile_patterns(node * n);
rnode * rnode_match(rnode * n, char * path, int path_len); node * rtree_match(node * n, char * path, int path_len);
bool rnode_has_slug_edges(rnode *n); bool node_has_slug_edges(node *n);
rnode * rnode_lookup(rnode * tree, char * path, int path_len); node * rtree_lookup(node * tree, char * path, int path_len);
redge * redge_create(char * pattern, int pattern_len, rnode * child); edge * edge_create(char * pattern, int pattern_len, node * child);
void redge_branch(redge *e, int dl); void edge_branch(edge *e, int dl);
void redge_free(redge * edge); void edge_free(edge * edge);
#endif /* !NODE_H */ #endif /* !NODE_H */

View file

@ -21,26 +21,29 @@
// String value as the index http://judy.sourceforge.net/doc/JudySL_3x.htm // String value as the index http://judy.sourceforge.net/doc/JudySL_3x.htm
/** /**
* Create a rnode object * Create a node object
*/ */
rnode * rnode_create(int cap) { node * rtree_create(int cap) {
rnode * n = (rnode*) malloc( sizeof(rnode) ); node * n = (node*) malloc( sizeof(node) );
n->edges = (redge**) malloc( sizeof(redge*) * 10 ); n->edges = (edge**) malloc( sizeof(edge*) * 10 );
n->edge_len = 0; n->edge_len = 0;
n->edge_cap = 10; n->edge_cap = 10;
n->endpoint = 0; n->endpoint = 0;
n->combined_pattern = NULL; n->combined_pattern = NULL;
// n->edge_patterns = token_array_create(10);
return n; return n;
} }
void rnode_free(rnode * tree) { void rtree_free(node * tree) {
for (int i = 0 ; i < tree->edge_len ; i++ ) { for (int i = 0 ; i < tree->edge_len ; i++ ) {
if (tree->edges[i]) { if (tree->edges[i]) {
redge_free(tree->edges[ i ]); edge_free(tree->edges[ i ]);
} }
} }
if (tree->combined_pattern)
free(tree->combined_pattern);
free(tree->edges); free(tree->edges);
// token_array_free(tree->edge_patterns); // token_array_free(tree->edge_patterns);
free(tree); free(tree);
@ -50,18 +53,18 @@ void rnode_free(rnode * tree) {
/* parent node, edge pattern, child */ /* parent node, edge pattern, child */
redge * rnode_add_child(rnode * n, char * pat , rnode *child) { edge * rtree_add_child(node * n, char * pat , node *child) {
// find the same sub-pattern, if it does not exist, create one // find the same sub-pattern, if it does not exist, create one
redge * e; edge * e;
e = rnode_find_edge(n, pat); e = node_find_edge(n, pat);
if (e) { if (e) {
return e; return e;
} }
e = redge_create( pat, strlen(pat), child); e = edge_create( pat, strlen(pat), child);
rnode_append_edge(n, e); rtree_append_edge(n, e);
// token_array_append(n->edge_patterns, pat); // token_array_append(n->edge_patterns, pat);
// assert( token_array_len(n->edge_patterns) == n->edge_len ); // assert( token_array_len(n->edge_patterns) == n->edge_len );
return e; return e;
@ -69,17 +72,22 @@ redge * rnode_add_child(rnode * n, char * pat , rnode *child) {
void rnode_append_edge(rnode *n, redge *e) { void rtree_append_edge(node *n, edge *e) {
if (!n->edges) {
n->edge_cap = 3;
n->edges = malloc(sizeof(edge) * n->edge_cap);
}
if (n->edge_len >= n->edge_cap) { if (n->edge_len >= n->edge_cap) {
n->edge_cap *= 2; n->edge_cap *= 2;
n->edges = realloc(n->edges, sizeof(redge) * n->edge_cap); n->edges = realloc(n->edges, sizeof(edge) * n->edge_cap);
} }
n->edges[ n->edge_len++ ] = e; n->edges[ n->edge_len++ ] = e;
} }
redge * rnode_find_edge(rnode * n, char * pat) { edge * node_find_edge(node * n, char * pat) {
redge * e; edge * e;
for (int i = 0 ; i < n->edge_len ; i++ ) { for (int i = 0 ; i < n->edge_len ; i++ ) {
e = n->edges[i]; e = n->edges[i];
if ( strcmp(e->pattern, pat) == 0 ) { if ( strcmp(e->pattern, pat) == 0 ) {
@ -89,18 +97,18 @@ redge * rnode_find_edge(rnode * n, char * pat) {
return NULL; return NULL;
} }
void rnode_compile(rnode *n) void rtree_compile(node *n)
{ {
bool use_slug = rnode_has_slug_edges(n); bool use_slug = node_has_slug_edges(n);
if ( use_slug ) { if ( use_slug ) {
rnode_compile_patterns(n); rtree_compile_patterns(n);
} else { } else {
// use normal text matching... // use normal text matching...
n->combined_pattern = NULL; n->combined_pattern = NULL;
} }
for (int i = 0 ; i < n->edge_len ; i++ ) { for (int i = 0 ; i < n->edge_len ; i++ ) {
rnode_compile(n->edges[i]->child); rtree_compile(n->edges[i]->child);
} }
} }
@ -109,7 +117,7 @@ void rnode_compile(rnode *n)
* This function combines ['/foo', '/bar', '/{slug}'] into (/foo)|(/bar)|/([^/]+)} * This function combines ['/foo', '/bar', '/{slug}'] into (/foo)|(/bar)|/([^/]+)}
* *
*/ */
void rnode_compile_patterns(rnode * n) { void rtree_compile_patterns(node * n) {
char * cpat; char * cpat;
char * p; char * p;
@ -119,7 +127,7 @@ void rnode_compile_patterns(rnode * n) {
p = cpat; p = cpat;
redge *e = NULL; edge *e = NULL;
for ( int i = 0 ; i < n->edge_len ; i++ ) { for ( int i = 0 ; i < n->edge_len ; i++ ) {
e = n->edges[i]; e = n->edges[i];
if ( e->has_slug ) { if ( e->has_slug ) {
@ -152,17 +160,21 @@ void rnode_compile_patterns(rnode * n) {
&error, /* for error message */ &error, /* for error message */
&erroffset, /* for error offset */ &erroffset, /* for error offset */
NULL); /* use default character tables */ NULL); /* use default character tables */
if (n->pcre_pattern == NULL) if (n->pcre_pattern == NULL) {
{
printf("PCRE compilation failed at offset %d: %s\n", erroffset, error); printf("PCRE compilation failed at offset %d: %s\n", erroffset, error);
return; return;
} }
n->pcre_extra = pcre_study(n->pcre_pattern, 0, &error);
if (n->pcre_extra == NULL) {
printf("PCRE study failed at offset %s\n", error);
return;
}
} }
rnode * rnode_match(rnode * n, char * path, int path_len) { node * rtree_match(node * n, char * path, int path_len) {
if (n->combined_pattern && n->pcre_pattern) { if (n->combined_pattern && n->pcre_pattern) {
info("pcre matching %s on %s\n", n->combined_pattern, path); info("pcre matching %s on %s\n", n->combined_pattern, path);
// int ovector_count = (n->edge_len + 1) * 2; // int ovector_count = (n->edge_len + 1) * 2;
@ -203,7 +215,7 @@ rnode * rnode_match(rnode * n, char * path, int path_len) {
int restlen = path_len - ovector[2*i+1]; // fully match to the end int restlen = path_len - ovector[2*i+1]; // fully match to the end
info("matched item => restlen:%d edges:%d i:%d\n", restlen, n->edge_len, i); info("matched item => restlen:%d edges:%d i:%d\n", restlen, n->edge_len, i);
if (restlen) { if (restlen) {
return rnode_match( n->edges[i - 1]->child, substring_start + substring_length, restlen); return rtree_match( n->edges[i - 1]->child, substring_start + substring_length, restlen);
} }
return n->edges[i - 1]->child; return n->edges[i - 1]->child;
} }
@ -212,20 +224,20 @@ rnode * rnode_match(rnode * n, char * path, int path_len) {
return NULL; return NULL;
} }
redge *e = rnode_find_edge_str(n, path, path_len); edge *e = node_find_edge_str(n, path, path_len);
if (e) { if (e) {
int len = path_len - e->pattern_len; int len = path_len - e->pattern_len;
if(len == 0) { if(len == 0) {
return e->child; return e->child;
} else { } else {
return rnode_match(e->child, path + e->pattern_len, len); return rtree_match(e->child, path + e->pattern_len, len);
} }
} }
return NULL; return NULL;
} }
redge * rnode_find_edge_str(rnode * n, char * str, int str_len) { edge * node_find_edge_str(node * n, char * str, int str_len) {
redge *e; edge *e;
for ( int i = 0 ; i < n->edge_len ; i++ ) { for ( int i = 0 ; i < n->edge_len ; i++ ) {
e = n->edges[i]; e = n->edges[i];
char *p = e->pattern; char *p = e->pattern;
@ -240,13 +252,13 @@ redge * rnode_find_edge_str(rnode * n, char * str, int str_len) {
} }
rnode * rnode_lookup(rnode * tree, char * path, int path_len) { node * rtree_lookup(node * tree, char * path, int path_len) {
token_array * tokens = split_route_pattern(path, path_len); token_array * tokens = split_route_pattern(path, path_len);
rnode * n = tree; node * n = tree;
redge * e = NULL; edge * e = NULL;
for ( int i = 0 ; i < tokens->len ; i++ ) { for ( int i = 0 ; i < tokens->len ; i++ ) {
e = rnode_find_edge(n, token_array_fetch(tokens, i) ); e = node_find_edge(n, token_array_fetch(tokens, i) );
if (!e) { if (!e) {
return NULL; return NULL;
} }
@ -258,35 +270,46 @@ rnode * rnode_lookup(rnode * tree, char * path, int path_len) {
return NULL; return NULL;
} }
node * node_create() {
node * n = (node*) malloc( sizeof(node) );
n->edges = NULL;
n->edge_len = 0;
n->edge_cap = 0;
n->endpoint = 0;
n->combined_pattern = NULL;
n->pcre_pattern = NULL;
return n;
}
rnode * rnode_insert_tokens(rnode * tree, token_array * tokens) {
rnode * n = tree; node * rtree_insert_tokens(node * tree, token_array * tokens) {
redge * e = NULL; node * n = tree;
edge * e = NULL;
for ( int i = 0 ; i < tokens->len ; i++ ) { for ( int i = 0 ; i < tokens->len ; i++ ) {
e = rnode_find_edge(n, token_array_fetch(tokens, i) ); e = node_find_edge(n, token_array_fetch(tokens, i) );
if (e) { if (e) {
n = e->child; n = e->child;
continue; continue;
} }
// insert node // insert node
rnode * child = rnode_create(3); node * child = node_create();
rnode_add_child(n, strdup(token_array_fetch(tokens,i)) , child); rtree_add_child(n, strdup(token_array_fetch(tokens,i)) , child);
n = child; n = child;
} }
n->endpoint++; n->endpoint++;
return n; return n;
} }
rnode * rnode_insert_route(rnode *tree, char *route) node * rtree_insert_path(node *tree, char *route, void * route_ptr)
{ {
return rnode_insert_routel(tree, route, strlen(route) ); return rtree_insert_pathn(tree, route, strlen(route) , route_ptr);
} }
rnode * rnode_insert_routel(rnode *tree, char *route, int route_len) node * rtree_insert_pathn(node *tree, char *route, int route_len, void * route_ptr)
{ {
rnode * n = tree; node * n = tree;
redge * e = NULL; edge * e = NULL;
char * p = route; char * p = route;
@ -316,12 +339,11 @@ rnode * rnode_insert_routel(rnode *tree, char *route, int route_len)
if ( dl == 0 ) { if ( dl == 0 ) {
// not found, we should just insert a whole new edge // not found, we should just insert a whole new edge
rnode * child = rnode_create(3); node * child = rtree_create(3);
rnode_add_child(n, strndup(route, route_len) , child); rtree_add_child(n, strndup(route, route_len) , child);
// printf("edge not found, insert one: %s\n", route); info("edge not found, insert one: %s\n", route);
child->route_ptr = route_ptr;
n = child; return child;
return n;
} else if ( dl == e->pattern_len ) { // fully-equal to the pattern of the edge } else if ( dl == e->pattern_len ) { // fully-equal to the pattern of the edge
char * subroute = route + dl; char * subroute = route + dl;
@ -329,10 +351,11 @@ rnode * rnode_insert_routel(rnode *tree, char *route, int route_len)
// there are something more we can insert // there are something more we can insert
if ( subroute_len > 0 ) { if ( subroute_len > 0 ) {
return rnode_insert_routel(e->child, subroute, subroute_len); return rtree_insert_pathn(e->child, subroute, subroute_len, route_ptr);
} else { } else {
// no more, // no more,
e->child->endpoint++; // make it as an endpoint, TODO: put the route value e->child->endpoint++; // make it as an endpoint, TODO: put the route value
e->child->route_ptr = route_ptr;
return e->child; return e->child;
} }
@ -343,19 +366,19 @@ rnode * rnode_insert_routel(rnode *tree, char *route, int route_len)
/* it's partically matched with the pattern, /* it's partically matched with the pattern,
* we should split the end point and make a branch here... * we should split the end point and make a branch here...
*/ */
rnode *c2; // child 1, child 2 node *c2; // child 1, child 2
redge *e2; // edge 1, edge 2 edge *e2; // edge 1, edge 2
char * s2 = route + dl; char * s2 = route + dl;
int s2_len = 0; int s2_len = 0;
redge_branch(e, dl); edge_branch(e, dl);
// here is the new edge from. // here is the new edge from.
c2 = rnode_create(3); c2 = rtree_create(3);
s2_len = route_len - dl; s2_len = route_len - dl;
e2 = redge_create(strndup(s2, s2_len), s2_len, c2); e2 = edge_create(strndup(s2, s2_len), s2_len, c2);
// printf("edge right: %s\n", e2->pattern); // printf("edge right: %s\n", e2->pattern);
rnode_append_edge(e->child, e2); rtree_append_edge(e->child, e2);
// truncate the original edge pattern // truncate the original edge pattern
free(e->pattern); free(e->pattern);
@ -364,6 +387,7 @@ rnode * rnode_insert_routel(rnode *tree, char *route, int route_len)
// move n->edges to c1 // move n->edges to c1
c2->endpoint++; c2->endpoint++;
c2->route_ptr = route_ptr;
return c2; return c2;
} else if ( dl > 0 ) { } else if ( dl > 0 ) {
@ -372,14 +396,14 @@ rnode * rnode_insert_routel(rnode *tree, char *route, int route_len)
return NULL; return NULL;
} }
// token_array * t = split_route_pattern(route, strlen(route)); // token_array * t = split_route_pattern(route, strlen(route));
// return rnode_insert_tokens(tree, t); // return rtree_insert_tokens(tree, t);
// n->endpoint++; // n->endpoint++;
return n; return n;
} }
bool rnode_has_slug_edges(rnode *n) { bool node_has_slug_edges(node *n) {
bool found = FALSE; bool found = FALSE;
redge *e; edge *e;
for ( int i = 0 ; i < n->edge_len ; i++ ) { for ( int i = 0 ; i < n->edge_len ; i++ ) {
e = n->edges[i]; e = n->edges[i];
e->has_slug = contains_slug(e->pattern); e->has_slug = contains_slug(e->pattern);
@ -389,61 +413,69 @@ bool rnode_has_slug_edges(rnode *n) {
return found; return found;
} }
void redge_branch(redge *e, int dl) {
rnode *c1; // child 1, child 2 /**
redge *e1; // edge 1, edge 2 * branch the edge pattern at "dl" offset
*
*/
void edge_branch(edge *e, int dl) {
node *c1; // child 1, child 2
edge *e1; // edge 1, edge 2
char * s1 = e->pattern + dl; char * s1 = e->pattern + dl;
int s1_len = 0; int s1_len = 0;
redge **tmp_edges = e->child->edges; edge **tmp_edges = e->child->edges;
int tmp_edge_len = e->child->edge_len; int tmp_edge_len = e->child->edge_len;
// the suffix edge of the leaf // the suffix edge of the leaf
c1 = rnode_create(3); c1 = rtree_create(3);
s1_len = e->pattern_len - dl; s1_len = e->pattern_len - dl;
e1 = redge_create(strndup(s1, s1_len), s1_len, c1); e1 = edge_create(strndup(s1, s1_len), s1_len, c1);
// printf("edge left: %s\n", e1->pattern); // printf("edge left: %s\n", e1->pattern);
// Migrate the child edges to the new edge we just created. // Migrate the child edges to the new edge we just created.
for ( int i = 0 ; i < tmp_edge_len ; i++ ) { for ( int i = 0 ; i < tmp_edge_len ; i++ ) {
rnode_append_edge(c1, tmp_edges[i]); rtree_append_edge(c1, tmp_edges[i]);
e->child->edges[i] = NULL; e->child->edges[i] = NULL;
} }
e->child->edge_len = 0; e->child->edge_len = 0;
e->child->endpoint--;
rnode_append_edge(e->child, e1); info("branched pattern: %s", e1->pattern);
rtree_append_edge(e->child, e1);
c1->endpoint++; c1->endpoint++;
} }
redge * redge_create(char * pattern, int pattern_len, rnode * child) { edge * edge_create(char * pattern, int pattern_len, node * child) {
redge * edge = (redge*) malloc( sizeof(redge) ); edge * e = (edge*) malloc( sizeof(edge) );
edge->pattern = pattern; e->pattern = pattern;
edge->pattern_len = pattern_len; e->pattern_len = pattern_len;
edge->child = child; e->child = child;
return edge; return e;
} }
void redge_free(redge * e) { void edge_free(edge * e) {
if (e->pattern) { if (e->pattern) {
free(e->pattern); free(e->pattern);
} }
if ( e->child ) { if ( e->child ) {
rnode_free(e->child); rtree_free(e->child);
} }
} }
void rnode_dump(rnode * n, int level) { void rtree_dump(node * n, int level) {
if ( n->edge_len ) { if ( n->edge_len ) {
if ( n->combined_pattern ) { if ( n->combined_pattern ) {
printf(" regexp: %s", n->combined_pattern); printf(" regexp: %s", n->combined_pattern);
} }
printf("\n"); printf("\n");
for ( int i = 0 ; i < n->edge_len ; i++ ) { for ( int i = 0 ; i < n->edge_len ; i++ ) {
redge * e = n->edges[i]; edge * e = n->edges[i];
print_indent(level); print_indent(level);
printf(" |-\"%s\"", e->pattern); printf(" |-\"%s\"", e->pattern);
@ -453,7 +485,7 @@ void rnode_dump(rnode * n, int level) {
} }
if ( e->child && e->child->edges ) { if ( e->child && e->child->edges ) {
rnode_dump( e->child, level + 1); rtree_dump( e->child, level + 1);
} }
printf("\n"); printf("\n");
} }

View file

@ -11,31 +11,31 @@ START_TEST (test_ltrim_slash)
} }
END_TEST END_TEST
START_TEST (test_rnode_construct_uniq) START_TEST (test_node_construct_uniq)
{ {
rnode * n = rnode_create(10); node * n = rtree_create(10);
rnode * child = rnode_create(3); node * child = rtree_create(3);
// fail_if( rnode_add_child(n, strdup("/add") , child) != NULL ); // fail_if( rtree_add_child(n, strdup("/add") , child) != NULL );
// fail_if( rnode_add_child(n, strdup("/add") , child) != NULL ); // fail_if( rtree_add_child(n, strdup("/add") , child) != NULL );
rnode_free(n); rtree_free(n);
} }
END_TEST END_TEST
START_TEST (test_rnode_find_edge) START_TEST (test_node_find_edge)
{ {
rnode * n = rnode_create(10); node * n = rtree_create(10);
rnode * child = rnode_create(3); node * child = rtree_create(3);
fail_if( rnode_add_child(n, strdup("/add") , child) == FALSE ); fail_if( rtree_add_child(n, strdup("/add") , child) == FALSE );
fail_if( rnode_find_edge(n, "/add") == NULL ); fail_if( node_find_edge(n, "/add") == NULL );
fail_if( rnode_find_edge(n, "/bar") != NULL ); fail_if( node_find_edge(n, "/bar") != NULL );
rnode_free(n); rtree_free(n);
} }
END_TEST END_TEST
@ -43,25 +43,25 @@ END_TEST
START_TEST (test_compile) START_TEST (test_compile)
{ {
token_array *t; token_array *t;
rnode * n; node * n;
n = rnode_create(10); n = rtree_create(10);
rnode_insert_routel(n, "/zoo", strlen("/zoo") ); rtree_insert_pathn(n, "/zoo", strlen("/zoo"), NULL);
rnode_insert_routel(n, "/foo", strlen("/foo") ); rtree_insert_pathn(n, "/foo", strlen("/foo"), NULL);
rnode_insert_routel(n, "/bar", strlen("/bar") ); rtree_insert_pathn(n, "/bar", strlen("/bar"), NULL);
rnode_compile(n); rtree_compile(n);
fail_if( n->combined_pattern ); fail_if( n->combined_pattern );
fail_if( NULL == rnode_find_edge_str(n, "/", strlen("/") ) ); fail_if( NULL == node_find_edge_str(n, "/", strlen("/") ) );
rnode_insert_routel(n, "/foo/{id}", strlen("/foo/{id}") ); rtree_insert_pathn(n, "/foo/{id}", strlen("/foo/{id}"), NULL);
rnode_insert_routel(n, "/{id}", strlen("/{id}") ); rtree_insert_pathn(n, "/{id}", strlen("/{id}"), NULL);
rnode_compile(n); rtree_compile(n);
rnode_dump(n, 0); rtree_dump(n, 0);
fail_if(n->edges[0]->child->combined_pattern == NULL); fail_if(n->edges[0]->child->combined_pattern == NULL);
redge *e = rnode_find_edge_str(n, "/", strlen("/") ); edge *e = node_find_edge_str(n, "/", strlen("/") );
fail_if( NULL == e ); fail_if( NULL == e );
/* /*
@ -70,26 +70,28 @@ START_TEST (test_compile)
printf( "%s\n", n->edges[0]->child->combined_pattern); printf( "%s\n", n->edges[0]->child->combined_pattern);
printf( "%s\n", n->combined_pattern ); printf( "%s\n", n->combined_pattern );
*/ */
rnode *m = rnode_match( e->child , "foo", strlen("foo") ); node *m = rtree_match( e->child , "foo", strlen("foo") );
fail_if( NULL == m ); fail_if( NULL == m );
m = rnode_match( n , "/foo", strlen("/foo") ); m = rtree_match( n , "/foo", strlen("/foo") );
fail_if( NULL == m ); fail_if( NULL == m );
m = rnode_match( n , "/zoo", strlen("/zoo") ); m = rtree_match( n , "/zoo", strlen("/zoo") );
fail_if( NULL == m ); fail_if( NULL == m );
m = rnode_match( n , "/bar", strlen("/bar") ); m = rtree_match( n , "/bar", strlen("/bar") );
fail_if( NULL == m ); fail_if( NULL == m );
m = rnode_match( n , "/xxx", strlen("/xxx") ); m = rtree_match( n , "/xxx", strlen("/xxx") );
fail_if( NULL == m ); fail_if( NULL == m );
m = rnode_match( n , "/foo/xxx", strlen("/foo/xxx") ); m = rtree_match( n , "/foo/xxx", strlen("/foo/xxx") );
fail_if( NULL == m ); fail_if( NULL == m );
m = rnode_match( n , "/not_found", strlen("/not_found") ); m = rtree_match( n , "/not_found", strlen("/not_found") );
fail_if( NULL == m ); // should be the node of "/" fail_if( NULL == m ); // should be the node of "/"
ck_assert_int_eq( m->endpoint , 0 ); // should not be an endpoint
} }
END_TEST END_TEST
@ -138,49 +140,49 @@ START_TEST (test_compile_slug)
END_TEST END_TEST
START_TEST (test_rnode_insert_routel) START_TEST (test_rtree_insert_pathn)
{ {
rnode * n = rnode_create(10); node * n = rtree_create(10);
// printf("Inserting /foo/bar\n"); // printf("Inserting /foo/bar\n");
rnode_insert_routel(n, "/foo/bar", strlen("/foo/bar") ); rtree_insert_pathn(n, "/foo/bar", strlen("/foo/bar"), NULL);
// rnode_dump(n, 0); // rtree_dump(n, 0);
// printf("Inserting /foo/zoo\n"); // printf("Inserting /foo/zoo\n");
rnode_insert_routel(n, "/foo/zoo", strlen("/foo/zoo") ); rtree_insert_pathn(n, "/foo/zoo", strlen("/foo/zoo"), NULL);
// rnode_dump(n, 0); // rtree_dump(n, 0);
// printf("Inserting /f/id\n"); // printf("Inserting /f/id\n");
rnode_insert_routel(n, "/f/id", strlen("/f/id") ); rtree_insert_pathn(n, "/f/id", strlen("/f/id") , NULL);
// rnode_dump(n, 0); // rtree_dump(n, 0);
// printf("Inserting /post/{id}\n"); // printf("Inserting /post/{id}\n");
rnode_insert_routel(n, "/post/{id}", strlen("/post/{id}") ); rtree_insert_pathn(n, "/post/{id}", strlen("/post/{id}"), NULL);
// rnode_dump(n, 0); // rtree_dump(n, 0);
// printf("Inserting /post/{handle}\n"); // printf("Inserting /post/{handle}\n");
rnode_insert_routel(n, "/post/{handle}", strlen("/post/{handle}") ); rtree_insert_pathn(n, "/post/{handle}", strlen("/post/{handle}"), NULL);
// rnode_dump(n, 0); // rtree_dump(n, 0);
// printf("Inserting /post/{handle}-{id}\n"); // printf("Inserting /post/{handle}-{id}\n");
rnode_insert_routel(n, "/post/{handle}-{id}", strlen("/post/{handle}-{id}") ); rtree_insert_pathn(n, "/post/{handle}-{id}", strlen("/post/{handle}-{id}"), NULL);
rnode_compile(n); rtree_compile(n);
// rnode_dump(n, 0); // rtree_dump(n, 0);
/* /*
fail_if( rnode_lookup(n , "/a/jj/kk" , strlen("/a/jj/kk") ) == NULL ); fail_if( rtree_lookup(n , "/a/jj/kk" , strlen("/a/jj/kk") ) == NULL );
fail_if( rnode_lookup(n , "/a/jj" , strlen("/a/jj") ) != NULL ); fail_if( rtree_lookup(n , "/a/jj" , strlen("/a/jj") ) != NULL );
fail_if( rnode_lookup(n , "/a/jj/kk/ll" , strlen("/a/jj/kk/ll") ) != NULL ); fail_if( rtree_lookup(n , "/a/jj/kk/ll" , strlen("/a/jj/kk/ll") ) != NULL );
fail_if( rnode_lookup(n, "/xxxx", strlen("xxxx") ) != NULL ); fail_if( rtree_lookup(n, "/xxxx", strlen("xxxx") ) != NULL );
*/ */
// fail_if( rnode_find_edge(n, "/add") == NULL ); // fail_if( node_find_edge(n, "/add") == NULL );
// fail_if( rnode_find_edge(n, "/bar") != NULL ); // fail_if( node_find_edge(n, "/bar") != NULL );
rnode_free(n); rtree_free(n);
} }
END_TEST END_TEST
@ -253,9 +255,9 @@ Suite* r3_suite (void) {
tcase_add_test(tcase, test_route_split); tcase_add_test(tcase, test_route_split);
tcase_add_test(tcase, test_token_array); tcase_add_test(tcase, test_token_array);
tcase_add_test(tcase, test_ltrim_slash); tcase_add_test(tcase, test_ltrim_slash);
tcase_add_test(tcase, test_rnode_construct_uniq); tcase_add_test(tcase, test_node_construct_uniq);
tcase_add_test(tcase, test_rnode_find_edge); tcase_add_test(tcase, test_node_find_edge);
tcase_add_test(tcase, test_rnode_insert_routel); tcase_add_test(tcase, test_rtree_insert_pathn);
tcase_add_test(tcase, test_compile_slug); tcase_add_test(tcase, test_compile_slug);
tcase_add_test(tcase, test_compile); tcase_add_test(tcase, test_compile);