Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adrifoster committed Jan 29, 2025
1 parent 5d5b086 commit 53df550
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 71 deletions.
11 changes: 8 additions & 3 deletions biogeochem/FatesPatchMod.F90
Original file line number Diff line number Diff line change
Expand Up @@ -968,13 +968,15 @@ subroutine InsertCohort(this, cohort)
if (.not. associated(cohort)) then
call endrun(msg="cohort is not allocated", &
additional_msg=errMsg(sourcefile, __LINE__))
return
end if

! check for inconsistent list state
if ((.not. associated(this%shortest) .and. associated(this%tallest)) .or. &
(associated(this%shortest) .and. .not. associated(this%tallest))) then
call endrun(msg="inconsistent list state", &
additional_msg=errMsg(sourcefile, __LINE__))
additional_msg=errMsg(sourcefile, __LINE__))
return
end if

! nothing in the list - add to head
Expand Down Expand Up @@ -1012,9 +1014,12 @@ subroutine InsertCohort(this, cohort)
do while (associated(temp_cohort2))

! validate list structure before insertion
if (associated(temp_cohort1%taller) .and. .not. associated(temp_cohort1%taller%shorter, temp_cohort1)) then

if (associated(temp_cohort1%taller) .and. &
.not. associated(temp_cohort1%taller%shorter, temp_cohort1)) then
call endrun(msg="corrupted list structure", &
additional_msg=errMsg(sourcefile, __LINE__))
return
end if

if ((cohort%height >= temp_cohort1%height) .and. (cohort%height < temp_cohort2%height)) then
Expand Down Expand Up @@ -1159,7 +1164,7 @@ subroutine SortCohorts(this)
! empty list
return
else if (.not. associated(this%shortest) .or. .not. associated(this%tallest)) then
call endrun(msg="one of shortest or tallest is null", &
call endrun(msg="inconsistent list state", &
additional_msg=errMsg(sourcefile, __LINE__))
return
end if
Expand Down
4 changes: 3 additions & 1 deletion testing/functional_testing/patch/FatesTestPatch.F90
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ program FatesTestPatch
character(len=:), allocatable :: param_file ! input parameter file
type(fates_patch_type), pointer :: patch ! patch
type(fates_cohort_type), pointer :: cohort ! cohort
type(fates_cohort_type), pointer :: cohort1, cohort2, cohort3 ! cohorts
type(fates_cohort_type), pointer :: new_cohort ! cohort to insert
integer :: i ! patch array location

! CONSTANTS:
Expand All @@ -39,7 +41,7 @@ program FatesTestPatch
i = patch_data%PatchDataPosition(patch_name='tropical')
call GetSyntheticPatch(patch_data%patches(i), num_levsoil, patch)

! print out list in ascending order
! print out list in ascending order
cohort => patch%shortest
write(*,*) 'Patch structure:'
do while (associated(cohort))
Expand Down
54 changes: 54 additions & 0 deletions testing/unit_testing/count_cohorts_test/test_CountCohorts.pf
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module test_CountCohorts
use FatesConstantsMod, only : r8 => fates_r8
use FatesCohortMod, only : fates_cohort_type
use FatesPatchMod, only : fates_patch_type
use FatesFactoryMod, only : CreateTestPatchList
use funit

implicit none
Expand Down Expand Up @@ -53,6 +54,59 @@ module test_CountCohorts

end subroutine SingleCohort_CountCohorts_GivesOne

@Test
subroutine SmallList_CountCohorts_GivesEight(this)
! tests that for a patch with eight cohorts, num_cohorts is 8
class(TestCountCohorts), intent(inout) :: this ! test object
type(fates_patch_type) :: patch ! patch object
type(fates_cohort_type), pointer :: cohort, next_cohort ! cohort objects
integer, parameter :: expected_cohorts = 8 ! expected number of cohorts
integer :: i ! looping index


! create the patch and list
allocate(cohort)
patch%shortest => cohort
do i = 2, expected_cohorts
allocate(next_cohort)
cohort%taller => next_cohort
next_cohort%shorter => cohort
cohort => next_cohort
end do
patch%tallest => cohort

! count cohorts
call patch%CountCohorts()
@assertEqual(patch%num_cohorts, expected_cohorts)

end subroutine SmallList_CountCohorts_GivesEight

@Test
subroutine LargeList_CountCohorts_GivesOneHundred(this)
! tests that for a patch with 100 cohorts, num_cohorts is 100
class(TestCountCohorts), intent(inout) :: this ! test object
type(fates_patch_type) :: patch ! patch object
type(fates_cohort_type), pointer :: cohort, next_cohort ! cohort objects
integer, parameter :: expected_cohorts = 100 ! expected number of cohorts
integer :: i ! looping index

! create the patch and list
allocate(cohort)
patch%shortest => cohort
do i = 2, expected_cohorts
allocate(next_cohort)
cohort%taller => next_cohort
next_cohort%shorter => cohort
cohort => next_cohort
end do
patch%tallest => cohort

! count cohorts
call patch%CountCohorts()
@assertEqual(patch%num_cohorts, expected_cohorts)

end subroutine LargeList_CountCohorts_GivesOneHundred

end module test_CountCohorts


113 changes: 102 additions & 11 deletions testing/unit_testing/insert_cohort_test/test_InsertCohort.pf
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ module test_InsertCohort
! DESCRIPTION:
! Tests the patch's InsertCohort method
!
use FatesConstantsMod, only : r8 => fates_r8
use FatesCohortMod, only : fates_cohort_type
use FatesPatchMod, only : fates_patch_type
use FatesFactoryMod, only : CreateTestPatchList
use FatesConstantsMod, only : r8 => fates_r8
use FatesCohortMod, only : fates_cohort_type
use FatesPatchMod, only : fates_patch_type
use FatesUnitTestUtils, only : endrun_msg
use FatesFactoryMod, only : CreateTestPatchList
use funit

implicit none
Expand All @@ -22,9 +23,7 @@ module test_InsertCohort
contains

subroutine setUp(this)
class(TestInsertCohort), intent(inout) :: this ! test object
type(fates_cohort_type), pointer :: new_node, head ! cohort objects

class(TestInsertCohort), intent(inout) :: this ! test object
! heights for cohors
real(r8), parameter :: heights(8) = (/2.0_r8, 5.0_r8, 10.0_r8, 12.0_r8, 12.5_r8, &
15.0_r8, 20.0_r8, 25.0_r8/)
Expand Down Expand Up @@ -200,7 +199,7 @@ module test_InsertCohort
end subroutine InsertCohort_SameHeight

@Test
subroutine InsertCohort_LargeList(this)
subroutine InsertCohort_SmallList(this)
! test inserting many cohorts into a list and it is ordered correctly
class(TestInsertCohort), intent(inout) :: this ! test object
type(fates_patch_type) :: patch ! patch object
Expand Down Expand Up @@ -249,10 +248,10 @@ module test_InsertCohort
end if
end do

end subroutine InsertCohort_LargeList
end subroutine InsertCohort_SmallList

@Test
subroutine InsertCohort_LargeList_IdenticalHeights(this)
subroutine InsertCohort_SmallList_IdenticalHeights(this)
! test inserting many cohorts of the same height into a list and it is ordered correctly
class(TestInsertCohort), intent(inout) :: this ! test object
type(fates_patch_type) :: patch ! patch object
Expand Down Expand Up @@ -281,7 +280,99 @@ module test_InsertCohort
i = i + 1
end do

end subroutine InsertCohort_LargeList_IdenticalHeights
end subroutine InsertCohort_SmallList_IdenticalHeights

@Test
subroutine InsertCohort_Unassociated_Errors(this)
! test inserting an unassociated cohort errors
class(TestInsertCohort), intent(inout) :: this ! test object
type(fates_patch_type) :: patch ! patch object
type(fates_cohort_type), pointer :: cohort ! cohort object
character(len=:), allocatable :: expected_msg ! expected error message for failure

expected_msg = endrun_msg("cohort is not allocated")

! make sure cohort is null
cohort => null()

! try to insert, should fail
call patch%InsertCohort(cohort)
@assertExceptionRaised(expected_msg)

end subroutine InsertCohort_Unassociated_Errors

@Test
subroutine InsertCohort_InconsistentListState_Errors(this)
! test inserting a cohort into an inconsistent list state errors
class(TestInsertCohort), intent(inout) :: this ! test object
type(fates_patch_type) :: patch ! patch object
type(fates_cohort_type), pointer :: cohort, new_cohort ! cohort objects
character(len=:), allocatable :: expected_msg ! expected error message for failure

expected_msg = endrun_msg("inconsistent list state")

! allocate and link one cohort incorrectly
allocate(cohort)
cohort%height = 5.0_r8
patch%shortest => cohort

! allocate a new cohort and try to insert
allocate(new_cohort)
new_cohort%height = 10.0_r8

! should fail
call patch%InsertCohort(new_cohort)
@assertExceptionRaised(expected_msg)

! try the opposite
patch%shortest => null()
patch%tallest => cohort

call patch%InsertCohort(new_cohort)
@assertExceptionRaised(expected_msg)

end subroutine InsertCohort_InconsistentListState_Errors

@Test
subroutine InsertCohort_CorruptedListStructure_Errors(this)
! tests that inserting a cohort into a currupted list structure errors
class(TestInsertCohort), intent(inout) :: this ! test object
type(fates_patch_type) :: patch ! patch object
type(fates_cohort_type), pointer :: cohort1, cohort2, cohort3 ! cohorts
type(fates_cohort_type), pointer :: new_cohort ! cohort to insert
character(len=:), allocatable :: expected_msg ! expected error message for failure

expected_msg = endrun_msg("corrupted list structure")

! allocate and link cohorts
allocate(cohort1)
allocate(cohort2)
allocate(cohort3)
cohort1%height = 1.0_r8
cohort2%height = 2.0_r8
cohort3%height = 4.0_r8

! set up a list
patch%shortest => cohort1
patch%tallest => cohort3

cohort1%taller => cohort2
cohort2%shorter => cohort1
cohort2%taller => cohort3
cohort3%shorter => cohort2

! break shorter chain
nullify(cohort2%shorter)! breaks backwards link

! allocate a new cohort to insert
allocate(new_cohort)
new_cohort%height = 3.0_r8

! should fail
call patch%InsertCohort(new_cohort)
@assertExceptionRaised(expected_msg)

end subroutine InsertCohort_CorruptedListStructure_Errors

end module test_InsertCohort

Expand Down
Loading

0 comments on commit 53df550

Please sign in to comment.