Skip to content

Commit

Permalink
Merge pull request #13 from rodneylab/test__add_catch2_skeleton
Browse files Browse the repository at this point in the history
test: add Catch2 skeleton
  • Loading branch information
rodneylab authored Apr 20, 2024
2 parents 4ae7348 + 22e209a commit 39f3b67
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 84 deletions.
17 changes: 2 additions & 15 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
strategy:
fail-fast: false
matrix:
language: ['c-cpp', 'python']
language: ['c-cpp']
# CodeQL supports [ 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'swift' ]
# Use only 'java-kotlin' to analyze code written in Java, Kotlin or both
# Use only 'javascript-typescript' to analyze code written in JavaScript, TypeScript or both
Expand All @@ -54,22 +54,9 @@ jobs:
# Prefix the list here with "+" to use these queries and those in the config file.
# If the Autobuild fails above, remove it and uncomment the following three lines.
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.
- if: matrix.language == 'python'
# For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
# queries: security-extended,security-and-quality

# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun

# Autobuild attempts to build any compiled languages (C/C++, C#, Go, Java, or Swift).
# If this step fails, then you should remove it and run the build manually (see below)
# - name: Autobuild
# uses: github/codeql-action/autobuild@v3
name: Autobuild
uses: github/codeql-action/autobuild@df5a14dc28094dc936e103b37d749c6628682b60 # v3.25.0
- if: matrix.language == 'c-cpp'
name: install
run: sudo apt-get update && sudo apt-get install gcovr lcov
run: sudo apt-get update && sudo apt-get install lcov libxrandr-dev libxcursor-dev libudev-dev libopenal-dev libflac-dev libvorbis-dev libgl1-mesa-dev libegl1-mesa-dev freeglut3-dev libxinerama-dev libxi-dev
- if: matrix.language == 'c-cpp'
name: Configure
shell: bash
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ jobs:
uses: aminya/setup-cpp@290824452986e378826155f3379d31bce8753d76 # v0.37.0
with:
clangtidy: true
cmake: true
compiler: llvm
- name: configure
run: |
cmake -H. -Bbuild -G "Unix Makefiles" -DCMAKE_BUILD_TYPE="Debug"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
- name: Generate JSON coverage report
working-directory: ./build
run: |
gcovr -r .. . --filter ../src/ --txt-metric branch --cobertura > coverage.xml
gcovr -r .. . --filter ../src/ --branch --cobertura > coverage.xml
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@84508663e988701840491b86de86b666e8a86bed # v4.3.0
env:
Expand Down
76 changes: 11 additions & 65 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,74 +27,14 @@ target_compile_options(
include(cmake/StaticAnalysers.cmake)
enable_clang_tidy()

set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${PROJECT_SOURCE_DIR}/cmake")
option(RUN_TESTS "Enable tests" ON)
include(coverage)
add_coverage_target("Catch_tests/*")

# The configurations we support
set(CMAKE_CONFIGURATION_TYPES "Debug;Release;Distribution")

# if(MSVC) # 64 bit architecture set(CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE
# "x64")
#
# # Set runtime library if(USE_STATIC_MSVC_RUNTIME_LIBRARY)
# set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>") else()
# set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
# endif()
#
# # Set general compiler flags set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}
# /Zc:__cplusplus /Gm- /Wall /WX /MP /nologo /diagnostics:classic /FC
# /fp:except- /Zc:inline" )
#
# # Optionally generate debug symbols if(GENERATE_DEBUG_SYMBOLS)
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zi") endif()
#
# # Remove any existing compiler flag that enables RTTI string(REPLACE "/GR" ""
# CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
#
# # Set compiler flag for disabling RTTI set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}
# /GR-")
#
# if("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "ARM") # On ARM the exception handling
# flag is missing which causes warnings set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}
# /EHsc") endif()
#
# # Set compiler flags for various configurations set(CMAKE_CXX_FLAGS_DEBUG "/GS
# /Od /Ob0 /RTC1") set(CMAKE_CXX_FLAGS_RELEASE "/GS- /Gy /O2 /Oi /Ot")
# set(CMAKE_CXX_FLAGS_DISTRIBUTION "/GS- /Gy /O2 /Oi /Ot")
#
# # Set linker flags if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
# if(CROSS_PLATFORM_DETERMINISTIC) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}
# /fp:precise") else() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /fp:fast") #
# Clang doesn't use # fast math because it # cannot be turned off # inside a
# single # compilation unit endif() elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL
# "Clang") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /showFilenames")
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Qunused-arguments") # Clang emits #
# warnings # about unused # arguments # such as /MP # and /GL endif() else() #
# Set general compiler flags set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall
# -Werror")
#
# # Optionally generate debug symbols if(GENERATE_DEBUG_SYMBOLS)
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g") endif()
#
# if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") # Also disable
# -Wstringop-overflow or it will generate false positives that # can't be
# disabled from code when link-time optimizations are enabled Also # turn off
# automatic fused multiply add contractions, there doesn't seem to # be a way to
# do this selectively through the macro JPH_PRECISE_MATH_OFF set(CMAKE_CXX_FLAGS
# "${CMAKE_CXX_FLAGS} -Wno-stringop-overflow -ffp-contract=off") else() # Do not
# use -ffast-math since it cannot be turned off in a single # compilation unit
# under clang, see Core.h set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}
# -ffp-model=precise")
#
# # On clang 14 and later we can turn off float contraction through a pragma, #
# older versions and deterministic versions need it off always, see Core.h
# if(CMAKE_CXX_COMPILER_VERSION LESS 14 OR CROSS_PLATFORM_DETERMINISTIC)
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffp-contract=off") endif() endif()
#
# # Set compiler flags for various configurations set(CMAKE_CXX_FLAGS_DEBUG "")
# set(CMAKE_CXX_FLAGS_RELEASE "-O3") set(CMAKE_CXX_FLAGS_DISTRIBUTION "-O3")
#
# # Set linker flags if(NOT ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows"))
# set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread") endif()
# endif()

# Set linker flags
set(CMAKE_EXE_LINKER_FLAGS_DISTRIBUTION "${CMAKE_EXE_LINKER_FLAGS_RELEASE}")

Expand Down Expand Up @@ -124,3 +64,9 @@ target_compile_definitions(

# Make this project the startup project
set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT "JoltRaylibHelloWorld")

option(RUN_UNIT_TESTS "Run Catch2 unit tests" ON)
if(RUN_UNIT_TESTS)
enable_testing()
add_subdirectory(Catch_tests)
endif()
16 changes: 16 additions & 0 deletions Catch_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
find_package(Catch2 3)
if(NOT Catch2_FOUND)
cpmaddpackage("gh:catchorg/Catch2#8ac8190e494a381072c89f5e161b92a08d98b37b"
)# v3.5.3
endif()

list(APPEND CMAKE_MODULE_PATH ${Catch2_SOURCE_DIR}/extras)

add_executable(Catch_tests_run test.cpp)

target_link_libraries(Catch_tests_run PRIVATE jolt_raylib_hello_world_compiler_flags)
target_link_libraries(Catch_tests_run PRIVATE Catch2::Catch2WithMain)
target_include_directories(Catch_tests_run PUBLIC "${PROJECT_SOURCE_DIR}/src")

include(Catch)
catch_discover_tests(Catch_tests_run)
17 changes: 17 additions & 0 deletions Catch_tests/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_floating_point.hpp>

#include <cstdint>

uint32_t factorial(uint32_t number)
{
return number <= 1 ? number : factorial(number - 1) * number;
}

TEST_CASE("Factorials are computed", "[factorial]")
{
REQUIRE(factorial(1) == 1);
REQUIRE(factorial(2) == 2);
REQUIRE(factorial(3) == 6);
REQUIRE(factorial(10) == 3'628'800);
}
6 changes: 3 additions & 3 deletions Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ set(USE_F16C ON)
set(USE_FMADD ON)

# Requires C++ 17
#set(CMAKE_CXX_STANDARD 17)
#set(CMAKE_CXX_STANDARD_REQUIRED ON)
#set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

function(jolt_raylib_hello_world_setup_dependencies)
message(STATUS "Include Dear ImGui")
Expand Down
63 changes: 63 additions & 0 deletions cmake/coverage.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Source: https://github.com/cmake-modules/lcov
#
# MIT License
#
# Copyright (c) 2020 cmake-modules
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

function(add_coverage_target exclude)

find_program(GCOV gcov)
if (NOT GCOV)
message(WARNING "program gcov not found")
endif()

find_program(LCOV lcov)
if (NOT LCOV)
message(WARNING "program lcov not found")
endif()

find_program(GENHTML genhtml)
if (NOT GENHTML)
message(WARNING "program genhtml not found")
endif()

if (LCOV AND GCOV AND GENHTML)
#set(covname cov.info)
set(covname lcov.txt)
add_compile_options(-fprofile-arcs -ftest-coverage)
add_link_options(--coverage)
add_custom_target(cov DEPENDS ${covname})
add_custom_command(
OUTPUT ${covname}
COMMAND ${LCOV} -c -o ${covname} -d . -b . --gcov-tool ${GCOV}
COMMAND ${LCOV} -r ${covname} -o ${covname} ${exclude} --exclude "*/SFML/System" --exclude "*/catch2/*" --exclude "v1/*"
COMMAND ${LCOV} -l ${covname}
COMMAND ${GENHTML} ${covname} -output coverage
COMMAND ${LCOV} -l ${covname} 2>/dev/null | grep Total | sed 's/|//g' | sed 's/Total://g' | awk '{print $1}' | sed s/%//g > coverage/total
)
set_directory_properties(PROPERTIES
ADDITIONAL_CLEAN_FILES ${covname}
)
else()
message(WARNING "unable to add target `cov`: missing coverage tools")
endif()

endfunction()

0 comments on commit 39f3b67

Please sign in to comment.