Loading concurrency/concurrency1.go +26 −7 Original line number Diff line number Diff line package concurrency // Sequence ... // Sequence is a channel for int streams. type Sequence chan int // SequenceOf returns a stream of numbers of the supplied value. func SequenceOf(v int) Sequence { i := make(Sequence) s := make(Sequence) go func(x int, r Sequence) { go func(x int) { defer close(s) for { r <- x s <- x } }(v, i) }(v) return i return s } // Take ... // Take a limited number of items from a producer. func (s Sequence) Take(c int) []int { r := []int{} i := 0 for x := range s { if !(i < c) { break } r = append(r, x) i++ } return r } // Equal checks two int-slices for equality. func Equal(a, b []int) bool { if len(a) != len(b) { return false } for i, v := range a { if v != b[i] { return false } } return true } concurrency/concurrency1_test.go +25 −18 Original line number Diff line number Diff line package concurrency import ( "reflect" "sync" "testing" ) //func TestAnything(t *testing.T) { //} func TestBasic(t *testing.T) { done := make(chan bool) go func(done chan bool) { Loading @@ -32,11 +28,6 @@ func TestSequenceVeryPrimitive(t *testing.T) { } func TestSequencePrimitive(t *testing.T) { // ****************************** //t.Skip("test is broken") // ****************************** t.Log("TestSequencePrimitive() started.") defer t.Log("TestSequencePrimitive() done.") seq := make(chan int) Loading @@ -57,15 +48,31 @@ func TestSequencePrimitive(t *testing.T) { } } func TestSequenceFunctions(t *testing.T) { // ****************************** //t.Skip("test is broken") // ****************************** l0 := []int{1, 1, 1, 1, 1} if r := SequenceOf(1).Take(5); reflect.DeepEqual(r, l0) { t.Errorf("SequenceOf(1).Take(5) is %v, expected %v", r, l0) func TestSliceEquality(t *testing.T) { for _, d := range []struct { a, b []int r bool }{ {a: []int{}, b: []int{}, r: true}, {a: []int{}, b: []int{1}, r: false}, {a: []int{1}, b: []int{}, r: false}, {a: []int{1}, b: []int{1}, r: true}, {a: []int{1, 2}, b: []int{1, 2}, r: true}, {a: []int{1, 2, 3}, b: []int{1, 2, 3}, r: true}, {a: []int{1, 2, 3}, b: []int{1, 2}, r: false}, {a: []int{1, 2}, b: []int{1, 2, 3}, r: false}, {a: []int{1, 2, 3}, b: []int{1, 2, 4}, r: false}, {a: []int{1, 2, 4}, b: []int{1, 2, 3}, r: false}, } { if r := Equal(d.a, d.b); r != d.r { t.Errorf("Equal(%#v, %#v) is %t, expected %t", d.a, d.b, r, d.r) } } } func TestSequenceFunctions(t *testing.T) { l := []int{1, 1, 1, 1, 1} if r := SequenceOf(1).Take(5); !Equal(r, l) { t.Errorf("SequenceOf(1).Take(5) is %#v, expected %#v", r, l) } } Loading
concurrency/concurrency1.go +26 −7 Original line number Diff line number Diff line package concurrency // Sequence ... // Sequence is a channel for int streams. type Sequence chan int // SequenceOf returns a stream of numbers of the supplied value. func SequenceOf(v int) Sequence { i := make(Sequence) s := make(Sequence) go func(x int, r Sequence) { go func(x int) { defer close(s) for { r <- x s <- x } }(v, i) }(v) return i return s } // Take ... // Take a limited number of items from a producer. func (s Sequence) Take(c int) []int { r := []int{} i := 0 for x := range s { if !(i < c) { break } r = append(r, x) i++ } return r } // Equal checks two int-slices for equality. func Equal(a, b []int) bool { if len(a) != len(b) { return false } for i, v := range a { if v != b[i] { return false } } return true }
concurrency/concurrency1_test.go +25 −18 Original line number Diff line number Diff line package concurrency import ( "reflect" "sync" "testing" ) //func TestAnything(t *testing.T) { //} func TestBasic(t *testing.T) { done := make(chan bool) go func(done chan bool) { Loading @@ -32,11 +28,6 @@ func TestSequenceVeryPrimitive(t *testing.T) { } func TestSequencePrimitive(t *testing.T) { // ****************************** //t.Skip("test is broken") // ****************************** t.Log("TestSequencePrimitive() started.") defer t.Log("TestSequencePrimitive() done.") seq := make(chan int) Loading @@ -57,15 +48,31 @@ func TestSequencePrimitive(t *testing.T) { } } func TestSequenceFunctions(t *testing.T) { // ****************************** //t.Skip("test is broken") // ****************************** l0 := []int{1, 1, 1, 1, 1} if r := SequenceOf(1).Take(5); reflect.DeepEqual(r, l0) { t.Errorf("SequenceOf(1).Take(5) is %v, expected %v", r, l0) func TestSliceEquality(t *testing.T) { for _, d := range []struct { a, b []int r bool }{ {a: []int{}, b: []int{}, r: true}, {a: []int{}, b: []int{1}, r: false}, {a: []int{1}, b: []int{}, r: false}, {a: []int{1}, b: []int{1}, r: true}, {a: []int{1, 2}, b: []int{1, 2}, r: true}, {a: []int{1, 2, 3}, b: []int{1, 2, 3}, r: true}, {a: []int{1, 2, 3}, b: []int{1, 2}, r: false}, {a: []int{1, 2}, b: []int{1, 2, 3}, r: false}, {a: []int{1, 2, 3}, b: []int{1, 2, 4}, r: false}, {a: []int{1, 2, 4}, b: []int{1, 2, 3}, r: false}, } { if r := Equal(d.a, d.b); r != d.r { t.Errorf("Equal(%#v, %#v) is %t, expected %t", d.a, d.b, r, d.r) } } } func TestSequenceFunctions(t *testing.T) { l := []int{1, 1, 1, 1, 1} if r := SequenceOf(1).Take(5); !Equal(r, l) { t.Errorf("SequenceOf(1).Take(5) is %#v, expected %#v", r, l) } }