Skip to content

Commit

Permalink
Feature/unit tests for chgres_cube.fd/utils.f90 (#276)
Browse files Browse the repository at this point in the history
* Create unit-tests for testing to_upper and to_lower. This commit references issue #257

* Add unit test to_upper_lower into CMake build

* Clean up build and push for draft pull request

* Making following improvement:
- Using env parameter to assign source code
- Add auther name
- Modify comment
- Change test failing procedure
- rename directory for unit test

* turned on testing in workflows

* added test as test in cmake

* added test as test in cmake

* clean up test cmake file

* renamed test

* cleanup output

* cleanup output

* moved to new test directory

* added tests subdirectory

Co-authored-by: Edward Hartnett <[email protected]>
Co-authored-by: Edward Hartnett <[email protected]>
  • Loading branch information
3 people authored Feb 12, 2021
1 parent f955ba1 commit edc1af7
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ jobs:
mkdir build && cd build
cmake .. -DCMAKE_PREFIX_PATH='~;~/jasper;~/nceplibs'
make -j2
make test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ jobs:
mkdir build && cd build
cmake .. -DCMAKE_PREFIX_PATH='~;~/jasper'
make -j2
make test
Expand Down
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ if(NOT CMAKE_BUILD_TYPE MATCHES "^(Debug|Release|RelWithDebInfo|MinSizeRel)$")
CACHE STRING "Choose the type of build." FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
"MinSizeRel" "RelWithDebInfo")
message(STATUS "Set BUILD_TESTING to YES and build unit testing package under tests")
set(BUILD_TESTING "YES")
endif()

# Set compiler flags.
Expand Down Expand Up @@ -73,6 +75,12 @@ endif()

add_subdirectory(sorc)

# Run unit tests.
include(CTest)
if(BUILD_TESTING)
add_subdirectory(tests)
endif()

# If doxygen documentation we enabled, build it.
if(ENABLE_DOCS)
find_package(Doxygen REQUIRED)
Expand Down
8 changes: 8 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This is the CMake file for the tests directory of the UFS_UTILS
# project.
#
# Ed Hartnett 2/11/21

# Add the test subdirecotries.
add_subdirectory(chres_cube)

42 changes: 42 additions & 0 deletions tests/chres_cube/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# This is the cmake build file for the tests directory of the
# UFS_UTILS project.
#
# George Gayno, Lin Gan, Ed Hartnett

set(fortran_src
"${CMAKE_SOURCE_DIR}/sorc/chgres_cube.fd/utils.f90"
ftst_utils.F90)

if(CMAKE_Fortran_COMPILER_ID MATCHES "^(Intel)$")
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -r8 -convert big_endian -assume byterecl")
elseif(CMAKE_Fortran_COMPILER_ID MATCHES "^(GNU)$")
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -ffree-line-length-0 -fdefault-real-8 -fconvert=big-endian")
endif()

include_directories(
${PROJECT_SOURCE_DIR}
)

set(exe_name ftst_utils)
add_executable(${exe_name} ${fortran_src})
add_test(NAME ftst_utils COMMAND ftst_utils)
target_link_libraries(
ftst_utils
nemsio::nemsio
sfcio::sfcio
sigio::sigio
bacio::bacio_4
sp::sp_d
w3nco::w3nco_d
esmf
wgrib2::wgrib2_lib
wgrib2::wgrib2_api
MPI::MPI_Fortran
NetCDF::NetCDF_Fortran)
if(OpenMP_Fortran_FOUND)
target_link_libraries(ftst_utils OpenMP::OpenMP_Fortran)
endif()




48 changes: 48 additions & 0 deletions tests/chres_cube/ftst_utils.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
! Unit test for to_upper() and to_lower() functions under UFS_UTILS
! package, chres_cube utility.
!
! Lin Gan NCEP/EMC

program ftst_utils


implicit none

logical :: match_result

character(len=12) :: test_input_char_1, test_input_char_2, u_st_base, l_st_base

u_st_base="STAGGERLOCCE"
l_st_base="staggerlocce"
test_input_char_1="sTAGGErLOCCE"
test_input_char_2="staGGErLOCCE"

print*, "Starting Unit Testing to_upper_lower."
print*, "testing to_lower and to_upper..."

!-------------------------------------------------------------------------
! Execute testing below by running target function with testing string
! When match_result set to be T - compare to base line is identical
! When match_result set to be F - compare to base line is NOT identical
!-------------------------------------------------------------------------

call to_lower(test_input_char_1)
match_result = test_input_char_1 == l_st_base
if (.not.match_result) then
stop
endif

call to_upper(test_input_char_2)
match_result = test_input_char_2 == u_st_base
if (.not.match_result) then
stop
endif

!-------------------------------------------------------------------------
! Display final result
!-------------------------------------------------------------------------

print*, "OK"
print*, "SUCCESS!"

end program ftst_utils

0 comments on commit edc1af7

Please sign in to comment.