Commit 5cc707fb authored by Anders Jensen Løvig's avatar Anders Jensen Løvig
Browse files

Some circuit

parent fbc0ec4b
package circuit
type Wire = int
type WireValue = bool
type Circuit struct {
wires []WireValue
}
// // The function that evaluates this gate.
// fun gateFun
// // The input wire indices
// inputs []int
// // The output wire index
// output int
// }
// type Builder struct {
// numInputs int
// gates []Gate
// }
// func NewBuilder(numInputs int) *Builder {
// return &Builder{
// numInputs: numInputs,
// gates: make([]*gate, 0),
// }
// }
// func (b *Builder) AddNotGate(in int) (out int) {
// b.AddGate()
// }
// func (b *Builder) allocWires(count int) (wires []int) {
// }
// func btoi(b bool) (i int) {
// if b {
// i = 1
// }
// return
// }
// func itob(i int) (b bool) {
// if i != 0 {
// b = true
// }
// return
// }
package circuit
import "testing"
func TestBuilderInputs(t *testing.T) {
// b := NewBuilder()
// A_in := b.AddInputs(3)
// if len(A_in) != 3 {
// t.Errorf("Expected length of A_in to be 3 (was %d)", len(A_in))
// }
// for i := 0; i < len(A_in); i++ {
// if A_in[i] != i {
// t.Errorf("Expected A_in[%d] == %d (was %d)", i, i, A_in[i])
// }
// if b.wires[A_in[i]] != i {
// t.Errorf("Expected b.wires[%d] == %d (was %d)", A_in[i], i, b.wires[A_in[i]])
// }
// }
// B_in := b.AddInputs(1)
// if len(B_in) != 1 {
// t.Errorf("Expected length of B_in to be 1 (was %d)", len(B_in))
// }
// for i := 0; i < len(B_in); i++ {
// if B_in[i] != i+3 {
// t.Errorf("Expected B_in[%d] == %d (was %d)", i, i+3, B_in[i])
// }
// }
}
package circuit
type Gate interface {
Evalaute(c *Circuit)
}
//////////////// NOT ////////////////
type NotGate struct {
in Wire
out Wire
}
func (g *NotGate) Evaluate(c *Circuit) {
c.wires[g.out] = !c.wires[g.in]
}
//////////////// AND ////////////////
type AndGate struct {
in []Wire
out Wire
}
func (g *AndGate) Evaluate(c *Circuit) {
r := true
for w := range g.in {
r = r && c.wires[w]
}
c.wires[g.out] = r
}
//////////////// OR /////////////////
type OrGate struct {
in []Wire
out Wire
}
func (g *OrGate) Evaluate(c *Circuit) {
r := false
for w := range g.in {
r = r || c.wires[w]
}
c.wires[g.out] = r
}
package circuit
import "testing"
func TestNotGate(t *testing.T) {
c := &Circuit{wires: make([]bool, 2)}
g := &NotGate{in: 0, out: 1}
g.Evaluate(c)
if c.wires[1] != true {
t.Errorf("Expected NOT gate: (0) -> (1)")
}
c.wires[0] = true
g.Evaluate(c)
if c.wires[1] != false {
t.Errorf("Expected NOT gate: (1) -> (0)")
}
}
func TestAndGate(t *testing.T) {
c := &Circuit{wires: make([]bool, 3)}
g := &AndGate{in: []Wire{0, 1}, out: 2}
g.Evaluate(c)
if c.wires[2] != false {
t.Errorf("Expected AND gate: (0, 0) -> (0)")
}
c.wires[0] = true
g.Evaluate(c)
if c.wires[2] != false {
t.Errorf("Expected AND gate: (1, 0) -> (0)")
}
c.wires[1] = true
g.Evaluate(c)
if c.wires[2] != true {
t.Errorf("Expected AND gate: (1, 1) -> (1)")
}
c.wires[0] = false
g.Evaluate(c)
if c.wires[2] != false {
t.Errorf("Expected AND gate: (0, 1) -> (0)")
}
c.wires = []bool{true, true, false, false}
g = &AndGate{in: []Wire{0, 1, 2}, out: 3}
g.Evaluate(c)
if c.wires[3] != false {
t.Errorf("Expected AND gate: (1, 1, 0) -> (0)")
}
c.wires[2] = true
g.Evaluate(c)
if c.wires[3] != true {
t.Errorf("Expected AND gate: (1, 1, 1) -> (1)")
}
}
func TestOrGate(t *testing.T) {
c := &Circuit{wires: make([]bool, 3)}
g := &OrGate{in: []Wire{0, 1}, out: 2}
g.Evaluate(c)
if c.wires[2] != false {
t.Errorf("Expected OR gate: (0, 0) -> (0)")
}
c.wires[0] = true
g.Evaluate(c)
if c.wires[2] != true {
t.Errorf("Expected OR gate: (1, 0) -> (1)")
}
c.wires[1] = true
g.Evaluate(c)
if c.wires[2] != true {
t.Errorf("Expected OR gate: (1, 1) -> (1)")
}
c.wires[0] = false
g.Evaluate(c)
if c.wires[2] != true {
t.Errorf("Expected OR gate: (0, 1) -> (1)")
}
c.wires = []bool{false, false, true, false}
g = &OrGate{in: []Wire{0, 1, 2}, out: 3}
g.Evaluate(c)
if c.wires[3] != true {
t.Errorf("Expected OR gate: (0, 0, 1) -> (1)")
}
c.wires[2] = false
g.Evaluate(c)
if c.wires[3] != false {
t.Errorf("Expected OR gate: (0, 0, 0) -> (0)")
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment