Files
FemLite/tests/unit/test_strength_reduction_report.f90
T
2026-06-26 00:48:57 +08:00

147 lines
4.6 KiB
Fortran

program test_strength_reduction_report
use fem_precision
use fem_error
use fem_strength_reduction_driver
use fem_strength_reduction_report
implicit none
character(len=*), parameter :: TMP = 'test_strength_reduction_report.csv'
call check_formatting()
call check_write_report()
call check_error_paths()
call delete_file(TMP)
contains
subroutine check_formatting()
type(sr_search_options) :: options
type(sr_search_state) :: state
character(len=256) :: line
integer :: ierr
call build_sample_state(options, state, ierr)
call require_success(ierr, 10)
call sr_report_header(line, ierr)
call require_success(ierr, 11)
if (index(line, 'trial,factor,residual') .ne. 1) call fail(12)
call sr_report_trial_csv(state, 1, line, ierr)
call require_success(ierr, 13)
if (index(line, '1,') .ne. 1) call fail(14)
if (index(line, ',1,SEARCHING') .le. 0) call fail(15)
call sr_report_trial_csv(state, 2, line, ierr)
call require_success(ierr, 16)
if (index(line, ',0,BRACKETED') .le. 0) call fail(17)
call sr_report_summary_line(state, line, ierr)
call require_success(ierr, 18)
if (index(line, 'status=BRACKETED') .ne. 1) call fail(19)
if (index(line, 'trials=2') .le. 0) call fail(20)
if (index(line, 'stable=1') .le. 0) call fail(21)
if (index(line, 'unstable=1') .le. 0) call fail(22)
end subroutine check_formatting
subroutine check_write_report()
type(sr_search_options) :: options
type(sr_search_state) :: state
integer :: ierr
call build_sample_state(options, state, ierr)
call require_success(ierr, 40)
call delete_file(TMP)
call sr_report_write(TMP, state, ierr)
call require_success(ierr, 41)
call require_file_contains(TMP, 'trial,factor,residual', 42)
call require_file_contains(TMP, 'BRACKETED', 43)
call require_file_contains(TMP, 'status=BRACKETED', 44)
call delete_file(TMP)
end subroutine check_write_report
subroutine check_error_paths()
type(sr_search_options) :: options
type(sr_search_state) :: state
character(len=8) :: small
integer :: ierr
call build_sample_state(options, state, ierr)
call require_success(ierr, 70)
call sr_report_header(small, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(71)
call sr_report_trial_csv(state, 99, small, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(72)
call sr_report_summary_line(state, small, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(73)
call sr_report_write('', state, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(74)
end subroutine check_error_paths
subroutine build_sample_state(options, state, ierr)
type(sr_search_options), intent(out) :: options
type(sr_search_state), intent(out) :: state
integer, intent(out) :: ierr
call sr_search_options_init(options)
call sr_search_begin(options, state, ierr)
if (ierr .ne. FEM_SUCCESS) return
call sr_search_record_trial(options, state%next_factor, 0.05_rk, 0.1_rk, &
0.1_rk, .false., state, ierr)
if (ierr .ne. FEM_SUCCESS) return
call sr_search_record_trial(options, state%next_factor, 1.2_rk, 0.1_rk, &
0.96_rk, .false., state, ierr)
end subroutine build_sample_state
subroutine require_file_contains(filename, needle, code)
character(len=*), intent(in) :: filename
character(len=*), intent(in) :: needle
integer, intent(in) :: code
character(len=256) :: line
integer :: unit
integer :: ios
integer :: found
found = 0
unit = 96
open(unit=unit, file=trim(filename), status='old', action='read', &
iostat=ios)
if (ios .ne. 0) call fail(code)
do
read(unit, '(A)', iostat=ios) line
if (ios .lt. 0) exit
if (ios .gt. 0) call fail(code + 1000)
if (index(line, trim(needle)) .gt. 0) found = 1
end do
close(unit)
if (found .ne. 1) call fail(code + 2000)
end subroutine require_file_contains
subroutine delete_file(filename)
character(len=*), intent(in) :: filename
integer :: unit
integer :: ios
unit = 97
open(unit=unit, file=trim(filename), status='old', iostat=ios)
if (ios .eq. 0) close(unit, status='delete')
end subroutine delete_file
subroutine require_success(ierr, code)
integer, intent(in) :: ierr
integer, intent(in) :: code
if (ierr .ne. FEM_SUCCESS) call fail(code)
end subroutine require_success
subroutine fail(code)
integer, intent(in) :: code
call delete_file(TMP)
write(*,*) 'test_strength_reduction_report failed:', code
stop 1
end subroutine fail
end program test_strength_reduction_report