-
Notifications
You must be signed in to change notification settings - Fork 4
/
OptimiserTests.fs
250 lines (221 loc) · 9.42 KB
/
OptimiserTests.fs
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/// <summary>
/// Tests for the optimiser.
/// </summary>
module Starling.Tests.Optimiser
open Chessie.ErrorHandling
open NUnit.Framework
open Starling.Collections
open Starling.Utils
open Starling.Core.TypeSystem
open Starling.Core.Expr
open Starling.Core.Var
open Starling.Core.Symbolic
open Starling.Core.Symbolic.Traversal
open Starling.Core.Model
open Starling.Core.View
open Starling.Core.View.Traversal
open Starling.Core.Command
open Starling.Core.Traversal
open Starling.Optimiser.Graph
open Starling.Optimiser.Term
/// Tests for the term optimiser.
type OptimiserTests() =
(*
* Term optimisation
*)
/// A test environment of arithmetic after substitutions.
static member AfterArithSubs =
[ ("serving", IAdd [siBefore "serving"; IInt 1L])
("ticket", siBefore "ticket") ]
|> Map.ofList
static member AfterBoolSubs =
[ ("flag", BNot (sbBefore "flag"))
("turn", sbBefore "turn") ]
|> Map.ofList
/// Test cases for discovering Boolean after-before pairs.
static member BoolAfterDiscoveries =
[ TestCaseData(bEq (sbAfter "foo") (sbBefore "foo"))
.Returns( [ ("foo", sbBefore "foo") ]
|> Map.ofList)
.SetName("Detect a simple Boolean after-before pair")
TestCaseData(BNot (bEq (sbAfter "foo") (sbBefore "foo")))
.Returns(Map.empty : Map<string, SMBoolExpr> )
.SetName("Ignore a negated Boolean after-before pair")
TestCaseData(BAnd
[ bEq (sbAfter "foo") (sbBefore "foo")
bEq (sbAfter "bar") (BNot (sbBefore "bar")) ] )
.Returns( [ ("foo", sbBefore "foo")
("bar", BNot (sbBefore "bar")) ]
|> Map.ofList)
.SetName("Detect a conjunction of Boolean after-before pairs")
TestCaseData(BOr
[ bEq (sbAfter "foo") (sbBefore "foo")
bEq (sbAfter "bar") (BNot (sbBefore "bar")) ] )
.Returns(Map.empty : Map<string, SMBoolExpr> )
.SetName("Ignore a disjunction of Boolean after-before pairs") ]
/// Test discovery of Boolean before-after pairs.
[<TestCaseSource("BoolAfterDiscoveries")>]
member x.``Boolean before-after pairs should be found correctly`` b =
b |> findBoolAfters |> Map.ofList
/// Test cases for simplification.
static member ObviousBools =
[ TestCaseData(BNot BFalse : MBoolExpr)
.Returns(BTrue : MBoolExpr)
.SetName("Simplify !false as a tautology")
TestCaseData(BAnd [BTrue; BTrue; BNot BFalse; bEq BFalse BFalse] : MBoolExpr)
.Returns(BTrue : MBoolExpr)
.SetName("Simplify true&&true&&!false&&false==false to true")
TestCaseData(BImplies (BFalse, bEq BTrue BFalse) : MBoolExpr)
.Returns(BTrue : MBoolExpr)
.SetName("Simplify false=>true==false to true")
TestCaseData(BNot BTrue : MBoolExpr)
.Returns(BFalse : MBoolExpr)
.SetName("Simplify !true as a contradiction")
TestCaseData(BAnd [BTrue; BFalse; BNot BFalse; bEq BFalse BFalse] : MBoolExpr)
.Returns(BFalse : MBoolExpr)
.SetName("Simplify true&&false&&!false&&false==false to false")
TestCaseData(BImplies (BTrue, bEq BTrue BFalse) : MBoolExpr)
.Returns(BFalse : MBoolExpr)
.SetName("Simplify true=>true==false to false")
TestCaseData(BImplies ((bAfter "s"), BFalse))
.Returns(BNot (bAfter "s"))
.SetName("Simplify =>False into a Negation")
TestCaseData(BImplies ((bAfter "s"), BTrue))
.Returns(BTrue : MBoolExpr)
.SetName("Simplify =>True into a True")
TestCaseData(BImplies (BGt (normalInt (iAfter "s"), normalInt (iAfter "t")), BFalse))
.Returns(BLe (normalInt (iAfter "s"), normalInt (iAfter "t" )))
.SetName("Simplify =>False wrapped around a > into <=")
TestCaseData(BImplies (BTrue, (bAfter "s")))
.Returns((bAfter "s"))
.SetName("Simplify True=>s into s")
TestCaseData(BImplies (BFalse, (bAfter "s")))
.Returns(BTrue : MBoolExpr)
.SetName("Simplify False=>s into True")
]
/// Test Boolean simplification
[<TestCaseSource("ObviousBools")>]
member x.``Boolean expressions should be simplified properly`` (b : BoolExpr<MarkedVar>) =
simp b
/// Test cases for rewriting Boolean expressions containing afters.
module AfterExprs =
open Starling.Utils.Testing
open Starling.Core.Pretty
open Starling.Core.Traversal.Pretty
open Starling.Optimiser.Pretty
/// Test after-elimination of Booleans.
let check expected case : unit =
let trav =
tliftToBoolSrc
(tliftToTypedSymVarSrc
(afterSubs OptimiserTests.AfterArithSubs OptimiserTests.AfterBoolSubs))
let result = mapTraversal trav (normalBool case)
assertOkAndEqual expected result
(printTraversalError printTermOptError >> printUnstyled)
[<Test>]
let ``Remove arithmetic afters in a simple equality`` () =
check
(iEq
(IAdd [ siBefore "serving"; IInt 1L ] )
(siBefore "ticket"))
(iEq (siAfter "serving") (siAfter "ticket"))
[<Test>]
let ``Remove arithmetic afters in after-before relation`` () =
check
(iEq
(IAdd [ siBefore "serving"; IInt 1L ] )
(IAdd [ siBefore "serving"; IInt 1L ] ))
(iEq
(siAfter "serving")
(IAdd [ siBefore "serving"; IInt 1L ] ))
[<Test>]
let ``Remove arithmetic afters only if in the environment`` () =
check
(BGt (normalInt (IAdd [ siBefore "serving"; IInt 1L ]), normalInt (siAfter "t")))
(BGt (normalInt (siAfter "serving"), normalInt (siAfter "t")))
[<Test>]
let ``Remove Boolean afters in a simple equality`` () =
check
(bEq
(BNot (sbBefore "flag"))
(sbBefore "turn"))
(bEq (sbAfter "flag") (sbAfter "turn"))
[<Test>]
let ``Remove Boolean afters in after-before relation`` () =
check
(bEq (BNot (sbBefore "flag")) (BNot (sbBefore "flag")))
(bEq (sbAfter "flag") (BNot (sbBefore "flag")))
[<Test>]
let ``Remove Boolean afters only if in the environment`` () =
check
(BAnd [ BNot (sbBefore "flag"); sbAfter "pole" ])
(BAnd [ sbAfter "flag"; sbAfter "pole" ] )
[<Test>]
let ``Remove afters of both types simultaneously`` () =
check
(BAnd
[ BGt (normalInt (IAdd [ siBefore "serving"; IInt 1L ] ), normalInt (siAfter "t"))
BOr [ BNot (sbBefore "flag"); sbAfter "pole" ]] )
(BAnd
[ BGt (normalInt (siAfter "serving"), normalInt (siAfter "t"))
BOr [ sbAfter "flag"; sbAfter "pole" ]] )
[<Test>]
let ``Remove arithmetic afters from a complex expression`` () =
check
(BNot
(BImplies
(BNot (sbBefore "flag"),
BGt (normalInt (IAdd [siBefore "serving"; IInt 1L]), normalInt (siAfter "t")))))
(BNot
(BImplies
(sbAfter "flag",
BGt (normalInt (siAfter "serving"), normalInt (siAfter "t")))))
/// Test cases for substituting afters in a func.
module AfterFuncs =
open Starling.Utils.Testing
open Starling.Core.Pretty
open Starling.Core.Traversal.Pretty
open Starling.Optimiser.Pretty
/// Test after-elimination of Booleans.
let check expected case : unit =
let trav =
tliftOverFunc
(tliftToExprSrc
(tliftToTypedSymVarSrc
(afterSubs OptimiserTests.AfterArithSubs OptimiserTests.AfterBoolSubs)))
let result = mapTraversal trav case
assertOkAndEqual expected result
(printTraversalError printTermOptError >> printUnstyled)
[<Test>]
let ``Substitute afters in a func with all-after params`` () =
check
{ Name = "foo"
Params = [ normalIntExpr (IAdd [siBefore "serving"; IInt 1L])
normalBoolExpr (BNot (sbBefore "flag")) ] }
{ Name = "foo"
Params = [ normalIntExpr (siAfter "serving")
normalBoolExpr (sbAfter "flag") ] }
/// <summary>
/// Test cases for deciding whether to allow graph optimisations.
/// </summary>
module GraphOptGuards =
open Starling.Core.Graph
/// A graph consisting of one no-operation cycle.
let nopCycle =
{ Name = "test"
Contents =
Map.ofList
[ ("x",
(Advisory (Multiset.empty),
Set.ofList
[ { Name = "xloop"; Dest = "x"; Command = [] } ],
Set.ofList
[ { Name = "xloop"; Src = "x"; Command = [] } ],
Normal)
) ] }
[<Test>]
let ``nopConnected cannot select the target node`` () =
Assert.False (nopConnected nopCycle "x" "x")
[<Test>]
let ``canCollapseUnobservable cannot collapse {p}nop{p}nop{p}`` () =
Assert.False (canCollapseUnobservable Map.empty nopCycle "x" [] "x" [] "x")