Skip to content

Commit

Permalink
split test to tests and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
levalup committed Jun 16, 2024
1 parent 417dbef commit 7fe453d
Show file tree
Hide file tree
Showing 14 changed files with 163 additions and 64 deletions.
61 changes: 5 additions & 56 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
60 changes: 60 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 ()
File renamed without changes.
24 changes: 24 additions & 0 deletions examples/buf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Created by Levalup.
// L.eval: Let programmer get rid of only work jobs.
//

#include <iostream>

#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;
}
2 changes: 1 addition & 1 deletion test/callback.cpp → examples/callback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include <cmath>

#include "uvcxx/cxx/callback.h"
#include "uvcxx/utils/callback.h"

int main() {
using namespace uvcxx;
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion test/fs_event.cpp → examples/fs_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions test/promise.cpp → examples/promise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include <cmath>

#include "uvcxx/cxx/promise.h"
#include "uvcxx/utils/promise.h"

int main() {
using namespace uvcxx;
Expand All @@ -27,4 +27,4 @@ int main() {
}

return 0;
}
}
4 changes: 2 additions & 2 deletions test/queue.cpp → examples/queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

#include <iostream>

#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;
Expand Down
File renamed without changes.
66 changes: 66 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 ()
4 changes: 2 additions & 2 deletions test/helloworld.cpp → tests/helloworld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 7fe453d

Please sign in to comment.