-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
161 lines (136 loc) · 3.76 KB
/
test.js
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
'use strict'
const test = require('tape')
const solve = require('.')
const encodeInt32Array = require('./lib/encode')
const {
encodeStrings, encodeIntegers,
solveWithStrings, solveUnsafe,
SATISFIABLE, UNSATISFIABLE
} = solve
const formula1 = [
['!A', 'B', 'C'],
['!B', 'C'],
['!C', 'D'],
['!D', 'A', 'B', 'C']
]
const assumptions1 = ['A', '!C']
const formula2 = [
['A', 'B'],
['!A', 'B']
]
const assumptions2 = ['!A', 'B']
test('encodeInt32Array works', (t) => {
const buf = encodeInt32Array([
1,
-1,
1000,
-1000
])
t.ok(Buffer.isBuffer(buf))
const expectedBuf = Buffer.from([
0x01, 0x00, 0x00, 0x00,
0xff, 0xff, 0xff, 0xff,
0xe8, 0x03, 0x00, 0x00,
0x18, 0xfc, 0xff, 0xff
])
t.equal(buf.toString('hex'), expectedBuf.toString('hex'))
t.end()
})
test('encodeStrings works', (t) => {
const [encFormula, encAssumptions] = encodeStrings(formula1, assumptions1)
const expectedFormula = encodeInt32Array([
-1, 2, 3, 0,
-2, 3, 0,
-3, 4, 0,
-4, 1, 2, 3, 0
])
t.ok(Buffer.isBuffer(encFormula))
t.equal(encFormula.toString('hex'), expectedFormula.toString('hex'))
const expectedAssumptions = encodeInt32Array([1, -3])
t.ok(Buffer.isBuffer(encAssumptions))
t.equal(encAssumptions.toString('hex'), expectedAssumptions.toString('hex'))
t.end()
})
test('encodeIntegers works', (t) => {
const [encFormula, encAssumptions] = encodeIntegers([
[ 1, 2], // A, B
[-1, 2] // !A, B
], [
-1 // !A
])
const expectedFormula = encodeInt32Array([
1, 2, 0,
-1, 2, 0
])
t.ok(Buffer.isBuffer(encFormula))
t.equal(encFormula.toString('hex'), expectedFormula.toString('hex'))
const expectedAssumptions = encodeInt32Array([-1])
t.ok(Buffer.isBuffer(encAssumptions))
t.equal(encAssumptions.toString('hex'), expectedAssumptions.toString('hex'))
t.end()
})
test('solve works', (t) => {
// (¬A ∨ B)∧(¬B ∨ C)∧(¬C ∨ A)
// A = 1, B = 2, C = 3
const {statusCode, satisfiable} = solve([
[-1, 2],
[-2, 3],
[-3, 1]
], [
1
])
t.equal(statusCode, SATISFIABLE)
t.equal(satisfiable, true)
t.end()
})
test('solve works with >255 variables', (t) => {
// We check if it supports more than 255 variables in order to assert
// that it does not write Int8 values.
const largeFomula = []
const expectedSolution = []
for (let i = 1, j = 0; i < 1000; i++) {
const v = i * (i % 2 === 0 ? -1 : 1) // 1, -2, 3, -4, …
largeFomula.push([v])
expectedSolution.push(v)
}
const {satisfiable, solution} = solve(largeFomula)
t.equal(satisfiable, true)
t.deepEqual(solution, expectedSolution)
t.end()
})
test('solveUnsafe works', (t) => {
// (¬A ∨ B)∧(¬B ∨ C)∧(¬C ∨ A)
// A = 1, B = 2, C = 3
const encFormula = encodeInt32Array([
-1, 2, 0,
-2, 3, 0,
-3, 1, 0
])
const encAssumptions = encodeInt32Array([])
const solution = solveUnsafe(encFormula, encAssumptions)
t.equal(solution[0], 10) // assert if satisfiable
t.end()
})
test('solveWithStrings solves a simple problem', (t) => {
const {statusCode, satisfiable} = solveWithStrings(formula2)
t.equal(statusCode, SATISFIABLE)
t.equal(satisfiable, true)
t.end()
})
test('solveWithStrings supports assumptions', (t) => {
const {solution, statusCode, satisfiable} = solveWithStrings(formula2, assumptions2)
t.equal(solution.length, 2)
t.equal(solution[0], -1)
t.equal(solution[1], 2)
t.equal(statusCode, SATISFIABLE)
t.equal(satisfiable, true)
t.end()
})
test('solveWithStrings correctly proves unsatisfiability', (t) => {
const formula = [['A'], ['!A']]
const assumptions = []
const {statusCode, satisfiable} = solveWithStrings(formula, assumptions)
t.equal(statusCode, UNSATISFIABLE)
t.equal(satisfiable, false)
t.end()
})