-
Notifications
You must be signed in to change notification settings - Fork 4
/
GuardedViewTests.fs
265 lines (237 loc) · 10.4 KB
/
GuardedViewTests.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/// <summary>
/// Tests for guarded views.
/// </summary>
///
module Starling.Tests.GuardedView
open Chessie.ErrorHandling
open Starling.Utils
open Starling.Collections
open Starling.Core.GuardedView
open Starling.Core.GuardedView.Traversal
open Starling.Core.TypeSystem
open Starling.Core.Expr
open Starling.Core.Var
open Starling.Core.Traversal
open Starling.Core.View
open Starling.Core.View.Traversal
open Starling.Core.Symbolic
open Starling.Core.Model
module Tests =
open NUnit.Framework
open Starling.Utils.Testing
open Starling.Core.TypeSystem
/// <summary>
/// Test traversal for position-based substitution.
/// </summary>
let positionTestSub : Traversal<Expr<Var>, Expr<Var>, unit, unit> =
let ptsVar ctx tv =
let exp =
match typeOf tv with
| AnIntR ty ->
let v =
match ctx with
| Positions (Positive::xs) -> IInt 1L
| Positions (Negative::xs) -> IInt 0L
| _ -> IInt -1L
Expr.Int (ty, v)
| AnArrayR ty ->
let v =
match ctx with
| Positions (Positive::xs) -> AVar "pos"
| Positions (Negative::xs) -> AVar "neg"
| _ -> AVar "?"
Expr.Array (ty, v)
| ABoolR ty ->
let v =
match ctx with
| Positions (x::xs) -> Context.overapprox x
| _ -> BVar "?"
Expr.Bool (ty, v)
ok (ctx, exp)
tliftToExprSrc ptsVar
/// <summary>
/// Case studies for testing Term traversal.
/// </summary>
module TermTraversal =
open Starling.Utils.Testing
open Starling.Core.Pretty
open Starling.Core.Traversal.Pretty
/// <summary>
/// Tests term traversal on positional substitutions.
/// </summary>
let check
(expected : Term<BoolExpr<Var>, GView<Var>, Func<Expr<Var>>>)
(pos : TraversalContext<unit>)
(gv : Term<BoolExpr<Var>, GView<Var>, Func<Expr<Var>>>) : unit =
let trav = tliftOverTerm positionTestSub
let result = trav pos gv
assertOkAndEqual (pos, expected) result
(printTraversalError (fun _ -> String "?") >> printUnstyled)
[<Test>]
let ``successfully translate a positive Term`` () =
check
{ Cmd = BAnd [ bEq BFalse BFalse; bEq BFalse (BNot BTrue) ]
WPre =
Multiset.ofFlatList
[ gfunc BTrue "bar"
[ normalIntExpr (IInt 0L); normalBoolExpr BFalse ]
gfunc (BGt (normalInt (IInt 1L), normalInt (IInt 1L))) "barbaz"
[ normalIntExpr (IAdd [ IInt 0L; IInt 0L ]) ] ]
Goal =
(vfunc "bar" [ normalIntExpr (IInt 1L); normalBoolExpr BTrue ]) }
(Context.positive ())
{ Cmd =
BAnd
[ bEq (BVar "foo") (BVar "bar")
bEq (BVar "baz") (BNot (BVar "baz")) ]
WPre =
Multiset.ofFlatList
[ gfunc (BVar "foo") "bar"
[ normalIntExpr (IVar "baz")
normalBoolExpr (BVar "fizz") ]
gfunc (BGt (normalInt (IVar "foobar"), normalInt (IVar "barbar"))) "barbaz"
[ normalIntExpr
(IAdd [ IVar "foobaz"; IVar "bazbaz" ]) ] ]
Goal =
vfunc "bar"
[ normalIntExpr (IVar "baz")
normalBoolExpr (BVar "barbaz") ] }
[<Test>]
let ``Successfully translate a negative DTerm`` () =
check
{ Cmd = BAnd [ bEq BTrue BTrue; bEq BTrue (BNot BFalse) ]
WPre =
Multiset.ofFlatList
[ gfunc BFalse "bar"
[ normalIntExpr (IInt 1L); normalBoolExpr BTrue ]
gfunc (BGt (normalInt (IInt 0L), normalInt (IInt 0L))) "barbaz"
[ normalIntExpr (IAdd [ IInt 1L; IInt 1L ]) ] ]
Goal =
vfunc "bar" [ normalIntExpr (IInt 0L); normalBoolExpr BFalse ] }
(Context.negative ())
{ Cmd =
BAnd
[ bEq (BVar "foo") (BVar "bar")
bEq (BVar "baz") (BNot (BVar "baz")) ]
WPre =
Multiset.ofFlatList
[ gfunc (BVar "foo") "bar"
[ normalIntExpr (IVar "baz"); normalBoolExpr (BVar "fizz") ]
gfunc (BGt (normalInt (IVar "foobar"), normalInt (IVar "barbar"))) "barbaz"
[ normalIntExpr
(IAdd [ IVar "foobaz"; IVar "bazbaz" ]) ] ]
Goal =
vfunc "bar"
[ normalIntExpr (IVar "baz"); normalBoolExpr (BVar "barbaz") ] }
/// <summary>
/// Case studies for testing GFunc traversal.
/// </summary>
module GFuncTraversal =
open Starling.Utils.Testing
open Starling.Core.Pretty
open Starling.Core.Traversal.Pretty
/// <summary>
/// Tests GFunc traversal on positional substitutions.
/// </summary>
let check
(expected : GFunc<Var>)
(pos : TraversalContext<unit>)
(gv : GFunc<Var>) : unit =
let trav = tliftOverGFunc positionTestSub
let result = trav pos gv
assertOkAndEqual (pos, expected) result
(printTraversalError (fun _ -> String "?") >> printUnstyled)
[<Test>]
let ``GFunc substitution in +ve case works properly`` () =
check
(gfunc BFalse "bar" [ normalIntExpr (IInt 1L); normalBoolExpr BTrue ] )
(Context.positive ())
(gfunc (BVar "foo") "bar"
[ normalIntExpr (IVar "baz"); normalBoolExpr (BVar "fizz") ] )
[<Test>]
let ``GFunc substitution in -ve case works properly`` () =
check
(gfunc BTrue "bar" [ normalIntExpr (IInt 0L); normalBoolExpr BFalse ] )
(Context.negative ())
(gfunc (BVar "foo") "bar"
[ normalIntExpr (IVar "baz"); normalBoolExpr (BVar "fizz") ])
/// <summary>
/// Case studies for testing GView traversal.
/// </summary>
module GViewTraversal =
open Starling.Utils.Testing
open Starling.Core.Pretty
open Starling.Core.Traversal.Pretty
/// <summary>
/// Tests GView traversal on positional substitutions.
/// </summary>
let check
(expected : GView<Var>)
(pos : TraversalContext<unit>)
(gv : GView<Var>) : unit =
let trav = tchainM (tliftOverGFunc positionTestSub) id
let result = trav pos gv
assertOkAndEqual (pos, expected) result
(printTraversalError (fun _ -> String "?") >> printUnstyled)
[<Test>]
let ``+ve empty GView substitution is a no-op`` () =
check Multiset.empty (Context.positive ()) Multiset.empty
[<Test>]
let ``-ve empty GView substitution is a no-op`` () =
check Multiset.empty (Context.negative ()) Multiset.empty
[<Test>]
let ``Singleton GView substitution in +ve case works properly`` () =
check
(Multiset.singleton
(gfunc BFalse "bar"
[ normalIntExpr (IInt 1L)
normalBoolExpr BTrue ] ))
(Context.positive ())
(Multiset.singleton
(gfunc (BVar "foo") "bar"
[ normalIntExpr (IVar "baz")
normalBoolExpr (BVar "fizz") ] ))
[<Test>]
let ``Singleton GView substitution in -ve case works properly`` () =
check
(Multiset.singleton
(gfunc BTrue "bar"
[ normalIntExpr (IInt 0L); normalBoolExpr BFalse ] ))
(Context.negative ())
(Multiset.singleton
(gfunc (BVar "foo") "bar"
[ normalIntExpr (IVar "baz")
normalBoolExpr (BVar "fizz") ] ))
[<Test>]
let ``Multi GView substitution in +ve case works properly`` () =
check
(Multiset.ofFlatList
[ gfunc BFalse "bar"
[ normalIntExpr (IInt 1L)
normalBoolExpr BTrue ]
gfunc (BGt (normalInt (IInt 0L), normalInt (IInt 0L))) "barbaz"
[ normalIntExpr
(IAdd [ IInt 1L; IInt 1L ]) ] ] )
(Context.positive ())
(Multiset.ofFlatList
[ gfunc (BVar "foo") "bar"
[ normalIntExpr (IVar "baz")
normalBoolExpr (BVar "fizz") ]
gfunc (BGt (normalInt (IVar "foobar"), normalInt (IVar "barbar"))) "barbaz"
[ normalIntExpr
(IAdd [ IVar "foobaz"; IVar "bazbaz" ]) ] ] )
[<Test>]
let ``Multi GView substitution in -ve case works properly`` () =
check
(Multiset.ofFlatList
[ gfunc BTrue "bar"
[ normalIntExpr (IInt 0L); normalBoolExpr BFalse ]
gfunc (BGt (normalInt (IInt 1L), normalInt (IInt 1L))) "barbaz"
[ normalIntExpr (IAdd [ IInt 0L; IInt 0L ]) ] ] )
(Context.negative ())
(Multiset.ofFlatList
[ gfunc (BVar "foo") "bar"
[ normalIntExpr (IVar "baz"); normalBoolExpr (BVar "fizz") ]
gfunc (BGt (normalInt (IVar "foobar"), normalInt (IVar "barbar"))) "barbaz"
[ normalIntExpr
(IAdd [ IVar "foobaz"; IVar "bazbaz" ]) ] ] )