Skip to content

Commit

Permalink
Implement fpp-style macros CHECK and ASSERT
Browse files Browse the repository at this point in the history
  • Loading branch information
aradi committed Aug 18, 2024
1 parent ab39c4f commit 5a65f90
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 7 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ jobs:
CMAKE_PREFIX_PATH=${INSTALL_DIR} cmake -B ${BUILD_DIR} -G Ninja test/export
cmake --build ${BUILD_DIR}
${BUILD_DIR}/app/testapp
${BUILD_DIR}/app/testapp_fpp
${BUILD_DIR}/app/testapp_fypp
rm -rf ${BUILD_DIR}
Expand Down
4 changes: 2 additions & 2 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ target_sources(
)
target_link_libraries(fortuno_example_testapp PRIVATE fortuno_example_mylib Fortuno::fortuno_serial)


add_subdirectory(fpp)
if (FYPP)
add_subdirectory(fypp)
endif ()
endif ()
37 changes: 37 additions & 0 deletions example/fpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# This file is part of Fortuno.
# Licensed under the BSD-2-Clause Plus Patent license.
# SPDX-License-Identifier: BSD-2-Clause-Patent

list(APPEND CMAKE_MESSAGE_CONTEXT Fpp)

add_library(fortuno_example_fpp_mylib)
set_target_properties(
fortuno_example_fpp_mylib PROPERTIES
OUTPUT_NAME mylib
)
target_sources(
fortuno_example_fpp_mylib PRIVATE
mylib_fpp.f90
)

add_executable(fortuno_example_fpp_testapp)
set_target_properties(
fortuno_example_fpp_testapp PROPERTIES
OUTPUT_NAME testapp_fpp
)
target_sources(
fortuno_example_fpp_testapp PRIVATE
simple_fpp_tests.F90
testapp_fpp.f90
)
target_link_libraries(
fortuno_example_fpp_testapp PRIVATE
fortuno_example_fpp_mylib
Fortuno::fortuno_serial
Fortuno::fortuno_include_dir
)
# GNU compiler (as of version 13.2) needs special flag to cope with extra long lines
if (CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
set(fc_line_length_option "--free-line-length-none")
endif ()
target_compile_options(fortuno_example_fpp_testapp PRIVATE "${fc_line_length_option}")
34 changes: 34 additions & 0 deletions example/fpp/mylib_fpp.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
! This file is part of Fortuno.
! Licensed under the BSD-2-Clause Plus Patent license.
! SPDX-License-Identifier: BSD-2-Clause-Patent

!> Demo module/library to be tested
module mylib_fpp
implicit none

private
public :: factorial

contains

!> Calculates the factorial of a number
function factorial(nn) result(fact)

!> Number to calculate the factorial of
integer, intent(in) :: nn

!> Factorial (note, there is no check made for integer overflow!)
integer :: fact

integer :: ii

fact = 1
do ii = 2, nn
fact = fact * ii
end do
! We create a "bug" which manifests only for certain input values
if (nn == 2 .or. nn > 10) fact = fact - 1

end function factorial

end module mylib_fpp
54 changes: 54 additions & 0 deletions example/fpp/simple_fpp_tests.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
! This file is part of Fortuno.
! Licensed under the BSD-2-Clause Plus Patent license.
! SPDX-License-Identifier: BSD-2-Clause-Patent

#include "fortuno_serial.fpp"

module simple_fpp_tests
use mylib_fpp, only : factorial
use fortuno_serial, only : is_equal, test => serial_case_item, suite => serial_suite_item,&
& test_item
implicit none

private
public :: simple_fpp_test_items

contains

! Test: 0! = 1
subroutine test_factorial_0()
CHECK(factorial(0) == 1)
end subroutine test_factorial_0

! Test: 1! = 1
subroutine test_factorial_1()
CHECK(factorial(1) == 1)
end subroutine test_factorial_1

! Test: 2! = 2 (will fail due to the bug in the implementation of the factorial function)
subroutine test_factorial_2()
! Two failing checks, you should see info about both in the output
CHECK_MSG(is_equal(factorial(2), 2), "Test failed for demonstration purposes")
CHECK(factorial(2) == 2)
end subroutine test_factorial_2


! Returns the tests from this module.
function simple_fpp_test_items() result(testitems)
type(test_item), allocatable :: testitems(:)

testitems = [&
! Adding a single test not belonging to any test suite
test("factorial_0", test_factorial_0),&

! Packing further tests into a suite in order to introduce more structure
! (e.g. running only tests being part of a given suite)
suite("simple", [&
test("factorial_1", test_factorial_1),&
test("factorial_2", test_factorial_2)&
])&
]

end function simple_fpp_test_items

end module simple_fpp_tests
17 changes: 17 additions & 0 deletions example/fpp/testapp_fpp.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
! This file is part of Fortuno.
! Licensed under the BSD-2-Clause Plus Patent license.
! SPDX-License-Identifier: BSD-2-Clause-Patent

!> Test app with command line interface, collecting and executing the tests.
program testapp_fpp
use fortuno_serial, only : execute_serial_cmd_app
use simple_fpp_tests, only : simple_fpp_test_items
implicit none

call execute_serial_cmd_app(&
testitems=[&
simple_fpp_test_items()&
]&
)

end program testapp_fpp
4 changes: 2 additions & 2 deletions include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ add_library(Fortuno::fortuno_include_dir ALIAS fortuno_include_dir)

if (FORTUNO_INSTALL)
install(
FILES fortuno_serial.fypp
FILES fortuno_serial.fpp fortuno_serial.fypp
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
COMPONENT Fortuno_development
)
install(
TARGETS fortuno_include_dir
EXPORT FortunoTargets
)
endif ()
endif ()
34 changes: 34 additions & 0 deletions include/fortuno_serial.fpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
! This file is part of Fortuno.
! Licensed under the BSD-2-Clause Plus Patent license.
! SPDX-License-Identifier: BSD-2-Clause-Patent

#ifndef __FORTUNO_SERIAL_FPP__
#define __FORTUNO_SERIAL_FPP__

#define CHECK(cond)\
block;\
use fortuno_serial, only : serial_check;\
call serial_check(cond, file=__FILE__, line=__LINE__);\
end block;

#define CHECK_MSG(cond, msg)\
block;\
use fortuno_serial, only : serial_check;\
call serial_check(cond, msg, file=__FILE__, line=__LINE__);\
end block;

#define ASSERT(cond)\
block;\
use fortuno_serial, only : serial_check, serial_check_failed;\
call serial_check(cond, file=__FILE__, line=__LINE__);\
if (serial_check_failed) return;\
end block;

#define ASSERT_MSG(cond, msg)\
block;\
use fortuno_serial, only : serial_check, serial_check_failed;\
call serial_check(cond, msg, file=__FILE__, line=__LINE__);\
if (serial_check_failed) return;\
end block;

#endif
4 changes: 2 additions & 2 deletions include/fortuno_serial.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#! Imports the prerequisites needed by the macro definitions in this file
#:def FORTUNO_SERIAL_IMPORTS()
use fortuno_serial, only : serial_case_item, serial_check, serial_failed
use fortuno_serial, only : serial_case_item, serial_check, serial_check_failed
#:enddef


Expand Down Expand Up @@ -81,7 +81,7 @@
#!
#:def ASSERT(cond, **kwargs)
$:CHECK(cond, **kwargs)
if (serial_failed()) return
if (serial_check_failed()) return
#:enddef


Expand Down
10 changes: 9 additions & 1 deletion test/export/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@
# SPDX-License-Identifier: BSD-2-Clause-Patent

add_executable(testapp)

target_sources(testapp PRIVATE testapp.f90)
target_link_libraries(testapp PRIVATE Fortuno::fortuno_serial)

add_executable(testapp_fpp)
target_sources(testapp_fpp PRIVATE testapp_fpp.F90)
target_link_libraries(testapp_fpp PRIVATE Fortuno::fortuno_serial Fortuno::fortuno_include_dir)
# GNU compiler (as of version 13.2) needs special flag to cope with extra long lines
if (CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
set(fc_line_length_option "--free-line-length-none")
endif ()
target_compile_options(testapp_fpp PRIVATE "${fc_line_length_option}")

if (FYPP)
add_executable(testapp_fypp)

Expand Down
43 changes: 43 additions & 0 deletions test/export/app/testapp_fpp.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
! This file is part of Fortuno.
! Licensed under the BSD-2-Clause Plus Patent license.
! SPDX-License-Identifier: BSD-2-Clause-Patent

#include "fortuno_serial.fpp"

!> Unit tests
module testapp_fpp_tests
use fortuno_serial, only : is_equal, test => serial_case_item, test_item
implicit none

private
public :: test_items

contains


subroutine test_success()
CHECK(is_equal(1, 1))
end subroutine test_success


function test_items() result(testitems)
type(test_item), allocatable :: testitems(:)

testitems = [&
test("success", test_success)&
]

end function test_items

end module testapp_fpp_tests


!> Test app driving Fortuno unit tests
program testapp_fpp
use testapp_fpp_tests, only : test_items
use fortuno_serial, only : execute_serial_cmd_app
implicit none

call execute_serial_cmd_app(testitems=test_items())

end program testapp_fpp

0 comments on commit 5a65f90

Please sign in to comment.