Rename token_list to token_array

This commit is contained in:
c9s 2014-05-15 12:21:45 +08:00
parent f8539fd950
commit 767dba6e86
3 changed files with 23 additions and 24 deletions

View file

@ -12,21 +12,21 @@ typedef unsigned char bool;
#define FALSE 0
#define TRUE 1
typedef struct _token_list {
typedef struct _token_array {
char **tokens;
int len;
int cap;
} token_list;
} token_array;
token_list * token_list_create(int cap);
token_array * token_array_create(int cap);
bool token_list_is_full(token_list * l);
bool token_array_is_full(token_array * l);
bool token_list_resize(token_list *l, int new_cap);
bool token_array_resize(token_array *l, int new_cap);
bool token_list_append(token_list * list, char * token);
bool token_array_append(token_array * list, char * token);
void token_list_free(token_list *l);
void token_array_free(token_array *l);
#endif /* !TOKEN_H */

View file

@ -8,27 +8,27 @@
#include "token.h"
token_list * token_list_create(int cap) {
token_list * list = (token_list*) malloc( sizeof(token_list) );
token_array * token_array_create(int cap) {
token_array * list = (token_array*) malloc( sizeof(token_array) );
list->len = 0;
list->cap = cap;
list->tokens = (char**) malloc( sizeof(char*) * cap);
return list;
}
bool token_list_is_full(token_list * l) {
bool token_array_is_full(token_array * l) {
return l->len >= l->cap;
}
bool token_list_resize(token_list *l, int new_cap) {
bool token_array_resize(token_array *l, int new_cap) {
l->tokens = realloc(l->tokens, sizeof(char**) * new_cap);
l->cap = new_cap;
return l->tokens != NULL;
}
bool token_list_append(token_list * l, char * token) {
if ( token_list_is_full(l) ) {
bool ret = token_list_resize(l, l->cap + 20);
bool token_array_append(token_array * l, char * token) {
if ( token_array_is_full(l) ) {
bool ret = token_array_resize(l, l->cap + 20);
if (ret == FALSE ) {
return FALSE;
}
@ -37,7 +37,7 @@ bool token_list_append(token_list * l, char * token) {
return TRUE;
}
void token_list_free(token_list *l) {
void token_array_free(token_array *l) {
for ( int i = 0; i < l->len ; i++ ) {
char * t = l->tokens[ i ];
free(t);
@ -46,4 +46,3 @@ void token_list_free(token_list *l) {
}

View file

@ -13,17 +13,17 @@ START_TEST (test_route)
}
END_TEST
START_TEST (test_token_list)
START_TEST (test_token_array)
{
token_list * l = token_list_create(10);
token_array * l = token_array_create(10);
fail_if( l == NULL );
fail_if( FALSE == token_list_resize(l, l->cap * 2) );
fail_if( FALSE == token_array_resize(l, l->cap * 2) );
fail_if( FALSE == token_list_append(l, strdup("abc") ) );
fail_if( FALSE == token_list_append(l, strdup("foo") ) );
fail_if( FALSE == token_list_append(l, strdup("bar") ) );
fail_if( FALSE == token_array_append(l, strdup("abc") ) );
fail_if( FALSE == token_array_append(l, strdup("foo") ) );
fail_if( FALSE == token_array_append(l, strdup("bar") ) );
token_list_free(l);
token_array_free(l);
}
END_TEST
@ -32,7 +32,7 @@ Suite* r3_suite (void) {
TCase *tcase = tcase_create("testcase");
tcase_add_test(tcase, test_route);
tcase_add_test(tcase, test_token_list);
tcase_add_test(tcase, test_token_array);
suite_add_tcase(suite, tcase);