-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOperatorTransTest.lean
More file actions
173 lines (167 loc) · 5.74 KB
/
Copy pathOperatorTransTest.lean
File metadata and controls
173 lines (167 loc) · 5.74 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/-
Copyright Strata Contributors
SPDX-License-Identifier: Apache-2.0 OR MIT
-/
module
meta import StrataPython.Specs
meta import all StrataPython.Specs.DDM
meta import StrataPython.PythonDialect
meta import StrataPythonTest.Util.Python
open StrataDDM (SourceRange)
open StrataPython
open StrataPython.Specs
meta section
def testDir : System.FilePath := "StrataPythonTestExtra/Specs"
/-- Golden DDM rendering of `Specs/operators.py`. Each precondition exercises a
distinct operator arm of `transExpr`/`transCompare` so the expected program
pins down the SpecExpr vocabulary every Python operator lowers to. -/
def expectedOps :=
#strata
program PythonSpecs;
type "OpRequest" = dict(
a : ident("builtins.int") [required=true]
b : ident("builtins.int") [required=true]
c : ident("builtins.int") [required=true]
score : ident("builtins.float") [required=true]
items : ident("typing.List", ident("builtins.int")) [required=true]
flag1 : ident("builtins.bool") [required=true]
flag2 : ident("builtins.bool") [required=true])
function "arithmetic"{
args: [
]
kwonly: [
]
kwargs: kw : dict(
a : ident("builtins.int") [required=true]
b : ident("builtins.int") [required=true]
c : ident("builtins.int") [required=true]
score : ident("builtins.float") [required=true]
items : ident("typing.List", ident("builtins.int")) [required=true]
flag1 : ident("builtins.bool") [required=true]
flag2 : ident("builtins.bool") [required=true])
return: ident("_types.NoneType")
overload: false
preconditions: [
ensure(add(kw[a], kw[b]) >=_int kw[c], "add in ge")
ensure(sub(kw[a], kw[b]) >=_int kw[c], "sub in ge")
ensure(mul(kw[a], kw[b]) >=_int kw[c], "mul in ge")
ensure(floorDiv(kw[a], kw[b]) >=_int kw[c], "floordiv in ge")
ensure(mod(kw[a], kw[b]) >=_int kw[c], "mod in ge")
ensure(pow(kw[a], kw[b]) >=_int kw[c], "pow in ge")
ensure(neg(kw[a]) >=_int kw[c], "neg in ge")
]
postconditions: [
]
}
function "comparisons"{
args: [
]
kwonly: [
]
kwargs: kw : dict(
a : ident("builtins.int") [required=true]
b : ident("builtins.int") [required=true]
c : ident("builtins.int") [required=true]
score : ident("builtins.float") [required=true]
items : ident("typing.List", ident("builtins.int")) [required=true]
flag1 : ident("builtins.bool") [required=true]
flag2 : ident("builtins.bool") [required=true])
return: ident("_types.NoneType")
overload: false
preconditions: [
ensure(pcmp("gt", kw[a], kw[b]), "gt")
ensure(pcmp("lt", kw[a], kw[b]), "lt")
ensure(pcmp("ne", kw[a], kw[b]), "ne")
ensure(pcmp("eq", kw[a], 5), "eq int")
ensure(pcmp("in", kw[a], kw[items]), "isin")
ensure(pcmp("notIn", kw[a], kw[items]), "notin")
ensure(kw[a] >=_int 1, "int ge")
ensure(kw[a] <=_int 10, "int le")
ensure(kw[score] >=_float "0.0", "float ge")
ensure(kw[score] <=_float "1.0", "float le")
]
postconditions: [
]
}
function "identity"{
args: [
]
kwonly: [
]
kwargs: kw : dict(
a : ident("builtins.int") [required=true]
b : ident("builtins.int") [required=true]
c : ident("builtins.int") [required=true]
score : ident("builtins.float") [required=true]
items : ident("typing.List", ident("builtins.int")) [required=true]
flag1 : ident("builtins.bool") [required=true]
flag2 : ident("builtins.bool") [required=true])
return: ident("_types.NoneType")
overload: false
preconditions: [
ensure(pcmp("eq", kw[a], noneLit), "is none")
ensure(pcmp("ne", kw[a], noneLit), "is not none")
]
postconditions: [
]
}
function "boolean"{
args: [
]
kwonly: [
]
kwargs: kw : dict(
a : ident("builtins.int") [required=true]
b : ident("builtins.int") [required=true]
c : ident("builtins.int") [required=true]
score : ident("builtins.float") [required=true]
items : ident("typing.List", ident("builtins.int")) [required=true]
flag1 : ident("builtins.bool") [required=true]
flag2 : ident("builtins.bool") [required=true])
return: ident("_types.NoneType")
overload: false
preconditions: [
ensure(and(kw[flag1], kw[flag2]), "and")
ensure(or(kw[flag1], kw[flag2]), "or")
ensure(not(kw[flag1]), "not")
]
postconditions: [
]
}
#end
meta def testCase : IO Unit := withPython fun pythonCmd => do
IO.FS.withTempFile fun _handle dialectFile => do
IO.FS.writeBinFile dialectFile StrataPython.Python.toIon
IO.FS.withTempDir fun strataDir => do
let r ←
translateFile
(pythonCmd := toString pythonCmd)
(dialectFile := dialectFile)
(strataDir := strataDir)
(pythonFile := testDir / "operators.py")
(searchPath := testDir)
(.ofComponent (.ofString "operators"))
|>.toBaseIO
match r with
| .ok (sigs, warnings) =>
let pgm := toDDMProgram sigs
let pgmCommands := pgm.commands.map (·.mapAnn (fun _ => ()))
let expCommands := expectedOps.commands.map (·.mapAnn (fun _ => ()))
if pgmCommands != expCommands then
let mut diffMsg := s!"actual has {pgmCommands.size} commands, expected {expCommands.size}"
for i in [:pgmCommands.size.max expCommands.size] do
let actual := pgmCommands[i]?
let expected := expCommands[i]?
if actual != expected then
diffMsg := s!"Command {i} differs."
break
throw <| IO.userError s!"Operator translation output mismatch. {diffMsg}"
-- Every operator in operators.py is fully supported, so no warnings.
if !warnings.isEmpty then
let warnStr := warnings.foldl (init := "") fun acc w => s!"{acc}\n {w}"
throw <| IO.userError s!"Unexpected warnings:{warnStr}"
| .error e =>
throw <| IO.userError e
#guard_msgs in
#eval testCase
end