Skip to content

Commit

Permalink
big update
Browse files Browse the repository at this point in the history
  • Loading branch information
franneck94 committed Mar 5, 2024
1 parent 7d97e72 commit 6118b00
Show file tree
Hide file tree
Showing 18 changed files with 137 additions and 96 deletions.
8 changes: 4 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",
"cmake.debugConfig": {
"args": [
"--verbose",
"true",
"--filename",
"${workspaceFolder}/app/test.json"
// "--verbose",
// "true",
// "--filename",
// "${workspaceFolder}/app/test.json"
],
"externalConsole": true
},
Expand Down
8 changes: 4 additions & 4 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
// "${workspaceFolder}/build/bin/main"
],
"args": [
"--verbose",
"true",
"--filename",
"${workspaceFolder}/app/test.json"
// "--verbose",
// "true",
// "--filename",
// "${workspaceFolder}/app/test.json"
],
"group": {
"kind": "build",
Expand Down
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ option(ENABLE_WARNINGS "Enable to add warnings to a target." ON)
option(ENABLE_WARNINGS_AS_ERRORS "Enable to treat warnings as errors." OFF)

option(ENABLE_TESTING "Enable a Unit Testing build." ON)
option(ENABLE_COVERAGE "Enable a Code Coverage build." OFF)
option(ENABLE_COVERAGE "Enable a Code Coverage build." ON)

option(ENABLE_CLANG_TIDY "Enable to add clang tidy." OFF)

Expand All @@ -35,8 +35,8 @@ option(ENABLE_CMAKE_FORMAT "Enable to add cmake-format." ON)
option(ENABLE_LTO "Enable to add Link Time Optimization." ON)

# Project/Library Names
set(LIBRARY_NAME "lib")
set(UNIT_TEST_NAME "unit_tests")
set(LIB_FOO_NAME "foo")
set(LIB_BAR_NAME "bar")
set(EXECUTABLE_NAME "main")

# CMAKE MODULES
Expand Down Expand Up @@ -157,6 +157,6 @@ install(
RUNTIME DESTINATION bin)

install(
TARGETS ${LIBRARY_NAME}
TARGETS ${LIB_FOO_NAME} ${LIB_BAR_NAME}
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib)
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ This is a template for C++ projects. What you get:
│ ├── ...
├── src
│ ├── CMakesLists.txt
│ ├── my_lib.h
│ └── my_lib.cc
│ ├── foo/
│ └── bar/
└── tests
├── CMakeLists.txt
└── main.cc
Expand Down
2 changes: 1 addition & 1 deletion app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ add_executable(${EXECUTABLE_NAME} ${APP_SOURCES})

target_link_libraries(
${EXECUTABLE_NAME}
PRIVATE ${LIBRARY_NAME}
PRIVATE ${LIB_FOO_NAME}
nlohmann_json::nlohmann_json
fmt::fmt
spdlog::spdlog
Expand Down
2 changes: 1 addition & 1 deletion app/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <spdlog/spdlog.h>

#include "config.hpp"
#include "my_lib.h"
#include "foo.h"

using json = nlohmann::json;
namespace fs = std::filesystem;
Expand Down
1 change: 0 additions & 1 deletion external/json
Submodule json deleted from 66f6b4
3 changes: 2 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
add_subdirectory(my_lib)
add_subdirectory(foo)
add_subdirectory(bar)
16 changes: 8 additions & 8 deletions src/my_lib/CMakeLists.txt → src/bar/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Sources and Headers
set(LIBRARY_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/my_lib.cc")
set(LIBRARY_HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/my_lib.h")
set(LIBRARY_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/bar.cc")
set(LIBRARY_HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/bar.h")
set(LIBRARY_INCLUDES "./" "${CMAKE_BINARY_DIR}/configured_files/include")

# MyLib Library
add_library(${LIBRARY_NAME} STATIC ${LIBRARY_SOURCES} ${LIBRARY_HEADERS})
target_include_directories(${LIBRARY_NAME} PUBLIC ${LIBRARY_INCLUDES})
add_library(${LIB_BAR_NAME} STATIC ${LIBRARY_SOURCES} ${LIBRARY_HEADERS})
target_include_directories(${LIB_BAR_NAME} PUBLIC ${LIBRARY_INCLUDES})
target_link_libraries(
${LIBRARY_NAME}
${LIB_BAR_NAME}
PRIVATE nlohmann_json::nlohmann_json
fmt::fmt
spdlog::spdlog
Expand All @@ -16,7 +16,7 @@ target_link_libraries(
if(${ENABLE_WARNINGS})
target_set_warnings(
TARGET
${LIBRARY_NAME}
${LIB_BAR_NAME}
ENABLE
${ENABLE_WARNINGS}
AS_ERRORS
Expand All @@ -26,11 +26,11 @@ endif()
if(${ENABLE_LTO})
target_enable_lto(
TARGET
${LIBRARY_NAME}
${LIB_BAR_NAME}
ENABLE
ON)
endif()

if(${ENABLE_CLANG_TIDY})
add_clang_tidy_to_target(${LIBRARY_NAME})
add_clang_tidy_to_target(${LIB_BAR_NAME})
endif()
13 changes: 13 additions & 0 deletions src/bar/bar.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <iostream>

#include "nlohmann/json.hpp"

#include "bar.h"

int fn_branch(bool do_branch1, bool do_branch2)
{
if (do_branch1 || do_branch2)
return 0;

return 1;
}
9 changes: 9 additions & 0 deletions src/bar/bar.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

template <typename T>
T summing(T number1, T number2)
{
return number1 + number2;
}

int fn_branch(bool do_branch1, bool do_branch2);
36 changes: 36 additions & 0 deletions src/foo/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Sources and Headers
set(LIBRARY_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/foo.cc")
set(LIBRARY_HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/foo.h")
set(LIBRARY_INCLUDES "./" "${CMAKE_BINARY_DIR}/configured_files/include")

# MyLib Library
add_library(${LIB_FOO_NAME} STATIC ${LIBRARY_SOURCES} ${LIBRARY_HEADERS})
target_include_directories(${LIB_FOO_NAME} PUBLIC ${LIBRARY_INCLUDES})
target_link_libraries(
${LIB_FOO_NAME}
PRIVATE nlohmann_json::nlohmann_json
fmt::fmt
spdlog::spdlog
cxxopts::cxxopts)

if(${ENABLE_WARNINGS})
target_set_warnings(
TARGET
${LIB_FOO_NAME}
ENABLE
${ENABLE_WARNINGS}
AS_ERRORS
${ENABLE_WARNINGS_AS_ERRORS})
endif()

if(${ENABLE_LTO})
target_enable_lto(
TARGET
${LIB_FOO_NAME}
ENABLE
ON)
endif()

if(${ENABLE_CLANG_TIDY})
add_clang_tidy_to_target(${LIB_FOO_NAME})
endif()
2 changes: 1 addition & 1 deletion src/my_lib/my_lib.cc → src/foo/foo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "nlohmann/json.hpp"

#include "my_lib.h"
#include "foo.h"

int print_hello_world()
{
Expand Down
File renamed without changes.
28 changes: 18 additions & 10 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
include(Catch)

set(TEST_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/main.cc")
set(TEST_HEADERS "")
add_executable(UnitTestFoo "test_foo.cc")
target_link_libraries(UnitTestFoo PUBLIC ${LIB_FOO_NAME})
target_link_libraries(UnitTestFoo PRIVATE Catch2::Catch2WithMain)

add_executable(${UNIT_TEST_NAME} ${TEST_SOURCES} ${TEST_HEADERS})

target_link_libraries(${UNIT_TEST_NAME} PUBLIC ${LIBRARY_NAME})
target_link_libraries(${UNIT_TEST_NAME} PRIVATE Catch2::Catch2WithMain)
add_executable(UnitTestBar "test_bar.cc")
target_link_libraries(UnitTestBar PUBLIC ${LIB_BAR_NAME})
target_link_libraries(UnitTestBar PRIVATE Catch2::Catch2WithMain)

target_set_warnings(
TARGET
${UNIT_TEST_NAME}
UnitTestFoo
ENABLE
${ENABLE_WARNINGS}
AS_ERRORS
${ENABLE_WARNINGS_AS_ERRORS})
target_set_warnings(
TARGET
UnitTestBar
ENABLE
${ENABLE_WARNINGS}
AS_ERRORS
${ENABLE_WARNINGS_AS_ERRORS})

catch_discover_tests(UnitTestFoo)
catch_discover_tests(UnitTestBar)

if(ENABLE_COVERAGE)
set(COVERAGE_MAIN "coverage")
set(COVERAGE_EXCLUDES
Expand All @@ -26,7 +36,7 @@ if(ENABLE_COVERAGE)
"${PROJECT_SOURCE_DIR}/tests/*"
"${PROJECT_SOURCE_DIR}/build/*"
"/usr/include/*")
set(COVERAGE_DEPENDENCIES ${UNIT_TEST_NAME})
set(COVERAGE_DEPENDENCIES UnitTestFoo UnitTestBar)

setup_target_for_coverage_lcov(
NAME
Expand All @@ -44,5 +54,3 @@ if(ENABLE_COVERAGE)
--title
"CPP Project Demo Coverage")
endif()

catch_discover_tests(${UNIT_TEST_NAME})
59 changes: 0 additions & 59 deletions tests/main.cc

This file was deleted.

17 changes: 17 additions & 0 deletions tests/test_bar.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <catch2/catch_test_macros.hpp>

#include "bar.h"

TEST_CASE("sum1")
{
REQUIRE(summing(0, 1) == 1);
REQUIRE(summing(1, 0) == 1);
}

TEST_CASE("branch1")
{
REQUIRE(fn_branch(true, false) == 0);
REQUIRE(fn_branch(true, true) == 0);
REQUIRE(fn_branch(false, true) == 0);
REQUIRE(fn_branch(false, false) == 1);
}
17 changes: 17 additions & 0 deletions tests/test_foo.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <catch2/catch_test_macros.hpp>

#include "foo.h"

TEST_CASE("factorial1")
{
REQUIRE(factorial(0) == 1);
REQUIRE(factorial(1) == 1);
REQUIRE(factorial(2) == 2);
REQUIRE(factorial(3) == 6);
REQUIRE(factorial(10) == 3628800);
}

TEST_CASE("helloworld1")
{
REQUIRE(print_hello_world() == 1);
}

0 comments on commit 6118b00

Please sign in to comment.