Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add two very simple examples of using doctest with CMake (#209)
* add some very simple examples of using doctest after having installed it using cmake * ran clang-format * update naming in comments
- Loading branch information
Showing
7 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL | ||
#define DOCTEST_CONFIG_IMPLEMENT | ||
#include <doctest/doctest.h> | ||
|
||
#include "dll.h" | ||
#include <stdio.h> | ||
|
||
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#pragma once | ||
|
||
#include "exporting.h" | ||
|
||
extern "C" { | ||
DLL_API void say_hello_dll(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL | ||
#include <doctest/doctest.h> | ||
|
||
#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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#define DOCTEST_CONFIG_IMPLEMENT | ||
#include <doctest/doctest.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; | ||
} | ||
|
||
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) |