3 use fruit
, only : assert_equals, add_fail, assert_false, assert_true
4 use collections_mod, only :
map_type,
stack_type,
queue_type,
list_type, c_put,
c_free, c_get,
c_key_at,
c_contains,
c_size, &
14 class(*),
pointer :: iptr
16 call assert_equals(.true.,
c_is_empty(my_map),
"map_type is empty")
19 allocate(iptr, source=i)
20 call c_put(my_map,
str(i), iptr)
21 call assert_equals(i,
c_size(my_map),
"map_type size incremented with put")
26 allocate(iptr, source=v)
27 call c_put(my_map,
str(j), iptr)
28 call assert_equals(10,
c_size(my_map),
"map_type size remains unchanged after duplicate key")
39 class(*),
pointer :: iptr, data
41 call assert_equals(.true.,
c_is_empty(my_map),
"map_type is empty")
44 allocate(iptr, source=i)
45 call c_put(my_map,
"A", iptr)
46 data=>c_get(my_map,
"A")
49 call assert_equals(i,
data,
"Value of key-value pair is correct")
51 call add_fail(
"Unknown type")
53 call assert_equals(1,
c_size(my_map),
"Size of may is one due to unique key")
62 class(*),
pointer :: iptr, data
65 call c_put(my_map,
"A", iptr)
67 data=>c_get(my_map,
"A")
70 call assert_equals(50,
data,
"Value of entry has been changed through modifying original variable")
72 call add_fail(
"Unknown type")
83 class(*),
pointer :: iptr, data
85 call assert_equals(.true.,
c_is_empty(my_map),
"map_type is empty")
89 call assert_false(
c_contains(my_map,
str(i)),
"map_type does not contain the key before put")
90 allocate(iptr, source=v)
91 call c_put(my_map,
str(i), iptr)
92 call assert_true(
c_contains(my_map,
str(i)),
"map_type contains the key after put")
93 data => c_get(my_map,
str(i))
96 call assert_equals(i*10,
data,
"Value of entry is consistent")
98 call assert_equals(
str(i),
c_key_at(my_map, i),
"Key at location i is consistent")
103 allocate(iptr, source=x)
104 call c_put(my_map,
str(j), iptr)
105 data => c_get(my_map,
str(j))
108 call assert_equals(j*100,
data,
"Value modified due to duplicate key")
110 call add_fail(
"Unknown type")
112 data => c_value_at(my_map, j)
115 call assert_equals(j*100,
data,
"Value at returned correct value at i")
117 call add_fail(
"Unknown type")
129 class(*),
pointer :: iptr, data
131 call assert_equals(.true.,
c_is_empty(my_map),
"map_type is empty")
135 allocate(iptr, source=v)
136 call c_put(my_map,
str(i), iptr)
137 data=>c_get(my_map,
str(i))
140 call assert_equals(i*10,
data,
"Value of key is consistent")
142 call add_fail(
"Unknown type")
147 call assert_true(
c_contains(my_map,
str(11-j)),
"map_type contains the key pre-removal")
149 call assert_false(
c_contains(my_map,
str(11-j)),
"map_type does not contain the key post-removal")
150 call assert_equals(10-j,
c_size(my_map),
"Size of map_type is consistent post-removal")
152 call assert_equals(.true.,
c_is_empty(my_map),
"map_type is empty at the end")
158 character(len=15) function str(k)
159 integer,
intent(in) :: k
167 class(*),
pointer :: iptr, data
170 call c_push(my_stack, iptr)
172 data=>c_pop(my_stack)
175 call assert_equals(50,
data,
"Stack data modified through changing original variable")
177 call add_fail(
"Unknown type")
187 class(*),
pointer :: iptr, data
189 call assert_equals(.true.,
c_is_empty(my_stack),
"Stack is empty")
192 allocate(iptr, source=i)
193 call c_push(my_stack, iptr)
194 call assert_equals(i,
c_size(my_stack),
"Size of stack_type increasing as data pushed")
197 call assert_equals(.false.,
c_is_empty(my_stack),
"Stack is not empty after values pushed")
200 data => c_pop(my_stack)
203 call assert_equals(
data, 11-j,
"Stack pop gives LIFO value")
204 call assert_equals(10-j,
c_size(my_stack),
"Stack pop removes the LIFO value")
206 call add_fail(
"Type unknown")
216 class(*),
pointer :: iptr, data
219 call c_push(my_queue, iptr)
221 data=>c_pop(my_queue)
224 call assert_equals(50,
data,
"Queue data modified by changing the original variable")
226 call add_fail(
"Unknown type")
236 class(*),
pointer :: iptr, data
238 call assert_equals(.true.,
c_is_empty(my_queue),
"Queue is empty")
241 allocate(iptr, source=i)
242 call c_push(my_queue, iptr)
243 call assert_equals(i,
c_size(my_queue),
"Queue size increases as elements are pushed")
246 call assert_equals(.false.,
c_is_empty(my_queue),
"Queue is not empty after elements pushed")
249 data => c_pop(my_queue)
252 call assert_equals(
data, j,
"Queue popped element is FIFO")
253 call assert_equals(10-j,
c_size(my_queue),
"Queue pop removes element")
255 call add_fail(
"Type unknown")
266 class(*),
pointer :: iptr
268 call assert_equals(.true.,
c_is_empty(my_list),
"List is empty")
271 allocate(iptr, source=i)
272 call c_add(my_list, iptr)
273 call assert_equals(i,
c_size(my_list),
"List add increases list_type size")
276 call assert_equals(.false.,
c_is_empty(my_list),
"List is not empty after element adds")
284 class(*),
pointer :: iptr, data
287 call c_add(my_list, iptr)
289 data=>c_get(my_list, 0)
292 call assert_equals(50,
data,
"List element modified by changing original value")
294 call add_fail(
"Unknown type")
305 class(*),
pointer :: iptr, data
308 allocate(iptr, source=i)
309 call c_add(my_list, iptr)
312 call assert_equals(10,
c_size(my_list),
"List size increased after adding elements")
315 data => c_get(my_list, j)
318 call assert_equals(j,
data,
"Element at location j is consistent with expectations")
320 call add_fail(
"Type unknown")
331 integer :: i, element_to_remove, j, k
332 class(*),
pointer :: iptr=>null(), data=>null()
335 allocate(iptr, source=i)
336 call c_add(my_list, iptr)
341 allocate(iptr, source=j)
342 call c_insert(my_list, iptr, i)
345 call assert_equals(20,
c_size(my_list),
"Post addition and insertion list_type size is correct")
348 data => c_get(my_list, k)
351 call assert_equals(
data, merge(k-10, k*100, k .gt. 10),
"Element at k is consistent with addition and insertion")
353 call add_fail(
"Unknown type")
364 integer :: i, j, element_to_remove
365 class(*),
pointer :: iptr=>null(), data=>null()
369 call random_number(r)
370 element_to_remove = int(r*9)+1
373 allocate(iptr, source=i)
374 call c_add(my_list, iptr)
377 data => c_get(my_list, element_to_remove)
380 call assert_equals(element_to_remove,
data,
"Element to remove is consistent")
382 call add_fail(
"Unknown type")
384 call c_remove(my_list, element_to_remove)
387 if (j .ne. element_to_remove)
then 388 data => c_get(my_list, j)
391 call assert_equals(merge(j, j+1, j .lt. element_to_remove),
data,
"After element removal element at j is consistent")
393 call add_fail(
"Unknown type")
403 integer :: i, n, clock
404 integer,
dimension(:),
allocatable :: seed
406 call random_seed(
size = n)
409 call system_clock(count=clock)
411 seed = clock + 37 * (/ (i - 1, i = 1, n) /)
412 call random_seed(put=seed)
421 use fruit
, only : init_fruit, run_test_case, fruit_summary
subroutine test_map_type_put
Retrieves the key currently being held at a specific index in the map or "" if the index > map elemen...
Returns whether a collection is empty.
subroutine init_random_seed()
subroutine test_map_type_remove
subroutine test_map_type_put_unique
subroutine test_stack_type_pointers
subroutine test_map_type_get
program test_collections_driver
subroutine test_list_type_insert
subroutine test_stack_type_push_pop
Map data structure that holds string (length 20 maximum) key value pairs.
Returns the number of elements in the collection.
subroutine test_list_type_pointers
Collection data structures.
subroutine test_list_type_get
character(len=15) function str(k)
subroutine test_map_type_pointers
List data structure which implements a doubly linked list. This list will preserve its order...
Queue (FIFO) data structure.
subroutine test_queue_type_push_pop
Frees up all the allocatable, heap, memory associated with a list, stack, queue or map...
subroutine test_queue_type_pointers
subroutine test_list_type_remove
Stack (FILO) data structure.
Determines whether or not a map contains a specific key.
subroutine test_list_type_add
Removes a specific element from the list or map.