|
| 1 | +! Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. |
| 2 | +! |
| 3 | +! Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +! you may not use this file except in compliance with the License. |
| 5 | +! You may obtain a copy of the License at |
| 6 | +! |
| 7 | +! http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +! |
| 9 | +! Unless required by applicable law or agreed to in writing, software |
| 10 | +! distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +! See the License for the specific language governing permissions and |
| 13 | +! limitations under the License. |
| 14 | + |
| 15 | +! c_associated call variants |
| 16 | + |
| 17 | +logical function f1() ! scalars |
| 18 | + use, intrinsic :: iso_c_binding, only: c_associated, c_loc, c_null_ptr, c_ptr |
| 19 | + implicit none |
| 20 | + |
| 21 | + interface |
| 22 | + type(c_ptr) function copy(pp) |
| 23 | + import |
| 24 | + type(c_ptr) :: pp |
| 25 | + end function copy |
| 26 | + end interface |
| 27 | + |
| 28 | + integer, target :: a(5), b(5) |
| 29 | + type(c_ptr) :: p0, pa, pb, px |
| 30 | + logical :: T1, T2, T3, T4, T5, T6, T7, T8 |
| 31 | + |
| 32 | + p0 = c_null_ptr |
| 33 | + pa = c_loc(a) |
| 34 | + pb = c_loc(b) |
| 35 | + px = pa |
| 36 | + |
| 37 | + T1 = .not. c_associated(p0) |
| 38 | + T2 = c_associated(pa) |
| 39 | + T3 = c_associated(pa, px) |
| 40 | + T4 = .not. c_associated(pa, pb) |
| 41 | + |
| 42 | + T5 = .not. c_associated(copy(p0)) |
| 43 | + T6 = c_associated(copy(pa)) |
| 44 | + T7 = c_associated(copy(pa), copy(px)) |
| 45 | + T8 = .not. c_associated(copy(pa), copy(pb)) |
| 46 | + |
| 47 | + print*, 'f1: ', T1, T2, T3, T4, ' ', T5, T6, T7, T8 |
| 48 | + f1 = all([T1, T2, T3, T4, T5, T6, T7, T8]) |
| 49 | +end function f1 |
| 50 | + |
| 51 | +logical function f2() ! elements |
| 52 | + use, intrinsic :: iso_c_binding, only: c_associated, c_loc, c_ptr |
| 53 | + implicit none |
| 54 | + |
| 55 | + interface |
| 56 | + type(c_ptr) function copy(pp) |
| 57 | + import |
| 58 | + type(c_ptr) :: pp |
| 59 | + end function copy |
| 60 | + end interface |
| 61 | + |
| 62 | + integer, pointer :: a(:), b(:), x(:) |
| 63 | + logical :: T1, T2, T3, T4, T5, T6, T7, T8 |
| 64 | + |
| 65 | + allocate(x(5)) |
| 66 | + a => x |
| 67 | + b => x(2:5) |
| 68 | + |
| 69 | + T1 = c_associated(c_loc(a(5))) |
| 70 | + T2 = .not. c_associated(c_loc(a(1)), c_loc(b(1))) |
| 71 | + T3 = c_associated(c_loc(a(2)), c_loc(b)) |
| 72 | + T4 = c_associated(c_loc(a(2)), c_loc(b(1))) |
| 73 | + |
| 74 | + T5 = c_associated(copy(c_loc(a(5)))) |
| 75 | + T6 = .not. c_associated(copy(c_loc(a(1))), copy(c_loc(b(1)))) |
| 76 | + T7 = c_associated(copy(c_loc(a(2))), copy(c_loc(b))) |
| 77 | + T8 = c_associated(copy(c_loc(a(2))), copy(c_loc(b(1)))) |
| 78 | + |
| 79 | + print*, 'f2: ', T1, T2, T3, T4, ' ', T5, T6, T7, T8 |
| 80 | + f2 = all([T1, T2, T3, T4, T5, T6, T7, T8]) |
| 81 | +end function f2 |
| 82 | + |
| 83 | +logical function f3() ! components |
| 84 | + use, intrinsic :: iso_c_binding, only: c_associated, c_loc, c_null_ptr, c_ptr |
| 85 | + implicit none |
| 86 | + |
| 87 | + interface |
| 88 | + type(c_ptr) function copy(pp) |
| 89 | + import |
| 90 | + type(c_ptr) :: pp |
| 91 | + end function copy |
| 92 | + end interface |
| 93 | + |
| 94 | + type tt |
| 95 | + type(c_ptr) :: a, b, z |
| 96 | + end type tt |
| 97 | + |
| 98 | + type(tt) :: v |
| 99 | + integer, target :: x(5) |
| 100 | + logical :: T1, T2, T3, T4, T5, T6, T7, T8 |
| 101 | + |
| 102 | + v%a = c_loc(x) |
| 103 | + v%b = c_loc(x(2)) |
| 104 | + v%z = c_null_ptr |
| 105 | + |
| 106 | + T1 = c_associated(v%a) |
| 107 | + T2 = .not. c_associated(v%a, v%b) |
| 108 | + T3 = c_associated(v%b, c_loc(x(2))) |
| 109 | + T4 = .not. c_associated(v%z) |
| 110 | + |
| 111 | + T5 = c_associated(copy(v%a)) |
| 112 | + T6 = .not. c_associated(copy(v%a), copy(v%b)) |
| 113 | + T7 = c_associated(copy(v%b), copy(c_loc(x(2)))) |
| 114 | + T8 = .not. c_associated(copy(v%z)) |
| 115 | + |
| 116 | + print*, 'f3: ', T1, T2, T3, T4, ' ', T5, T6, T7, T8 |
| 117 | + f3 = all([T1, T2, T3, T4, T5, T6, T7, T8]) |
| 118 | +end function f3 |
| 119 | + |
| 120 | +type(c_ptr) function copy(pp) |
| 121 | + use, intrinsic :: iso_c_binding, only: c_ptr |
| 122 | + implicit none |
| 123 | + type(c_ptr) :: pp |
| 124 | + copy = pp |
| 125 | +end function copy |
| 126 | + |
| 127 | + implicit none |
| 128 | + logical :: LL, f1, f2, f3 |
| 129 | + |
| 130 | + LL = f1() |
| 131 | + LL = LL .and. f2() |
| 132 | + LL = LL .and. f3() |
| 133 | + |
| 134 | + if (.not. LL) print*, 'FAIL' |
| 135 | + if ( LL) print*, 'PASS' |
| 136 | +end |
0 commit comments