Repository navigation
Expand file tree
/
Copy pathcard_test.go
More file actions
96 lines (91 loc) · 1.96 KB
/
Copy pathcard_test.go
File metadata and controls
96 lines (91 loc) · 1.96 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
// Copyright 2018 The Gini Authors. All rights reserved. Use of this source
// code is governed by a license that can be found in the License file.
package logic
import (
"math/rand"
"testing"
"github.com/irifrance/gini"
"github.com/irifrance/gini/z"
)
func TestCardSort(t *testing.T) {
N := 64
Iters := 16
c := NewC()
ms := make([]z.Lit, N)
for i := range ms {
ms[i] = c.Lit()
}
cs := c.CardSort(ms)
sv := gini.New()
c.ToCnf(sv)
for i := 0; i < Iters; i++ {
rand.Shuffle(len(ms), func(i, j int) { ms[i], ms[j] = ms[j], ms[i] })
n := rand.Intn(N)
npos := 0
nneg := 0
for j := 0; j < n; j++ {
m := ms[j]
if rand.Intn(2) == 1 {
m = m.Not()
nneg++
} else {
npos++
}
sv.Assume(m)
}
//t.Logf("assumed %d pos %d neg out of %d\n", npos, nneg, N)
sv.Test(nil) // puts previous assumptions in different scope
for i := 0; i <= npos; i++ {
sv.Assume(cs.Geq(i))
res := sv.Solve()
if res != 1 {
t.Errorf("assumed %d geq %d gave %d\n", npos, i, res)
}
sv.Assume(cs.Less(i))
res = sv.Solve()
if res != -1 {
t.Errorf("assumed %d less %d gave %d\n", npos, i, res)
}
}
for i := 0; i <= nneg; i++ {
sv.Assume(cs.Leq(N - i))
res := sv.Solve()
if res != 1 {
t.Errorf("assume %d false leq %d gave %d\n", nneg, N-i, res)
}
sv.Assume(cs.Gr(N - i))
res = sv.Solve()
if res != -1 {
t.Errorf("assume %d false gr %d gave %d\n", nneg, N-i, res)
}
}
sv.Untest()
}
}
func TestCardNotPowerOfTwo(t *testing.T) {
c := NewC()
s := gini.New()
ms := make([]z.Lit, 11)
for i := range ms {
ms[i] = c.Lit()
}
cs := c.CardSort(ms)
c.ToCnf(s)
for i := 0; i < 11; i++ {
for j := 0; j < i; j++ {
s.Assume(ms[j])
}
s.Test(nil)
for j := 0; j < i; j++ {
s.Assume(cs.Geq(j))
if s.Solve() != 1 {
t.Errorf("geq %d gave unsat after %d assumes\n", j, i)
}
s.Assume(cs.Less(j))
if s.Solve() != -1 {
t.Errorf("less %d gave sat after %d assumes\n", j, i)
}
}
s.Untest()
}
}