Skip to content

Commit ec5f4b6

Browse files
committed
Not reallocate array
1 parent 7e0b592 commit ec5f4b6

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

src/stdlib_io_np_save.fypp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,9 @@ contains
149149
!> Name of the array to be added. A default name will be used if not provided.
150150
character(len=*), intent(in), optional :: name
151151

152-
integer :: i, arr_size
152+
integer :: i
153153
type(t_array_${t1[0]}$${k1}$_${rank}$) :: t_arr
154+
type(t_array_wrapper) :: wrapper
154155

155156
if (present(stat)) stat = 0
156157

@@ -177,18 +178,16 @@ contains
177178
return
178179
end if
179180

180-
arr_size = size(arrays)
181-
do i = 1, arr_size
181+
do i = 1, size(arrays)
182182
if (arrays(i)%array%name == t_arr%name) then
183183
if (present(stat)) stat = 1
184184
if (present(msg)) msg = "Array with the same name '"//t_arr%name//"' already exists."
185185
return
186186
end if
187187
end do
188188

189-
arr_size = arr_size + 1
190-
allocate(arrays(arr_size))
191-
arrays(arr_size)%array = t_arr
189+
wrapper%array = t_arr
190+
arrays = [arrays, wrapper]
192191
end
193192
#:endfor
194193
#:endfor

test/io/test_np.f90

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ subroutine collect_np(testsuite)
4848
new_unittest("npz_load_arr_cmplx", npz_load_arr_cmplx), &
4949
new_unittest("npz_load_two_arr_iint64_rdp", npz_load_two_arr_iint64_rdp), &
5050
new_unittest("npz_load_two_arr_iint64_rdp_comp", npz_load_two_arr_iint64_rdp_comp), &
51-
new_unittest("npz_add_to_empty_arr", npz_add_to_empty_arr), &
51+
new_unittest("npz_add_arr_to_empty", npz_add_arr_to_empty), &
52+
new_unittest("npz_add_two_arrays", npz_add_two_arrays), &
5253
new_unittest("npz_save_empty_array_input", npz_save_empty_array_input, should_fail=.true.) &
5354
]
5455
end subroutine collect_np
@@ -934,12 +935,11 @@ subroutine npz_load_two_arr_iint64_rdp_comp(error)
934935
end select
935936
end
936937

937-
subroutine npz_add_to_empty_arr(error)
938+
subroutine npz_add_arr_to_empty(error)
938939
type(error_type), allocatable, intent(out) :: error
939940

940941
type(t_array_wrapper), allocatable :: arrays(:)
941942
integer :: stat
942-
character(*), parameter :: filename = "npz_add_arr.npz"
943943
real(dp), allocatable :: input_array(:,:)
944944

945945
allocate(input_array(10, 4))
@@ -959,10 +959,44 @@ subroutine npz_add_to_empty_arr(error)
959959
"Precision loss when adding array.")
960960
if (allocated(error)) return
961961
class default
962-
call test_failed(error, "Array in '"//filename//"' is of wrong type.")
962+
call test_failed(error, "Array is of wrong type.")
963963
end select
964964
end
965965

966+
subroutine npz_add_two_arrays(error)
967+
type(error_type), allocatable, intent(out) :: error
968+
969+
type(t_array_wrapper), allocatable :: arrays(:)
970+
integer :: stat
971+
real(dp), allocatable :: array_1(:,:)
972+
real(sp), allocatable :: array_2(:)
973+
974+
allocate(array_1(10, 4))
975+
call random_number(array_1)
976+
call add_array(arrays, array_1, stat)
977+
call check(error, stat, "Error adding an array to the list of arrays.")
978+
if (allocated(error)) return
979+
call check(error, size(arrays) == 1, "Array was not added to the list of arrays.")
980+
if (allocated(error)) return
981+
call check(error, arrays(1)%array%name == "arr_0.npy", "Wrong array name.")
982+
if (allocated(error)) return
983+
select type (typed_array => arrays(1)%array)
984+
class is (t_array_rdp_2)
985+
call check(error, size(typed_array%values), size(array_1), "Array sizes to not match.")
986+
if (allocated(error)) return
987+
call check(error, any(abs(typed_array%values - array_1) <= epsilon(1.0_dp)), &
988+
"Precision loss when adding array.")
989+
if (allocated(error)) return
990+
class default
991+
call test_failed(error, "Array 1 is of wrong type.")
992+
end select
993+
994+
allocate(array_2(10))
995+
call random_number(array_2)
996+
call add_array(arrays, array_2, stat)
997+
call check(error, stat, "Error adding an array to the list of arrays.")
998+
end
999+
9661000
! subroutine npz_add_arr(error)
9671001
! type(error_type), allocatable, intent(out) :: error
9681002

0 commit comments

Comments
 (0)