Skip to content

Commit 875def4

Browse files
committed
added test cases for inserting a list in itself
1 parent ea46f19 commit 875def4

File tree

1 file changed

+67
-7
lines changed

1 file changed

+67
-7
lines changed

src/tests/stringlist/test_insert_at.f90

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module test_insert_at
44
use stdlib_string_type, only: string_type, operator(//), operator(==)
55
use stdlib_stringlist_type, only: stringlist_type, fidx, bidx, list_head, list_tail, operator(==)
66
use stdlib_strings, only: to_string
7+
use stdlib_optval, only: optval
78
implicit none
89

910
contains
@@ -351,24 +352,82 @@ subroutine test_constructor
351352

352353
end subroutine test_constructor
353354

355+
subroutine test_insert_at_same_list
356+
type(stringlist_type) :: work_list
357+
type(stringlist_type) :: temp_list
358+
integer :: i, j
359+
integer, parameter :: first = -100
360+
integer, parameter :: last = 100
361+
integer, parameter :: stride = 4
362+
363+
write (*,*) "test_insert_at_same_list: Starting work_list!"
364+
365+
call work_list%insert_at( list_head, work_list )
366+
call work_list%insert_at( list_tail, work_list )
367+
368+
do j = -10, 10
369+
call work_list%insert_at( fidx(j), work_list )
370+
call work_list%insert_at( bidx(j), work_list )
371+
372+
end do
373+
374+
call compare_list( work_list, 0, 0, 13 )
375+
376+
do j = first, last
377+
call work_list%insert_at( list_tail, string_type( to_string(j) ) )
378+
end do
379+
380+
temp_list = work_list
381+
call work_list%insert_at( list_head, work_list )
382+
call compare_list( work_list, first, last + 1, 14, to=last - first + 1 )
383+
call compare_list( work_list, first, last + 1, 15, from=last - first + 2 )
384+
385+
work_list = temp_list
386+
call work_list%insert_at( list_tail, work_list )
387+
call compare_list( work_list, first, last + 1, 16, to=last - first + 1 )
388+
call compare_list( work_list, first, last + 1, 17, from=last - first + 2 )
389+
390+
work_list = temp_list
391+
call compare_list( work_list, first, last + 1, 18 )
392+
393+
do j = 1, last - first + 2
394+
temp_list = work_list
395+
call temp_list%insert_at( fidx(j), temp_list )
396+
397+
call compare_list( temp_list, first, first + j - 1, 19, to=j - 1)
398+
call compare_list( temp_list, first, last + 1, 20, from=j, to=j + last - first)
399+
call compare_list( temp_list, first + j - 1, last + 1, 21, from=j + last - first + 1)
400+
401+
end do
402+
403+
end subroutine test_insert_at_same_list
404+
354405
! compares input stringlist 'list' with an array of consecutive integers
355406
! array is 'first' inclusive and 'last' exclusive
356-
subroutine compare_list(list, first, last, call_number)
407+
subroutine compare_list(list, first, last, call_number, from, to)
357408
type(stringlist_type), intent(in) :: list
358409
integer, intent(in) :: first, last, call_number
359-
integer :: i, j
410+
integer, intent(in), optional :: from, to
411+
integer :: i, j, k, work_from, work_to, length
412+
413+
length = list%len()
414+
work_from = optval( from, 1 )
415+
work_to = optval( to, length )
360416

361-
call check( abs( last - first ) == list%len(), "compare_list: length mis-match&
417+
call check( abs( last - first ) == max( 0, work_to - work_from + 1 ), "compare_list: length mis-match&
362418
& call_number " // to_string( call_number ) )
363419

364420
j = merge(-1, 1, last < first)
365-
do i = 1, list%len()
366-
call check( list%get( fidx(i) ) == to_string( first + ( ( i - 1 ) * j ) ), &
421+
do i = work_from, work_to
422+
call check( list%get( fidx(i) ) == to_string( first + ( ( i - work_from ) * j ) ), &
367423
& "compare_list: call_number " // to_string( call_number ) &
368424
& // " fidx( " // to_string( i ) // " )")
369-
call check( list%get( bidx(i) ) == to_string( last - ( i * j ) ), &
425+
426+
k = length - ( work_to - ( i - work_from ) ) + 1
427+
call check( list%get( bidx(k) ) == &
428+
& to_string( last - ( ( i - work_from + 1 ) * j ) ), &
370429
& "compare_list: call_number " // to_string( call_number ) &
371-
& // " bidx( " // to_string( i ) // " )")
430+
& // " bidx( " // to_string( k ) // " )")
372431
end do
373432

374433
end subroutine compare_list
@@ -385,6 +444,7 @@ program tester
385444
call test_insert_at_string_3
386445
call test_insert_at_array
387446
call test_insert_at_list
447+
call test_insert_at_same_list
388448
call test_constructor
389449

390450
end program tester

0 commit comments

Comments
 (0)