hasty_test_hash_table.f90 Source File

HASTY test hash table.

This File Depends On

sourcefile~~hasty_test_hash_table.f90~~EfferentGraph sourcefile~hasty_test_hash_table.f90 hasty_test_hash_table.f90 sourcefile~hasty.f90 hasty.f90 sourcefile~hasty.f90->sourcefile~hasty_test_hash_table.f90 sourcefile~tester.f90 tester.f90 sourcefile~tester.f90->sourcefile~hasty_test_hash_table.f90 sourcefile~hasty_key_base.f90 hasty_key_base.f90 sourcefile~hasty_key_base.f90->sourcefile~hasty.f90 sourcefile~hasty_key_morton.f90 hasty_key_morton.f90 sourcefile~hasty_key_base.f90->sourcefile~hasty_key_morton.f90 sourcefile~hasty_hash_table.f90 hasty_hash_table.F90 sourcefile~hasty_key_base.f90->sourcefile~hasty_hash_table.f90 sourcefile~hasty_dictionary.f90 hasty_dictionary.f90 sourcefile~hasty_key_base.f90->sourcefile~hasty_dictionary.f90 sourcefile~hasty_dictionary_node.f90 hasty_dictionary_node.f90 sourcefile~hasty_key_base.f90->sourcefile~hasty_dictionary_node.f90 sourcefile~hasty_key_morton.f90->sourcefile~hasty.f90 sourcefile~hasty_hash_table.f90->sourcefile~hasty.f90 sourcefile~hasty_dictionary.f90->sourcefile~hasty.f90 sourcefile~hasty_dictionary.f90->sourcefile~hasty_hash_table.f90 sourcefile~hasty_dictionary_node.f90->sourcefile~hasty.f90 sourcefile~hasty_dictionary_node.f90->sourcefile~hasty_dictionary.f90 sourcefile~hasty_content_adt.f90 hasty_content_adt.f90 sourcefile~hasty_content_adt.f90->sourcefile~hasty.f90 sourcefile~hasty_content_adt.f90->sourcefile~hasty_hash_table.f90 sourcefile~hasty_content_adt.f90->sourcefile~hasty_dictionary_node.f90 sourcefile~penf.f90 penf.F90 sourcefile~penf.f90->sourcefile~hasty_key_base.f90 sourcefile~penf.f90->sourcefile~hasty_key_morton.f90 sourcefile~penf.f90->sourcefile~hasty_hash_table.f90 sourcefile~penf.f90->sourcefile~hasty_dictionary.f90 sourcefile~penf.f90->sourcefile~hasty_dictionary_node.f90 sourcefile~penf_b_size.f90 penf_b_size.F90 sourcefile~penf_b_size.f90->sourcefile~penf.f90 sourcefile~penf_stringify.f90 penf_stringify.F90 sourcefile~penf_b_size.f90->sourcefile~penf_stringify.f90 sourcefile~penf_global_parameters_variables.f90 penf_global_parameters_variables.F90 sourcefile~penf_global_parameters_variables.f90->sourcefile~penf.f90 sourcefile~penf_global_parameters_variables.f90->sourcefile~penf_b_size.f90 sourcefile~penf_global_parameters_variables.f90->sourcefile~penf_stringify.f90 sourcefile~penf_stringify.f90->sourcefile~penf.f90
Help


Source Code

!< HASTY test hash table.
program hasty_test_hash_table
!-----------------------------------------------------------------------------------------------------------------------------------
!< HASTY test hash table.
!-----------------------------------------------------------------------------------------------------------------------------------
use, intrinsic :: iso_fortran_env, only : int32
use hasty
use tester
!-----------------------------------------------------------------------------------------------------------------------------------

!-----------------------------------------------------------------------------------------------------------------------------------
type(tester_t)        :: hasty_tester    !< Tests handler.
class(*), allocatable :: a_key           !< A key.
class(*), pointer     :: a_content       !< A content.
class(*), allocatable :: another_content !< Another content.
type(hash_table)      :: a_table         !< A table.
integer(int32)        :: max_content     !< Maximum content value.
!-----------------------------------------------------------------------------------------------------------------------------------

!-----------------------------------------------------------------------------------------------------------------------------------
call hasty_tester%init

call initialize

call tests_non_captured

call a_table%add_clone(key=5_int32, content=13_int32)
a_content => a_table%get_pointer(key=5_int32)
call test_assert_equal(content=a_content, reference=13_int32)

call hasty_tester%assert_equal(a_table%has_key(3_int32), .true.)

call hasty_tester%assert_equal(len(a_table), 2_int32)

max_content = 0
call a_table%traverse(iterator=iterator_max)
call hasty_tester%assert_equal(max_content, 13)

call a_table%remove(key=3_int32)
call hasty_tester%assert_equal(a_table%has_key(3_int32), .false.)

call a_table%get_clone(key=5_int32, content=another_content)
if (allocated(another_content)) then
  call test_assert_equal(content=another_content, reference=13_int32)
endif

call hasty_tester%assert_equal(int(a_table%ids(), int32), [5_int32, 5_int32])

call a_table%destroy
call a_table%initialize(buckets_number=11, homogeneous=.true., typeguard_key='a string', typeguard_content=1_int32)
call hasty_tester%assert_equal(a_table%is_initialized(), .true.)
call hasty_tester%assert_equal(a_table%is_homogeneous(), .true.)

call hasty_tester%print
!-----------------------------------------------------------------------------------------------------------------------------------
contains
  ! auxiliary procedures
  subroutine initialize
  !---------------------------------------------------------------------------------------------------------------------------------
  !< Initialize tests.
  !---------------------------------------------------------------------------------------------------------------------------------

  !---------------------------------------------------------------------------------------------------------------------------------
  allocate(a_key, source=3_int32)
  allocate(a_content, source=12_int32)

  call a_table%add_pointer(key=a_key, content=a_content)
  call a_table%add_clone(key=5_int32, content=13_int32)
  !---------------------------------------------------------------------------------------------------------------------------------
  endsubroutine initialize

  subroutine iterator_max(key, content, done)
  !---------------------------------------------------------------------------------------------------------------------------------
  !< Iterator that computes the max of contents.
  !---------------------------------------------------------------------------------------------------------------------------------
  class(*),          intent(in)  :: key     !< The node key.
  class(*), pointer, intent(in)  :: content !< The generic content.
  logical,           intent(out) :: done    !< Flag to set to true to stop traversing.
  !---------------------------------------------------------------------------------------------------------------------------------

  !---------------------------------------------------------------------------------------------------------------------------------
  if (associated(content)) then
    select type(content)
    type is(integer(int32))
      max_content = max(max_content, content)
    endselect
  endif
  done = .false.
  !---------------------------------------------------------------------------------------------------------------------------------
  endsubroutine iterator_max

  ! tests
  subroutine test_assert_equal(content, reference)
  !---------------------------------------------------------------------------------------------------------------------------------
  !< Test `content==reference`.
  !---------------------------------------------------------------------------------------------------------------------------------
  class(*),       intent(in) :: content   !< Content value.
  integer(int32), intent(in) :: reference !< Reference value.
  !---------------------------------------------------------------------------------------------------------------------------------

  !---------------------------------------------------------------------------------------------------------------------------------
  select type(content)
  type is(integer(int32))
    call hasty_tester%assert_equal(content, reference)
  endselect
  !---------------------------------------------------------------------------------------------------------------------------------
  endsubroutine test_assert_equal

  subroutine hash_table_finalize
  !---------------------------------------------------------------------------------------------------------------------------------
  !< Test [[hash_table:finalize]].
  !---------------------------------------------------------------------------------------------------------------------------------
  type(hash_table) :: local_hash_table !< A hash_table.
  !---------------------------------------------------------------------------------------------------------------------------------

  !---------------------------------------------------------------------------------------------------------------------------------
  call local_hash_table%add_clone(key=5_int32, content=13_int32)
  !---------------------------------------------------------------------------------------------------------------------------------
  endsubroutine hash_table_finalize

  subroutine tests_non_captured
  !---------------------------------------------------------------------------------------------------------------------------------
  !< Tests with non-captured results.
  !---------------------------------------------------------------------------------------------------------------------------------

  !---------------------------------------------------------------------------------------------------------------------------------
  print '(A)', 'Keys in table:'
  call a_table%print_keys

  call hash_table_finalize
  !---------------------------------------------------------------------------------------------------------------------------------
  endsubroutine tests_non_captured
endprogram hasty_test_hash_table