-
Notifications
You must be signed in to change notification settings - Fork 4
/
SymbolicTests.fs
243 lines (211 loc) · 7.99 KB
/
SymbolicTests.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
/// <summary>
/// Tests for <c>Symbolic</c>.
/// </summary>
module Starling.Tests.Core.Symbolic
open NUnit.Framework
open Starling.Collections
open Starling.Utils.Testing
open Starling.Core.TypeSystem
open Starling.Core.Var
open Starling.Core.Expr
open Starling.Core.Traversal
open Starling.Core.Symbolic
open Starling.Core.Symbolic.Traversal
open Starling.Core.Pretty
open Starling.Core.Traversal.Pretty
open Starling.Core.Symbolic.Pretty
/// <summary>
/// Unit tests for symbolic post-state rewriting.
/// </summary>
module PostStateRewrite =
let checkInt (expected : IntExpr<Sym<MarkedVar>>) (expr : IntExpr<Sym<Var>>)
: unit =
let trav = tliftToExprDest (traverseTypedSymWithMarker After)
let res = mapTraversal (tliftToIntSrc trav) (normalInt expr)
assertOkAndEqual expected res
(printTraversalError (fun () -> String "?" ) >> printUnstyled)
[<Test>]
let ``Rewrite single variable to post-state`` () =
checkInt (siAfter "target1") (siVar "target1")
[<Test>]
let ``Rewrite expression with one variable to post-state`` () =
checkInt
(IAdd [IInt 4L; siAfter "target1"])
(IAdd [IInt 4L; siVar "target1"])
[<Test>]
let ``Rewrite expression with two variables to post-state`` () =
checkInt
(ISub [siAfter "target1"; siAfter "target2"])
(ISub [siVar "target1"; siVar "target2"])
[<Test>]
let ``Rewrite expression with no variables to post-state`` () =
checkInt (IDiv (IInt 6L, IInt 0L)) (IDiv (IInt 6L, IInt 0L))
/// <summary>
/// Test cases for testing underapproximation of Booleans.
/// </summary>
module BoolApprox =
let check
(expected : BoolExpr<Sym<MarkedVar>>)
(ctx : TraversalContext<unit>)
(expr : BoolExpr<Sym<MarkedVar>>)
: unit =
let result = tliftToBoolSrc approx ctx (normalBool expr)
assertOkAndEqual (ctx, expected) result
(printTraversalError (fun () -> String "?" ) >> printUnstyled)
[<Test>]
let ``Don't alter +ve symbol-less expression`` () =
check
(BAnd
[ bEq (sbBefore "foo") (sbAfter "bar")
BGt (normalInt (siBefore "baz"), normalInt (IInt 1L)) ] )
(Context.positive ())
(BAnd
[ bEq (sbBefore "foo") (sbAfter "bar")
BGt (normalInt (siBefore "baz"), normalInt (IInt 1L)) ] )
[<Test>]
let ``Rewrite +ve param-less Bool symbol to false`` () =
check
BFalse
(Context.positive ())
(BVar (Sym [ SymString "test" ] ))
[<Test>]
let ``Rewrite -ve param-less Bool symbol to true`` () =
check
BTrue
(Context.negative ())
(BVar (Sym [ SymString "test" ] ))
[<Test>]
let ``Rewrite +ve Reg-params Bool symbol to false`` () =
check
BFalse
(Context.positive ())
(BVar
(Sym
[ SymString "test"
SymArg (normalIntExpr (siBefore "foo"))
SymArg (normalBoolExpr (sbAfter "bar")) ] ))
[<Test>]
let ``Rewrite -ve Reg-params Bool symbol to true`` () =
check
BTrue
(Context.negative ())
(BVar
(Sym
[ SymString "test"
SymArg (normalIntExpr (siBefore "foo"))
SymArg (normalBoolExpr (sbAfter "bar")) ] ))
[<Test>]
let ``Rewrite +ve implication correctly`` () =
check
(BImplies (BTrue, BFalse))
(Context.positive ())
(BImplies
(BVar
(Sym
[ SymString "test1"
SymArg (normalIntExpr (siBefore "foo"))
SymArg (normalBoolExpr (sbAfter "bar")) ] ),
BVar
(Sym
[ SymString "test2"
SymArg (normalIntExpr (siBefore "baz"))
SymArg (normalBoolExpr (sbAfter "barbaz")) ] )))
[<Test>]
let ``Rewrite -ve implication correctly`` () =
check
(BImplies (BFalse, BTrue))
(Context.negative ())
(BImplies
(BVar
(Sym
[ SymString "test1"
SymArg (normalIntExpr (siBefore "foo"))
SymArg (normalBoolExpr (sbAfter "bar")) ] ),
BVar
(Sym
[ SymString "test2"
SymArg (normalIntExpr (siBefore "baz"))
SymArg (normalBoolExpr (sbAfter "barbaz")) ] )))
/// <summary>
/// Test cases for finding variables in expressions.
/// </summary>
module FindSMVarsCases =
/// <summary>
/// Tests finding variables in symbolic expressions.
/// </summary>
let check
(expected : CTyped<MarkedVar> list)
(expr : Expr<Sym<MarkedVar>>)
: unit =
let result = findVars (tliftOverExpr collectSymVars) expr
assertOkAndEqual (Set.ofList expected) result
(printTraversalError (fun () -> String "?" ) >> printUnstyled)
[<Test>]
let ``Finding vars in a Boolean primitive returns empty`` () =
check [] (normalBoolExpr BTrue)
[<Test>]
let ``Finding vars in an integer primitive returns empty`` () =
check [] (normalIntExpr (IInt 1L))
[<Test>]
let ``Finding vars in a Boolean var returns that var`` () =
check [ normalBoolVar (Before "foo") ] (normalBoolExpr (sbBefore "foo"))
[<Test>]
let ``Finding vars in an integer var returns that var`` () =
check [ normalIntVar (After "bar") ] (normalIntExpr (siAfter "bar"))
[<Test>]
let ``Finding vars in a Boolean expression works correctly`` () =
check
[ normalBoolVar (Before "foo")
normalBoolVar (After "baz")
normalIntVar (Before "foobar")
normalIntVar (After "barbaz") ]
(normalBoolExpr
(BAnd
[ BOr [ sbBefore "foo"; sbAfter "baz" ]
BGt ( normalInt (siBefore "foobar"), normalInt (siAfter "barbaz")) ] ))
[<Test>]
let ``Finding vars in an integer expression works correctly`` () =
check
[ normalIntVar (Before "foo")
normalIntVar (After "bar")
normalIntVar (Before "foobar")
normalIntVar (After "barbaz") ]
(normalIntExpr
(IAdd
[ ISub [ siBefore "foo"; siAfter "bar" ]
IMul [ siBefore "foobar"; siAfter "barbaz" ]] ))
[<Test>]
let ``Finding vars in an Boolean symbol works correctly`` () =
check
[ normalIntVar (Before "bar")
normalBoolVar (After "baz") ]
(normalBoolExpr
(BVar
(Sym [ SymString "foo"
SymArg (normalIntExpr (siBefore "bar"))
SymArg (normalBoolExpr (sbAfter "baz")) ] )))
[<Test>]
let ``Finding vars in an integer symbol works correctly`` () =
check
[ normalIntVar (Before "bar"); normalBoolVar (After "baz") ]
(normalIntExpr
(IVar
(Sym
[ SymString "foo"
SymArg (normalIntExpr (siBefore "bar"))
SymArg (normalBoolExpr (sbAfter "baz")) ] )))
/// <summary>
/// Tests on the symbolic pretty printer.
/// </summary>
module Pretty =
open Starling.Core.Symbolic.Pretty
[<Test>]
let ``Pretty-printing a valid Sym interpolates variables properly`` () =
let sentence =
[ SymString "foo("
SymArg (normalIntExpr (siVar "bar"))
SymString ", "
SymArg (normalBoolExpr (sbVar "baz"))
SymString ")" ]
"(sym 'foo([|bar|], [|baz|])')"
?=? printUnstyled (printSym String (Sym sentence))