Skip to content

Commit 6e23b99

Browse files
committed
Add example static lib + unit tests
1 parent 3ab68cb commit 6e23b99

File tree

9 files changed

+66
-16
lines changed

9 files changed

+66
-16
lines changed

CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ endif()
1919
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
2020
include(Sanitizers)
2121

22+
# === Libraries ===
23+
add_subdirectory(src)
24+
2225
# === Application target ===
2326
add_executable(${PROJECT_NAME}
2427
src/main.cpp
2528
)
2629

27-
target_include_directories(${PROJECT_NAME}
28-
PRIVATE ${PROJECT_SOURCE_DIR}/include
30+
target_link_libraries(${PROJECT_NAME}
31+
PRIVATE arithmetic
2932
)
3033

3134
if(TARGET sanitizers)

include/.gitkeep

Whitespace-only changes.

include/math/arithmetic.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef MATH_ARITHMETIC_HPP
2+
#define MATH_ARITHMETIC_HPP
3+
4+
namespace math
5+
{
6+
7+
int add(int a, int b);
8+
int subtract(int a, int b);
9+
10+
} // namespace math
11+
12+
#endif

src/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
add_library(arithmetic STATIC
2+
arithmetic.cpp
3+
)
4+
5+
target_include_directories(arithmetic
6+
PUBLIC ${PROJECT_SOURCE_DIR}/include
7+
)
8+
9+
if(TARGET sanitizers)
10+
target_link_libraries(arithmetic PUBLIC sanitizers)
11+
endif()

src/arithmetic.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "math/arithmetic.hpp"
2+
3+
namespace math
4+
{
5+
6+
int add(int a, int b)
7+
{
8+
return a + b;
9+
}
10+
11+
int subtract(int a, int b)
12+
{
13+
return a - b;
14+
}
15+
16+
} // namespace math

src/main.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
#include <iostream> // cout
1+
#include <iostream>
2+
#include "math/arithmetic.hpp"
23

34
int main()
45
{
5-
std::cout << "Modern C++ project template.\n";
6+
const int a = 10;
7+
const int b = 4;
8+
9+
std::cout << "Add: " << math::add(a, b) << '\n';
10+
std::cout << "Subtract: " << math::subtract(a, b) << '\n';
11+
612
return 0;
713
}

tests/CMakeLists.txt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,11 @@ FetchContent_MakeAvailable(googletest)
1515
# === Unit Test Target ===
1616
add_executable(unit_tests
1717
test_main.cpp
18-
test_core.cpp
18+
test_arithmetic.cpp
1919
)
2020

2121
target_link_libraries(unit_tests
22-
PRIVATE gtest_main
23-
)
24-
25-
target_include_directories(unit_tests
26-
PRIVATE ${PROJECT_SOURCE_DIR}/include
22+
PRIVATE gtest_main arithmetic
2723
)
2824

2925
if(TARGET sanitizers)

tests/test_arithmetic.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <gtest/gtest.h>
2+
#include "math/arithmetic.hpp"
3+
4+
TEST(Arithmetic, Add)
5+
{
6+
EXPECT_EQ(math::add(2, 3), 5);
7+
}
8+
9+
TEST(Arithmetic, Subtract)
10+
{
11+
EXPECT_EQ(math::subtract(10, 3), 7);
12+
}

tests/test_core.cpp

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)