Skip to content

Commit

Permalink
Merge pull request #188 from petertseng/react-tests
Browse files Browse the repository at this point in the history
React tests
  • Loading branch information
soniakeys committed Oct 28, 2015
2 parents 5b868c5 + c7616dc commit 313e326
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 23 deletions.
2 changes: 1 addition & 1 deletion react/example.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package react

const TestVersion = 1
const TestVersion = 2

type reactor struct {
cells []*cell
Expand Down
96 changes: 74 additions & 22 deletions react/react_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import "testing"
// Also define an exported TestVersion with a value that matches
// the internal testVersion here.

const testVersion = 1
const testVersion = 2

// Retired:
// 1 afa5c1278857457403a30479663d26d4e1c8c496

// This is a compile time check to see if you've properly implemented New().
var _ Reactor = New()
Expand All @@ -21,47 +24,96 @@ func TestTestVersion(t *testing.T) {
}
}

func assertCellValue(t *testing.T, c Cell, expected int, explanation string) {
observed := c.Value()
if observed != expected {
t.Fatalf("%s: expected %d, got %d", explanation, expected, observed)
}
}

// Setting the value of an input cell changes the observable Value()
func TestSetInput(t *testing.T) {
r := New()
i := r.CreateInput(1)
if i.Value() != 1 {
t.Fatalf("i.Value() doesn't match initial value")
}
assertCellValue(t, i, 1, "i.Value() doesn't match initial value")
i.SetValue(2)
if i.Value() != 2 {
t.Fatalf("i.Value() doesn't match changed value")
}
assertCellValue(t, i, 2, "i.Value() doesn't match changed value")
}

// The value of a compute cell is determined by the value of the dependencies.
func TestBasicCompute(t *testing.T) {
// The value of a compute 1 cell is determined by the value of the dependencies.
func TestBasicCompute1(t *testing.T) {
r := New()
i := r.CreateInput(1)
c := r.CreateCompute1(i, func(v int) int { return v + 1 })
if c.Value() != 2 {
t.Fatalf("c.Value() isn't properly computed based on initial input cell value")
}
assertCellValue(t, c, 2, "c.Value() isn't properly computed based on initial input cell value")
i.SetValue(2)
if c.Value() != 3 {
t.Fatalf("c.Value() isn't properly computed based on changed input cell value")
}
assertCellValue(t, c, 3, "c.Value() isn't properly computed based on changed input cell value")
}

// The value of a compute 2 cell is determined by the value of the dependencies.
func TestBasicCompute2(t *testing.T) {
r := New()
i1 := r.CreateInput(1)
i2 := r.CreateInput(2)
c := r.CreateCompute2(i1, i2, func(v1, v2 int) int { return v1 | v2 })
assertCellValue(t, c, 3, "c.Value() isn't properly computed based on initial input cell values")
i1.SetValue(4)
assertCellValue(t, c, 6, "c.Value() isn't properly computed when first input cell value changes")
i2.SetValue(8)
assertCellValue(t, c, 12, "c.Value() isn't properly computed when second input cell value changes")
}

// Compute cells can depend on other compute cells.
func TestTopology(t *testing.T) {
// Compute 2 cells can depend on compute 1 cells.
func TestCompute2Diamond(t *testing.T) {
r := New()
i := r.CreateInput(1)
c1 := r.CreateCompute1(i, func(v int) int { return v + 1 })
c2 := r.CreateCompute1(i, func(v int) int { return v - 1 })
c3 := r.CreateCompute2(c1, c2, func(v1, v2 int) int { return v1 * v2 })
if c3.Value() != 0 {
t.Fatalf("c3.Value() isn't properly computed based on initial input cell value")
}
assertCellValue(t, c3, 0, "c3.Value() isn't properly computed based on initial input cell value")
i.SetValue(3)
if c3.Value() != 8 {
t.Fatalf("c3.Value() isn't properly computed based on changed input cell value")
assertCellValue(t, c3, 8, "c3.Value() isn't properly computed based on changed input cell value")
}

// Compute 1 cells can depend on other compute 1 cells.
func TestCompute1Chain(t *testing.T) {
r := New()
inp := r.CreateInput(1)
var c Cell = inp
for i := 2; i <= 8; i++ {
// must save current value of loop variable i for correct behavior.
// compute function has to use digitToAdd not i.
digitToAdd := i
c = r.CreateCompute1(c, func(v int) int { return v*10 + digitToAdd })
}
assertCellValue(t, c, 12345678, "c.Value() isn't properly computed based on initial input cell value")
inp.SetValue(9)
assertCellValue(t, c, 92345678, "c.Value() isn't properly computed based on changed input cell value")
}

// Compute 2 cells can depend on other compute 2 cells.
func TestCompute2Tree(t *testing.T) {
r := New()
ins := make([]InputCell, 3)
for i, v := range []int{1, 10, 100} {
ins[i] = r.CreateInput(v)
}

add := func(v1, v2 int) int { return v1 + v2 }

firstLevel := make([]ComputeCell, 2)
for i := 0; i < 2; i++ {
firstLevel[i] = r.CreateCompute2(ins[i], ins[i+1], add)
}

output := r.CreateCompute2(firstLevel[0], firstLevel[1], add)
assertCellValue(t, output, 121, "output.Value() isn't properly computed based on initial input cell values")

for i := 0; i < 3; i++ {
ins[i].SetValue(ins[i].Value() * 2)
}

assertCellValue(t, output, 242, "output.Value() isn't properly computed based on changed input cell values")
}

// Compute cells can have callbacks.
Expand Down

0 comments on commit 313e326

Please sign in to comment.