Skip to content

Commit 125ac80

Browse files
authoredFeb 9, 2021
Merge pull request #8 from LKedward/fix-empty-tbl-stats
Fix: table stats values for uninitialised table.
2 parents 9a8fae7 + 50d4be0 commit 125ac80

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed
 

‎src/fhash_tbl.f90

+7-8
Original file line numberDiff line numberDiff line change
@@ -192,19 +192,18 @@ subroutine fhash_tbl_stats(tbl,num_buckets,num_items,num_collisions,max_depth)
192192

193193
integer :: i, depth
194194

195-
if (.not.allocated(tbl%buckets)) then
196-
if (present(num_buckets)) num_buckets = 0
197-
return
198-
end if
195+
! Initialise stats
196+
if (present(num_items)) num_items = 0
197+
if (present(num_collisions)) num_collisions = 0
198+
if (present(max_depth)) max_depth = 0
199+
if (present(num_buckets)) num_buckets = 0
200+
201+
if (.not.allocated(tbl%buckets)) return
199202

200203
if (present(num_buckets)) then
201204
num_buckets = size(tbl%buckets)
202205
end if
203206

204-
if (present(num_items)) num_items = 0
205-
if (present(num_collisions)) num_collisions = 0
206-
if (present(max_depth)) max_depth = -1*huge(max_depth)
207-
208207
do i=1,size(tbl%buckets)
209208

210209
depth = node_depth(tbl%buckets(i))

‎test/test_tbl.f90

+36
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ subroutine collect_tbl(testsuite)
2222
& new_unittest("fhash-tbl-pointer-value", test_fhash_pointer_value), &
2323
& new_unittest("fhash-tbl-derived-type-value", test_fhash_derived_type_value), &
2424
& new_unittest("fhash-tbl-invalid-keys", test_fhash_invalid_keys), &
25+
& new_unittest("fhash-tbl-stats-empty", test_fhash_stats_empty), &
2526
& new_unittest("fhash-tbl-unset", test_fhash_unset), &
2627
& new_unittest("fhash-tbl-high-load", test_fhash_balanced_load) &
2728
]
@@ -367,6 +368,41 @@ subroutine test_fhash_invalid_keys(error)
367368

368369
end subroutine test_fhash_invalid_keys
369370

371+
!> Check stats for empty table
372+
subroutine test_fhash_stats_empty(error)
373+
type(error_t), allocatable, intent(out) :: error
374+
375+
type(fhash_tbl_t) :: tbl
376+
integer :: num_buckets, num_items, num_collisions, max_depth
377+
378+
call tbl%stats(num_buckets,num_items,num_collisions,max_depth)
379+
380+
if (num_buckets /= 0) then
381+
print *, 'num_buckets: ', num_buckets
382+
call test_failed(error,'empty table returned non-zero for num_buckets')
383+
return
384+
end if
385+
386+
if (num_items /= 0) then
387+
print *, 'num_items: ', num_items
388+
call test_failed(error,'empty table returned non-zero for num_items')
389+
return
390+
end if
391+
392+
if (num_collisions /= 0) then
393+
print *, 'num_collisions: ', num_collisions
394+
call test_failed(error,'empty table returned non-zero for num_collisions')
395+
return
396+
end if
397+
398+
if (max_depth > 0) then
399+
print *, 'max_depth: ', max_depth
400+
call test_failed(error,'empty table returned positive for max_depth')
401+
return
402+
end if
403+
404+
end subroutine test_fhash_stats_empty
405+
370406

371407
!> Store lots of values and check stats
372408
subroutine test_fhash_balanced_load(error)

0 commit comments

Comments
 (0)