END_TEST START_TEST (test_node_construct_and_free) { node * n = r3_tree_create(10); node * another_tree = r3_tree_create(3); r3_tree_free(n); r3_tree_free(another_tree); }
END_TEST START_TEST (test_r3_tree_insert_pathl) { node * n = r3_tree_create(10); r3_tree_insert_path(n, "/foo/bar", NULL); // r3_tree_dump(n, 0); r3_tree_insert_path(n, "/foo/zoo", NULL); // r3_tree_dump(n, 0); r3_tree_insert_path(n, "/f/id" , NULL); // r3_tree_dump(n, 0); r3_tree_insert_path(n, "/post/{id}", NULL); // r3_tree_dump(n, 0); r3_tree_insert_path(n, "/post/{handle}", NULL); r3_tree_insert_path(n, "/post/{handle}-{id}", NULL); r3_tree_compile(n); #ifdef DEBUG r3_tree_dump(n, 0); #endif 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; char *errstr = NULL; errstr = NULL; ret_edge = r3_node_find_common_prefix(n, "/foo/{slug}/bar", sizeof("/foo/{slug}/bar")-1, &prefix_len, &errstr); ck_assert(ret_edge); ck_assert_int_eq(prefix_len, 12); SAFE_FREE(errstr); errstr = NULL; ret_edge = r3_node_find_common_prefix(n, "/fo/{slug}/bar", sizeof("/fo/{slug}/bar")-1, &prefix_len, &errstr); ck_assert(ret_edge); ck_assert_int_eq(prefix_len, 3); SAFE_FREE(errstr); r3_tree_free(n); }
END_TEST START_TEST (test_pcre_patterns_insert) { node * n = r3_tree_create(10); // r3_tree_insert_path(n, "/foo-{user}-{id}", NULL, NULL); // r3_tree_dump(n, 0); r3_tree_insert_path(n, "/post/{handle:\\d+}-{id:\\d+}", NULL); r3_tree_compile(n); r3_tree_dump(n, 0); node *matched; matched = r3_tree_matchl(n, "/post/111-222", strlen("/post/111-222"), NULL); ck_assert(matched); ck_assert_int_gt(matched->endpoint, 0); // incomplete string shouldn't match matched = r3_tree_matchl(n, "/post/111-", strlen("/post/111-"), NULL); ck_assert(matched); ck_assert_int_eq(matched->endpoint, 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); r3_tree_dump(n, 0); r3_tree_free(n); }
void r3_edge_free(edge * e) { zfree(e->pattern); if ( e->child ) { r3_tree_free(e->child); } // free itself zfree(e); }
void r3_edge_free(R3Edge * e) { if (e) { if ( e->child ) { r3_tree_free(e->child); } // free itself // zfree(e); } }
END_TEST START_TEST (test_r3_node_construct_uniq) { node * n = r3_tree_create(10); node * child = r3_tree_create(3); // fail_if( r3_node_add_child(n, strdup("/add") , child) != NULL ); // fail_if( r3_node_add_child(n, strdup("/add") , child) != NULL ); r3_tree_free(n); }
END_TEST START_TEST(test_pcre_pattern_more) { match_entry * entry; entry = match_entry_createl( "/user/123" , strlen("/user/123") ); node * n = r3_tree_create(10); int var0 = 5; int var1 = 100; int var2 = 200; int var3 = 300; info("var0: %p\n", &var0); info("var1: %p\n", &var1); info("var2: %p\n", &var2); info("var3: %p\n", &var3); r3_tree_insert_path(n, "/user/{id:\\d+}", &var1); r3_tree_insert_path(n, "/user2/{id:\\d+}", &var2); r3_tree_insert_path(n, "/user3/{id:\\d{3}}", &var3); r3_tree_insert_path(n, "/user", &var0); r3_tree_compile(n, NULL); r3_tree_dump(n, 0); node *matched; matched = r3_tree_matchl(n, "/user/123", strlen("/user/123"), entry); ck_assert(matched); ck_assert(entry->vars->len > 0); ck_assert_str_eq(entry->vars->tokens[0],"123"); info("matched %p\n", matched->data); info("matched %p\n", matched->data); ck_assert_int_eq( *((int*) matched->data), var1); matched = r3_tree_matchl(n, "/user2/123", strlen("/user2/123"), entry); ck_assert(matched); ck_assert(entry->vars->len > 0); ck_assert_str_eq(entry->vars->tokens[0],"123"); ck_assert_int_eq( *((int*)matched->data), var2); matched = r3_tree_matchl(n, "/user3/123", strlen("/user3/123"), entry); ck_assert(matched); ck_assert(entry->vars->len > 0); ck_assert_str_eq(entry->vars->tokens[0],"123"); ck_assert_int_eq( *((int*)matched->data), var3); r3_tree_free(n); }
END_TEST START_TEST (test_insert_pathl) { node * n = r3_tree_create(10); node * ret; ret = r3_tree_insert_path(n, "/foo/bar", NULL); 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); ret = r3_tree_insert_path(n, "/foo/{name:\\d{5}}", NULL); ck_assert(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); 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; r3_tree_compile(n, &errstr); ck_assert(errstr == NULL); r3_tree_dump(n, 0); r3_tree_free(n); }
END_TEST START_TEST (test_pcre_patterns_insert) { node * n = r3_tree_create(10); // r3_tree_insert_path(n, "/foo-{user}-{id}", NULL, NULL); r3_tree_insert_path(n, "/post/{handle:\\d+}-{id:\\d+}", NULL); r3_tree_insert_path(n, "/post/foo", NULL); r3_tree_insert_path(n, "/post/bar", NULL); char *errstr = NULL; int errcode; errcode = r3_tree_compile(n, &errstr); ck_assert(errcode == 0); // no error // r3_tree_dump(n, 0); node *matched; matched = r3_tree_match(n, "/post/foo", NULL); ck_assert(matched); ck_assert(matched->endpoint > 0); matched = r3_tree_match(n, "/post/bar", NULL); ck_assert(matched); ck_assert(matched->endpoint > 0); matched = r3_tree_match(n, "/post/kkkfoo", NULL); ck_assert(!matched); matched = r3_tree_match(n, "/post/kkkbar", NULL); ck_assert(!matched); matched = r3_tree_matchl(n, "/post/111-222", strlen("/post/111-222"), NULL); ck_assert(matched); ck_assert(matched->endpoint > 0); // incomplete string shouldn't match matched = r3_tree_matchl(n, "/post/111-", strlen("/post/111-"), NULL); ck_assert(! matched); r3_tree_free(n); }
END_TEST START_TEST (test_r3_node_find_edge) { node * n = r3_tree_create(10); node * child = r3_tree_create(3); fail_if( r3_node_add_child(n, strdup("/add") , child) == FALSE ); fail_if( r3_node_find_edge(n, "/add") == NULL ); fail_if( r3_node_find_edge(n, "/bar") != NULL ); r3_tree_free(n); }
END_TEST START_TEST (test_incomplete_slug_path) { node * n = r3_tree_create(10); node * ret_node; // r3_tree_insert_path(n, "/foo-{user}-{id}", NULL, NULL); ret_node = r3_tree_insert_path(n, "/post/{handle", NULL); assert(!ret_node); ret_node = r3_tree_insert_path(n, "/post/{handle:\\", NULL); assert(!ret_node); ret_node = r3_tree_insert_path(n, "/post/{handle:\\d", NULL); assert(!ret_node); ret_node = r3_tree_insert_path(n, "/post/{handle:\\d{", NULL); assert(!ret_node); ret_node = r3_tree_insert_path(n, "/post/{handle:\\d{3", NULL); assert(!ret_node); r3_tree_insert_path(n, "/post/{handle:\\d{3}", NULL); r3_tree_insert_path(n, "/post/{handle:\\d{3}}/{", NULL); 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); ck_assert(ret_node); // OK to insert, but should return error when compiling patterns node * ret_node2 = r3_tree_insert_path(n, "/users/{idx:\\d{3}}/{idy:aaa}", NULL); ck_assert(ret_node2); 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_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, NULL); ck_assert(ret_edge); ck_assert_int_eq(prefix_len, 0); r3_tree_free(n); }
END_TEST START_TEST(test_pcre_pattern_simple) { match_entry * entry; entry = match_entry_createl( "/user/123" , strlen("/user/123") ); node * n = r3_tree_create(10); r3_tree_insert_path(n, "/user/{id:\\d+}", NULL); r3_tree_insert_path(n, "/user", NULL); r3_tree_compile(n, NULL); // r3_tree_dump(n, 0); node *matched; matched = r3_tree_matchl(n, "/user/123", strlen("/user/123"), entry); ck_assert(matched); ck_assert(entry->vars->len > 0); ck_assert_str_eq(entry->vars->tokens[0],"123"); r3_tree_free(n); }
END_TEST START_TEST (test_gvc_render_file) { node * n = r3_tree_create(1); r3_tree_insert_path(n, "/foo/bar/baz", NULL); r3_tree_insert_path(n, "/foo/bar/qux", NULL); r3_tree_insert_path(n, "/foo/bar/quux", NULL); r3_tree_insert_path(n, "/foo/bar/corge", NULL); r3_tree_insert_path(n, "/foo/bar/grault", NULL); r3_tree_insert_path(n, "/garply/grault/foo", NULL); r3_tree_insert_path(n, "/garply/grault/bar", NULL); r3_tree_insert_path(n, "/user/{id}", NULL); r3_tree_insert_path(n, "/post/{title:\\w+}", NULL); r3_tree_compile(n); r3_tree_render_file(n, "png", "check_gvc.png"); r3_tree_free(n); }
END_TEST START_TEST(test_insert_route) { int var1 = 22; int var2 = 33; match_entry * entry = match_entry_create("/blog/post"); entry->request_method = METHOD_GET; node * n = r3_tree_create(2); r3_tree_insert_route(n, METHOD_GET, "/blog/post", &var1); r3_tree_insert_route(n, METHOD_POST, "/blog/post", &var2); route *c = r3_tree_match_route(n, entry); fail_if(c == NULL); r3_tree_free(n); match_entry_free(entry); }
END_TEST START_TEST (test_insert_pathl_fail) { node * n = r3_tree_create(10); node * ret; char *errstr = NULL; ret = r3_tree_insert_pathl_ex(n, "/foo/{name:\\d{5}", strlen("/foo/{name:\\d{5}"), NULL, NULL, &errstr); ck_assert(ret == NULL); ck_assert(errstr != NULL); printf("%s\n", errstr); // Returns Incomplete slug pattern. PATTERN (16): '/foo/{name:\d{5}', OFFSET: 16, STATE: 1 SAFE_FREE(errstr); errstr = NULL; r3_tree_compile(n, &errstr); ck_assert(errstr == NULL); r3_tree_free(n); }
static void Trie_dealloc(Trie * self) { r3_tree_free(self.node); self->ob_type->tp_free((PyObject*)self); }