diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 87f02abccd4c3df075d47e5b6a79ac0d3a46d844..6f2f3eedc285f033820da35e3eaf536a9e3b139c 100755 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -2,16 +2,4 @@ cmake_minimum_required(VERSION 3.0) project(libdap-test) -set(CMAKE_CXX_STANDARD 11) -set(CMAKE_CXX_STANDARD_REQUIRED ON) - -# Tell CMake to run moc when necessary: -set(CMAKE_AUTOMOC ON) - -# As moc files are generated in the binary dir, tell CMake -# to always look for includes there: -set(CMAKE_INCLUDE_CURRENT_DIR ON) - -find_package(Qt5Test REQUIRED) - add_subdirectory(core) diff --git a/test/core/CMakeLists.txt b/test/core/CMakeLists.txt index 4df984fe6ffc83dc0572f5ba3b43d05a381e95f9..e81ee045ded490ab2639e39a18309fd2217c2e1d 100755 --- a/test/core/CMakeLists.txt +++ b/test/core/CMakeLists.txt @@ -2,11 +2,10 @@ cmake_minimum_required(VERSION 3.0) project(core-test) -file(GLOB SRC *.hpp *.cpp) +file(GLOB SRC *.h *.c) add_executable(${PROJECT_NAME} ${SRC}) -target_link_libraries(${PROJECT_NAME} Qt5::Test) target_link_libraries(${PROJECT_NAME} dap_core) add_test( diff --git a/test/core/DapConfig_test.hpp b/test/core/DapConfig_test.hpp deleted file mode 100755 index 129cb8569c30d6e06197368ae65c0f13af8f0e6e..0000000000000000000000000000000000000000 --- a/test/core/DapConfig_test.hpp +++ /dev/null @@ -1,95 +0,0 @@ -#pragma once -#include <QTest> -#include <QDebug> -#include "dap_config.h" - -class DapConfigTest : public QObject { - Q_OBJECT -private: - const QByteArray testconfigName = "test_dap_config"; - const QByteArray configForTesting = "[db_options]\n" - "db_type=mongoDb\n" - "[server_options]\n" - "timeout=1,0\n" - "vpn_enable=true\n" - "proxy_enable=false\n" - "TTL_session_key=600\n" - "str_arr=[vasya, petya, grisha, petushok@microsoft.com]\n" - "int_arr=[1, 3, 5]\n"; - static constexpr size_t STR_ARR_LEN = 4; - std::string m_strArrTestCase[STR_ARR_LEN] = {"vasya", "petya", - "grisha", "petushok@microsoft.com"}; - - static constexpr size_t INT_ARR_LEN = 3; - int32_t m_intArrTestCase[INT_ARR_LEN] = {1, 3, 5}; - - dap_config_t * m_cfg = nullptr; - - QFile configFile; -private slots: - void initTestCase() { - configFile.setFileName(testconfigName + ".cfg"); - if(!configFile.open(QIODevice::WriteOnly)) - { - qDebug() << "[DapConfigTest] Cant create testing config"; - QVERIFY(false); - } - configFile.write(configForTesting.data(), configForTesting.length()); - configFile.close(); - - // init dir path for configs files - dap_config_init("."); - m_cfg = dap_config_open(testconfigName.data()); - } - - void configOpenFail() { - // by default search in /opt/dap/etc path - QVERIFY(dap_config_open("RandomNeverExistName") == NULL); - } - - void getInt() { - int32_t resultTTL = dap_config_get_item_int32(m_cfg, "server_options", "TTL_session_key"); - QCOMPARE(resultTTL, 600); - } - - void getDobule() { - double timeout = dap_config_get_item_double(m_cfg, "server_options", "timeout"); - QVERIFY(qFuzzyCompare(timeout, 1.0)); - } - - void getBool() { - bool rBool = dap_config_get_item_bool(m_cfg, "server_options", "vpn_enable"); - QCOMPARE(rBool, true); - rBool = dap_config_get_item_bool(m_cfg, "server_options", "proxy_enable"); - QCOMPARE(rBool, false); - } - - void arrayStr() { - uint16_t arraySize; - char ** result_arr = dap_config_get_array_str(m_cfg, "server_options", "str_arr", &arraySize); - - QVERIFY(result_arr != NULL); - QVERIFY(arraySize == STR_ARR_LEN); - - for(uint i = 0; i < arraySize; i++) { - QVERIFY(::strcmp(result_arr[i], m_strArrTestCase[i].data()) == 0 ); - } - } - - void arrayInt() { - uint16_t arraySize; - char ** result_arr = dap_config_get_array_str(m_cfg, "server_options", "int_arr", &arraySize); - - QVERIFY(result_arr != NULL); - QVERIFY(arraySize == INT_ARR_LEN); - - for(uint i = 0; i < arraySize; i++) { - QCOMPARE(::atoi(result_arr[i]), m_intArrTestCase[i]); - } - } - - void cleanupTestCase() { - configFile.remove(); - dap_config_close(m_cfg); - } -}; diff --git a/test/core/TestHeaders.hpp b/test/core/TestHeaders.hpp deleted file mode 100755 index 4d09745bf8a88a15c0610e5479d008f9bf2d5cf5..0000000000000000000000000000000000000000 --- a/test/core/TestHeaders.hpp +++ /dev/null @@ -1,2 +0,0 @@ -#pragma once -#include "DapConfig_test.hpp" diff --git a/test/core/dap_config_test.c b/test/core/dap_config_test.c new file mode 100644 index 0000000000000000000000000000000000000000..b831ee9b46d262a4f08a4cc6cc71ca9af36dd257 --- /dev/null +++ b/test/core/dap_config_test.c @@ -0,0 +1,116 @@ +#include "dap_config_test.h" + +static const char * testconfigName = "test_dap_config.cfg"; +static const char * config_data = "[db_options]\n" + "db_type=mongoDb\n" + "[server_options]\n" + "timeout=1,0\n" + "vpn_enable=true\n" + "proxy_enable=false\n" + "TTL_session_key=600\n" + "str_arr=[vasya, petya, grisha, petushok@microsoft.com]\n" + "int_arr=[1, 3, 5]\n"; + +static const size_t STR_ARR_LEN = 4; +static const char * str_add_test_case[] = { + "vasya", + "petya", + "grisha", + "petushok@microsoft.com" +}; +static const size_t INT_ARR_LEN = 3; +static const int32_t int_arr_test_cases[] = {1, 3, 5}; + + +static FILE * config_file; +static dap_config_t * config; + +void create_test_config_file() { + config_file = fopen(testconfigName, "w+"); + assert(config_file != NULL && + "Can't create config file"); + + fwrite(config_data, sizeof(char), + strlen(config_data), config_file); + + fclose(config_file); +} + +void init_test_case() { + create_test_config_file(); + + // init dir path for configs files + dap_config_init("."); + + config = dap_config_open("test_dap_config"); +} + +void cleanup_test_case() { + assert(remove("test_dap_config.cfg") == 0 && + "Error remove config file"); + dap_config_close(config); +} + +void test_config_open_fail() { + assert(dap_config_open("RandomNeverExistName") == NULL + && "configOpenFail failed"); +} + +void test_get_int() { + int32_t resultTTL = dap_config_get_item_int32(config, + "server_options", + "TTL_session_key"); + assert(resultTTL == 600 && "get_int failed"); +} + +void test_get_double() { + double timeout = dap_config_get_item_double(config, + "server_options", + "timeout"); + assert(timeout == 1.0 && "test_get_double failed"); +} + +void test_get_bool() { + bool rBool = dap_config_get_item_bool(config, "server_options", "vpn_enable"); + assert(rBool == true && "test_get_bool failed"); + rBool = dap_config_get_item_bool(config, "server_options", "proxy_enable"); + assert(rBool == false && "test_get_bool failed"); +} + +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); + + for(uint i = 0; i < arraySize; i++) { + assert(strcmp(result_arr[i], str_add_test_case[i]) == 0 && "test_array_str failed"); + } +} + +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"); + + for(uint i = 0; i < arraySize; i++) { + assert(atoi(result_arr[i]) == int_arr_test_cases[i] && "test_array_int failed"); + } +} + + +void dap_config_tests_run() { + printf("Start running dap_config_tests\n"); + init_test_case(); + + test_config_open_fail(); + test_get_int(); + test_get_bool(); + test_array_str(); + test_array_int(); + + cleanup_test_case(); +} diff --git a/test/core/dap_config_test.h b/test/core/dap_config_test.h new file mode 100755 index 0000000000000000000000000000000000000000..d6a2d6e2d004cecf736c4e7ff7dbe20e0b20032d --- /dev/null +++ b/test/core/dap_config_test.h @@ -0,0 +1,105 @@ +#pragma once +#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); +//#pragma once +//#include <QTest> +//#include <QDebug> +//#include "dap_config.h" + +//class DapConfigTest : public QObject { +// Q_OBJECT +//private: +// const QByteArray testconfigName = "test_dap_config"; +// const QByteArray configForTesting = "[db_options]\n" +// "db_type=mongoDb\n" +// "[server_options]\n" +// "timeout=1,0\n" +// "vpn_enable=true\n" +// "proxy_enable=false\n" +// "TTL_session_key=600\n" +// "str_arr=[vasya, petya, grisha, petushok@microsoft.com]\n" +// "int_arr=[1, 3, 5]\n"; +// static constexpr size_t STR_ARR_LEN = 4; +// std::string m_strArrTestCase[STR_ARR_LEN] = {"vasya", "petya", +// "grisha", "petushok@microsoft.com"}; + +// static constexpr size_t INT_ARR_LEN = 3; +// int32_t m_intArrTestCase[INT_ARR_LEN] = {1, 3, 5}; + +// dap_config_t * m_cfg = nullptr; + +// QFile configFile; +//private slots: +// void initTestCase() { +// configFile.setFileName(testconfigName + ".cfg"); +// if(!configFile.open(QIODevice::WriteOnly)) +// { +// qDebug() << "[DapConfigTest] Cant create testing config"; +// QVERIFY(false); +// } +// configFile.write(configForTesting.data(), configForTesting.length()); +// configFile.close(); + +// // init dir path for configs files +// dap_config_init("."); +// m_cfg = dap_config_open(testconfigName.data()); +// } + +// void configOpenFail() { +// // by default search in /opt/dap/etc path +// QVERIFY(dap_config_open("RandomNeverExistName") == NULL); +// } + +// void getInt() { +// int32_t resultTTL = dap_config_get_item_int32(m_cfg, "server_options", "TTL_session_key"); +// QCOMPARE(resultTTL, 600); +// } + +// void getDobule() { +// double timeout = dap_config_get_item_double(m_cfg, "server_options", "timeout"); +// QVERIFY(qFuzzyCompare(timeout, 1.0)); +// } + +// void getBool() { +// bool rBool = dap_config_get_item_bool(m_cfg, "server_options", "vpn_enable"); +// QCOMPARE(rBool, true); +// rBool = dap_config_get_item_bool(m_cfg, "server_options", "proxy_enable"); +// QCOMPARE(rBool, false); +// } + +// void arrayStr() { +// uint16_t arraySize; +// char ** result_arr = dap_config_get_array_str(m_cfg, "server_options", "str_arr", &arraySize); + +// QVERIFY(result_arr != NULL); +// QVERIFY(arraySize == STR_ARR_LEN); + +// for(uint i = 0; i < arraySize; i++) { +// QVERIFY(::strcmp(result_arr[i], m_strArrTestCase[i].data()) == 0 ); +// } +// } + +// void arrayInt() { +// uint16_t arraySize; +// char ** result_arr = dap_config_get_array_str(m_cfg, "server_options", "int_arr", &arraySize); + +// QVERIFY(result_arr != NULL); +// QVERIFY(arraySize == INT_ARR_LEN); + +// for(uint i = 0; i < arraySize; i++) { +// QCOMPARE(::atoi(result_arr[i]), m_intArrTestCase[i]); +// } +// } + +// void cleanupTestCase() { +// configFile.remove(); +// dap_config_close(m_cfg); +// } +//}; diff --git a/test/core/main.c b/test/core/main.c new file mode 100755 index 0000000000000000000000000000000000000000..17a9c612d16e98c8cb8e4131d4b828fd2f260a4b --- /dev/null +++ b/test/core/main.c @@ -0,0 +1,8 @@ +#include "dap_config_test.h" +#include "dap_common.h" + +int main(void) { + // switch off debug info from library + set_log_level(L_CRITICAL); + dap_config_tests_run(); +} diff --git a/test/core/main.cpp b/test/core/main.cpp deleted file mode 100755 index 4d562182c3f475b34147bc6fc048f61ecaa63d17..0000000000000000000000000000000000000000 --- a/test/core/main.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include <QTest> -#include <QDebug> -#include "dap_common.h" - -#define RUN_TESTS(TestObject) { \ - TestObject tc; \ - if(QTest::qExec(&tc)) \ - exit(1); } - -/* comment this and add RUN_TESTS in main function - * for run and debugging one testing class */ -#define RUN_ALL_TESTS - -#ifdef RUN_ALL_TESTS -#include "TestHeaders.hpp" -void run_all_tests() { - RUN_TESTS(DapConfigTest) -} -#endif - - -int main(int argc, char *argv[]) -{ - QCoreApplication app(argc, argv); - app.setAttribute(Qt::AA_Use96Dpi, true); - QTEST_SET_MAIN_SOURCE_PATH -#ifdef RUN_ALL_TESTS - // switch off debug info from library - set_log_level(L_CRITICAL); - run_all_tests(); -#endif -} diff --git a/test/core/test_headers.h b/test/core/test_headers.h new file mode 100755 index 0000000000000000000000000000000000000000..5b9b6df475c2ccdd82539047f0eeaf6c8fdd029c --- /dev/null +++ b/test/core/test_headers.h @@ -0,0 +1,3 @@ +#pragma once + +#include "dap_config_test.h"