summaryrefslogtreecommitdiff
path: root/gcc/testsuite/gfortran.dg/structure_constructor_1.f03
blob: 8f8f58ef92057b8dd0eb68e65663c51f86d49e4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
! { dg-do run }
! Simple structure constructors, without naming arguments, default values
! or inheritance and the like.

PROGRAM test
  IMPLICIT NONE

  ! Empty structuer
  TYPE :: empty_t
  END TYPE empty_t

  ! Structure of basic data types
  TYPE :: basics_t
    INTEGER :: i
    REAL :: r
    COMPLEX :: c
    LOGICAL :: l
  END TYPE basics_t

  ! Structure with strings
  TYPE :: strings_t
    CHARACTER(len=5) :: str1, str2
    CHARACTER(len=10) :: long
  END TYPE strings_t

  ! Structure with arrays
  TYPE :: array_t
    INTEGER :: ints(2:5)
    REAL :: matrix(2, 2)
  END TYPE array_t

  ! Structure containing structures
  TYPE :: nestedStruct_t
    TYPE(basics_t) :: basics
    TYPE(array_t) :: arrays
  END TYPE nestedStruct_t

  TYPE(empty_t) :: empty
  TYPE(basics_t) :: basics
  TYPE(strings_t) :: strings
  TYPE(array_t) :: arrays
  TYPE(nestedStruct_t) :: nestedStruct

  empty = empty_t ()

  basics = basics_t (42, -1.5, (.5, .5), .FALSE.)
  IF (basics%i /= 42 .OR. basics%r /= -1.5 &
      .OR. basics%c /= (.5, .5) .OR. basics%l) THEN
    CALL abort()
  END IF

  strings = strings_t ("hello", "abc", "this one is long")
  IF (strings%str1 /= "hello" .OR. strings%str2 /= "abc" &
      .OR. strings%long /= "this one i") THEN
    CALL abort()
  END IF

  arrays = array_t ( (/ 1, 2, 3, 4 /), RESHAPE((/ 5, 6, 7, 8 /), (/ 2, 2 /)) )
  IF (arrays%ints(2) /= 1 .OR. arrays%ints(3) /= 2 &
      .OR. arrays%ints(4) /= 3 .OR. arrays%ints(5) /= 4 &
      .OR. arrays%matrix(1, 1) /= 5. .OR. arrays%matrix(2, 1) /= 6. &
      .OR. arrays%matrix(1, 2) /= 7. .OR. arrays%matrix(2, 2) /= 8.) THEN
    CALL abort()
  END IF

  nestedStruct = nestedStruct_t (basics_t (42, -1.5, (.5, .5), .FALSE.), arrays)
  IF (nestedStruct%basics%i /= 42 .OR. nestedStruct%basics%r /= -1.5 &
      .OR. nestedStruct%basics%c /= (.5, .5) .OR. nestedStruct%basics%l &
      .OR. ANY(nestedStruct%arrays%ints /= arrays%ints) &
      .OR. ANY(nestedStruct%arrays%matrix /= arrays%matrix)) THEN
    CALL abort()
  END IF

END PROGRAM test