Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify strnum code and testing #410

Merged
merged 2 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 11 additions & 22 deletions src/strnum.F90
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
!> contain only digits and an (optional) leading sign ('+' or '-')
!> character.
!>
!> If the string contains all blank characters, then num is returned
!> with a value of 0.
!> If the string is empty or contains all blank characters, then num
!> is returned with a value of 0.
!>
!> @param[in] str -- character*(*): String
!> @param[out] num -- integer: Value decoded from str
Expand All @@ -18,7 +18,7 @@
!> -1 = string contained one or more illegal characters
!>
!> @author J. Woollen @date 1994-01-06
recursive subroutine strnum(str,num,iret)
recursive subroutine strnum( str, num, iret )
use modv_im8b

implicit none
Expand All @@ -27,9 +27,9 @@ recursive subroutine strnum(str,num,iret)

integer, intent(out) :: num, iret

character str2*40, fmt*8
character str2*40

integer lens
integer lens, ios

!-----------------------------------------------------------------------
!-----------------------------------------------------------------------
Expand All @@ -47,27 +47,16 @@ recursive subroutine strnum(str,num,iret)
return
end if

iret = 0

! Check for blank input string.

if ( str .eq. ' ' ) then
num = 0
return
end if
! Decode the integer from the string.

! Verify that the string contains all legal characters.
iret = 0
num = 0

call strsuc ( str, str2, lens )
if ( verify ( str2(1:lens), "0123456789+-" ) .ne. 0 ) then
iret = -1
return
end if

! Decode the integer from the string.
if ( lens .eq. 0 ) return

write ( fmt, '(''(I'',I2,'')'')' ) lens
read ( str2(1:lens), fmt ) num
read ( str2(1:lens), '(I40)', iostat = ios ) num
if ( ios .ne. 0 ) iret = -1

return
end subroutine strnum
12 changes: 3 additions & 9 deletions test/intest7.F90
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
! This is a test for NCEPLIBS-bufr.
!
! Reads test file 'testfiles/IN_7' containing 2-03-YYY changed reference values, using inline ERRWRT to
! check error messages, and using UFBPOS, UFBTAB, and STRNUM
! check error messages, and using UFBPOS and UFBTAB
!
! J. Ator, 2/23/2023

Expand Down Expand Up @@ -38,7 +38,7 @@ program intest7

integer*4 isetprm, igetprm, ireadns, ibfms

integer imgdt, iret, jdate, nr8v, idx, nsub, num, iersn
integer imgdt, iret, jdate, nr8v, idx, nsub, num

integer mxr8pm, mxr8lv
parameter ( mxr8pm = 15 )
Expand All @@ -49,7 +49,7 @@ program intest7
character cmgtag*8

print *, 'Testing reading IN_7 containing 2-03-YYY changed reference values, using inline ERRWRT'
print *, 'to check error messages, and using UFBPOS, UFBTAB, and STRNUM'
print *, 'to check error messages, and using UFBPOS and UFBTAB'

#ifdef KIND_8
call setim8b ( .true. )
Expand Down Expand Up @@ -141,11 +141,5 @@ program intest7
call ufbtab ( -11, r8val, 1, 1, nsub, ' ' )
if ( ( nsub .ne. 402 ) .or. ( ibfms ( r8val ) .ne. 1 ) ) stop 18

! Test the error handling inside of STRNUM.
call strnum( '75.DUMMY', num, iersn )
if ( iersn .eq. 0 ) stop 19
call strnum( ' ', num, iersn )
if ( iersn .ne. 0 ) stop 20

print *, 'SUCCESS!'
end program intest7
37 changes: 23 additions & 14 deletions test/test_misc.F90
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ program test_misc
integer lun, il, im
integer ios
integer ierr, nemock
integer numbck, iret
integer numbck, num, iret
integer mtyp, msbt, inod

print *, 'Testing misc subroutines.'
Expand All @@ -21,13 +21,7 @@ program test_misc
call setim8b(.true.)
#endif

! adn30/idn30.
char5 = adn30(42, 5)
if (char5 .ne. '00042') stop 1
a = idn30(char5, 5)
if (a .ne. 42) stop 2

! testiing status()
! testing status()
open(unit = 11, file = 'testfiles/IN_2', form = 'UNFORMATTED', iostat = ios)
if (ios .ne. 0) stop 3
call openbf(11, 'IN', 11)
Expand Down Expand Up @@ -57,31 +51,46 @@ program test_misc
! if (lun .ne. 1 .or. il .ne. -1 .or. im .ne. 0) stop 4
! call closbf(11)

! Testing strnum
call strnum('8DUMMY8',num,iret)
if (iret .ne. -1) stop 400
call strnum('',num,iret)
if ((iret .ne. 0) .or. (num .ne. 0)) stop 400
call strnum(' ',num,iret)
if ((iret .ne. 0) .or. (num .ne. 0)) stop 400
call strnum(' ',num,iret)
if ((iret .ne. 0) .or. (num .ne. 0)) stop 400

! These tests only for the _4 run of test_misc.
#ifdef KIND_4
! Testing nemock(). Commented out until this issue is resolved:
! https://github.com/NOAA-EMC/NCEPLIBS-bufr/issues/400

! adn30/idn30.
char5 = adn30(42, 5)
if (char5 .ne. '00042') stop 1
a = idn30(char5, 5)
if (a .ne. 42) stop 2

! Testing nemock()
ierr = nemock('')
if (ierr .ne. -1) stop 100
ierr = nemock('012345678')
if (ierr .ne. -1) stop 100
ierr = nemock('???')
if (ierr .ne. -2) stop 100

! Testing numbck(numb)...
! Commented out until this issue is resolved:
! https://github.com/NOAA-EMC/NCEPLIBS-bufr/issues/400
! Testing numbck()
iret = numbck('ABCDEF')
print *,iret
if (iret .ne. -1) stop 200
iret = numbck('01CDEF')
if (iret .ne. -2) stop 201

! Testing nemtbax()
open(unit = 11, file = 'testfiles/IN_2', form = 'UNFORMATTED', iostat = ios)
if (ios .ne. 0) stop 3
call openbf(11, 'IN', 11)
call nemtbax(11, 'DUMB', mtyp, msbt, inod)
if (inod .ne. 0) stop 300

#endif

print *, 'SUCCESS'
Expand Down