Skip to content

Commit b6b2b77

Browse files
authored
Merge pull request #45 from urbytes21/dev_branch_1
id 1772860828
2 parents d28310d + d0be11a commit b6b2b77

15 files changed

Lines changed: 496 additions & 247 deletions

File tree

.github/workflows/cpp-build-test-coverage.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ jobs:
7676
- name: Run tests
7777
run: |
7878
cd build
79-
ls -l
80-
./cpp_lab_project_unit_test
79+
ctest --output-on-failure
8180
8281
# -------------------------------------------------------
8382
# Step 5: Generate code coverage report

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*build
22
*private*
33
*.vscode
4-
*Identifier
4+
*Identifier
5+
*Testing*

CMakeLists.txt

Lines changed: 60 additions & 201 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
# ROOT CMAKE
2+
#
3+
# ┌─────────────────┴─────────────────┐
4+
# │ │
5+
# src module tests module
6+
# │ │
7+
# ┌────┴────┐ │
8+
# │ │ │
9+
# core socket │
10+
# │ │ │
11+
# └──────┬──┘ │
12+
# │ │
13+
# ▼ ▼
14+
# cpp_lab_project cpp_lab_project_unit_test
15+
# (main executable) (GoogleTest executable)
16+
117
cmake_minimum_required(VERSION 3.14)
218

319
# ----------------------------------------------------------------------------------------
@@ -9,34 +25,59 @@ project(cpp_lab_project # ${PROJECT_NAME}
925
LANGUAGES CXX
1026
)
1127

12-
# Build timestamp
13-
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/include)
28+
# ----------------------------------------------------------------------------------------
29+
# Output directories to build/bin
30+
# ----------------------------------------------------------------------------------------
31+
# Executables
32+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
33+
# Shared libraries
34+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
35+
# Static libraries
36+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
37+
38+
# ----------------------------------------------------------------------------------------
39+
# Compiler and language configuration
40+
# ----------------------------------------------------------------------------------------
41+
# Require at least C++17 for GoogleTest and modern C++ features
42+
set(CMAKE_CXX_STANDARD 20)
43+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
44+
45+
# Export compile_commands.json (useful for clang-tidy, clangd, IDEs)
46+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
47+
48+
# ----------------------------------------------------------------------------------------
49+
# Build metadata
50+
# ----------------------------------------------------------------------------------------
51+
# Ensure directory exists for generated headers
52+
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/generated)
53+
54+
# Generate build timestamp
1455
string(TIMESTAMP BUILD_TIME "%Y-%m-%d %H:%M:%S")
1556

57+
# Generate version header from template
1658
configure_file(
1759
${CMAKE_SOURCE_DIR}/include/version.h.in
1860
${CMAKE_BINARY_DIR}/generated/version.h
1961
)
2062

21-
set(PROJECT_NAME_TEST ${PROJECT_NAME}_unit_test) # name for the unit-test executable
22-
2363
# ----------------------------------------------------------------------------------------
24-
# Compiler and language configuration
64+
# External dependencies (GoogleTest,...)
2565
# ----------------------------------------------------------------------------------------
26-
# Require at least C++17 for GoogleTest and modern C++ features
27-
set(CMAKE_CXX_STANDARD 20)
28-
set(CMAKE_CXX_STANDARD_REQUIRED ON)
29-
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # Enable output of compile commands for clang-tidy
66+
include(cmake/Dependencies.cmake)
3067

31-
# Optionally enforce warnings (good for learning/debugging)
68+
# ----------------------------------------------------------------------------------------
69+
# Compiler warnings (useful for learning/debugging)
70+
# ----------------------------------------------------------------------------------------
3271
add_compile_options(-Wall -Wextra -Wpedantic)
3372

34-
message("C Compiler: ${CMAKE_C_COMPILER}")
35-
message("C++ Compiler: ${CMAKE_CXX_COMPILER}")
36-
message("C++ Compiler ID: ${CMAKE_CXX_COMPILER_ID}")
37-
message("C++ Compiler Version: ${CMAKE_CXX_COMPILER_VERSION}")
73+
message(STATUS "C Compiler: ${CMAKE_C_COMPILER}")
74+
message(STATUS "C++ Compiler: ${CMAKE_CXX_COMPILER}")
75+
message(STATUS "C++ Compiler ID: ${CMAKE_CXX_COMPILER_ID}")
76+
message(STATUS "C++ Compiler Version: ${CMAKE_CXX_COMPILER_VERSION}")
3877

39-
# Option to enable coverage
78+
# ----------------------------------------------------------------------------------------
79+
# Code coverage configuration
80+
# ----------------------------------------------------------------------------------------
4081
option(ENABLE_COVERAGE "Enable coverage reporting" OFF)
4182

4283
if(ENABLE_COVERAGE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
@@ -46,194 +87,12 @@ if(ENABLE_COVERAGE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
4687
endif()
4788

4889
# ----------------------------------------------------------------------------------------
49-
# Dependencies - GoogleTest
90+
# Enable CTest framework (used by GoogleTest)
5091
# ----------------------------------------------------------------------------------------
51-
# FetchContent allows downloading dependencies at configure time
52-
include(FetchContent)
53-
54-
# Declare GoogleTest dependency
55-
FetchContent_Declare(
56-
googletest
57-
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
58-
)
59-
60-
# Make GoogleTest available
61-
FetchContent_MakeAvailable(googletest)
62-
63-
# Enable CTest framework to run tests with `ctest`
6492
enable_testing()
6593

6694
# ----------------------------------------------------------------------------------------
67-
# Project directories and files
68-
# ----------------------------------------------------------------------------------------
69-
# Header files directory
70-
set(APP_HEADERS
71-
"include"
72-
"${CMAKE_BINARY_DIR}/generated" # cmake generated headers
73-
)
74-
75-
# Core source files
76-
set(APP_SOURCES
77-
"src/DeleteMe.cpp"
78-
"src/core/basics/InitializeVariable.cpp"
79-
"src/core/basics/Operations.cpp"
80-
"src/core/basics/TypeQualifier.cpp"
81-
"src/core/basics/ControlFlow.cpp"
82-
"src/core/linkage/Internal.cpp"
83-
"src/core/linkage/Linkage.cpp"
84-
"src/core/linkage/External.cpp"
85-
"src/core/linkage/sharing/Sharing.cpp"
86-
"src/core/linkage/sharing/external/constants.cpp"
87-
"src/core/datatypes/Fundamental.cpp"
88-
"src/core/datatypes/CArray.cpp"
89-
"src/core/datatypes/CReferences.cpp"
90-
"src/core/datatypes/CPointers.cpp"
91-
"src/core/datatypes/CEnum.cpp"
92-
"src/core/datatypes/CStruct.cpp"
93-
"src/core/datatypes/CUnion.cpp"
94-
"src/core/datatypes/TypeConVersions.cpp"
95-
96-
# # Class
97-
"src/core/datatypes/class/Friend.cpp"
98-
"src/core/datatypes/class/CConstructors.cpp"
99-
"src/core/datatypes/class/CDestructors.cpp"
100-
"src/core/datatypes/class/SallowDeepCopying.cpp"
101-
"src/core/datatypes/class/RoleOfThreeFiveZero.cpp"
102-
"src/core/string/CString.cpp"
103-
"src/patterns/structural/Adapter.cpp"
104-
"src/patterns/structural/Bridge.cpp"
105-
"src/patterns/structural/Proxy.cpp"
106-
"src/patterns/structural/Composite.cpp"
107-
"src/patterns/structural/Flyweight.cpp"
108-
"src/patterns/structural/Facade.cpp"
109-
"src/patterns/structural/Decorator.cpp"
110-
"src/patterns/behavioral/ChainOfCommand.cpp"
111-
"src/patterns/behavioral/Command.cpp"
112-
"src/patterns/behavioral/Iterator.cpp"
113-
"src/patterns/behavioral/Mediator.cpp"
114-
"src/patterns/behavioral/Memento.cpp"
115-
"src/patterns/behavioral/Visitor.cpp"
116-
"src/patterns/behavioral/TemplateMethod.cpp"
117-
"src/patterns/behavioral/Strategy.cpp"
118-
"src/patterns/behavioral/State.cpp"
119-
"src/patterns/behavioral/Observer.cpp"
120-
"src/patterns/creational/Singleton.cpp"
121-
"src/patterns/creational/FactoryMethod.cpp"
122-
"src/patterns/creational/AbstractFactory.cpp"
123-
"src/patterns/creational/Builder.cpp"
124-
"src/patterns/creational/Prototype.cpp"
125-
"src/core/datatypes/class/Relationship.cpp"
126-
"src/core/datatypes/class/VirtualBase.cpp"
127-
"src/core/datatypes/class/Binding.cpp"
128-
129-
# # Exceptions
130-
"src/core/exception/BasicHandle.cpp"
131-
"src/core/exception/ThrowNoexcept.cpp"
132-
133-
# # Streams
134-
"src/core/filehandle/IOStream.cpp"
135-
"src/core/filehandle/StringStream.cpp"
136-
"src/core/filehandle/FileIO.cpp"
137-
"src/core/filehandle/Directory.cpp"
138-
"src/core/filehandle/OutputFormatting.cpp"
139-
"src/core/filehandle/BinaryFileHandling.cpp"
140-
141-
# # Container
142-
"src/core/datatypes/container/sequence/Array.cpp"
143-
"src/core/datatypes/container/sequence/Vector.cpp"
144-
"src/core/datatypes/container/unordered/UnorderedMap.cpp"
145-
"src/core/expression/Lambda.cpp"
146-
"src/core/expression/FunctionPointer.cpp"
147-
"src/core/datatypes/container/adapter/Queue.cpp"
148-
"src/core/datatypes/container/adapter/Stack.cpp"
149-
"src/core/datatypes/container/sequence/Deque.cpp"
150-
"src/core/datatypes/container/associative/Set.cpp"
151-
152-
# # LC
153-
"src/leetcode/arrays/two_sum/TwoSum.cpp"
154-
"src/leetcode/arrays/median_two_arrays/MedianTwoSortedArrays.cpp"
155-
"src/leetcode/arrays/container_with_most_water/ContainerWithMostWater.cpp"
156-
"src/leetcode/arrays/longest_common_prefix/Solution.cpp"
157-
"src/leetcode/arrays/3sum/Solution.cpp"
158-
"src/leetcode/arrays/4sum/Solution.cpp"
159-
160-
# # Controller
161-
"src/controller/pid/pid.cpp"
162-
"src/controller/pid/PIDSim.cpp"
163-
164-
# # Smart Pointers
165-
"src/core/datatypes/smart_pointer/Unique.cpp"
166-
"src/core/datatypes/smart_pointer/Shared.cpp"
167-
"src/core/datatypes/smart_pointer/Weak.cpp"
168-
169-
# # Overloading
170-
"src/core/overloading/ArithmeticOperator.cpp"
171-
"src/core/overloading/IOOperator.cpp"
172-
"src/core/overloading/UnaryOperator.cpp"
173-
"src/core/overloading/ComparisonOperator.cpp"
174-
"src/core/overloading/InDecOperator.cpp"
175-
"src/core/overloading/SubscriptOperator.cpp"
176-
"src/core/overloading/ParenthesisOperator.cpp"
177-
"src/core/overloading/TypeCast.cpp"
178-
"src/core/overloading/AssignmentOperator.cpp"
179-
"src/core/overloading/ClassMemberAccessOperator.cpp"
180-
"src/core/overloading/AllocationOperator.cpp"
181-
182-
# # Date and Time
183-
"src/core/datetime/CTime.cpp"
184-
185-
# # Socket
186-
"src/socket/simple_tcp/TCPServer.cpp"
187-
"src/socket/simple_tcp/SimpleTCPServer.cpp"
188-
)
189-
190-
# Test files
191-
set(APP_TESTS
192-
"tests/DeleteMeTest.cpp"
193-
"tests/two_sum_ut.cpp"
194-
"tests/median_two_sorted_arrays_ut.cpp"
195-
"tests/container_with_most_water_ut.cpp"
196-
"tests/longest_common_prefix_ut.cpp"
197-
"tests/three_sum_ut.cpp"
198-
"tests/four_sum_ut.cpp"
199-
)
200-
201-
# ----------------------------------------------------------------------------------------
202-
# Main application target
203-
# ----------------------------------------------------------------------------------------
204-
add_executable(${PROJECT_NAME}
205-
${APP_SOURCES}
206-
"src/main.cpp"
207-
)
208-
209-
# Add header include path
210-
target_include_directories(${PROJECT_NAME}
211-
PRIVATE ${APP_HEADERS}
212-
)
213-
214-
# Optional: add project-specific compiler options
215-
target_compile_options(${PROJECT_NAME}
216-
PRIVATE -Wall -Wextra -Wpedantic
217-
)
218-
95+
# Add project modules
21996
# ----------------------------------------------------------------------------------------
220-
# Unit testing target
221-
# ----------------------------------------------------------------------------------------
222-
add_executable(${PROJECT_NAME_TEST}
223-
${APP_SOURCES}
224-
${APP_TESTS}
225-
)
226-
227-
target_include_directories(${PROJECT_NAME_TEST}
228-
PRIVATE ${APP_HEADERS} # PRIVATE for internal use within a target,
229-
)
230-
231-
# Link GoogleTest, GoogleMock to the test executable
232-
target_link_libraries(${PROJECT_NAME_TEST}
233-
GTest::gtest_main
234-
GTest::gmock_main
235-
)
236-
237-
# Register tests with CTest
238-
include(GoogleTest)
239-
gtest_discover_tests(${PROJECT_NAME_TEST})
97+
add_subdirectory(src)
98+
add_subdirectory(tests)

cmake/Dependencies.cmake

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
# ----------------------------------------------------------------------------------------
3+
# Dependencies - GoogleTest
4+
# ----------------------------------------------------------------------------------------
5+
# FetchContent allows downloading dependencies at configure time
6+
include(FetchContent)
7+
8+
# Declare GoogleTest dependency
9+
FetchContent_Declare(
10+
googletest
11+
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
12+
)
13+
14+
# Make GoogleTest available
15+
FetchContent_MakeAvailable(googletest)

run.sh

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,23 @@
66

77
set -e # Exit immediately if a command fails
88

9-
PROJECT_EXEC="./build/cpp_lab_project"
9+
PROJECT_EXEC="./build/bin/cpp_lab_project"
1010
BUILD_DIR="./build"
1111

12+
if [ ! -d "build" ]; then
13+
echo "Build directory not found. Creating build directory..."
14+
15+
rm -rf build
16+
mkdir build
17+
cd build || exit
18+
19+
cmake ..
20+
cd ..
21+
22+
else
23+
echo "Build directory already exists."
24+
fi
25+
1226
clear
1327
echo "=============================="
1428
echo " Starting build pipeline... "

src/CMakeLists.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# ----------------------------------------------------------------------------------------
2+
# Main application target
3+
# ----------------------------------------------------------------------------------------
4+
5+
# Add project submodules
6+
add_subdirectory(core)
7+
add_subdirectory(controller)
8+
add_subdirectory(patterns)
9+
add_subdirectory(socket)
10+
11+
12+
# main application executable does NOT link to this library.
13+
add_subdirectory(leetcode)
14+
15+
# Header files directory
16+
set(APP_HEADERS
17+
${PROJECT_SOURCE_DIR}/include
18+
${CMAKE_BINARY_DIR}/generated # cmake generated headers
19+
)
20+
21+
# Define the main application executable
22+
add_executable(${PROJECT_NAME}
23+
main.cpp
24+
${CORE_SOURCES}
25+
${SOCKET_SOURCES}
26+
)
27+
28+
# Add header include paths
29+
# PRIVATE -> this target only
30+
# PUBLIC -> this target + dependent targets
31+
# INTERFACE -> dependent targets only
32+
target_include_directories(${PROJECT_NAME}
33+
PRIVATE ${APP_HEADERS}
34+
)
35+
36+
# Optional: add project-specific compiler options
37+
target_compile_options(${PROJECT_NAME}
38+
PRIVATE -Wall -Wextra -Wpedantic
39+
)

0 commit comments

Comments
 (0)