Skip to content

Instantly share code, notes, and snippets.

@alanbriolat
Created June 3, 2013 14:43
Show Gist options
  • Save alanbriolat/5698673 to your computer and use it in GitHub Desktop.
Save alanbriolat/5698673 to your computer and use it in GitHub Desktop.
Checking for ASSOCIATE deficiencies in Fortran compilers when combined with derived type arrays. Must be saved with uppercase extension (broken_associate.F90) so the C preprocessor bits will work.
program test_associate
implicit none
type foo_t
integer :: a, b, c
end type
type(foo_t), dimension(6) :: foo
type(foo_t) :: sfoo
foo%a = [1, 2, 3, 4, 5, 6]
foo%b = [7, 8, 9, 10, 11, 12]
foo%c = [13, 14, 15, 16, 17, 18]
sfoo%a = 30
sfoo%b = 40
sfoo%c = 50
print *, "Initial data:"
print *, "foo%a: ", foo%a
print *, "foo%b: ", foo%b
print *, "foo%c: ", foo%c
print *, "sfoo: ", "a: ", sfoo%a, "b: ", sfoo%b, "c: ", sfoo%c
print *, ""
print *, "Scalar component (in scalar): a => sfoo%a"
associate (a => sfoo%a, b => sfoo%b, c => sfoo%c)
call test(a == 30 .and. b == 40 .and. c == 50)
end associate
print *, ""
print *, "Scalar component (in array): a => foo(2)%a"
associate (a => foo(2)%a, b => foo(3)%b, c => foo(4)%c)
call test(a == 2 .and. b == 9 .and. c == 16)
end associate
print *, ""
print *, "Array: f => foo"
associate (f => foo)
#if defined(__GFORTRAN__)
print *, "FAILED (skipped): referencing a component (e.g. `f%a * 4`) crashes GFortran (tested on 4.6 and 4.8)"
#else
call test(all(f%b == [7, 8, 9, 10, 11, 12]))
#endif
end associate
print *, ""
print *, "Component of array: a => foo%a"
associate (a => foo%a)
! Component array references aren't strided properly in gfortran (tested 4.6 and 4.8)
call test(all(a == [1, 2, 3, 4, 5, 6]))
end associate
print *, ""
contains
subroutine test(passed)
logical, intent(in) :: passed
if (passed) then
print *, "PASSED"
else
print *, "FAILED"
end if
end subroutine
end program
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment