diff --git a/examples/dll_installed_doctest/CMakeLists.txt b/examples/dll_installed_doctest/CMakeLists.txt new file mode 100644 index 000000000..3fd9325c4 --- /dev/null +++ b/examples/dll_installed_doctest/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.0) + +project(example_dll VERSION 0.0.1 LANGUAGES CXX) + +find_package(doctest REQUIRED) + +add_library("dll" SHARED dll.cpp) +target_compile_features("dll" PRIVATE cxx_std_17) +target_compile_definitions("dll" PUBLIC -D_EXPORT) +target_link_libraries("dll" doctest::doctest) + +add_executable(${PROJECT_NAME} main.cpp) +target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17) +target_link_libraries(${PROJECT_NAME} dll) \ No newline at end of file diff --git a/examples/dll_installed_doctest/dll.cpp b/examples/dll_installed_doctest/dll.cpp new file mode 100644 index 000000000..63a577b9b --- /dev/null +++ b/examples/dll_installed_doctest/dll.cpp @@ -0,0 +1,22 @@ +#define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL +#define DOCTEST_CONFIG_IMPLEMENT +#include + +#include "dll.h" +#include + +extern "C" { +void say_hello_dll() { printf("%s", "Hello, World!\n"); } +} + +int factorial(int number) { + return number < 1 ? 1 : number <= 1 ? number : factorial(number - 1) * number; +} + +TEST_CASE("testing the factorial function") { + CHECK(factorial(0) == 1); + CHECK(factorial(1) == 1); + CHECK(factorial(2) == 2); + CHECK(factorial(3) == 6); + CHECK(factorial(10) == 3628800); +} \ No newline at end of file diff --git a/examples/dll_installed_doctest/dll.h b/examples/dll_installed_doctest/dll.h new file mode 100644 index 000000000..7f4b59481 --- /dev/null +++ b/examples/dll_installed_doctest/dll.h @@ -0,0 +1,7 @@ +#pragma once + +#include "exporting.h" + +extern "C" { +DLL_API void say_hello_dll(); +} \ No newline at end of file diff --git a/examples/dll_installed_doctest/exporting.h b/examples/dll_installed_doctest/exporting.h new file mode 100644 index 000000000..a0f624bef --- /dev/null +++ b/examples/dll_installed_doctest/exporting.h @@ -0,0 +1,15 @@ +#pragma once + +#ifdef _EXPORT +#ifdef _MSC_VER +#define DLL_API __declspec(dllexport) +#elif defined __GNUC__ +#define DLL_API __attribute__((visibility("default"))) +#endif +#else +#ifdef _MSC_VER +#define DLL_API __declspec(dllimport) +#elif defined __GNUC__ +#define DLL_API __attribute__((visibility("hidden"))) +#endif +#endif \ No newline at end of file diff --git a/examples/dll_installed_doctest/main.cpp b/examples/dll_installed_doctest/main.cpp new file mode 100644 index 000000000..307e93367 --- /dev/null +++ b/examples/dll_installed_doctest/main.cpp @@ -0,0 +1,34 @@ +#define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL +#include + +#include "dll.h" + +int main(int argc, char **argv) { + doctest::Context context; + context.applyCommandLine(argc, argv); + + int res = context.run(); // run doctest + + // important - query flags (and --exit) rely on the user doing this + if (context.shouldExit()) { + // propagate the result of the tests + return res; + } + + say_hello_dll(); // test dll func +} + +int square(const int number) { return number * number; } + +TEST_CASE("testing the square function") { + CHECK(square(2) == 4); + CHECK(square(4) == 16); + CHECK(square(5) == 25); + CHECK(square(8) == 64); +} + +// running notes +// ./example_dll --no-run (run normal program) +// ./example_dll --exit (run tests then exit) +// ./example_dll (run tests then run program) +// ./example_dll --success (print successful test casts) \ No newline at end of file diff --git a/examples/executable_installed_doctest/CMakeLists.txt b/examples/executable_installed_doctest/CMakeLists.txt new file mode 100644 index 000000000..6182a8a49 --- /dev/null +++ b/examples/executable_installed_doctest/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 3.0) + +project(example_exe VERSION 0.0.1 LANGUAGES CXX) + +find_package(doctest REQUIRED) + +add_executable(${PROJECT_NAME} main.cpp) +target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17) +target_link_libraries(${PROJECT_NAME} doctest::doctest) \ No newline at end of file diff --git a/examples/executable_installed_doctest/main.cpp b/examples/executable_installed_doctest/main.cpp new file mode 100644 index 000000000..e07e6a8e7 --- /dev/null +++ b/examples/executable_installed_doctest/main.cpp @@ -0,0 +1,35 @@ +#define DOCTEST_CONFIG_IMPLEMENT +#include + +int main(int argc, char **argv) { + doctest::Context context; + context.applyCommandLine(argc, argv); + + int res = context.run(); // run doctest + + // important - query flags (and --exit) rely on the user doing this + if (context.shouldExit()) { + // propagate the result of the tests + return res; + } + + printf("%s", "Hello, World!"); +} + +int factorial(const int number) { + return number < 1 ? 1 : number <= 1 ? number : factorial(number - 1) * number; +} + +TEST_CASE("testing the factorial function") { + CHECK(factorial(0) == 1); + CHECK(factorial(1) == 1); + CHECK(factorial(2) == 2); + CHECK(factorial(3) == 6); + CHECK(factorial(10) == 3628800); +} + +// running notes +// ./example_exe --no-run (run normal program) +// ./example_exe --exit (run tests then exit) +// ./example_exe (run tests then run program) +// ./example_exe --success (print successful test casts) \ No newline at end of file