From f0bd741f2a3e70d0d2bb9660d8985b62392ecac9 Mon Sep 17 00:00:00 2001 From: armatusmiles <akurotych@gmail.com> Date: Thu, 19 Jul 2018 23:29:40 +0300 Subject: [PATCH] Refactor tests --- .gitmodules | 3 +++ core/dap_common.h | 11 ++++------- test/CMakeLists.txt | 4 ++-- test/core/CMakeLists.txt | 11 ++++++----- test/core/dap_common_test.c | 5 +++-- test/core/dap_common_test.h | 6 +----- test/core/dap_config_test.c | 34 +++++++++++++++++----------------- test/core/dap_config_test.h | 6 +----- 8 files changed, 37 insertions(+), 43 deletions(-) create mode 100644 .gitmodules diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..282d799 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "libdap-test"] + path = libdap-test + url = https://github.com/kelvinblockchain/libdap-test diff --git a/core/dap_common.h b/core/dap_common.h index 264b48f..60cb956 100644 --- a/core/dap_common.h +++ b/core/dap_common.h @@ -37,29 +37,26 @@ #define DAP_PROTOCOL_VERSION 21 - - #if defined(__GNUC__) ||defined (__clang__) #define DAP_ALIGN_PACKED __attribute__((aligned(1),packed)) #endif enum log_level{L_CRITICAL=5,L_ERROR=4, L_WARNING=3,L_NOTICE=2,L_INFO=1,L_DEBUG=0}; -//extern enum log_level log_level; #ifdef __cplusplus extern "C" { #endif int dap_common_init( const char * a_log_file ); -void dap_common_deinit(); +void dap_common_deinit(void); void _log_it(const char * log_tag, enum log_level, const char * format,...); void _vlog_it(const char * log_tag, enum log_level, const char * format, va_list ap ); #define log_it(_log_level,...) _log_it(LOG_TAG,_log_level,##__VA_ARGS__) #define vlog_it(a_log_level,a_format,a_ap) _vlog_it(LOG_TAG,a_log_level,a_format,a_ap) -const char * log_error(); +const char * log_error(void); void set_log_level(enum log_level ll); #ifdef __GNUC__ @@ -71,8 +68,8 @@ char *strndup(const char *s, size_t n); #endif int time_to_rfc822(char * out, size_t out_size_max, time_t t); -int get_select_breaker(); -int send_select_break(); +int get_select_breaker(void); +int send_select_break(void); char * exec_with_ret(const char * a_cmd); char * exec_with_ret_multistring(const char * a_cmd); char * random_string_create(size_t a_length); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 6f2f3ee..ed2b77c 100755 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.0) -project(libdap-test) - +project(test) +add_subdirectory(libdap-test) add_subdirectory(core) diff --git a/test/core/CMakeLists.txt b/test/core/CMakeLists.txt index e81ee04..a5153d5 100755 --- a/test/core/CMakeLists.txt +++ b/test/core/CMakeLists.txt @@ -1,14 +1,15 @@ cmake_minimum_required(VERSION 3.0) -project(core-test) +project(core_test) -file(GLOB SRC *.h *.c) +file(GLOB SRCS *.c) +file(GLOB HRDS *.c) -add_executable(${PROJECT_NAME} ${SRC}) +add_executable(${PROJECT_NAME} ${SRCS} ${HRDS}) -target_link_libraries(${PROJECT_NAME} dap_core) +target_link_libraries(core_test dap_test dap_core) add_test( NAME core-test - COMMAND core-test + COMMAND core_test ) diff --git a/test/core/dap_common_test.c b/test/core/dap_common_test.c index cfc7339..dff06eb 100644 --- a/test/core/dap_common_test.c +++ b/test/core/dap_common_test.c @@ -4,10 +4,11 @@ void test_put_int() { const int INT_VAL = 10; const char * EXPECTED_RESULT = "10"; char * result_arr = itoa(INT_VAL); - assert(strcmp(result_arr, EXPECTED_RESULT) == 0 && "test_put_int failed"); + dap_assert(strcmp(result_arr, EXPECTED_RESULT) == 0, + "Check string result from itoa"); } void dap_common_test_run() { - printf("Start running dap_common_test\n"); + dap_print_module_name("dap_common"); test_put_int(); } diff --git a/test/core/dap_common_test.h b/test/core/dap_common_test.h index 00959ed..3934603 100644 --- a/test/core/dap_common_test.h +++ b/test/core/dap_common_test.h @@ -1,9 +1,5 @@ #pragma once +#include "dap_test.h" #include "dap_common.h" -#include "assert.h" -#include "stdbool.h" -#include "stdlib.h" -#include "stdio.h" -#include "string.h" extern void dap_common_test_run(void); diff --git a/test/core/dap_config_test.c b/test/core/dap_config_test.c index b831ee9..22c788d 100644 --- a/test/core/dap_config_test.c +++ b/test/core/dap_config_test.c @@ -27,8 +27,7 @@ static dap_config_t * config; void create_test_config_file() { config_file = fopen(testconfigName, "w+"); - assert(config_file != NULL && - "Can't create config file"); + dap_assert(config_file != NULL, "Create config file"); fwrite(config_data, sizeof(char), strlen(config_data), config_file); @@ -46,43 +45,43 @@ void init_test_case() { } void cleanup_test_case() { - assert(remove("test_dap_config.cfg") == 0 && - "Error remove config file"); + dap_assert(remove("test_dap_config.cfg") == 0, + "Remove config file"); dap_config_close(config); } void test_config_open_fail() { - assert(dap_config_open("RandomNeverExistName") == NULL - && "configOpenFail failed"); + dap_assert(dap_config_open("RandomNeverExistName") == NULL, + "Try open not exists config file"); } void test_get_int() { int32_t resultTTL = dap_config_get_item_int32(config, "server_options", "TTL_session_key"); - assert(resultTTL == 600 && "get_int failed"); + dap_assert(resultTTL == 600, "Get int from config"); } void test_get_double() { double timeout = dap_config_get_item_double(config, "server_options", "timeout"); - assert(timeout == 1.0 && "test_get_double failed"); + dap_assert(timeout == 1.0, "Get double from config"); } void test_get_bool() { bool rBool = dap_config_get_item_bool(config, "server_options", "vpn_enable"); - assert(rBool == true && "test_get_bool failed"); + dap_assert(rBool == true, "Get bool from config"); rBool = dap_config_get_item_bool(config, "server_options", "proxy_enable"); - assert(rBool == false && "test_get_bool failed"); + dap_assert(rBool == false, "Get bool from config"); } void test_array_str() { uint16_t arraySize; char ** result_arr = dap_config_get_array_str(config, "server_options", "str_arr", &arraySize); - assert(result_arr != NULL && "test_array_str failed, result_arr is NULL"); - assert(arraySize == STR_ARR_LEN); + dap_assert(result_arr != NULL, "Get array str from config"); + dap_assert(arraySize == STR_ARR_LEN, "Check array length"); for(uint i = 0; i < arraySize; i++) { assert(strcmp(result_arr[i], str_add_test_case[i]) == 0 && "test_array_str failed"); @@ -93,19 +92,20 @@ void test_array_int() { uint16_t arraySize; char ** result_arr = dap_config_get_array_str(config, "server_options", "int_arr", &arraySize); - assert(result_arr != NULL && "test_array_str failed, result_arr is NULL"); - assert(arraySize == INT_ARR_LEN && "test_array_str failed, arraySize is not equal INT_ARR_LEN"); + dap_assert(result_arr != NULL, "Get array int"); + dap_assert(arraySize == INT_ARR_LEN, "Check array int length"); + dap_test_msg("Testing array int values."); for(uint i = 0; i < arraySize; i++) { - assert(atoi(result_arr[i]) == int_arr_test_cases[i] && "test_array_int failed"); + dap_assert_PIF(atoi(result_arr[i]) == int_arr_test_cases[i], "Check array int"); } } void dap_config_tests_run() { - printf("Start running dap_config_tests\n"); - init_test_case(); + dap_print_module_name("dap_config"); + init_test_case(); test_config_open_fail(); test_get_int(); test_get_bool(); diff --git a/test/core/dap_config_test.h b/test/core/dap_config_test.h index 1381b66..0fd0c13 100755 --- a/test/core/dap_config_test.h +++ b/test/core/dap_config_test.h @@ -1,9 +1,5 @@ #pragma once +#include "dap_test.h" #include "dap_config.h" -#include "assert.h" -#include "stdbool.h" -#include "stdlib.h" -#include "stdio.h" -#include "string.h" extern void dap_config_tests_run(void); -- GitLab