-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement fpp-style macros CHECK and ASSERT
- Loading branch information
Showing
11 changed files
with
235 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |