update testing framework with "Check"

This commit is contained in:
c9s 2014-05-15 10:08:42 +08:00
parent 6bd6b49c87
commit fec873b411
7 changed files with 184 additions and 16 deletions

View file

@ -19,15 +19,17 @@ include_directories(. "${PROJECT_SOURCE_DIR}/include" ${INCLUDE_DIRECTORIES} /op
link_directories(${LINK_DIRECTORIES} /opt/local/lib) link_directories(${LINK_DIRECTORIES} /opt/local/lib)
find_package(PCRE REQUIRED) find_package(PCRE REQUIRED)
find_package(Judy REQUIRED) find_package(Judy REQUIRED)
find_package(Jemalloc REQUIRED)
set(LIBS ${LIBS} ${PCRE_LIBRARIES} ${Judy_LIBRARIES} r3) set(LIBS ${LIBS} ${PCRE_LIBRARIES} ${Judy_LIBRARIES} ${Jemalloc_LIBRARIES} r3)
set (CMAKE_CFLAGS "-Wall -pipe -g3 -funroll-loops")
# set (CMAKE_CXX_FLAGS "-std=c++0x -arch x86_64 -stdlib=libc++ -g3 -Wall -O0") # set (CMAKE_CXX_FLAGS "-std=c++0x -arch x86_64 -stdlib=libc++ -g3 -Wall -O0")
enable_testing() enable_testing()
add_subdirectory(src) add_subdirectory(src)
add_subdirectory(tests) add_subdirectory(tests)
add_test(test_tree ${CMAKE_CURRENT_BINARY_DIR}/test_tree) add_test(test_r3 ${CMAKE_CURRENT_BINARY_DIR}/tests/test_r3)
add_executable(demo demo.c) add_executable(demo demo.c)

View file

@ -0,0 +1,44 @@
# - Try to find jemalloc headers and libraries.
#
# Usage of this module as follows:
#
# find_package(JeMalloc)
#
# Variables used by this module, they can change the default behaviour and need
# to be set before calling find_package:
#
# JEMALLOC_ROOT_DIR Set this variable to the root installation of
# jemalloc if the module has problems finding
# the proper installation path.
#
# Variables defined by this module:
#
# JEMALLOC_FOUND System has jemalloc libs/headers
# JEMALLOC_LIBRARIES The jemalloc library/libraries
# JEMALLOC_INCLUDE_DIR The location of jemalloc headers
find_path(JEMALLOC_ROOT_DIR
NAMES include/jemalloc/jemalloc.h
)
find_library(JEMALLOC_LIBRARIES
NAMES jemalloc
HINTS ${JEMALLOC_ROOT_DIR}/lib
)
find_path(JEMALLOC_INCLUDE_DIR
NAMES jemalloc/jemalloc.h
HINTS ${JEMALLOC_ROOT_DIR}/include
)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(JeMalloc DEFAULT_MSG
JEMALLOC_LIBRARIES
JEMALLOC_INCLUDE_DIR
)
mark_as_advanced(
JEMALLOC_ROOT_DIR
JEMALLOC_LIBRARIES
JEMALLOC_INCLUDE_DIR
)

View file

@ -2,8 +2,10 @@
include_directories("${PROJECT_SOURCE_DIR}/include") include_directories("${PROJECT_SOURCE_DIR}/include")
# install(TARGETS swiftnav-static DESTINATION lib${LIB_SUFFIX}) # install(TARGETS swiftnav-static DESTINATION lib${LIB_SUFFIX})
set(libr3_SRCS node.c) set(libr3_SRCS node.c str.c)
# add_library(r3 STATIC ${libr3_SRCS}) set(CMAKE_CFLAGS "-Wall -pipe -g3 -funroll-loops")
# add_library(r3-static STATIC ${libr3_SRCS})
add_library(r3 SHARED ${libr3_SRCS}) add_library(r3 SHARED ${libr3_SRCS})
# target_link_libraries(r3 cblas) # target_link_libraries(r3 cblas)
# install(FILES ${libswiftnav_HEADERS} DESTINATION include/libswiftnav) # install(FILES ${libswiftnav_HEADERS} DESTINATION include/libswiftnav)

View file

@ -1 +1,12 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
// Jemalloc memory management
#include <jemalloc/jemalloc.h>
// Judy array
#include <Judy.h>
// String value as the index http://judy.sourceforge.net/doc/JudySL_3x.htm

99
src/str.c Normal file
View file

@ -0,0 +1,99 @@
/*
* str.c
* Copyright (C) 2014 c9s <c9s@c9smba.local>
*
* Distributed under terms of the MIT license.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "str.h"
/**
* This function is used to split route path into a string array, not for performance.
* hence this function should be safe.
*
* Split "/path/foo/{id}" into [ "/path" , "/foo" , "/{id}" ]
* Split "/path/bar/{id}" into [ "/path" , "/foo" , "/{id}" ]
* Split "/blog/post/{id}" into [ "/blog" , "/post" , "/{id}" ]
* Split "/blog/{id}" into [ "/blog" , "/{id}" ]
* Split "/blog" into [ "/blog" ]
* Split "/b" into [ "/b" ]
*
* @param char* pattern
* @param int pattern_len
*
* @return char**
*/
char** split_route_pattern(char *pattern, int pattern_len) {
char *p = pattern;
while (*p) {
// a slug
if ( *p == '{' ) {
char *s1 = p;
char *s2;
while (*(p++) != '}') {
if ( p - pattern > pattern_len ) {
// XXX: unexpected error (unclosed slug)
}
}
s2 = p;
}
}
return NULL;
}
char** str_split(char* a_str, const char a_delim)
{
char** result = 0;
size_t count = 0;
char* tmp = a_str;
char* last_comma = 0;
char delim[2];
delim[0] = a_delim;
delim[1] = 0;
/* Count how many elements will be extracted. */
while (*tmp)
{
if (a_delim == *tmp)
{
count++;
last_comma = tmp;
}
tmp++;
}
/* Add space for trailing token. */
count += last_comma < (a_str + strlen(a_str) - 1);
/* Add space for terminating null string so caller
knows where the list of returned strings ends. */
count++;
result = malloc(sizeof(char*) * count);
if (result)
{
size_t idx = 0;
char* token = strtok(a_str, delim);
while (token)
{
assert(idx < count);
*(result + idx++) = strdup(token);
token = strtok(0, delim);
}
assert(idx == count - 1);
*(result + idx) = 0;
}
return result;
}

View file

@ -11,13 +11,13 @@ else (NOT CHECK_FOUND)
include_directories(${CHECK_INCLUDE_DIRS}) include_directories(${CHECK_INCLUDE_DIRS})
# include_directories("${PROJECT_SOURCE_DIR}/include/r2") # include_directories("${PROJECT_SOURCE_DIR}/include/r2")
add_executable(test_tree test_tree.c) add_executable(test_r3 test_tree.c)
target_link_libraries(test_tree ${TEST_LIBS}) target_link_libraries(test_r3 ${TEST_LIBS})
add_custom_command( add_custom_command(
TARGET test_tree POST_BUILD TARGET test_r3 POST_BUILD
COMMENT "Running unit tests" COMMENT "Running unit tests"
COMMAND test_tree COMMAND test_r3
) )
endif (NOT CHECK_FOUND) endif (NOT CHECK_FOUND)

View file

@ -1,25 +1,35 @@
#include <stdio.h> #include <stdio.h>
#include <check.h> #include <check.h>
#include "str.h"
#include "node.h"
START_TEST (test_tree) START_TEST (test_route)
{ {
} }
END_TEST END_TEST
START_TEST (test_route2)
{
}
END_TEST
Suite* r3_suite (void) {
Suite *suite = suite_create("blah");
TCase *tcase = tcase_create("testcase");
tcase_add_test(tcase, test_route);
tcase_add_test(tcase, test_route2);
Suite* str_suite (void) {
Suite *suite = suite_create("test_tree");
/*
TCase *tcase = tcase_create("case");
tcase_add_test(tcase, test_tree);
suite_add_tcase(suite, tcase); suite_add_tcase(suite, tcase);
*/
return suite; return suite;
} }
int main (int argc, char *argv[]) { int main (int argc, char *argv[]) {
int number_failed; int number_failed;
Suite *suite = str_suite(); Suite *suite = r3_suite();
SRunner *runner = srunner_create(suite); SRunner *runner = srunner_create(suite);
srunner_run_all(runner, CK_NORMAL); srunner_run_all(runner, CK_NORMAL);
number_failed = srunner_ntests_failed(runner); number_failed = srunner_ntests_failed(runner);