hasty_test_dictionary Program

program~~hasty_test_dictionary~~UsesGraph program~hasty_test_dictionary hasty_test_dictionary module~hasty hasty module~hasty->program~hasty_test_dictionary iso_fortran_env iso_fortran_env iso_fortran_env->program~hasty_test_dictionary module~tester tester iso_fortran_env->module~tester module~penf_stringify penf_stringify iso_fortran_env->module~penf_stringify module~tester->program~hasty_test_dictionary module~hasty_dictionary_node hasty_dictionary_node module~hasty_dictionary_node->module~hasty module~hasty_dictionary hasty_dictionary module~hasty_dictionary_node->module~hasty_dictionary module~hasty_hash_table hasty_hash_table module~hasty_hash_table->module~hasty module~hasty_content_adt hasty_content_adt module~hasty_content_adt->module~hasty module~hasty_content_adt->module~hasty_dictionary_node module~hasty_content_adt->module~hasty_hash_table module~hasty_dictionary->module~hasty module~hasty_dictionary->module~hasty_hash_table module~hasty_key_base hasty_key_base module~hasty_key_base->module~hasty module~hasty_key_base->module~hasty_dictionary_node module~hasty_key_base->module~hasty_hash_table module~hasty_key_base->module~hasty_dictionary module~hasty_key_morton hasty_key_morton module~hasty_key_base->module~hasty_key_morton module~hasty_key_morton->module~hasty module~penf penf module~penf->module~hasty_dictionary_node module~penf->module~hasty_hash_table module~penf->module~hasty_dictionary module~penf->module~hasty_key_base module~penf->module~hasty_key_morton module~penf_global_parameters_variables penf_global_parameters_variables module~penf_global_parameters_variables->module~penf module~penf_b_size penf_b_size module~penf_global_parameters_variables->module~penf_b_size module~penf_global_parameters_variables->module~penf_stringify module~penf_b_size->module~penf module~penf_b_size->module~penf_stringify module~penf_stringify->module~penf
Help


HASTY test dictionary.

Calls

program~~hasty_test_dictionary~~CallsGraph program~hasty_test_dictionary hasty_test_dictionary proc~initialize~2 initialize program~hasty_test_dictionary->proc~initialize~2 proc~tests_non_captured tests_non_captured program~hasty_test_dictionary->proc~tests_non_captured proc~test_assert_equal~2 test_assert_equal program~hasty_test_dictionary->proc~test_assert_equal~2 proc~test_dictionary_finalize test_dictionary_finalize proc~tests_non_captured->proc~test_dictionary_finalize
Help

Source Code


Variables

Type AttributesNameInitial
type(tester_t) :: hasty_tester

Tests handler.

class(*), allocatable:: a_key

A key.

class(*), pointer:: a_content

A content.

class(*), pointer:: another_content

Another content.

type(dictionary) :: a_dictionary

A dictionary.

integer(kind=int32) :: max_content

Maximum content value.


Subroutines

subroutine initialize()

Initialize tests.

Arguments

None

subroutine iterator_max(key, content, done)

Iterator that computes the max of contents.

Arguments

Type IntentOptional AttributesName
class(*), intent(in) :: key

The node key.

class(*), intent(in), pointer:: content

The generic content.

logical, intent(out) :: done

Flag to set to true to stop traversing.

subroutine test_assert_equal(content, reference)

Test content==reference.

Arguments

Type IntentOptional AttributesName
class(*), intent(in) :: content

Content value.

integer(kind=int32), intent(in) :: reference

Reference value.

Test finalize.

Arguments

None

subroutine tests_non_captured()

Tests with non-captured results.

Arguments

None

Source Code

program hasty_test_dictionary
!-----------------------------------------------------------------------------------------------------------------------------------
!< HASTY test dictionary.
!-----------------------------------------------------------------------------------------------------------------------------------
use, intrinsic :: iso_fortran_env, only : int32, int64, output_unit
use hasty
use tester
!-----------------------------------------------------------------------------------------------------------------------------------

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

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

call initialize

call tests_non_captured

a_content => a_dictionary%get_pointer(key=3_int32)
call test_assert_equal(content=a_content, reference=12_int32)

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

a_content => a_dictionary%get_pointer(key=3_int32)
another_content => a_dictionary%get_pointer(key=3_int32)
if (associated(a_content).and.associated(another_content)) then
  select type(a_content)
  type is(integer(int32))
    a_content = 10_int32
  endselect
endif
call test_assert_equal(content=another_content, reference=10_int32)

a_content => null()
allocate(a_content, source=int(len(a_dictionary), int32))
call test_assert_equal(content=a_content, reference=3_int32)

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

call hasty_tester%assert_equal(a_dictionary%has_key(4_int32), .false.)

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

max_content = 0
call a_dictionary%traverse(iterator=iterator_max)
a_content => null()
allocate(a_content, source=max_content)
call test_assert_equal(content=a_content, reference=13_int32)

call a_dictionary%get_clone(key=5_int32, content=a_key)
call test_assert_equal(content=a_key, reference=13_int32)

call a_dictionary%remove(key='foo')
call hasty_tester%assert_equal(a_dictionary%ids(), [5_int64, 5_int64])

call a_dictionary%remove(key=5_int64)
call hasty_tester%assert_equal(a_dictionary%ids(), [0_int64, 0_int64])

call a_dictionary%destroy
a_content => null()
allocate(a_content, source=int(len(a_dictionary), int32))
call test_assert_equal(content=a_content, reference=0_int32)

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_dictionary%add_pointer(key=a_key, content=a_content)
  call a_dictionary%add_clone(key=5_int32, content=13_int32)
  call a_dictionary%add_clone(key='foo', content=0_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 test_dictionary_finalize
  !---------------------------------------------------------------------------------------------------------------------------------
  !< Test [[dictionary:finalize]].
  !---------------------------------------------------------------------------------------------------------------------------------
  type(dictionary) :: local_dictionary !< A dictionary.
  !---------------------------------------------------------------------------------------------------------------------------------

  !---------------------------------------------------------------------------------------------------------------------------------
  call local_dictionary%add_clone(key=5_int32, content=13_int32)
  !---------------------------------------------------------------------------------------------------------------------------------
  endsubroutine test_dictionary_finalize

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

  !---------------------------------------------------------------------------------------------------------------------------------
  print '(A)', 'Keys in dictionary:'
  call a_dictionary%print_keys

  print "(A)", 'Explicit loop over all nodes (key & contents)'
  do while(a_dictionary%loop(key=a_key, content=a_content))
    if (allocated(a_key)) then
      select type(a_key)
      type is(integer(int32))
        write(output_unit, "(A,I3)", advance='no') 'key ', a_key
      endselect
    endif
    if (associated(a_content)) then
      select type(a_content)
      type is(integer(int32))
        print "(A,I3)", ' content ', a_content
      endselect
    endif
  enddo

  print "(A)", 'Repeating the loop to check saved values (key & contents)'
  do while(a_dictionary%loop(key=a_key, content=a_content))
    if (allocated(a_key)) then
      select type(a_key)
      type is(integer(int32))
        write(output_unit, "(A,I3)", advance='no') 'key ', a_key
      endselect
    endif
    if (associated(a_content)) then
      select type(a_content)
      type is(integer(int32))
        print "(A,I3)", ' content ', a_content
      endselect
    endif
  enddo

  call test_dictionary_finalize
  !---------------------------------------------------------------------------------------------------------------------------------
  endsubroutine tests_non_captured
endprogram hasty_test_dictionary