Skip to content

Commit

Permalink
Updating compile flags for unit test and adding stringify unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
mwaxmonsky committed Jan 8, 2025
1 parent 0fba9fd commit 5d01c2e
Show file tree
Hide file tree
Showing 4 changed files with 262 additions and 34 deletions.
40 changes: 21 additions & 19 deletions src/core_utils/string_core_utils.F90
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,27 @@ module string_core_utils
implicit none
private

public :: to_str ! Convert integer to left justified string
public :: int_date_to_yyyymmdd ! Convert encoded date integer to "yyyy-mm-dd" format
public :: int_seconds_to_hhmmss ! Convert integer seconds past midnight to "hh:mm:ss" format
public :: stringify ! Convert one or more values of any intrinsic data types to a character string for pretty printing
public :: core_to_str ! Convert integer to left justified string
public :: core_int_date_to_yyyymmdd ! Convert encoded date integer to "yyyy-mm-dd" format
public :: core_int_seconds_to_hhmmss ! Convert integer seconds past midnight to "hh:mm:ss" format
public :: core_stringify ! Convert one or more values of any intrinsic data types to a character string for pretty printing

CONTAINS

character(len=10) pure function to_str(n)
character(len=10) pure function core_to_str(n)
! return default integer as a left justified string

! arguments
integer, intent(in) :: n
! character(len=10) :: local_str
!----------------------------------------------------------------------------

write(to_str,'(i0)') n
write(core_to_str,'(i0)') n
! core_to_str = local_str

end function to_str
end function core_to_str

character(len=10) pure function int_date_to_yyyymmdd (date)
character(len=10) pure function core_int_date_to_yyyymmdd (date)
! Undefined behavior if date <= 0

! Input arguments
Expand All @@ -36,12 +38,12 @@ character(len=10) pure function int_date_to_yyyymmdd (date)
month = (date - year*10000) / 100
day = date - year*10000 - month*100

write(int_date_to_yyyymmdd, '(i4.4,A,i2.2,A,i2.2)') &
write(core_int_date_to_yyyymmdd, '(i4.4,A,i2.2,A,i2.2)') &
year,'-',month,'-',day

end function int_date_to_yyyymmdd
end function core_int_date_to_yyyymmdd

character(len=8) pure function int_seconds_to_hhmmss (seconds)
character(len=8) pure function core_int_seconds_to_hhmmss (seconds)
! Undefined behavior if seconds outside [0, 86400]

! Input arguments
Expand All @@ -56,23 +58,23 @@ character(len=8) pure function int_seconds_to_hhmmss (seconds)
minutes = (seconds - hours*3600) / 60
secs = (seconds - hours*3600 - minutes*60)

write(int_seconds_to_hhmmss,'(i2.2,A,i2.2,A,i2.2)') &
write(core_int_seconds_to_hhmmss,'(i2.2,A,i2.2,A,i2.2)') &
hours,':',minutes,':',secs

end function int_seconds_to_hhmmss
end function core_int_seconds_to_hhmmss

!> Convert one or more values of any intrinsic data types to a character string for pretty printing.
!> If `value` contains more than one element, the elements will be stringified, delimited by `separator`, then concatenated.
!> If `value` contains exactly one element, the element will be stringified without using `separator`.
!> If `value` contains zero element or is of unsupported data types, an empty character string is produced.
!> If `separator` is not supplied, it defaults to `, ` (i.e., a comma and a space).
!> (KCW, 2024-02-04)
pure function stringify(value, separator)
pure function core_stringify(value, separator)
use, intrinsic :: iso_fortran_env, only: int32, int64, real32, real64

class(*), intent(in) :: value(:)
character(*), optional, intent(in) :: separator
character(:), allocatable :: stringify
character(:), allocatable :: core_stringify

integer, parameter :: sizelimit = 1024

Expand All @@ -88,7 +90,7 @@ pure function stringify(value, separator)
n = min(size(value), sizelimit)

if (n == 0) then
stringify = ''
core_stringify = ''
return
end if

Expand Down Expand Up @@ -153,11 +155,11 @@ pure function stringify(value, separator)

write(buffer, format) value
class default
stringify = ''
core_stringify = ''
return
end select

stringify = trim(buffer)
end function stringify
core_stringify = trim(buffer)
end function core_stringify

end module string_core_utils
18 changes: 15 additions & 3 deletions src/utils/string_utils.F90
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ module string_utils
use shr_string_mod, only: to_lower => shr_string_toLower
use cam_logfile, only: iulog
use cam_abortutils, only: endrun
use string_core_utils, only: to_str, int_date_to_yyyymmdd, int_seconds_to_hhmmss, stringify
use string_core_utils, only: core_int_date_to_yyyymmdd, core_int_seconds_to_hhmmss
use string_core_utils, only: core_stringify=>stringify

implicit none
private
Expand All @@ -14,6 +15,8 @@ module string_utils
public :: strlist_get_ind ! Gets the index of a given string in a list of strings
public :: date2yyyymmdd ! convert encoded date integer to "yyyy-mm-dd" format
public :: sec2hms ! convert integer seconds past midnight to "hh:mm:ss" format
public :: to_str
public :: stringify

! Private module variables
integer, parameter :: lower_to_upper = iachar("A") - iachar("a")
Expand Down Expand Up @@ -72,7 +75,7 @@ character(len=10) function date2yyyymmdd (date)
call endrun ('DATE2YYYYMMDD: negative date not allowed')
end if

date2yyyymmdd = int_date_to_yyyymmdd(date)
date2yyyymmdd = core_int_date_to_yyyymmdd(date)

end function date2yyyymmdd

Expand All @@ -86,10 +89,19 @@ character(len=8) function sec2hms (seconds)
call endrun ('SEC2HMS: bad input seconds: '//stringify((/seconds/)))
end if

sec2hms = int_seconds_to_hhmmss(seconds)
sec2hms = core_int_seconds_to_hhmmss(seconds)

end function sec2hms

character(len=10) pure function to_str(n)
use string_core_utils, only: core_to_str
integer, intent(in) :: n
character(len=10) :: local_str

local_str = core_to_str(n)
to_str = local_str
end function to_str

!=========================================================================================

end module string_utils
1 change: 1 addition & 0 deletions test/unit/fortran/src/core_utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
add_pfunit_ctest(core_utils_tests
TEST_SOURCES test_string_core_utils.pf
LINK_LIBRARIES core_utils)
target_compile_options(core_utils_tests PRIVATE -ffree-line-length-none)
Loading

0 comments on commit 5d01c2e

Please sign in to comment.