Skip to content

Commit 4aac52c

Browse files
authored
Add CPP unit tests (#116)
* Update test.yml * Add gtest and example * Remove (nonfunctional) nested options deserializer. Add unit tests for legacy deserialization * upgrade .yml to upload-artifact@v4
1 parent c4b8aac commit 4aac52c

35 files changed

Lines changed: 1112 additions & 407 deletions

.github/workflows/build.yml

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
cpack
3232
cp oneD*.pkg ../svOneDSolver-macOS.pkg
3333
- name: Upload artifact
34-
uses: actions/upload-artifact@v3
34+
uses: actions/upload-artifact@v4
3535
with:
3636
name: macOS Installer
3737
path: svOneDSolver-macOS.pkg
@@ -67,7 +67,7 @@ jobs:
6767
cpack
6868
cp oneD*.deb ../svOneDSolver-Ubuntu.deb
6969
- name: Upload artifact
70-
uses: actions/upload-artifact@v3
70+
uses: actions/upload-artifact@v4
7171
with:
7272
name: Ubuntu Installer
7373
path: svOneDSolver-Ubuntu.deb
@@ -81,38 +81,41 @@ jobs:
8181
asset_name: svOneDSolver-${{github.ref_name}}-Ubuntu.deb
8282
tag: ${{ github.ref }}
8383

84-
build-windows:
85-
runs-on: windows-latest
86-
steps:
87-
- name: Checkout
88-
uses: actions/checkout@v3
89-
- name: Install Cygwin
90-
uses: cygwin/cygwin-install-action@master
91-
with:
92-
install-dir: C:\cygwin64
93-
packages: make tcl zip gzip tar patch wget
94-
- name: Install Visual Studio BuildTools
95-
run: |
96-
choco install visualstudio2017buildtools visualstudio2017-workload-vctools
97-
- name: Build
98-
run: |
99-
cd BuildWithMake
100-
dos2unix ./windows-build.sh
101-
C:\cygwin64\bin\bash.exe -c "./build_windows_github.bat"
102-
cp Release/sim*.msi ../svOneDSolver-Windows.msi
103-
- name: Upload artifact
104-
uses: actions/upload-artifact@v3
105-
with:
106-
name: Windows Installer
107-
path: svOneDSolver-Windows.msi
108-
if-no-files-found: error
109-
- name: Upload release asset
110-
if: startsWith(github.ref, 'refs/tags/')
111-
uses: svenstaro/upload-release-action@v2
112-
with:
113-
repo_token: ${{ secrets.GITHUB_TOKEN }}
114-
file: svOneDSolver-Windows.msi
115-
asset_name: svOneDSolver-${{github.ref_name}}-Windows.msi
116-
tag: ${{ github.ref }}
84+
# TODO: There's an issue for fixing the Windows build.
85+
# Right now it's failing.
86+
#
87+
# build-windows:
88+
# runs-on: windows-latest
89+
# steps:
90+
# - name: Checkout
91+
# uses: actions/checkout@v3
92+
# - name: Install Cygwin
93+
# uses: cygwin/cygwin-install-action@master
94+
# with:
95+
# install-dir: C:\cygwin64
96+
# packages: make tcl zip gzip tar patch wget
97+
# - name: Install Visual Studio BuildTools
98+
# run: |
99+
# choco install visualstudio2017buildtools visualstudio2017-workload-vctools
100+
# - name: Build
101+
# run: |
102+
# cd BuildWithMake
103+
# dos2unix ./windows-build.sh
104+
# C:\cygwin64\bin\bash.exe -c "./build_windows_github.bat"
105+
# cp Release/sim*.msi ../svOneDSolver-Windows.msi
106+
# - name: Upload artifact
107+
# uses: actions/upload-artifact@v4
108+
# with:
109+
# name: Windows Installer
110+
# path: svOneDSolver-Windows.msi
111+
# if-no-files-found: error
112+
# - name: Upload release asset
113+
# if: startsWith(github.ref, 'refs/tags/')
114+
# uses: svenstaro/upload-release-action@v2
115+
# with:
116+
# repo_token: ${{ secrets.GITHUB_TOKEN }}
117+
# file: svOneDSolver-Windows.msi
118+
# asset_name: svOneDSolver-${{github.ref_name}}-Windows.msi
119+
# tag: ${{ github.ref }}
117120

118121

.github/workflows/test.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ jobs:
44
test:
55
runs-on: ubuntu-latest
66
steps:
7-
- uses: actions/checkout@v2
7+
- uses: actions/checkout@v3
88
- name: build solver
99
shell: bash
1010
run: |
@@ -19,4 +19,8 @@ jobs:
1919
pip install numpy
2020
- name: result tests
2121
run: |
22-
pytest
22+
pytest Tests/SystemTests
23+
- name: result unit tests
24+
run: |
25+
cd build_skyline
26+
ctest

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ build
3838
.cproject
3939
.project
4040
.settings/
41+
.vscode/

CMakeLists.txt

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.12)
2+
CMAKE_MINIMUM_REQUIRED(VERSION 3.11)
33

44
# COMMON SETTINGS
55
PROJECT(OneDSolver)
@@ -9,6 +9,7 @@ ENABLE_LANGUAGE(C CXX)
99
OPTION(BUILD_SV_INSTALLER "Build SimVascular Installer" OFF)
1010
OPTION(buildPy "Build Python Interface" OFF)
1111
OPTION(buildDocs "Build Documentation" OFF)
12+
OPTION(ENABLE_UNIT_TEST "Enable unit tests" ON)
1213
SET(sparseSolverType "skyline" CACHE STRING "Use Sparse Solver")
1314
SET_PROPERTY(CACHE sparseSolverType PROPERTY STRINGS skyline superlu csparse)
1415

@@ -74,7 +75,7 @@ ENDIF()
7475
# FILE(COPY "${CMAKE_CURRENT_SOURCE_DIR}/tests" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
7576

7677
# COMPILER FLAGS
77-
SET(CMAKE_CXX_FLAGS "-g -m64 -O3 -std=c++0x -fPIC")
78+
SET(CMAKE_CXX_FLAGS "-g -m64 -O3 -std=c++20 -fPIC")
7879

7980
# PLACE EXECUTABLE IN BIN FOLDER
8081
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
@@ -109,3 +110,72 @@ ENDIF()
109110
if (BUILD_SV_INSTALLER)
110111
add_subdirectory("${CMAKE_SOURCE_DIR}/Distribution")
111112
ENDIF()
113+
114+
# Unit tests and Google Test
115+
if(ENABLE_UNIT_TEST)
116+
117+
# Link pthread on ubuntu20
118+
find_package(Threads REQUIRED)
119+
120+
# Install Google Test
121+
include(FetchContent)
122+
FetchContent_Declare(
123+
googletest
124+
URL https://github.com/google/googletest/archive/refs/heads/main.zip
125+
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
126+
)
127+
FetchContent_MakeAvailable(googletest)
128+
129+
enable_testing()
130+
include(GoogleTest)
131+
132+
set(TESTBIN_DIRECTORY ${CMAKE_BINARY_DIR}/testbin)
133+
134+
# Copy test files into the build directory so tests can access them
135+
file(GLOB TEST_FILES "${CMAKE_CURRENT_SOURCE_DIR}/Tests/UnitTests/TestFiles/*")
136+
foreach(TEST_FILE ${TEST_FILES})
137+
# Print the file being copied for debugging
138+
file(COPY ${TEST_FILE} DESTINATION ${TESTBIN_DIRECTORY}/TestFiles/)
139+
endforeach()
140+
141+
# For now, we're filtering out "main" so we can include all the
142+
# other source files in the same way as the main executable.
143+
#
144+
# It would probably be better if we refactored to avoid including
145+
# main in the overall list of source files. Instead, we could just
146+
# include main only when we're building the executable. That could
147+
# be a good first refactoring to avoid this hacky post-processing
148+
# step.
149+
set(SRC_C_FOR_TESTS ${SRC_C})
150+
list(FILTER SRC_C_FOR_TESTS EXCLUDE REGEX "Source/main.cxx")
151+
152+
set(SRC_H_FOR_TESTS ${SRC_H})
153+
list(FILTER SRC_H_FOR_TESTS EXCLUDE REGEX "Source/main.h")
154+
155+
# Find all .cpp files in the UnitTests folder and subfolders
156+
file(GLOB TEST_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/Tests/UnitTests/*.cxx")
157+
158+
# Combine all test files into a single list
159+
add_executable(UnitTestsExecutable ${TEST_SOURCES} ${SRC_C_FOR_TESTS} ${SRC_H_FOR_TESTS})
160+
161+
# For some solvers, we may need to link against additional libraries here.
162+
# When we bump up against unit tests that require that, we should consider
163+
# changing the strategy for how we generate executables in cmakelists to
164+
# handle both use cases.
165+
target_link_libraries(UnitTestsExecutable gtest gtest_main pthread)
166+
167+
# It's likely we'll need to include additional directories for some
168+
# versions of the unit tests. That can be updated when/if we update
169+
# the general strategy.
170+
target_include_directories(UnitTestsExecutable PRIVATE ${SRCS_DIR})
171+
172+
# Set the output directory for the executable
173+
set_target_properties(UnitTestsExecutable PROPERTIES
174+
RUNTIME_OUTPUT_DIRECTORY ${TESTBIN_DIRECTORY}
175+
)
176+
177+
# Add the test to CTest, we're setting the working directory to testbin
178+
# because we're copying the input files there for now
179+
add_test(NAME UnitTests COMMAND UnitTestsExecutable WORKING_DIRECTORY ${TESTBIN_DIRECTORY})
180+
181+
endif()

0 commit comments

Comments
 (0)