From 563f908d0cb594e948bc4196a23de484521c3eff Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Fri, 14 Apr 2023 10:30:03 +0200 Subject: [PATCH] testing misc functionality --- src/materialpoint.f90 | 2 ++ src/misc.f90 | 83 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/src/materialpoint.f90 b/src/materialpoint.f90 index e32f6b20c..dda2e5870 100644 --- a/src/materialpoint.f90 +++ b/src/materialpoint.f90 @@ -8,6 +8,7 @@ module materialpoint use signal use CLI use prec + use misc use IO use YAML_types use YAML_parse @@ -47,6 +48,7 @@ subroutine materialpoint_initAll() call CLI_init() ! grid and mesh commandline interface call signal_init() call prec_init() + call misc_init() call IO_init() #if defined(MESH) call FEM_quadrature_init() diff --git a/src/misc.f90 b/src/misc.f90 index 388d78ce1..a56ea87c1 100644 --- a/src/misc.f90 +++ b/src/misc.f90 @@ -17,11 +17,24 @@ module misc end interface misc_optional public :: & + misc_init, & misc_optional contains +!-------------------------------------------------------------------------------------------------- +!> @brief Do self test. +!-------------------------------------------------------------------------------------------------- +subroutine misc_init() + + print'(/,1x,a)', '<<<+- misc init -+>>>' + + call misc_selfTest() + +end subroutine misc_init + + !-------------------------------------------------------------------------------------------------- !> @brief Return bool value if given, otherwise default. !-------------------------------------------------------------------------------------------------- @@ -97,4 +110,74 @@ pure function misc_optional_string(given,default) result(var) end function misc_optional_string + +!-------------------------------------------------------------------------------------------------- +!> @brief Check correctness of some misc functions. +!-------------------------------------------------------------------------------------------------- +subroutine misc_selfTest() + + real(pReal) :: r + + call random_number(r) + if (test_str('DAMASK') /= 'DAMASK') error stop 'optional_string, present' + if (test_str() /= 'default') error stop 'optional_string, not present' + if (misc_optional(default='default') /= 'default') error stop 'optional_string, default only' + if (test_int(20191102) /= 20191102) error stop 'optional_int, present' + if (test_int() /= 42) error stop 'optional_int, not present' + if (misc_optional(default=20191102) /= 20191102) error stop 'optional_int, default only' + if (dNeq(test_real(r),r)) error stop 'optional_float, present' + if (dNeq(test_real(),0.0_pReal)) error stop 'optional_float, not present' + if (dNeq(misc_optional(default=r),r)) error stop 'optional_float, default only' + if (test_bool(r<0.5_pReal) .neqv. r<0.5_pReal) error stop 'optional_bool, present' + if (.not. test_bool()) error stop 'optional_bool, not present' + if (misc_optional(default=r>0.5_pReal) .neqv. r>0.5_pReal) error stop 'optional_bool, default only' + +contains + + function test_str(str_in) result(str_out) + + character(len=:), allocatable :: str_out + character(len=*), intent(in), optional :: str_in + + + str_out = misc_optional_string(str_in,'default') + + end function test_str + + + function test_int(int_in) result(int_out) + + integer :: int_out + integer, intent(in), optional :: int_in + + + int_out = misc_optional_integer(int_in,42) + + end function test_int + + + function test_real(real_in) result(real_out) + + real(pReal) :: real_out + real(pReal), intent(in), optional :: real_in + + + real_out = misc_optional_real(real_in,0.0_pReal) + + end function test_real + + + function test_bool(bool_in) result(bool_out) + + logical :: bool_out + logical, intent(in), optional :: bool_in + + + bool_out = misc_optional_bool(bool_in,.true.) + + end function test_bool + + +end subroutine misc_selfTest + end module misc