Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(List): add minimal unit tests for ListType #1546

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
254 changes: 254 additions & 0 deletions autotest/TestList.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
module TestList
use KindModule, only: I4B
use testdrive, only: error_type, unittest_type, new_unittest, check
use ConstantsModule, only: LINELENGTH
use ListModule, only: ListType
implicit none
private
public :: collect_list

type :: IntNodeType
integer :: value
end type IntNodeType

contains

subroutine collect_list(testsuite)
type(unittest_type), allocatable, intent(out) :: testsuite(:)
testsuite = [ &
new_unittest("add_count_get_item", &
test_add_count_get_item), &
new_unittest("get_get_index_contains", &
test_get_index_contains), &
new_unittest("get_next_previous_item_reset", &
test_get_next_previous_item_reset), &
new_unittest("insert_after", &
test_insert_after), &
new_unittest("remove_node", &
test_remove_node) &
]
end subroutine collect_list

subroutine test_add_count_get_item(error)
type(error_type), allocatable, intent(out) :: error
type(ListType), pointer :: list
type(IntNodeType), pointer :: n
class(*), pointer :: p

allocate (list)
allocate (n)

! empty
call check(error, list%Count() == 0, "count should be 0")
if (allocated(error)) return

! add one node
n%value = 1
p => n
call list%Add(p)

! check count
call check(error, list%Count() == 1, "count should be 1")
if (allocated(error)) return

! retrieve item
p => list%GetItem(1)
call check(error, associated(p, n))
select type (item => p)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure i've seen this syntax before. Item is not declared, but yet it is assigned the correct type.

type is (IntNodeType)
call check(error, item%value == 1, "wrong value")
class default
call check(error, .false., "wrong node type")
end select
if (allocated(error)) return

deallocate (list)
deallocate (n)
end subroutine test_add_count_get_item

subroutine test_get_index_contains(error)
type(error_type), allocatable, intent(out) :: error
type(ListType), pointer :: list
type(IntNodeType), pointer :: n1, n2
class(*), pointer :: p
integer(I4B) :: i

allocate (list)
allocate (n1)
allocate (n2)

! add nodes
n1%value = 1
n2%value = 2
p => n1
call list%Add(p)
p => n2
call list%Add(p)

! check count
call check(error, list%Count() == 2, "count should be 1")
if (allocated(error)) return

! check get index
i = list%GetIndex(p)
call check(error, i == 2, "wrong index")
if (allocated(error)) return

! check contains
p => n1
call check(error, list%ContainsObject(p), "should contain n1")
if (allocated(error)) return
p => n2
call check(error, list%ContainsObject(p), "should contain n2")
if (allocated(error)) return

deallocate (list)
deallocate (n1)
deallocate (n2)
end subroutine test_get_index_contains

subroutine test_get_next_previous_item_reset(error)
type(error_type), allocatable, intent(out) :: error
type(ListType), pointer :: list
type(IntNodeType), pointer :: n1, n2, n3
class(*), pointer :: p
integer(I4B) :: i

allocate (list)
allocate (n1)
allocate (n2)
allocate (n3)

! add nodes
n1%value = 1
n2%value = 2
n3%value = 3
p => n1
call list%Add(p)
p => n2
call list%Add(p)
p => n3
call list%Add(p)

! check count
call check(error, list%Count() == 3, "count should be 3")
if (allocated(error)) return

! check get next/previous item
p => list%GetNextItem()
call check(error, associated(p, n1))
p => list%GetNextItem()
call check(error, associated(p, n2))
p => list%GetPreviousItem()
call check(error, associated(p, n1))
p => list%GetNextItem()
call check(error, associated(p, n2))
p => list%GetNextItem()
call check(error, associated(p, n3))
p => list%GetNextItem()
call check(error, (.not. associated(p)))
call list%Reset()
p => list%GetPreviousItem()
call check(error, (.not. associated(p)))

deallocate (list)
deallocate (n1)
deallocate (n2)
deallocate (n3)
end subroutine test_get_next_previous_item_reset

subroutine test_insert_after(error)
type(error_type), allocatable, intent(out) :: error
type(ListType), pointer :: list
type(IntNodeType), pointer :: n1, n2, n3
class(*), pointer :: p

allocate (list)
allocate (n1)
allocate (n2)
allocate (n3)

! add nodes 1 and 3
n1%value = 1
n2%value = 2
n3%value = 3
p => n1
call list%Add(p)
p => n3
call list%Add(p)

! check count
call check(error, list%Count() == 2, "count should be 2")
if (allocated(error)) return

! insert item after first item
p => n2
call list%InsertAfter(p, 1)

! check count
call check(error, list%Count() == 3, "count should be 3")
if (allocated(error)) return

! check get next/previous item
call list%Reset()
p => list%GetNextItem()
call check(error, associated(p, n1))
p => list%GetNextItem()
call check(error, associated(p, n2))
p => list%GetNextItem()
call check(error, associated(p, n3))
if (allocated(error)) return

deallocate (list)
deallocate (n1)
deallocate (n2)
deallocate (n3)
end subroutine test_insert_after

subroutine test_remove_node(error)
type(error_type), allocatable, intent(out) :: error
type(ListType), pointer :: list
type(IntNodeType), pointer :: n1, n2, n3
class(*), pointer :: p

allocate (list)
allocate (n1)
allocate (n2)
allocate (n3)

! add nodes
n1%value = 1
n2%value = 2
n3%value = 3
p => n1
call list%Add(p)
p => n2
call list%Add(p)
p => n3
call list%Add(p)

! check count
call check(error, list%Count() == 3, "count should be 3")
if (allocated(error)) return

! remove first node
call list%RemoveNode(1, .false.)
call check(error, list%Count() == 2, "count should be 2")
p => list%GetItem(1)
call check(error, associated(p, n2))
p => list%GetItem(2)
call check(error, associated(p, n3))

! remove last node
call list%RemoveNode(2, .false.)
call check(error, list%Count() == 1, "count should be 1")
p => list%GetItem(1)
call check(error, associated(p, n2))

deallocate (list)
deallocate (n1)
deallocate (n2)
deallocate (n3)
end subroutine test_remove_node

end module TestList
1 change: 1 addition & 0 deletions autotest/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ if test_drive.found() and not fc_id.contains('intel')
'GeomUtil',
'HashTable',
'InputOutput',
'List',
'MathUtil',
'Message',
'Sim'
Expand Down
2 changes: 2 additions & 0 deletions autotest/tester.f90
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ program tester
use TestGeomUtil, only: collect_geomutil
use TestHashTable, only: collect_hashtable
use TestInputOutput, only: collect_inputoutput
use TestList, only: collect_list
use TestMathUtil, only: collect_mathutil
use TestMessage, only: collect_message
use TestSim, only: collect_sim
Expand All @@ -23,6 +24,7 @@ program tester
new_testsuite("GeomUtil", collect_geomutil), &
new_testsuite("HashTable", collect_hashtable), &
new_testsuite("InputOutput", collect_inputoutput), &
new_testsuite("List", collect_list), &
new_testsuite("MathUtil", collect_mathutil), &
new_testsuite("Message", collect_message), &
new_testsuite("Sim", collect_sim) &
Expand Down