Skip to content
This repository has been archived by the owner on Nov 7, 2023. It is now read-only.

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jchristopherson committed Sep 23, 2020
1 parent db2082e commit 92dc4cb
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set(test_sources
test_fcore_list.f90
test_fcore_text_io.f90
test_fcore_binary_io.f90
test_fcore_dictionary.f90
)

# Build the executable
Expand Down
4 changes: 4 additions & 0 deletions tests/test_fcore.f90
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ program main
use test_fcore_list
use test_fcore_text_io
use test_fcore_binary_io
use test_fcore_dictionary
implicit none

! Local Variables
Expand Down Expand Up @@ -55,6 +56,9 @@ program main
local = test_binary_read_write()
if (.not.local) overall = .false.

local = test_dictionary_1()
if (.not.local) overall = .false.

! End
if (overall) then
print '(A)', "FCORE: ALL TESTS PASSED"
Expand Down
89 changes: 89 additions & 0 deletions tests/test_fcore_dictionary.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
! test_fcore_dictionary.f90

module test_fcore_dictionary
use iso_fortran_env
use collections
implicit none
contains
! ------------------------------------------------------------------------------
function test_dictionary_1() result(rst)
! Arguments
logical :: rst

! Parameters
character(len = *), parameter :: str1 = "Test String 1"
character(len = *), parameter :: str2 = "Test String 2"
character(len = *), parameter :: str3 = "Test String 3"

! Local Variables
integer(int64) :: key1, key2, key3
type(hash_code) :: hash
type(dictionary) :: x
class(*), pointer :: item

! Initialization
rst = .true.

! Generate hash values
key1 = hash%get(str1)
key2 = hash%get(str2)
key3 = hash%get(str3)

! Add the items to the dictionary
call x%add(key1, str1)
call x%add(key2, str2)
call x%add(key3, str3)

! Check the size
if (x%get_count() /= 3) then
rst = .false.
print '(AI0A)', "TEST_DICTIONARY_1 (Test 1): Expected 3 items " // &
"in the dictionary, but found ", x%get_count(), "."
return
end if

! Check to ensure each key exists in the collection
if (.not.x%contains_key(key1)) then
rst = .false.
print '(A)', "TEST_DICTIONARY_1 (Test 2): " // &
"Expected to find key 1, but did not."
return
end if

if (.not.x%contains_key(key2)) then
rst = .false.
print '(A)', "TEST_DICTIONARY_1 (Test 3): " // &
"Expected to find key 2, but did not."
return
end if

if (.not.x%contains_key(key3)) then
rst = .false.
print '(A)', "TEST_DICTIONARY_1 (Test 4): " // &
"Expected to find key 3, but did not."
return
end if

! Check with an invalid key
if (x%contains_key(key2 - key1) .and. key2 - key1 /= key3) then
rst = .false.
print '(A)', "TEST_DICTIONARY_1 (Test 5): " // &
"Did not expect to find an invalid key, but did."
return
end if

! Retrieve an item and check
item => x%get(key2)
select type (item)
type is (character(len = *))
if (item /= str2) then
rst = .false.
print '(A)', "TEST_DICTIONARY_1 (Test 6): " // &
"Expected to find " // str2 // ", but found " // &
item // " instead."
end if
end select
end function

! ------------------------------------------------------------------------------
end module

0 comments on commit 92dc4cb

Please sign in to comment.