forked from etnz/boolgebra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quine-mccluskey_test.go
109 lines (94 loc) · 2.5 KB
/
quine-mccluskey_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
package boolgebra
import "testing"
//TestReduce check that we get the prime correctly using the wikipedia examples
func TestReduce(t *testing.T) {
/*
Number
of 1s Minterm Binary
Representation
1 m4 0100
m8 1000
2 (m9) 1001
m10 1010
m12 1100
3 m11 1011
(m14) 1110
4 m15 1111
*/
m4 := m("a'bc'd'")
m8 := m("ab'c'd'")
m9 := m("ab'c'd")
m10 := m("ab'cd'")
m12 := m("abc'd'")
m11 := m("ab'cd")
m14 := m("abcd'")
m15 := m("abcd")
x := expression{m4, m8, m9, m10, m12, m11, m14, m15}
//primes are
//m4_12 := m("bc'd'")
//m8_9_10_11 := m("ab'")
//m8_10_12_14 := m("ad'")
//m10_11_14_15 := m("ac")
y := reduce(x)
t.Logf("reduced = %v", y)
}
//newminterm creates a new minterm using ' at the end of the string to find out that its a neg
func m(x string) minterm {
res := make(minterm)
for i, id := range x {
nextisquote := i+1 < len(x) && x[i+1] == '\''
if id != '\'' {
res[string(id)] = !nextisquote
}
}
return res
}
// TestPosLen make sure that we count the number of true correctly
func TestPositives(t *testing.T) {
x := minterm{"A": true, "B": true, "C": false, "E": true}
if positives(x) != 3 {
t.Errorf("invalid minterm %v PosLen attribute got %v want 3", x, positives(x))
}
}
// TestMinterm_combine gold test some minterm combinations
func TestCombine(t *testing.T) {
x := minterm{"A": true, "B": true, "C": false, "E": true}
var y, r, c minterm
var ok bool
// 1,0 -> _
y = minterm{"A": true, "B": true, "C": false, "E": false}
r = minterm{"A": true, "B": true, "C": false}
c, ok = combine(x, y)
if !ok {
t.Errorf("failed to combine(%v,%v)", x, y)
} else if !equals(c, r) {
t.Errorf("combine(%v,%v) got %v expected %v", x, y, c, r)
}
// 1,_ -> _
y = minterm{"A": true, "B": true, "C": false}
r = minterm{"A": true, "B": true, "C": false}
c, ok = combine(x, y)
if !ok {
t.Errorf("failed to combine(%v,%v)", x, y)
} else if !equals(c, r) {
t.Errorf("combine(%v,%v) got %v expected %v", x, y, c, r)
}
// 0,1 -> _
y = minterm{"A": true, "B": true, "C": true, "E": true}
r = minterm{"A": true, "B": true, "E": true}
c, ok = combine(x, y)
if !ok {
t.Errorf("failed to combine(%v,%v)", x, y)
} else if !equals(c, r) {
t.Errorf("combine(%v,%v) got %v expected %v", x, y, c, r)
}
// 0,_ -> _
y = minterm{"A": true, "B": true, "E": true}
r = minterm{"A": true, "B": true, "E": true}
c, ok = combine(x, y)
if !ok {
t.Errorf("failed to combine(%v,%v)", x, y)
} else if !equals(c, r) {
t.Errorf("combine(%v,%v) got %v expected %v", x, y, c, r)
}
}