Merge pull request #48 from c9s/slug-parser

slug fix and tests
This commit is contained in:
Pedro 2014-06-02 10:21:48 +08:00
commit 33e16138bd
11 changed files with 2584 additions and 83 deletions

76
gen_route_tests.rb Normal file
View file

@ -0,0 +1,76 @@
#!/usr/bin/env ruby
puts <<END
/** DO NOT MODIFY THIS FILE, THIS TEST FILE IS AUTO-GENERATED. **/
#include "config.h"
#include <stdio.h>
#include <check.h>
#include <stdlib.h>
#include <assert.h>
#include "r3.h"
#include "r3_str.h"
#include "zmalloc.h"
START_TEST (test_routes)
{
node * n = r3_tree_create(10);
node * m = NULL;
END
arr = ["foo", "bar", "baz", "qux", "quux", "corge", "grault", "garply"]
paths = arr.permutation(3).map { |a| "/#{a.join '/'}" }
paths.each_index do |idx|
path = paths.fetch(idx)
puts " char *data#{idx} = \"#{path}\";"
puts " r3_tree_insert_path(n, \"#{path}\", (void*) data#{idx});"
end
puts <<END
char *err = NULL;
r3_tree_compile(n, &err);
ck_assert(err == NULL);
END
paths.each_index do |idx|
path = paths.fetch(idx)
puts " m = r3_tree_match(n, \"#{path}\", NULL);"
puts " ck_assert(m != NULL);"
puts " ck_assert(m->data == data#{idx});"
puts " ck_assert(m->endpoint > 0);"
end
puts <<END
r3_tree_free(n);
}
END_TEST
Suite* r3_suite (void) {
Suite *suite = suite_create("r3 routes tests");
TCase *tcase = tcase_create("testcase");
tcase_add_test(tcase, test_routes);
suite_add_tcase(suite, tcase);
return suite;
}
int main (int argc, char *argv[]) {
int number_failed;
Suite *suite = r3_suite();
SRunner *runner = srunner_create(suite);
srunner_run_all(runner, CK_NORMAL);
number_failed = srunner_ntests_failed(runner);
srunner_free(runner);
return number_failed;
}
END

View file

@ -109,6 +109,8 @@ edge * r3_node_find_edge(const node * n, const char * pat, int pat_len);
void r3_node_append_edge(node *n, edge *child); void r3_node_append_edge(node *n, edge *child);
edge * r3_node_find_common_prefix(node *n, char *path, int path_len, int *prefix_len);
node * r3_tree_insert_pathl(node *tree, const char *path, int path_len, void * data); node * r3_tree_insert_pathl(node *tree, const char *path, int path_len, void * data);
route * r3_tree_insert_routel(node *tree, int method, const char *path, int path_len, void *data); route * r3_tree_insert_routel(node *tree, int method, const char *path, int path_len, void *data);
@ -142,7 +144,7 @@ node * r3_tree_matchl(const node * n, const char * path, int path_len, match_ent
bool r3_node_has_slug_edges(const node *n); bool r3_node_has_slug_edges(const node *n);
edge * r3_edge_create(const char * pattern, int pattern_len, node * child); edge * r3_edge_createl(const char * pattern, int pattern_len, node * child);
node * r3_edge_branch(edge *e, int dl); node * r3_edge_branch(edge *e, int dl);

View file

@ -24,7 +24,7 @@
#include "slug.h" #include "slug.h"
#include "zmalloc.h" #include "zmalloc.h"
edge * r3_edge_create(const char * pattern, int pattern_len, node * child) { edge * r3_edge_createl(const char * pattern, int pattern_len, node * child) {
edge * e = (edge*) zmalloc( sizeof(edge) ); edge * e = (edge*) zmalloc( sizeof(edge) );
e->pattern = (char*) pattern; e->pattern = (char*) pattern;
e->pattern_len = pattern_len; e->pattern_len = pattern_len;
@ -54,7 +54,7 @@ node * r3_edge_branch(edge *e, int dl) {
// the suffix edge of the leaf // the suffix edge of the leaf
new_child = r3_tree_create(3); new_child = r3_tree_create(3);
s1_len = e->pattern_len - dl; s1_len = e->pattern_len - dl;
e1 = r3_edge_create(zstrndup(s1, s1_len), s1_len, new_child); e1 = r3_edge_createl(zstrndup(s1, s1_len), s1_len, new_child);
// 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 < e->child->edge_len ; i++ ) { for ( int i = 0 ; i < e->child->edge_len ; i++ ) {

View file

@ -82,6 +82,11 @@ void r3_tree_free(node * tree) {
tree = NULL; tree = NULL;
} }
/**
* Connect two node objects, and create an edge object between them.
*/
edge * r3_node_connectl(node * n, const char * pat, int len, int dupl, node *child) { edge * r3_node_connectl(node * n, const char * pat, int len, int dupl, 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
edge * e; edge * e;
@ -94,7 +99,7 @@ edge * r3_node_connectl(node * n, const char * pat, int len, int dupl, node *chi
if (dupl) { if (dupl) {
pat = zstrndup(pat, len); pat = zstrndup(pat, len);
} }
e = r3_edge_create(pat, len, child); e = r3_edge_createl(pat, len, child);
CHECK_PTR(e); CHECK_PTR(e);
r3_node_append_edge(n, e); r3_node_append_edge(n, e);
return e; return e;
@ -118,10 +123,13 @@ void r3_node_append_edge(node *n, edge *e) {
/** /**
* Find the existing edge with specified pattern (include slug) * Find the existing edge with specified pattern (include slug)
*
* if "pat" is a slug, we should compare with the specified pattern.
*/ */
edge * r3_node_find_edge(const node * n, const char * pat, int pat_len) { edge * r3_node_find_edge(const node * n, const char * pat, int pat_len) {
edge * e; edge * e;
for (int i = 0 ; i < n->edge_len ; i++ ) { int i;
for (i = 0 ; i < n->edge_len ; i++ ) {
e = n->edges[i]; e = n->edges[i];
// there is a case: "{foo}" vs "{foo:xxx}", // there is a case: "{foo}" vs "{foo:xxx}",
@ -473,36 +481,89 @@ node * r3_tree_insert_pathl(node *tree, const char *path, int path_len, void * d
/** /**
* Return the last inserted node. * Find common prefix from the edges of the node.
*
* Some cases of the common prefix:
*
* 1. "/foo/{slug}" vs "/foo/bar" => common prefix = "/foo/"
* 2. "{slug}/hate" vs "{slug}/bar" => common prefix = "{slug}/"
* 2. "/z/{slug}/hate" vs "/z/{slog}/bar" => common prefix = "/z/"
* 3. "{slug:xxx}/hate" vs "{slug:yyy}/bar" => common prefix = ""
* 4. "aaa{slug:xxx}/hate" vs "aab{slug:yyy}/bar" => common prefix = "aa"
* 5. "/foo/{slug}/hate" vs "/fo{slug}/bar" => common prefix = "/fo"
*/ */
node * r3_tree_insert_pathl_(node *tree, const char *path, int path_len, route * route, void * data, char **errstr) edge * r3_node_find_common_prefix(node *n, char *path, int path_len, int *prefix_len) {
{ int i = 0;
node * n = tree; int prefix = 0;
edge *e = NULL; edge *e = NULL;
for(i = 0 ; i < n->edge_len ; i++ ) {
/* length of common prefix */
int prefix_len = 0;
for( int i = 0 ; i < n->edge_len ; i++ ) {
// ignore all edges with slug // ignore all edges with slug
if (n->edges[i]->has_slug) prefix = strndiff( (char*) path, n->edges[i]->pattern, n->edges[i]->pattern_len);
continue;
prefix_len = strndiff( (char*) path, n->edges[i]->pattern, n->edges[i]->pattern_len);
// printf("prefix_len: %d %s vs %s\n", prefix_len, path, n->edges[i]->pattern );
// no common, consider insert a new edge // no common, consider insert a new edge
if ( prefix_len > 0 ) { if ( prefix > 0 ) {
e = n->edges[i]; e = n->edges[i];
break; break;
} }
} }
// branch the edge at correct position (avoid broken slugs) // found common prefix edge
const char *slug_s; if (prefix > 0) {
if ( (slug_s = inside_slug(path, path_len, ((char*) path + prefix_len), NULL)) != NULL ) { r3_slug_t *slug;
prefix_len = slug_s - path; int ret = 0;
char *p = NULL;
char *offset = NULL;
offset = path;
p = path + prefix;
slug = r3_slug_new(path, path_len);
do {
ret = r3_slug_parse(slug, path, path_len, offset, NULL);
// found slug
if (ret == 1) {
// inside slug, backtrace to the begin of the slug
if ( p >= slug->begin && p <= slug->end ) {
prefix = slug->begin - path - 1;
break;
} else if ( p < slug->begin ) {
break;
} else if ( p >= slug->end && p < (path + path_len) ) {
offset = slug->end + 1;
prefix = p - path;
continue;
} else {
break;
} }
} else {
break;
}
} while(ret == 1);
}
*prefix_len = prefix;
return e;
}
/**
* Return the last inserted node.
*/
node * r3_tree_insert_pathl_(node *tree, const char *path, int path_len, route * route, void * data, char **errstr)
{
node * n = tree;
// common edge
edge * e = NULL;
/* length of common prefix */
int prefix_len = 0;
e = r3_node_find_common_prefix(tree, path, path_len, &prefix_len);
const char * subpath = path + prefix_len; const char * subpath = path + prefix_len;
const int subpath_len = path_len - prefix_len; const int subpath_len = path_len - prefix_len;

View file

@ -72,19 +72,17 @@ r3_slug_t * r3_slug_parse_next(r3_slug_t *s, char **errstr) {
} }
*/ */
r3_slug_t * r3_slug_parse(char *needle, int needle_len, char *offset, char **errstr) { int r3_slug_parse(r3_slug_t *s, char *needle, int needle_len, char *offset, char **errstr) {
r3_slug_t * s = r3_slug_new(needle, needle_len); s->path = needle;
if (!s) { s->path_len = needle_len;
return NULL;
}
if (!offset) { if (offset == NULL) {
offset = (char*) needle; // from the begining of the needle offset = (char*) needle; // from the begining of the needle
} }
// there is no slug // there is no slug
if (!r3_path_contains_slug_char(offset)) { if (!r3_path_contains_slug_char(offset)) {
return NULL; return 0;
} }
int cnt = 0; int cnt = 0;
@ -92,7 +90,6 @@ r3_slug_t * r3_slug_parse(char *needle, int needle_len, char *offset, char **err
char * p = offset; char * p = offset;
while( (p-needle) < needle_len) { while( (p-needle) < needle_len) {
// escape one character // escape one character
if (*p == '\\' ) { if (*p == '\\' ) {
p++; p++; p++; p++;
@ -135,13 +132,14 @@ r3_slug_t * r3_slug_parse(char *needle, int needle_len, char *offset, char **err
p++; p++;
}; };
if (state > 0) { if (state != 0) {
if (errstr) { if (errstr) {
asprintf(errstr, "Incomplete slug pattern. PATH (%d): '%s', OFFSET: %ld, STATE: %d", needle_len, needle, p - needle, state); asprintf(errstr, "Incomplete slug pattern. PATH (%d): '%s', OFFSET: %ld, STATE: %d", needle_len, needle, p - needle, state);
} }
return NULL; return -1;
} }
return s; info("found slug\n");
return 1;
} }

View file

@ -43,7 +43,7 @@ r3_slug_t * r3_slug_new(char * path, int path_len);
int r3_slug_check(r3_slug_t *s); int r3_slug_check(r3_slug_t *s);
r3_slug_t * r3_slug_parse(char *needle, int needle_len, char *offset, char **errstr); int r3_slug_parse(r3_slug_t *s, char *needle, int needle_len, char *offset, char **errstr);
char * r3_slug_to_str(const r3_slug_t *s); char * r3_slug_to_str(const r3_slug_t *s);

View file

@ -21,6 +21,12 @@ check_slug_SOURCES = check_slug.c
TESTS += check_tree TESTS += check_tree
check_tree_SOURCES = check_tree.c check_tree_SOURCES = check_tree.c
TESTS += check_routes
check_routes_SOURCES = check_routes.c
TESTS += check_str_array
check_str_array_SOURCES = check_str_array.c
if ENABLE_JSON if ENABLE_JSON
TESTS += check_json TESTS += check_json

2062
tests/check_routes.c Normal file

File diff suppressed because it is too large Load diff

View file

@ -96,19 +96,20 @@ START_TEST (test_incomplete_slug)
END_TEST END_TEST
/*
START_TEST (test_slug_parse_with_pattern) START_TEST (test_slug_parse_with_pattern)
{ {
char * pattern = "/user/{name:\\d{3}}"; char * pattern = "/user/{name:\\d{3}}";
char * errstr = NULL; char * errstr = NULL;
r3_slug_t *s = r3_slug_parse(pattern, strlen(pattern), pattern, &errstr); r3_slug_t s;
ck_assert(s); int ret;
ret = r3_slug_parse(&s, pattern, strlen(pattern), pattern, &errstr);
ck_assert(ret);
char * out = r3_slug_to_str(s); char * out = r3_slug_to_str(&s);
ck_assert(out); ck_assert(out);
printf("%s\n",out); printf("%s\n",out);
free(out); free(out);
r3_slug_free(s);
} }
END_TEST END_TEST
@ -117,7 +118,9 @@ START_TEST (test_slug_parse_without_pattern)
{ {
char * pattern = "/user/{name}"; char * pattern = "/user/{name}";
char * errstr = NULL; char * errstr = NULL;
r3_slug_t *s = r3_slug_parse(pattern, strlen(pattern), pattern, &errstr); r3_slug_t *s = r3_slug_new(pattern, strlen(pattern));
int ret;
ret = r3_slug_parse(s, pattern, strlen(pattern), pattern, &errstr);
ck_assert(s); ck_assert(s);
char * out = r3_slug_to_str(s); char * out = r3_slug_to_str(s);
@ -128,6 +131,7 @@ START_TEST (test_slug_parse_without_pattern)
r3_slug_free(s); r3_slug_free(s);
} }
END_TEST END_TEST
*/
@ -177,8 +181,8 @@ Suite* r3_suite (void) {
tcase_add_test(tcase, test_slug_compile); tcase_add_test(tcase, test_slug_compile);
tcase_add_test(tcase, test_pattern_to_opcode); tcase_add_test(tcase, test_pattern_to_opcode);
tcase_add_test(tcase, test_incomplete_slug); tcase_add_test(tcase, test_incomplete_slug);
tcase_add_test(tcase, test_slug_parse_with_pattern); // tcase_add_test(tcase, test_slug_parse_with_pattern);
tcase_add_test(tcase, test_slug_parse_without_pattern); // tcase_add_test(tcase, test_slug_parse_without_pattern);
suite_add_tcase(suite, tcase); suite_add_tcase(suite, tcase);
return suite; return suite;

53
tests/check_str_array.c Normal file
View file

@ -0,0 +1,53 @@
/*
* check_str_array.c
* Copyright (C) 2014 c9s <c9s@c9smba.local>
*
* Distributed under terms of the MIT license.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <check.h>
#include "r3.h"
#include "r3_str.h"
#include "zmalloc.h"
START_TEST (test_str_array)
{
str_array * l = str_array_create(3);
ck_assert(l);
ck_assert(str_array_append(l, zstrdup("abc")));
ck_assert( l->len == 1 );
ck_assert(str_array_append(l, zstrdup("foo") ));
ck_assert( l->len == 2 );
ck_assert( str_array_append(l, zstrdup("bar") ) );
ck_assert( l->len == 3 );
ck_assert( str_array_append(l, zstrdup("zoo") ) );
ck_assert( l->len == 4 );
ck_assert( str_array_resize(l, l->cap * 2) );
str_array_free(l);
}
END_TEST
Suite* r3_suite (void) {
Suite *suite = suite_create("str_array test");
TCase *tcase = tcase_create("testcase");
tcase_add_test(tcase, test_str_array);
suite_add_tcase(suite, tcase);
return suite;
}
int main (int argc, char *argv[]) {
int number_failed;
Suite *suite = r3_suite();
SRunner *runner = srunner_create(suite);
srunner_run_all(runner, CK_NORMAL);
number_failed = srunner_ntests_failed(runner);
srunner_free(runner);
return number_failed;
}

View file

@ -10,6 +10,198 @@
START_TEST (test_find_common_prefix)
{
node * n = r3_tree_create(10);
edge * e = r3_edge_createl(zstrdup("/foo/{slug}"), sizeof("/foo/{slug}")-1, NULL);
r3_node_append_edge(n,e);
int prefix_len;
edge *ret_edge = NULL;
prefix_len = 0;
ret_edge = r3_node_find_common_prefix(n, "/foo", sizeof("/foo")-1, &prefix_len);
ck_assert(ret_edge);
ck_assert_int_eq(prefix_len, 4);
prefix_len = 0;
ret_edge = r3_node_find_common_prefix(n, "/foo/", sizeof("/foo/")-1, &prefix_len);
ck_assert(ret_edge);
ck_assert_int_eq(prefix_len, 5);
prefix_len = 0;
ret_edge = r3_node_find_common_prefix(n, "/foo/{slog}", sizeof("/foo/{slog}")-1, &prefix_len);
ck_assert(ret_edge);
ck_assert_int_eq(prefix_len, 5);
prefix_len = 0;
ret_edge = r3_node_find_common_prefix(n, "/foo/{bar}", sizeof("/foo/{bar}")-1, &prefix_len);
ck_assert(ret_edge);
ck_assert_int_eq(prefix_len, 5);
prefix_len = 0;
ret_edge = r3_node_find_common_prefix(n, "/foo/bar", sizeof("/foo/bar")-1, &prefix_len);
ck_assert(ret_edge);
ck_assert_int_eq(prefix_len, 5);
prefix_len = 0;
ret_edge = r3_node_find_common_prefix(n, "/bar/", sizeof("/bar/")-1, &prefix_len);
ck_assert(ret_edge);
ck_assert_int_eq(prefix_len, 1);
prefix_len = 0;
ret_edge = r3_node_find_common_prefix(n, "{bar}", sizeof("{bar}")-1, &prefix_len);
ck_assert(!ret_edge);
ck_assert_int_eq(prefix_len, 0);
r3_tree_free(n);
}
END_TEST
START_TEST (test_find_common_prefix_after)
{
node * n = r3_tree_create(10);
edge * e = r3_edge_createl(zstrdup("{slug}/foo"), sizeof("{slug}/foo")-1, NULL);
r3_node_append_edge(n,e);
int prefix_len;
edge *ret_edge = NULL;
prefix_len = 0;
ret_edge = r3_node_find_common_prefix(n, "/foo", sizeof("/foo")-1, &prefix_len);
ck_assert(ret_edge == NULL);
ck_assert_int_eq(prefix_len, 0);
prefix_len = 0;
ret_edge = r3_node_find_common_prefix(n, "{slug}/bar", sizeof("{slug}/bar")-1, &prefix_len);
ck_assert(ret_edge);
ck_assert_int_eq(prefix_len, 7);
prefix_len = 0;
ret_edge = r3_node_find_common_prefix(n, "{slug}/foo", sizeof("{slug}/foo")-1, &prefix_len);
ck_assert(ret_edge);
ck_assert_int_eq(prefix_len, 10);
r3_tree_free(n);
}
END_TEST
START_TEST (test_find_common_prefix_double_middle)
{
node * n = r3_tree_create(10);
edge * e = r3_edge_createl(zstrdup("{slug}/foo/{name}"), sizeof("{slug}/foo/{name}")-1, NULL);
r3_node_append_edge(n,e);
int prefix_len;
edge *ret_edge = NULL;
prefix_len = 0;
ret_edge = r3_node_find_common_prefix(n, "{slug}/foo/{number}", sizeof("{slug}/foo/{number}")-1, &prefix_len);
ck_assert(ret_edge);
ck_assert_int_eq(prefix_len, 11);
r3_tree_free(n);
}
END_TEST
START_TEST (test_find_common_prefix_middle)
{
node * n = r3_tree_create(10);
edge * e = r3_edge_createl(zstrdup("/foo/{slug}/hate"), sizeof("/foo/{slug}/hate")-1, NULL);
r3_node_append_edge(n,e);
int prefix_len;
edge *ret_edge = NULL;
prefix_len = 0;
ret_edge = r3_node_find_common_prefix(n, "/foo/{slug}/bar", sizeof("/foo/{slug}/bar")-1, &prefix_len);
ck_assert(ret_edge);
ck_assert_int_eq(prefix_len, 12);
prefix_len = 0;
ret_edge = r3_node_find_common_prefix(n, "/fo/{slug}/bar", sizeof("/fo/{slug}/bar")-1, &prefix_len);
ck_assert(ret_edge);
ck_assert_int_eq(prefix_len, 3);
r3_tree_free(n);
}
END_TEST
START_TEST (test_find_common_prefix_same_pattern)
{
node * n = r3_tree_create(10);
edge * e = r3_edge_createl(zstrdup("/foo/{slug:xxx}/hate"), sizeof("/foo/{slug:xxx}/hate")-1, NULL);
r3_node_append_edge(n,e);
int prefix_len;
edge *ret_edge = NULL;
prefix_len = 0;
ret_edge = r3_node_find_common_prefix(n, "/foo/{slug:yyy}/hate", sizeof("/foo/{slug:yyy}/hate")-1, &prefix_len);
ck_assert(ret_edge);
ck_assert_int_eq(prefix_len, 5);
prefix_len = 0;
ret_edge = r3_node_find_common_prefix(n, "/foo/{slug}/hate", sizeof("/foo/{slug}/hate")-1, &prefix_len);
ck_assert(ret_edge);
ck_assert_int_eq(prefix_len, 5);
r3_tree_free(n);
}
END_TEST
START_TEST (test_find_common_prefix_same_pattern2)
{
node * n = r3_tree_create(10);
edge * e = r3_edge_createl(zstrdup("{slug:xxx}/hate"), sizeof("{slug:xxx}/hate")-1, NULL);
r3_node_append_edge(n,e);
int prefix_len;
edge *ret_edge = NULL;
prefix_len = 0;
ret_edge = r3_node_find_common_prefix(n, "{slug:yyy}/hate", sizeof("{slug:yyy}/hate")-1, &prefix_len);
ck_assert(ret_edge);
ck_assert_int_eq(prefix_len, 0);
r3_tree_free(n);
}
END_TEST
START_TEST (test_ltrim_slash) START_TEST (test_ltrim_slash)
{ {
fail_if( strcmp( ltrim_slash("/blog") , "blog" ) != 0 ); fail_if( strcmp( ltrim_slash("/blog") , "blog" ) != 0 );
@ -17,7 +209,7 @@ START_TEST (test_ltrim_slash)
} }
END_TEST END_TEST
START_TEST (test_r3_node_construct_and_free) START_TEST (test_node_construct_and_free)
{ {
node * n = r3_tree_create(10); node * n = r3_tree_create(10);
node * another_tree = r3_tree_create(3); node * another_tree = r3_tree_create(3);
@ -112,12 +304,17 @@ START_TEST (test_incomplete_slug_path)
r3_tree_insert_path(n, "/post/{handle:\\d{3}}/{a}", NULL); r3_tree_insert_path(n, "/post/{handle:\\d{3}}/{a}", NULL);
ret_node = r3_tree_insert_path(n, "/users/{idx:\\d{3}}/{idy}", NULL); ret_node = r3_tree_insert_path(n, "/users/{idx:\\d{3}}/{idy}", NULL);
assert(ret_node); ck_assert(ret_node);
// OK to insert, but should return error when compiling patterns // OK to insert, but should return error when compiling patterns
// FIXME: this one returns the inserted node object. node * ret_node2 = r3_tree_insert_path(n, "/users/{idx:\\d{3}}/{idy:aaa}", NULL);
ret_node = r3_tree_insert_path(n, "/users/{idx:\\d{3}}/{idy:aaa}", NULL); ck_assert(ret_node2);
assert(ret_node); ck_assert(ret_node2 != ret_node); // make sure it's another node
char *errstr = NULL;
r3_tree_compile(n, &errstr);
ck_assert(errstr == NULL); // no error
r3_tree_dump(n, NULL); r3_tree_dump(n, NULL);
@ -243,32 +440,73 @@ END_TEST
START_TEST (testr3_tree_insert_pathl) START_TEST (test_insert_pathl)
{ {
node * n = r3_tree_create(10); node * n = r3_tree_create(10);
r3_tree_insert_path(n, "/foo/bar", NULL); node * ret;
// r3_tree_dump(n, 0);
r3_tree_insert_path(n, "/foo/zoo", NULL); ret = r3_tree_insert_path(n, "/foo/bar", NULL);
// r3_tree_dump(n, 0); ck_assert(ret);
ret = r3_tree_insert_path(n, "/foo/zoo", NULL);
ck_assert(ret);
ret = r3_tree_insert_path(n, "/foo/{id}", NULL);
ck_assert(ret);
ret = r3_tree_insert_path(n, "/foo/{number:\\d+}", NULL);
ck_assert(ret);
ret = r3_tree_insert_path(n, "/foo/{name:\\w+}", NULL);
ck_assert(ret);
ret = r3_tree_insert_path(n, "/foo/{name:\\d+}", NULL);
ck_assert(ret);
r3_tree_insert_path(n, "/f/id" , NULL); ret = r3_tree_insert_path(n, "/foo/{name:\\d{5}}", NULL);
// r3_tree_dump(n, 0); ck_assert(ret);
r3_tree_insert_path(n, "/post/{id}", NULL); ret = r3_tree_insert_path(n, "/foo/{idx}/{idy}", NULL);
// r3_tree_dump(n, 0); ck_assert(ret);
r3_tree_insert_path(n, "/post/{handle}", NULL); ret = r3_tree_insert_path(n, "/foo/{idx}/{idh}", NULL);
ck_assert(ret);
r3_tree_insert_path(n, "/post/{handle}-{id}", NULL); ret = r3_tree_insert_path(n, "/f/id" , NULL);
ck_assert(ret);
ret = r3_tree_insert_path(n, "/post/{id}", NULL);
ck_assert(ret);
ret = r3_tree_insert_path(n, "/post/{handle}", NULL);
ck_assert(ret);
ret = r3_tree_insert_path(n, "/post/{handle}-{id}", NULL);
ck_assert(ret);
char * errstr = NULL; char * errstr = NULL;
r3_tree_compile(n, &errstr); r3_tree_compile(n, &errstr);
ck_assert(errstr == NULL);
r3_tree_dump(n, 0);
r3_tree_free(n);
}
END_TEST
START_TEST (test_compile_fail)
{
node * n = r3_tree_create(10);
node * ret;
ret = r3_tree_insert_path(n, "/foo/{idx}/{idy:)}", NULL);
ck_assert(ret);
ret = r3_tree_insert_path(n, "/foo/{idx}/{idh:(}", NULL);
ck_assert(ret);
char * errstr = NULL;
r3_tree_compile(n, &errstr);
ck_assert(errstr);
fprintf(stderr, "Compile failed: %s\n", errstr);
free(errstr);
#ifdef DEBUG
r3_tree_dump(n, 0); r3_tree_dump(n, 0);
#endif
r3_tree_free(n); r3_tree_free(n);
} }
END_TEST END_TEST
@ -277,28 +515,23 @@ END_TEST
START_TEST (test_str_array)
{
str_array * l = str_array_create(3);
ck_assert(l);
ck_assert(str_array_append(l, zstrdup("abc")));
ck_assert( l->len == 1 );
ck_assert(str_array_append(l, zstrdup("foo") ));
ck_assert( l->len == 2 );
fail_if( FALSE == str_array_append(l, zstrdup("bar") ) );
fail_if( l->len != 3 );
fail_if( FALSE == str_array_append(l, zstrdup("zoo") ) );
fail_if( l->len != 4 );
fail_if( FALSE == str_array_resize(l, l->cap * 2) );
str_array_free(l);
}
END_TEST
START_TEST(test_route_cmp) START_TEST(test_route_cmp)
{ {
@ -421,11 +654,18 @@ Suite* r3_suite (void) {
Suite *suite = suite_create("r3 core tests"); Suite *suite = suite_create("r3 core tests");
TCase *tcase = tcase_create("testcase"); TCase *tcase = tcase_create("testcase");
tcase_add_test(tcase, test_r3_node_construct_and_free);
tcase_add_test(tcase, test_str_array); tcase_add_test(tcase, test_find_common_prefix);
tcase_add_test(tcase, test_find_common_prefix_after);
tcase_add_test(tcase, test_find_common_prefix_double_middle);
tcase_add_test(tcase, test_find_common_prefix_middle);
tcase_add_test(tcase, test_find_common_prefix_same_pattern);
tcase_add_test(tcase, test_find_common_prefix_same_pattern2);
tcase_add_test(tcase, test_insert_pathl);
tcase_add_test(tcase, test_node_construct_and_free);
tcase_add_test(tcase, test_ltrim_slash); tcase_add_test(tcase, test_ltrim_slash);
tcase_add_test(tcase, testr3_tree_insert_pathl);
tcase_add_test(tcase, test_compile); tcase_add_test(tcase, test_compile);
tcase_add_test(tcase, test_compile_fail);
tcase_add_test(tcase, test_route_cmp); tcase_add_test(tcase, test_route_cmp);
tcase_add_test(tcase, test_insert_route); tcase_add_test(tcase, test_insert_route);
tcase_add_test(tcase, test_pcre_pattern_simple); tcase_add_test(tcase, test_pcre_pattern_simple);
@ -434,7 +674,6 @@ Suite* r3_suite (void) {
tcase_add_test(tcase, test_pcre_patterns_insert_2); tcase_add_test(tcase, test_pcre_patterns_insert_2);
tcase_add_test(tcase, test_pcre_patterns_insert_3); tcase_add_test(tcase, test_pcre_patterns_insert_3);
tcase_add_test(tcase, test_incomplete_slug_path); tcase_add_test(tcase, test_incomplete_slug_path);
suite_add_tcase(suite, tcase); suite_add_tcase(suite, tcase);
return suite; return suite;