forked from mitchellh/go-z3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_test.go
88 lines (69 loc) · 1.5 KB
/
model_test.go
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
package z3
import (
"testing"
)
func TestModelAssignments(t *testing.T) {
config := NewConfig()
defer config.Close()
ctx := NewContext(config)
defer ctx.Close()
// Create a symbol
x := ctx.Const(ctx.Symbol("x"), ctx.IntSort())
// x + 4 = 16
ast := x.Add(ctx.Int(4, ctx.IntSort())).
Eq(ctx.Int(16, ctx.IntSort()))
t.Logf("\nAST:\n%s", ast.String())
// Create the solver
s := ctx.NewSolver()
defer s.Close()
// Assert constraints
s.Assert(ast)
// Solve
result := s.Check()
if result != True {
t.Fatalf("bad: %v", result)
}
// Get the model
m := s.Model()
defer m.Close()
t.Logf("\nModel:\n%s", m.String())
// Get the exact value
am := m.Assignments()
assign := am["x"]
t.Logf("Assignment: %s", assign)
if assign.Int() != 12 {
t.Fatalf("bad: %s", assign)
}
}
func TestModelEval(t *testing.T) {
config := NewConfig()
defer config.Close()
ctx := NewContext(config)
defer ctx.Close()
// Create a symbol
x := ctx.Const(ctx.Symbol("x"), ctx.IntSort())
// x + 4 = 16
ast := x.Add(ctx.Int(4, ctx.IntSort())).
Eq(ctx.Int(16, ctx.IntSort()))
t.Logf("\nAST:\n%s", ast.String())
// Create the solver
s := ctx.NewSolver()
defer s.Close()
// Assert constraints
s.Assert(ast)
// Solve
result := s.Check()
if result != True {
t.Fatalf("bad: %v", result)
}
// Get the model
m := s.Model()
defer m.Close()
t.Logf("\nModel:\n%s", m.String())
// Get the exact value
assign := m.Eval(x)
t.Logf("Assignment: %s", assign)
if assign.Int() != 12 {
t.Fatalf("bad: %s", assign)
}
}