From e4eb7e97fec0ba9b0c5281b6f9819c617f4f5fdf Mon Sep 17 00:00:00 2001 From: sgeard Date: Sat, 10 Jun 2023 12:02:37 +0100 Subject: [PATCH 1/2] Added size to amap and minor changes --- src/amap.f90 | 16 ++++++++++++++++ src/rpn_stack_sm.f90 | 14 +++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/amap.f90 b/src/amap.f90 index d9e47ca..b83b291 100644 --- a/src/amap.f90 +++ b/src/amap.f90 @@ -24,6 +24,8 @@ module amap generic, public :: write(formatted) => write_value_t procedure, private :: set_to_value_t generic, public :: assignment(=) => set_to_value_t + procedure, private :: equals_value_t + generic, public :: operator(==) => equals_value_t end type value_t ! Map elements are (key,value) pairs @@ -45,6 +47,7 @@ module amap procedure, public :: find => find_amap_t procedure, public :: print => print_amap_t procedure, public :: clear => clear_amap_t + procedure, public :: size => size_amap_t procedure, private :: is_key_kt procedure, private :: is_key_kvt generic, public :: contains => is_key_kt, is_key_kvt @@ -52,6 +55,12 @@ module amap contains + function size_amap_t(this) result(r) + class(amap_t), intent(in) :: this + integer :: r + r = this%high_water + end function size_amap_t + subroutine clear_amap_t(this) class(amap_t), intent(inout) :: this if (allocated(this%pairs)) then @@ -169,6 +178,13 @@ contains r = trim(adjustl(this%k)) == trim(adjustl(k%k)) end function equals_key_t + function equals_value_t(this, v) result(r) + class(value_t), intent(in) :: this + real(8), intent(in) :: v + logical :: r + r = this%v == v + end function equals_value_t + subroutine write_key_t(key, unit, iotype, v_list, iostat, iomsg) class(key_t), intent(in) :: key integer, intent(in) :: unit diff --git a/src/rpn_stack_sm.f90 b/src/rpn_stack_sm.f90 index 20772d5..7b9a4d4 100644 --- a/src/rpn_stack_sm.f90 +++ b/src/rpn_stack_sm.f90 @@ -1,5 +1,6 @@ ! Implementation code for stack submodule (rpn_stack) stack_sm + use iso_fortran_env, only: output_unit implicit none contains @@ -22,8 +23,8 @@ contains integer :: i, j if (ve_mode) then do i=stk%high_water,1,-1 - write(6,fmt='(a)',advance='no') stk%legend(i)//' ' - write(6,'(dt)') stk%sdata(i) + write(output_unit,fmt='(a)',advance='no') stk%legend(i)//' ' + write(output_unit,'(dt)') stk%sdata(i) end do else write(6,fmt='(dt)') stk%sdata(1) @@ -141,6 +142,9 @@ end submodule stack_sm ! Implementation code for rpn_t submodule (rpn_stack) rpn_sm + use iso_fortran_env, only: output_unit + implicit none + contains module subroutine write_rpns(se, unit, iotype, v_list, iostat, iomsg) @@ -159,13 +163,13 @@ contains call to_string(z%re,str_re) call to_string(z%im,str_im) if (se%is_cartesian()) then - write(6,'(a)') '('//str_re//','//str_im//')' + write(output_unit,'(a)') '('//str_re//','//str_im//')' else - write(6,'(a)') '('//str_re//','//str_im//') p' + write(output_unit,'(a)') '('//str_re//','//str_im//') p' end if else call to_string(z%re,str_re) - write(6,'(a)') str_re + write(output_unit,'(a)') str_re end if end subroutine write_rpns From 7534cedb89baada863bf48f51b6a1635dfa6cee8 Mon Sep 17 00:00:00 2001 From: sgeard Date: Sat, 10 Jun 2023 12:03:55 +0100 Subject: [PATCH 2/2] Improved testing of amap --- test/test_amap.f90 | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/test/test_amap.f90 b/test/test_amap.f90 index c570c7f..7fb0361 100644 --- a/test/test_amap.f90 +++ b/test/test_amap.f90 @@ -1,18 +1,36 @@ program test_amap use amap + use iso_fortran_env, only: output_unit implicit none type(amap_t) :: my_amap type(value_t) :: x + write(output_unit,fmt='(a)',advance='no') 'checking set ... ' call my_amap%set('one',1.0d0) call my_amap%set('two',2.d0) call my_amap%set('three',3.0d0) + call my_amap%set('four',7.0d0) call my_amap%set('four',4.0d0) call my_amap%set('five',5.0d0) - call my_amap%print + if (my_amap%size() == 5) then + write(output_unit,'(a)') 'passed' + else + write(output_unit,'(a)') 'FAILED' + end if - x = my_amap%get('four') - write(6,'(f0.6)') my_amap%get_value('four') - print *,my_amap%contains('one'),my_amap%contains('ten') + write(output_unit,fmt='(a)',advance='no') 'checking existence ... ' + if (my_amap%contains('one') .and. .not. my_amap%contains('ten')) then + write(output_unit,'(a)') 'passed' + else + write(output_unit,'(a)') 'FAILED' + end if + + write(output_unit,fmt='(a)',advance='no') 'checking data ... ' + if (my_amap%get('four') == 4.0d0) then + write(output_unit,'(a)') 'passed' + else + write(output_unit,'(a)') 'FAILED' + end if + end program test_amap