Skip to content

Commit c7629bc

Browse files
committed
more auto-allocate cases
1 parent 787d8cc commit c7629bc

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

array/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
set_directory_properties(PROPERTIES LABELS array)
2+
13
add_executable(rotflip test_rot90.f90 rot90.f90)
24
add_test(NAME array:RotFlip COMMAND rotflip)
35

array/auto_allocate.f90

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
program auto_allocate
22
!! demonstrate Fortran 2003 automatic allocation of arrays
33
!!
4-
!! NOTE: if an array is manually allocated, Fortran 2003 allows auto-reallocation bigger or smaller
4+
!! NOTE: even if an array allocate(), Fortran 2003 still allows auto-reallocation bigger or smaller
5+
!! The A(:) syntax preserves previously allocated LHS shape, truncating RHS
56

7+
use, intrinsic :: iso_fortran_env, only : stderr=>error_unit
68
implicit none (type, external)
79

810
real, allocatable, dimension(:) :: A, B, C, D, E
@@ -36,8 +38,21 @@ program auto_allocate
3638
D = [1,2]
3739
E = [3,4,5,7]
3840

39-
if (size(D) /= 2) error stop 'auto-allocate smaller'
40-
if (size(E) /= 4) error stop 'auto-allocate bigger'
41+
if (size(D) /= 2) error stop 'allocate() auto-allocate small'
42+
if (size(E) /= 4) error stop 'allocate() auto-allocate big'
43+
44+
!> (:) syntax truncates, does not change shape, whether or not allocate() used first
45+
A(:) = [9,8,7]
46+
if (size(A) /= 2) error stop '(:) syntax smaller'
47+
if (any(A /= [9,8])) error stop '(:) assign small'
48+
49+
E(:) = [1,2,3]
50+
if (size(E) /= 4) error stop 'allocate() (:) syntax small'
51+
if (any(E /= [1,2,3,7])) error stop 'allocate() (:) assign small'
52+
53+
E(:) = [5,4,3,2,1]
54+
if (size(E) /= 4) error stop 'allocate() (:) syntax: big'
55+
if (any(E /= [5,4,3,2])) error stop 'allocate() (:) assign: big'
4156

4257
print *, 'OK: auto-allocate array'
4358

0 commit comments

Comments
 (0)