Skip to content

Specific assertions #412

Open
Open
@Beliavsky

Description

@Beliavsky

In my codes I often want to check whether

(1) variable1 == variable2 (they could be array sizes)
(2) a variable >= value (for example an array index being positive)
(3) a variable <= value (within an array bound)
(4) low <= variable <= high
(5) variable1 <= variable2

Could stdlib have specific assertion subroutines for these common cases? Probably someone has already coded them for their own use. For example for (5) I have

subroutine assert_le(n1,n2,name1,name2,procedure)
! print an error message and stop if n1 > n2
integer          , intent(in) :: n1,n2
character (len=*), intent(in) :: procedure   ! name of program unit where assertion is made
character (len=*), intent(in) :: name1,name2 ! variable names 
if (n1 > n2) then
   write (*,"(a,2(1x,a),' =',2(1x,i0),a)") "in " // trim(procedure) // ",", &
          trim(name1),trim(name2),n1,n2," need " // trim(name1) // " <= " // trim(name2) // ", STOPPING"
   error stop
end if
end subroutine assert_le

invoked with code such as

call assert_le(ntrain,n,"ntrain","n","main")

There is #72 "Implement standard assert subroutine and associated macros", but a general assert subroutine testing a condition will give a less informative message.

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: utilitiescontainers, strings, files, OS/environment integration, unit testing, assertions, logging, ...

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions