-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
614 additions
and
26 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
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,46 @@ | ||
# 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 Fypp) | ||
|
||
add_library(fortuno_example_fypp_mylib) | ||
set_target_properties( | ||
fortuno_example_fypp_mylib PROPERTIES | ||
OUTPUT_NAME mylib | ||
) | ||
target_sources( | ||
fortuno_example_fypp_mylib PRIVATE | ||
mylib.f90 | ||
) | ||
|
||
add_executable(fortuno_example_fypp_testapp) | ||
set_target_properties( | ||
fortuno_example_fypp_testapp PROPERTIES | ||
OUTPUT_NAME testapp_fypp | ||
) | ||
|
||
set( | ||
fypp-sources | ||
fixtured_fypp_tests.fypp | ||
parametrized_fypp_tests.fypp | ||
simple_fypp_tests.fypp | ||
) | ||
|
||
get_target_property(_fortuno_incdir Fortuno::fortuno_include_dir INTERFACE_INCLUDE_DIRECTORIES) | ||
fortuno_preprocess( | ||
${FYPP} "-I${_fortuno_incdir};--file-var-root=${CMAKE_SOURCE_DIR}" | ||
.fypp .f90 | ||
"${fypp-sources}" fypp-f90-sources | ||
) | ||
|
||
target_sources( | ||
fortuno_example_fypp_testapp PRIVATE | ||
${fypp-f90-sources} | ||
testapp_fypp.f90 | ||
) | ||
target_link_libraries( | ||
fortuno_example_fypp_testapp | ||
PRIVATE | ||
fortuno_example_fypp_mylib Fortuno::fortuno_serial | ||
) |
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,96 @@ | ||
! 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.fypp" | ||
|
||
module fixtured_fypp_tests | ||
use mylib_fypp, only : factorial | ||
use fortuno_serial, only : char_rep_int, is_equal, named_state, named_item,& | ||
& suite => serial_suite_item, store_state => serial_store_state,& | ||
& serial_case_base, test_item | ||
$:FORTUNO_SERIAL_IMPORTS() | ||
implicit none | ||
|
||
private | ||
public :: fixtured_fypp_test_items | ||
|
||
|
||
! Fixtured test case creating a random number before running a test procedure. | ||
type, extends(serial_case_base) :: random_test_case | ||
|
||
! Test procedure to be invoked once fixture setup had been executed | ||
procedure(test_recursion_down), pointer, nopass :: proc | ||
|
||
contains | ||
|
||
! Overrides run procedure to set up fixture before test procedure is invoked. | ||
procedure :: run => random_test_case_run | ||
|
||
end type random_test_case | ||
|
||
contains | ||
|
||
|
||
! TEST n! = n * (n - 1)! | ||
$:TEST("recursion_down", args=["nn"]) | ||
integer, intent(in) :: nn | ||
@:CHECK(is_equal(factorial(nn), nn * factorial(nn - 1))) | ||
$:END_TEST() | ||
|
||
|
||
! TEST (n + 1)! = (n + 1) * n! | ||
$:TEST("recursion_up", args=["nn"]) | ||
integer, intent(in) :: nn | ||
@:CHECK(is_equal(factorial(nn + 2), (nn + 1) * factorial(nn))) | ||
$:END_TEST() | ||
|
||
|
||
! Returns the tests from this module. | ||
function fixtured_fypp_test_items() result(testitems) | ||
type(test_item), allocatable :: testitems(:) | ||
|
||
testitems = [& | ||
suite("fixtured", [& | ||
$:TEST_ITEMS(constructor="random_test") | ||
])& | ||
] | ||
#! Stop if there are tests without corresponding generated test_item constructors | ||
$:STOP_ON_MISSING_TEST_ITEMS() | ||
|
||
end function fixtured_fypp_test_items | ||
|
||
|
||
! Convenience function returning a random_test_case instance wrapped as test_item. | ||
function random_test(name, proc) result(testitem) | ||
character(*), intent(in) :: name | ||
procedure(test_recursion_down) :: proc | ||
type(test_item) :: testitem | ||
|
||
testitem%item = random_test_case(name=name, proc=proc) | ||
|
||
end function random_test | ||
|
||
|
||
! Run procedure of the random_test_case class. | ||
subroutine random_test_case_run(this) | ||
class(random_test_case), intent(in) :: this | ||
|
||
real :: rand | ||
integer :: nn | ||
|
||
! Set-up fixture by creating a random number | ||
call random_number(rand) | ||
! Note: factorial(n) with n > 13 overflows with 32 bit integers | ||
nn = int(13 * rand) + 1 | ||
! Store internal state to aid introspection/identification later | ||
call store_state(& | ||
named_state([& | ||
named_item("n", char_rep_int(nn))& | ||
&])& | ||
) | ||
call this%proc(nn) | ||
|
||
end subroutine random_test_case_run | ||
|
||
end module fixtured_fypp_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,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_fypp | ||
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_fypp |
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,76 @@ | ||
! 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.fypp" | ||
|
||
module parametrized_fypp_tests | ||
use mylib_fypp, only : factorial | ||
use fortuno_serial, only : as_char, is_equal, serial_case_base, suite => serial_suite_item,& | ||
& test_item | ||
$:FORTUNO_SERIAL_IMPORTS() | ||
implicit none | ||
|
||
private | ||
public :: parametrized_fypp_test_items | ||
|
||
|
||
!> Contains argument and expected result of a factorial() invokation | ||
type :: arg_res | ||
integer :: arg, res | ||
end type | ||
|
||
!> Argument/result pairs to check for | ||
type(arg_res), parameter :: testcaseparams(*) = [& | ||
& arg_res(1, 1), arg_res(2, 2), arg_res(3, 6), arg_res(4, 24), arg_res(5, 120)& | ||
& ] | ||
|
||
|
||
!> Parametrized test checking for an individual argument/result pair. | ||
type, extends(serial_case_base) :: parametrized_test_case | ||
type(arg_res) :: argres | ||
contains | ||
procedure :: run => parametrized_test_case_run | ||
end type parametrized_test_case | ||
|
||
contains | ||
|
||
|
||
!> Returns the tests of this module. | ||
function parametrized_fypp_test_items() result(testitems) | ||
type(test_item), allocatable :: testitems(:) | ||
|
||
integer :: ii | ||
|
||
testitems = [& | ||
suite("parametrized", [& | ||
(parametrized_test("factorial", testcaseparams(ii)), ii = 1, size(testcaseparams))& | ||
])& | ||
] | ||
|
||
end function parametrized_fypp_test_items | ||
|
||
|
||
!> Run method of the parametrized test (executing the check directly) | ||
subroutine parametrized_test_case_run(this) | ||
class(parametrized_test_case), intent(in) :: this | ||
|
||
@:CHECK(is_equal(factorial(this%argres%arg), this%argres%res)) | ||
|
||
end subroutine parametrized_test_case_run | ||
|
||
|
||
!> Convenience wrapper to generate a test case wrapped as test_item for a given argres pair. | ||
function parametrized_test(prefix, argres) result(testitem) | ||
character(*), intent(in) :: prefix | ||
type(arg_res), intent(in) :: argres | ||
type(test_item) :: testitem | ||
|
||
character(:), allocatable :: name | ||
|
||
name = prefix // "_" // as_char(argres%arg) | ||
testitem%item = parametrized_test_case(name=name, argres=argres) | ||
|
||
end function parametrized_test | ||
|
||
end module parametrized_fypp_tests |
Oops, something went wrong.