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); }
void r3_tree_dump(const node * n, int level) { print_indent(level); printf("(o)"); if ( n->combined_pattern ) { printf(" regexp:%s", n->combined_pattern); } printf(" endpoint:%d", n->endpoint); if (n->data) { printf(" data:%p", n->data); } printf("\n"); for ( int i = 0 ; i < n->edge_len ; i++ ) { edge * e = n->edges[i]; print_indent(level + 1); printf("|-\"%s\"", e->pattern); if (e->opcode ) { printf(" opcode:%d", e->opcode); } if ( e->child ) { printf("\n"); r3_tree_dump( e->child, level + 1); } printf("\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); }
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); }
void r3_tree_dump(node * n, int level) { if ( n->combined_pattern ) { printf(" regexp:%s", n->combined_pattern); } printf(" endpoint:%d", n->endpoint); if (n->data) { printf(" data:%p", n->data); } printf("\n"); for ( int i = 0 ; i < n->edge_len ; i++ ) { edge * e = n->edges[i]; print_indent(level); printf(" |-\"%s\"", e->pattern); if (e->has_slug) { printf(" slug:"); printf("%s", compile_slug(e->pattern, e->pattern_len) ); } if ( e->child ) { r3_tree_dump( e->child, level + 1); } printf("\n"); } }
END_TEST /** * Test for (\d{2})/([^/]+) */ START_TEST (test_pcre_patterns_insert_3) { node * n = r3_tree_create(10); printf("Inserting /post/{idx:\\d{2}}/{idy}\n"); r3_tree_insert_path(n, "/post/{idx:\\d{2}}/{idy}", NULL); r3_tree_dump(n, 0); printf("Inserting /zoo\n"); r3_tree_insert_path(n, "/zoo", NULL); r3_tree_dump(n, 0); r3_tree_insert_path(n, "/foo", NULL); r3_tree_insert_path(n, "/bar", NULL); char *errstr = NULL; r3_tree_compile(n, &errstr); r3_tree_dump(n, 0); node *matched; matched = r3_tree_match(n, "/post/11/22", NULL); ck_assert((int)matched); matched = r3_tree_match(n, "/post/11", NULL); ck_assert(!matched); matched = r3_tree_match(n, "/post/11/", NULL); ck_assert(!matched); /* matched = r3_tree_match(n, "/post/113", NULL); ck_assert(!matched); */ }
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_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 /** * Test for \d{2}/\d{2} */ START_TEST (test_pcre_patterns_insert_2) { node * n = r3_tree_create(10); r3_tree_insert_path(n, "/post/{idx:\\d{2}}/{idy:\\d{2}}", NULL); r3_tree_insert_path(n, "/zoo", NULL); r3_tree_insert_path(n, "/foo", NULL); r3_tree_insert_path(n, "/bar", NULL); char *errstr = NULL; r3_tree_compile(n, &errstr); r3_tree_dump(n, 0); node *matched; matched = r3_tree_match(n, "/post/11/22", NULL); ck_assert((int)matched); ck_assert(matched->endpoint > 0); }
END_TEST START_TEST (test_compile) { str_array *t; node * n; n = r3_tree_create(10); node *m; edge *e ; r3_tree_insert_path(n, "/zoo", NULL); r3_tree_insert_path(n, "/foo", NULL); r3_tree_insert_path(n, "/bar", NULL); r3_tree_compile(n); fail_if( n->combined_pattern ); fail_if( NULL == r3_node_find_edge_str(n, "/", strlen("/") ) ); #ifdef DEBUG r3_tree_dump(n, 0); #endif r3_tree_insert_path(n, "/foo/{id}", NULL); r3_tree_insert_path(n, "/{id}", NULL); r3_tree_compile(n); r3_tree_compile(n); // test double compile #ifdef DEBUG r3_tree_dump(n, 0); #endif /* fail_if(n->edges[0]->child->combined_pattern == NULL); e = r3_node_find_edge_str(n, "/", strlen("/") ); fail_if( NULL == e ); */ /* printf( "%s\n", e->pattern ); printf( "%s\n", e->child->combined_pattern ); printf( "%s\n", n->edges[0]->child->combined_pattern); printf( "%s\n", n->combined_pattern ); */ match_entry * entry; entry = match_entry_createl( "foo" , strlen("/foo") ); m = r3_tree_matchl( n , "/foo", strlen("/foo"), entry); fail_if( NULL == m ); entry = match_entry_createl( "/zoo" , strlen("/zoo") ); m = r3_tree_matchl( n , "/zoo", strlen("/zoo"), entry); fail_if( NULL == m ); entry = match_entry_createl( "/bar" , strlen("/bar") ); m = r3_tree_matchl( n , "/bar", strlen("/bar"), entry); fail_if( NULL == m ); entry = match_entry_createl( "/xxx" , strlen("/xxx") ); m = r3_tree_matchl( n , "/xxx", strlen("/xxx"), entry); fail_if( NULL == m ); entry = match_entry_createl( "/foo/xxx" , strlen("/foo/xxx") ); m = r3_tree_matchl( n , "/foo/xxx", strlen("/foo/xxx"), entry); fail_if( NULL == m ); entry = match_entry_createl( "/some_id" , strlen("/some_id") ); m = r3_tree_matchl( n , "/some_id", strlen("/some_id"), entry); fail_if( NULL == m ); ck_assert_int_gt( m->endpoint , 0 ); // should not be an endpoint }