-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_array_semantics.py
More file actions
130 lines (85 loc) · 2.89 KB
/
Copy pathtest_array_semantics.py
File metadata and controls
130 lines (85 loc) · 2.89 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""Tests split by stable ownership concept from `test_compile_time_values.py`."""
from tests.fortran._support.semantic_conversion import (
array_contract,
fortran_module_to_semantic_module,
get_function,
parse_fortran_source,
)
def test_array_constraints():
source = """
module array_mod
contains
subroutine scale(x)
real(8), intent(inout) :: x(:)
end subroutine
end module
"""
fmod = parse_fortran_source(source)
smod = fortran_module_to_semantic_module(fmod)
func = get_function(smod, "scale")
x = func.arguments[0]
assert x.semantic_type.name == "Float64"
assert x.semantic_type.rank == 1
contract = array_contract(x.semantic_type)
assert contract.category == "assumed_shape"
assert contract.shape == ["::Strided"]
assert contract.source_shape == [":"]
assert contract.order is None
def test_matrix_semantics():
source = """
module linalg_mod
contains
subroutine matvec(A, x, y)
real(8), intent(in) :: A(:, :)
real(8), intent(in) :: x(:)
real(8), intent(out) :: y(:)
end subroutine
end module
"""
fmod = parse_fortran_source(source)
smod = fortran_module_to_semantic_module(fmod)
func = get_function(smod, "matvec")
A = func.arguments[0]
assert A.semantic_type.rank == 2
contract = array_contract(A.semantic_type)
assert A.semantic_type.shape == ["::Strided", "::Strided"]
assert contract.source_shape == [":", ":"]
assert contract.category == "assumed_shape"
assert contract.order == "ORDER_F"
def test_explicit_bound_ranges_remain_shaped_storage_contracts():
source = """
module bound_mod
contains
subroutine bounded(n, default_bound, zero_bound, shifted_bound)
integer, intent(in) :: n
real(8), intent(inout) :: default_bound(1:n)
real(8), intent(inout) :: zero_bound(0:n-1)
real(8), intent(inout) :: shifted_bound(2:n+1)
end subroutine bounded
end module bound_mod
"""
module = fortran_module_to_semantic_module(parse_fortran_source(source))
args = {arg.name: arg for arg in get_function(module, "bounded").arguments}
default_bound = array_contract(args["default_bound"].semantic_type)
assert default_bound.category == "explicit_shape"
assert default_bound.shape == ["n"]
zero_bound = array_contract(args["zero_bound"].semantic_type)
assert zero_bound.category == "explicit_shape"
assert zero_bound.shape == ["n"]
shifted_bound = array_contract(args["shifted_bound"].semantic_type)
assert shifted_bound.category == "explicit_shape"
assert shifted_bound.shape == ["n"]
def test_explicit_shape():
source = """
module shape_mod
contains
subroutine foo(A)
real(8), intent(in) :: A(10, 20)
end subroutine
end module
"""
fmod = parse_fortran_source(source)
smod = fortran_module_to_semantic_module(fmod)
func = get_function(smod, "foo")
A = func.arguments[0]
assert A.semantic_type.shape == ["10", "20"]