From 3989f2150f7a892fc721f9b511f1dde046adaf60 Mon Sep 17 00:00:00 2001 From: c9s Date: Thu, 15 May 2014 12:53:48 +0800 Subject: [PATCH] ltrim_slash and related test --- include/str.h | 3 +++ src/str.c | 7 +++++++ tests/test_tree.c | 22 ++++++++++++++++++++-- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/include/str.h b/include/str.h index 2559c70..e1f9411 100644 --- a/include/str.h +++ b/include/str.h @@ -7,5 +7,8 @@ #ifndef STR_H #define STR_H +char * ltrim_slash(char* str); + +char** str_split(char* a_str, const char a_delim); #endif /* !STR_H */ diff --git a/src/str.c b/src/str.c index b814e0a..399a0ed 100644 --- a/src/str.c +++ b/src/str.c @@ -13,6 +13,13 @@ +char * ltrim_slash(char* str) +{ + char * p = str; + while (*p == '/') p++; + return strdup(p); +} + char** str_split(char* a_str, const char a_delim) { char** result = 0; diff --git a/tests/test_tree.c b/tests/test_tree.c index c0e7241..d899c11 100644 --- a/tests/test_tree.c +++ b/tests/test_tree.c @@ -4,6 +4,14 @@ #include "node.h" #include "token.h" +START_TEST (test_ltrim_slash) +{ + fail_if( strcmp( ltrim_slash("/blog") , "blog" ) != 0 ); + fail_if( strcmp( ltrim_slash("blog") , "blog" ) != 0 ); +} +END_TEST + + START_TEST (test_route) { token_array *t; @@ -42,13 +50,22 @@ END_TEST START_TEST (test_token_array) { - token_array * l = token_array_create(10); + token_array * l = token_array_create(3); fail_if( l == NULL ); - fail_if( FALSE == token_array_resize(l, l->cap * 2) ); fail_if( FALSE == token_array_append(l, strdup("abc") ) ); + fail_if( l->len != 1 ); + fail_if( FALSE == token_array_append(l, strdup("foo") ) ); + fail_if( l->len != 2 ); + fail_if( FALSE == token_array_append(l, strdup("bar") ) ); + fail_if( l->len != 3 ); + + fail_if( FALSE == token_array_append(l, strdup("zoo") ) ); + fail_if( l->len != 4 ); + + fail_if( FALSE == token_array_resize(l, l->cap * 2) ); token_array_free(l); } @@ -60,6 +77,7 @@ Suite* r3_suite (void) { TCase *tcase = tcase_create("testcase"); tcase_add_test(tcase, test_route); tcase_add_test(tcase, test_token_array); + tcase_add_test(tcase, test_ltrim_slash); suite_add_tcase(suite, tcase);