From af17ead850f90cf6e4476aa74975e68d7293fb27 Mon Sep 17 00:00:00 2001 From: Anastasis Grammenos Date: Sun, 16 Sep 2018 15:27:31 +0300 Subject: Add unit test infrastructure --- CMakeLists.txt | 11 ++++++++--- README.org | 32 ++++++++++++++++++++++++-------- res/check_ck | 31 +++++++++++++++++++++++++++++-- unit/ck-test.c | 24 ++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 13 deletions(-) create mode 100644 unit/ck-test.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 1d1b628..008a402 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,11 +25,12 @@ endif(CK_ASAN) set(SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src) set(RES_DIR ${CMAKE_CURRENT_SOURCE_DIR}/res) set(TESTS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/tests) +set(UNIT_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/unit) # Set source code locations set(ckBin_src ${SRC_DIR}/ck.c) +set(ckUnitTest_src ${UNIT_TEST_DIR}/ck-test.c) set(ckLib_src - ${SRC_DIR}/ck.c ${SRC_DIR}/actionparser.c ${SRC_DIR}/actions.c ${SRC_DIR}/actionhelper.c @@ -69,8 +70,12 @@ add_library (ckLib SHARED # Link add_executable(ck ${ckBin_src}) -target_link_libraries (ck ckLib) -target_link_libraries (ck ${SQLITE3_LIBRARIES}) +target_link_libraries(ck ckLib) +target_link_libraries(ck ${SQLITE3_LIBRARIES}) +## unit test +add_executable(ck-test ${ckUnitTest_src}) +target_link_libraries(ck-test ckLib) +target_link_libraries(ck-test ${SQLITE3_LIBRARIES}) # copy test files set(BIN_TESTS_DIR ${CMAKE_BINARY_DIR}/res/tests) diff --git a/README.org b/README.org index 320a11b..9f83ddc 100644 --- a/README.org +++ b/README.org @@ -8,13 +8,14 @@ - cmake - sqlite3-dev -** gcc -Edit the =CMakeLists.txt= file and change the =clang= to =gcc= +** compiler +By default I use clang, if you want to use gcc just +edit the =CMakeLists.txt= file and change =clang= to =gcc= ** make #+BEGIN_SRC sh # clone the repo -> cd ~/code; git clone https://github.com/gramanas/ck +> cd ~/code; git clone https://gitlab.com/grm-grm/ck # make a build directory and enter it > mkdir ~/ck_build; cd ~/ck_build; # run cmake @@ -31,7 +32,7 @@ Just build with address sanitizer enabled like so: llvm has better asan than gcc, so I use that. #+BEGIN_SRC sh # clone the repo -> cd ~/code; git clone https://github.com/gramanas/ck +> cd ~/code; git clone https://gitlab.com/grm-grm/ck # make a build directory and enter it > mkdir ~/ck_build; cd ~/ck_build; # run cmake @@ -43,12 +44,27 @@ llvm has better asan than gcc, so I use that. #+END_SRC ** tests -The testing "suite" is just a bash script -that executs all the bash scripts under -=tests/= +The testing "suite" is a bash script that runs regression +and unit tests. Regression tests are under the =tests/= directory +and are bash scripts that test =ck= functionality. Unit tests reside +under =unit/= directory and test the code. *** run tests Simply go to the build dir and type. #+BEGIN_SRC sh -./check_ck +$ ./check_ck +#+END_SRC + +*** test suite +#+BEGIN_SRC sh +$ ./check_ck -h +ck test suite +use without flags to run all tests + +flags: + -u, --unit run only the unit tests + -r, --regression run only the regression tests + -c, --clear remove test files + use it if the tests fail + -h, --help, * print this #+END_SRC diff --git a/res/check_ck b/res/check_ck index 6e61e5c..ce35396 100755 --- a/res/check_ck +++ b/res/check_ck @@ -3,10 +3,12 @@ BIN=@CMAKE_BINARY_DIR@ TEST_LOCATION=@PROJECT_TESTING_GROUNDS@ +# used in regression tests function running { echo "--[TESTING $1]--" } +# used in regression tests function init { running $1 mkdir -p $TEST_LOCATION/vc @@ -23,7 +25,17 @@ function clear_tests { rm -rf $TEST_LOCATION } -function run { +function unit_tests { + echo -e "################################" + echo -e "########## Unit Tests ##########" + echo -e "################################" + ./ck-test +} + +function regression_tests { + echo -e "######################################" + echo -e "########## Regression Tests ##########" + echo -e "######################################" DIR=@BIN_TESTS_DIR@ for i in $( ls $DIR ); do ERROR="TEST "$i" FAILED:" @@ -33,10 +45,17 @@ function run { done } +function run { + unit_tests + regression_tests +} + function print_help { echo -e "ck test suite" - echo -e "run without flags to begin testing" + echo -e "use without flags to run all tests" echo -e "\nflags:" + echo -e " -u, --unit\t\trun only the unit tests" + echo -e " -r, --regression\trun only the regression tests" echo -e " -c, --clear\t\tremove test files" echo -e " \t\t use it if the tests fail" echo -e " -h, --help, *\t\tprint this" @@ -58,6 +77,14 @@ do clear_tests exit ;; + -u | --unit) + unit_tests + exit + ;; + -r | --regression) + regression_tests + exit + ;; *) # unknown option print_help exit diff --git a/unit/ck-test.c b/unit/ck-test.c new file mode 100644 index 0000000..01156f1 --- /dev/null +++ b/unit/ck-test.c @@ -0,0 +1,24 @@ +#include "cklist.h" +#include "stdio.h" + +#define CK_UNIT_TESTS \ + X(ck_list_init, "Initialize ck list") \ + X(ck_list_add, "Add elements to ck list") + +void ck_list_init() { + printf("Test1\n"); +} + +void ck_list_add() { + printf("Test2\n"); +} + +int main() { +#define X(TEST, DESC) \ + printf("--[%s]--\n", DESC); \ + TEST(); \ + printf("--[Passed]--\n\n"); + CK_UNIT_TESTS +#undef X +} + -- cgit v1.2.3