-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
225 lines (184 loc) · 5.58 KB
/
main_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
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
package main
import (
"reflect"
"testing"
)
func TestAdd(t *testing.T) {
c := &cnf{}
c.push(&clause{literals: []int{1, 2, 3, 0}})
if !reflect.DeepEqual(c.head.literals, []int{1, 2, 3, 0}) {
t.Error("Add Failure")
}
}
func TestDeepCopy(t *testing.T) {
c := &cnf{}
c.push(&clause{literals: []int{1, 2, 3, 0}})
newC := c.deepCopy()
if !(c.head != newC.head && reflect.DeepEqual(c.head.literals, newC.head.literals)) ||
!(c.tail != newC.tail && reflect.DeepEqual(c.tail.literals, newC.tail.literals)) {
t.Error("DeepCopy Failure")
}
}
func TestDeleteCommon(t *testing.T) {
c := &cnf{}
c.push(&clause{literals: []int{1, 2, 3, 0}})
c.push(&clause{literals: []int{2, 3, 4, 0}})
c.push(&clause{literals: []int{3, 4, 5, 0}})
c.deleteClause(c.head.next)
if !reflect.DeepEqual(c.head.literals, []int{1, 2, 3, 0}) ||
!reflect.DeepEqual(c.head.next.literals, []int{3, 4, 5, 0}) ||
!reflect.ValueOf(c.head.next.next).IsNil() {
t.Error("Delete Failure")
}
}
func TestDeleteHead(t *testing.T) {
c := &cnf{}
c.push(&clause{literals: []int{1, 2, 3, 0}})
c.push(&clause{literals: []int{2, 3, 4, 0}})
c.push(&clause{literals: []int{3, 4, 5, 0}})
c.deleteClause(c.head)
if !reflect.DeepEqual(c.head.literals, []int{2, 3, 4, 0}) ||
!reflect.DeepEqual(c.head.next.literals, []int{3, 4, 5, 0}) {
t.Error("Delete head Failure")
}
}
func TestDeleteTail(t *testing.T) {
c := &cnf{}
c.push(&clause{literals: []int{1, 2, 3, 0}})
c.push(&clause{literals: []int{2, 3, 4, 0}})
c.push(&clause{literals: []int{3, 4, 5, 0}})
c.deleteClause(c.tail)
if !reflect.DeepEqual(c.head.literals, []int{1, 2, 3, 0}) ||
!reflect.DeepEqual(c.head.next.literals, []int{2, 3, 4, 0}) {
t.Error("delete Tail Failure")
}
}
func TestSimplifyByUnitRule(t *testing.T) {
c := &cnf{}
c.push(&clause{literals: []int{1, 2, -3}})
c.push(&clause{literals: []int{1, -2}})
c.push(&clause{literals: []int{-1}})
c.push(&clause{literals: []int{2, 3}})
c.simplifyByOneLiteralRule()
if !reflect.DeepEqual(c.head.literals, []int{}) ||
!reflect.ValueOf(c.head.next).IsNil() {
t.Error("Unit Elimination Failure")
}
}
func TestSimplifyByPureRule(t *testing.T) {
c := &cnf{}
c.push(&clause{literals: []int{1, 2}})
c.push(&clause{literals: []int{-1, 2}})
c.push(&clause{literals: []int{3, 4}})
c.push(&clause{literals: []int{-3, -4}})
c.simplifyByPureLiteralRule()
if !reflect.DeepEqual(c.head.literals, []int{3, 4}) ||
!reflect.DeepEqual(c.head.next.literals, []int{-3, -4}) ||
!reflect.ValueOf(c.head.next.next).IsNil() {
t.Error("Pure Elimination Failure")
}
}
func TestParseLiterals(t *testing.T) {
raw := "1 2 3 4 0"
if literals, err := parseLiterals(raw); err != nil || !reflect.DeepEqual(literals, []int{1, 2, 3, 4}) {
t.Error("Parse Literals Failure")
}
}
func TestRemoveLiteral(t *testing.T) {
c := &cnf{}
c.push(&clause{literals: []int{1, 2}})
c.head.removeLiteral(1)
if !reflect.DeepEqual(c.head.literals, []int{1}) {
t.Error("Remove Literal Failure")
}
}
func TestFindIndex(t *testing.T) {
c := &cnf{}
c.push(&clause{literals: []int{1, 2}})
if i, found := c.head.findIndex(2); i != 1 || !found {
t.Error("Find Index Failure")
}
if _, found := c.head.findIndex(3); found {
t.Error("Find Index Failure")
}
}
func TestHasEmptyClause(t *testing.T) {
c := &cnf{}
if c.hasEmptyClause() {
t.Error("Has Empty Clause Failure")
}
c.push(&clause{literals: []int{}})
if !c.hasEmptyClause() {
t.Error("Has Empty Clause Failure")
}
}
func TestGetAtomicFormula(t *testing.T) {
c := &cnf{}
c.push(&clause{literals: []int{1, 2, 3, 4}})
c.push(&clause{literals: []int{1, 2, 4}})
c.push(&clause{literals: []int{2, 3}})
c.push(&clause{literals: []int{-2, 3}})
if c.getAtomicFormula() != 3 {
t.Error("Get Atomic Formula Failure")
}
}
func TestGetClausesMinLen(t *testing.T) {
c := &cnf{}
c.push(&clause{literals: []int{1, 2, 3, 4}})
c.push(&clause{literals: []int{1, 2, 4}})
c.push(&clause{literals: []int{2, 3}})
c.push(&clause{literals: []int{-2, 3}})
length := c.getClausesMinLen()
if length != 2 {
t.Error("Get Min Len Clauses Failure")
}
}
func TestGetMinLenClauses(t *testing.T) {
c := &cnf{}
c.push(&clause{literals: []int{1, 2, 3, 4}})
c.push(&clause{literals: []int{1, 2, 4}})
c.push(&clause{literals: []int{2, 3}})
c.push(&clause{literals: []int{-2, 3}})
nc := c.getMinLenClauses(c.getClausesMinLen())
if !reflect.DeepEqual(nc.head.literals, []int{2, 3}) ||
!reflect.DeepEqual(nc.head.next.literals, []int{-2, 3}) ||
!reflect.ValueOf(nc.head.next.next).IsNil() {
t.Error("Get Min Len Clauses Failure")
}
}
func TestFindCountMaxLiteral(t *testing.T) {
c := &cnf{}
c.push(&clause{literals: []int{1, 2, 3, 4}})
c.push(&clause{literals: []int{1, 2, 4}})
c.push(&clause{literals: []int{2, -3}})
c.push(&clause{literals: []int{2, 3}})
if findCountMaxLiteral(c.makeLiteralsMap()) != 2 {
t.Error("Find Count Max Literals Failure")
}
}
func TestGetPureLiterals(t *testing.T) {
c := &cnf{}
c.push(&clause{literals: []int{1, 2, 3}})
c.push(&clause{literals: []int{-1, 2}})
c.push(&clause{literals: []int{2, -3}})
c.push(&clause{literals: []int{2, 3}})
if !reflect.DeepEqual(c.getPureLiterals(c.makeLiteralsMap()), []int{2}) {
t.Error("Get Pure Literals Failure")
}
}
func TestMakeLiteralsMap(t *testing.T) {
c := &cnf{}
c.push(&clause{literals: []int{1, 2, 3}})
c.push(&clause{literals: []int{1, 2}})
c.push(&clause{literals: []int{2, -3}})
c.push(&clause{literals: []int{2, 3}})
m := c.makeLiteralsMap()
if m[1].negative != 0 &&
m[1].positive != 2 &&
m[2].negative != 0 &&
m[2].positive != 4 &&
m[3].negative != 1 &&
m[3].positive != 2 {
t.Error("Make Literals Map Failure")
}
}