From 7fe453dcca57618499d14c653dc0d98edfa40fcb Mon Sep 17 00:00:00 2001 From: levalup Date: Sun, 16 Jun 2024 10:50:19 +0800 Subject: [PATCH] split test to tests and examples --- CMakeLists.txt | 61 +++------------------------ examples/CMakeLists.txt | 60 +++++++++++++++++++++++++++ {test => examples}/async.cpp | 0 examples/buf.cpp | 24 +++++++++++ {test => examples}/callback.cpp | 2 +- {test => examples}/fs.cpp | 0 {test => examples}/fs_event.cpp | 2 +- {test => examples}/getaddrinfo.cpp | 0 {test => examples}/idle.cpp | 0 {test => examples}/promise.cpp | 4 +- {test => examples}/queue.cpp | 4 +- {test => examples}/timer.cpp | 0 tests/CMakeLists.txt | 66 ++++++++++++++++++++++++++++++ {test => tests}/helloworld.cpp | 4 +- 14 files changed, 163 insertions(+), 64 deletions(-) create mode 100644 examples/CMakeLists.txt rename {test => examples}/async.cpp (100%) create mode 100644 examples/buf.cpp rename {test => examples}/callback.cpp (92%) rename {test => examples}/fs.cpp (100%) rename {test => examples}/fs_event.cpp (96%) rename {test => examples}/getaddrinfo.cpp (100%) rename {test => examples}/idle.cpp (100%) rename {test => examples}/promise.cpp (94%) rename {test => examples}/queue.cpp (85%) rename {test => examples}/timer.cpp (100%) create mode 100644 tests/CMakeLists.txt rename {test => tests}/helloworld.cpp (79%) diff --git a/CMakeLists.txt b/CMakeLists.txt index e10a00d..7a1fcc2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,61 +16,10 @@ include_directories(libuv/include include) link_libraries(libuv::libuv) file(GLOB_RECURSE HEADERS ${PROJECT_SOURCE_DIR}/include/*.h) -file(GLOB TESTS ${PROJECT_SOURCE_DIR}/test/*) -set(TARGETS) +# build examples +add_subdirectory(examples) -foreach (path ${TESTS}) - if (IS_DIRECTORY ${path}) - string(REGEX MATCH "[^/]*$" dir "${path}") - - # check ignore - set(NEED_IGNORE 0) - foreach (pattern ${IGNORE}) - string(REGEX MATCH "^${pattern}$" matched "${dir}") - if (NOT "${matched}" STREQUAL "") - set(NEED_IGNORE 1) - break() - endif () - endforeach () - if (${NEED_IGNORE}) - message(STATUS "==[ Ignore: ${dir}") - continue() - endif () - - file(GLOB_RECURSE DIR_SRC ${path}/*.cxx ${path}/*.cpp ${path}/*.cc ${path}/*.c) - if ("${DIR_SRC}" STREQUAL "") - continue() - endif () - - # check if target exists - list(FIND TARGETS "${dir}" TARGET_INDEX) - if(TARGET_INDEX GREATER -1) - target_sources(${dir} PUBLIC ${DIR_SRC}) - continue() - endif() - - # add executable - add_executable(${dir} ${DIR_SRC} ${HEADERS}) - list(APPEND TARGETS ${dir}) - message(STATUS "==[ Add executable: ${dir}") - else () - string(REGEX MATCH "[^/]*.[(c)|(cc)|(cpp)|(cxx)]$" file_ext "${path}") - if ("${file_ext}" STREQUAL "") - continue() - endif () - string(REGEX MATCH "^[^.]*" file "${file_ext}") - - # check if target exists - list(FIND TARGETS "${file}" TARGET_INDEX) - if(TARGET_INDEX GREATER -1) - target_sources(${file} PUBLIC ${path}) - continue() - endif() - - # add executable - add_executable(${file} ${path} ${HEADERS}) - list(APPEND TARGETS ${file}) - message(STATUS "==[ Add executable: ${file}") - endif () -endforeach () +# add tests +enable_testing() +add_subdirectory(tests) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 0000000..2ab8396 --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,60 @@ +set(PREFIX example) + +file(GLOB FILES ${CMAKE_CURRENT_SOURCE_DIR}/*) + +set(TARGETS) + +foreach (path ${FILES}) + if (IS_DIRECTORY ${path}) + string(REGEX MATCH "[^/]*$" dir "${path}") + + # check ignore + set(NEED_IGNORE 0) + foreach (pattern ${IGNORE}) + string(REGEX MATCH "^${pattern}$" matched "${dir}") + if (NOT "${matched}" STREQUAL "") + set(NEED_IGNORE 1) + break() + endif () + endforeach () + if (${NEED_IGNORE}) + message(STATUS "==[ Ignore: ${dir}") + continue() + endif () + + file(GLOB_RECURSE DIR_SRC ${path}/*.cxx ${path}/*.cpp ${path}/*.cc ${path}/*.c) + if ("${DIR_SRC}" STREQUAL "") + continue() + endif () + + # check if target exists + list(FIND TARGETS "${PREFIX}-${dir}" TARGET_INDEX) + if(TARGET_INDEX GREATER -1) + target_sources(${PREFIX}-${dir} PUBLIC ${DIR_SRC}) + continue() + endif() + + # add executable + add_executable(${PREFIX}-${dir} ${DIR_SRC} ${HEADERS}) + list(APPEND TARGETS "${PREFIX}-${dir}") + message(STATUS "==[ Add ${PREFIX}: ${dir}") + else () + string(REGEX MATCH "[^/]*.[(c)|(cc)|(cpp)|(cxx)]$" file_ext "${path}") + if ("${file_ext}" STREQUAL "") + continue() + endif () + string(REGEX MATCH "^[^.]*" file "${file_ext}") + + # check if target exists + list(FIND TARGETS "${PREFIX}-${file}" TARGET_INDEX) + if(TARGET_INDEX GREATER -1) + target_sources(${PREFIX}-${file} PUBLIC ${path}) + continue() + endif() + + # add executable + add_executable(${PREFIX}-${file} ${path} ${HEADERS}) + list(APPEND TARGETS "${PREFIX}-${file}") + message(STATUS "==[ Add ${PREFIX}: ${file}") + endif () +endforeach () diff --git a/test/async.cpp b/examples/async.cpp similarity index 100% rename from test/async.cpp rename to examples/async.cpp diff --git a/examples/buf.cpp b/examples/buf.cpp new file mode 100644 index 0000000..3f2734b --- /dev/null +++ b/examples/buf.cpp @@ -0,0 +1,24 @@ +// +// Created by Levalup. +// L.eval: Let programmer get rid of only work jobs. +// + +#include + +#include "uvcxx/buf.h" + +int main() { + // create copyable shared buf + uv::buf_t buf; + buf.resize(16); //< allocate memory + std::strcpy(buf.data(), "hello"); + buf.resize(8); //< reuse the memory + + // data still store "hello" + std::cout << buf.data() << " " << buf.size() << " " << buf.capacity(); + + // convert to uv's C-structure + uv_buf_t *uv_buf = buf; + + return 0; +} diff --git a/test/callback.cpp b/examples/callback.cpp similarity index 92% rename from test/callback.cpp rename to examples/callback.cpp index 807d186..c621863 100644 --- a/test/callback.cpp +++ b/examples/callback.cpp @@ -5,7 +5,7 @@ #include -#include "uvcxx/cxx/callback.h" +#include "uvcxx/utils/callback.h" int main() { using namespace uvcxx; diff --git a/test/fs.cpp b/examples/fs.cpp similarity index 100% rename from test/fs.cpp rename to examples/fs.cpp diff --git a/test/fs_event.cpp b/examples/fs_event.cpp similarity index 96% rename from test/fs_event.cpp rename to examples/fs_event.cpp index 21c787d..128b554 100644 --- a/test/fs_event.cpp +++ b/examples/fs_event.cpp @@ -8,7 +8,7 @@ #include "uvcxx/fs_event.h" #include "uvcxx/timer.h" -#include "uvcxx/utils/to_string.h" +#include "uvcxx/cxx/to_string.h" int main() { auto target = "a.txt"; diff --git a/test/getaddrinfo.cpp b/examples/getaddrinfo.cpp similarity index 100% rename from test/getaddrinfo.cpp rename to examples/getaddrinfo.cpp diff --git a/test/idle.cpp b/examples/idle.cpp similarity index 100% rename from test/idle.cpp rename to examples/idle.cpp diff --git a/test/promise.cpp b/examples/promise.cpp similarity index 94% rename from test/promise.cpp rename to examples/promise.cpp index 9d30193..5a01624 100644 --- a/test/promise.cpp +++ b/examples/promise.cpp @@ -5,7 +5,7 @@ #include -#include "uvcxx/cxx/promise.h" +#include "uvcxx/utils/promise.h" int main() { using namespace uvcxx; @@ -27,4 +27,4 @@ int main() { } return 0; -} \ No newline at end of file +} diff --git a/test/queue.cpp b/examples/queue.cpp similarity index 85% rename from test/queue.cpp rename to examples/queue.cpp index e3708d5..c8b5588 100644 --- a/test/queue.cpp +++ b/examples/queue.cpp @@ -5,8 +5,8 @@ #include -#include "uvcxx/cxx/queue.h" -#include "uvcxx/cxx/callback.h" +#include "uvcxx/utils/queue.h" +#include "uvcxx/utils/callback.h" int main() { using namespace uvcxx; diff --git a/test/timer.cpp b/examples/timer.cpp similarity index 100% rename from test/timer.cpp rename to examples/timer.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..8b9982d --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,66 @@ +set(PREFIX test) + +file(GLOB FILES ${CMAKE_CURRENT_SOURCE_DIR}/*) + +set(TARGETS) + +foreach (path ${FILES}) + if (IS_DIRECTORY ${path}) + string(REGEX MATCH "[^/]*$" dir "${path}") + + # check ignore + set(NEED_IGNORE 0) + foreach (pattern ${IGNORE}) + string(REGEX MATCH "^${pattern}$" matched "${dir}") + if (NOT "${matched}" STREQUAL "") + set(NEED_IGNORE 1) + break() + endif () + endforeach () + if (${NEED_IGNORE}) + message(STATUS "==[ Ignore: ${dir}") + continue() + endif () + + file(GLOB_RECURSE DIR_SRC ${path}/*.cxx ${path}/*.cpp ${path}/*.cc ${path}/*.c) + if ("${DIR_SRC}" STREQUAL "") + continue() + endif () + + # check if target exists + list(FIND TARGETS "${PREFIX}-${dir}" TARGET_INDEX) + if(TARGET_INDEX GREATER -1) + target_sources(${PREFIX}-${dir} PUBLIC ${DIR_SRC}) + continue() + endif() + + # add executable + add_executable(${PREFIX}-${dir} ${DIR_SRC} ${HEADERS}) + list(APPEND TARGETS "${PREFIX}-${dir}") + message(STATUS "==[ Add ${PREFIX}: ${dir}") + else () + string(REGEX MATCH "[^/]*.[(c)|(cc)|(cpp)|(cxx)]$" file_ext "${path}") + if ("${file_ext}" STREQUAL "") + continue() + endif () + string(REGEX MATCH "^[^.]*" file "${file_ext}") + + # check if target exists + list(FIND TARGETS "${PREFIX}-${file}" TARGET_INDEX) + if(TARGET_INDEX GREATER -1) + target_sources(${PREFIX}-${file} PUBLIC ${path}) + continue() + endif() + + # add executable + add_executable(${PREFIX}-${file} ${path} ${HEADERS}) + list(APPEND TARGETS "${PREFIX}-${file}") + message(STATUS "==[ Add ${PREFIX}: ${file}") + endif () +endforeach () + +foreach (target ${TARGETS}) + string(REGEX MATCH "test-(.*)" name "${target}") + set(name ${CMAKE_MATCH_1}) + add_test(NAME ${name} COMMAND ${target}) +endforeach () diff --git a/test/helloworld.cpp b/tests/helloworld.cpp similarity index 79% rename from test/helloworld.cpp rename to tests/helloworld.cpp index a1eb5cf..2bead47 100644 --- a/test/helloworld.cpp +++ b/tests/helloworld.cpp @@ -10,8 +10,8 @@ int main() { uv::loop_t loop; // = uv::default_loop(); - std::cout << "Default loop." << std::endl; + std::cout << "using default loop" << std::endl; loop.run(); return 0; -} \ No newline at end of file +}