|
| 1 | +module TestList |
| 2 | + use testdrive, only: error_type, unittest_type, new_unittest, check |
| 3 | + use ListModule, only: ListType, ListNodeType |
| 4 | + |
| 5 | + implicit none |
| 6 | + private |
| 7 | + public :: collect_list |
| 8 | + |
| 9 | + type :: IntNodeType |
| 10 | + integer :: value |
| 11 | + end type IntNodeType |
| 12 | +contains |
| 13 | + |
| 14 | + subroutine collect_list(testsuite) |
| 15 | + type(unittest_type), allocatable, intent(out) :: testsuite(:) |
| 16 | + testsuite = [ & |
| 17 | + new_unittest("count", test_count), & |
| 18 | + new_unittest("add", test_add), & |
| 19 | + new_unittest("remove_node_by_index", test_remove_node_by_index) & |
| 20 | + ] |
| 21 | + end subroutine collect_list |
| 22 | + |
| 23 | + subroutine test_count(error) |
| 24 | + type(error_type), allocatable, intent(out) :: error |
| 25 | + type(ListType), pointer :: list => null() |
| 26 | + type(IntNodeType), pointer :: p1 => null() |
| 27 | + class(*), pointer :: p => null() |
| 28 | + |
| 29 | + ! allocate |
| 30 | + allocate (list) |
| 31 | + allocate (p1) |
| 32 | + |
| 33 | + ! empty case |
| 34 | + call check(error, list%Count() == 0) |
| 35 | + if (allocated(error)) return |
| 36 | + |
| 37 | + ! one node case |
| 38 | + p1%value = 1 |
| 39 | + p => p1 |
| 40 | + call list%Add(p) |
| 41 | + call check(error, list%Count() == 1) |
| 42 | + if (allocated(error)) return |
| 43 | + |
| 44 | + ! deallocate |
| 45 | + deallocate (list) |
| 46 | + deallocate (p1) |
| 47 | + end subroutine test_count |
| 48 | + |
| 49 | + subroutine test_add(error) |
| 50 | + type(error_type), allocatable, intent(out) :: error |
| 51 | + type(ListType), pointer :: list => null() |
| 52 | + type(IntNodeType), pointer :: p1 => null() |
| 53 | + class(*), pointer :: p => null() |
| 54 | + |
| 55 | + ! allocate |
| 56 | + allocate (list) |
| 57 | + allocate (p1) |
| 58 | + |
| 59 | + ! add a node |
| 60 | + p1%value = 1 |
| 61 | + p => p1 |
| 62 | + call list%Add(p) |
| 63 | + call check(error, list%Count() == 1) |
| 64 | + if (allocated(error)) return |
| 65 | + |
| 66 | + ! deallocate |
| 67 | + deallocate (list) |
| 68 | + end subroutine test_add |
| 69 | + |
| 70 | + subroutine test_remove_node_by_index(error) |
| 71 | + type(error_type), allocatable, intent(out) :: error |
| 72 | + type(ListType), pointer :: list => null() |
| 73 | + type(IntNodeType), pointer :: p1 => null() |
| 74 | + class(*), pointer :: p => null() |
| 75 | + |
| 76 | + ! allocate |
| 77 | + allocate (list) |
| 78 | + allocate (p1) |
| 79 | + |
| 80 | + ! add a node |
| 81 | + p1%value = 1 |
| 82 | + p => p1 |
| 83 | + call list%Add(p) |
| 84 | + call check(error, list%Count() == 1) |
| 85 | + if (allocated(error)) return |
| 86 | + |
| 87 | + ! remove a node |
| 88 | + call list%RemoveNode(1, destroyValue=.false.) |
| 89 | + call check(error, list%Count() == 0) |
| 90 | + |
| 91 | + ! deallocate |
| 92 | + deallocate (list) |
| 93 | + deallocate (p1) |
| 94 | + end subroutine test_remove_node_by_index |
| 95 | + |
| 96 | +end module TestList |
0 commit comments