func TestGrowingBufferSameSize(t *testing.T) { stream := &closeChecker{Stream: functional.Slice(functional.Count(), 0, 5)} b := NewGrowingBuffer(intSlice, 5) b.Consume(stream) verifyClosed(t, stream) verifyFetched(t, b, 0, 5) }
func TestBufferSmall(t *testing.T) { stream := &closeChecker{Stream: functional.Slice(functional.Count(), 0, 6)} b := NewBuffer(make([]int, 5)) b.Consume(stream) verifyClosed(t, stream) verifyFetched(t, b, 0, 5) }
func TestPageBufferParitalFirst(t *testing.T) { stream := &closeChecker{ Stream: functional.Slice(functional.Count(), 0, 1)} pb := NewPageBuffer(make([]int, 6), 0) pb.Consume(stream) verifyPageFetched(t, pb, 0, 1, 0, true) }
func TestPageBufferEmptyThirdTooHigh(t *testing.T) { stream := &closeChecker{ Stream: functional.Slice(functional.Count(), 0, 6)} pb := NewPageBuffer(make([]int, 6), 3) pb.Consume(stream) verifyPageFetched(t, pb, 3, 6, 1, true) }
func TestPtrGrowingBuffer(t *testing.T) { stream := &closeChecker{Stream: functional.Slice(functional.Count(), 0, 6)} b := NewPtrGrowingBuffer(intPtrSlice, 5, nil) var c ErrorReportingConsumer = b c.Consume(stream) verifyClosed(t, stream) verifyPtrFetched(t, b, 0, 6) }
func TestPageBufferFullSecond(t *testing.T) { stream := &closeChecker{ Stream: functional.Slice(functional.Count(), 0, 6)} pb := NewPageBuffer(make([]int, 6), 1) pb.Consume(stream) verifyClosed(t, stream) verifyPageFetched(t, pb, 3, 6, 1, true) }
func TestGrowingBufferSmall(t *testing.T) { stream := &closeChecker{Stream: functional.Slice(functional.Count(), 0, 6)} b := NewGrowingBuffer(intSlice, 5) b.Consume(stream) verifyClosed(t, stream) verifyFetched(t, b, 0, 6) if actual := cap(b.Values().([]int)); actual != 10 { t.Errorf("Expected capacit of 10, got %v", actual) } }
func TestPtrGrowingBuffer2(t *testing.T) { stream := &closeChecker{Stream: functional.Slice(functional.Count(), 0, 6)} b := NewPtrGrowingBuffer( intPtrSlice, 1, func() interface{} { return new(int) }) b.Consume(stream) verifyClosed(t, stream) verifyPtrFetched(t, b, 0, 6) if actual := cap(b.Values().([]*int)); actual != 8 { t.Errorf("Expected capacit of 8, got %v", actual) } }
// Power returns a Stream that emits the power set of items. Next of // returned Stream emits to an []int that has same length as items. func Power(items []int) functional.Stream { len := len(items) if len == 0 { return functional.Slice(ess, 0, 1) } return functional.Concat( Power(items[:len-1]), functional.Deferred(func() functional.Stream { return functional.Filter( appendFilterer(items[len-1]), Power(items[:len-1])) })) }
func main() { orig := make([]int, 100) for i := range orig { orig[i] = i } // Return the 10000th up to the 10010th element of the power set of // {0, 1, .. 99}. // This entire power set would have 2^100 elements in it! s := functional.Slice(Power(orig), 10000, 10010) result := make([]int, len(orig)) for s.Next(&result) == nil { fmt.Println(result) } }
func TestCompose2(t *testing.T) { consumer1 := errorReportingConsumerForTesting{} consumer2 := errorReportingConsumerForTesting{e: consumerError} errorReportingConsumer := Compose( new(int), nil, &consumer1, Modify( &consumer2, func(s functional.Stream) functional.Stream { return functional.Slice(s, 0, 2) })) errorReportingConsumer.Consume( closeErrorStream{functional.Slice(functional.Count(), 0, 5)}) if err := errorReportingConsumer.Error(); err != consumerError { t.Errorf("Expected consumerError, got %v", err) } if output := consumer1.count; output != 5 { t.Errorf("Expected 5, got %v", output) } if output := consumer2.count; output != 2 { t.Errorf("Expected 2, got %v", output) } }
func TestCompose(t *testing.T) { consumer1 := errorReportingConsumerForTesting{} consumer2 := errorReportingConsumerForTesting{} errorReportingConsumer := Compose( new(int), nil, &consumer1, &consumer2) errorReportingConsumer.Consume( closeErrorStream{functional.Slice(functional.Count(), 0, 5)}) if err := errorReportingConsumer.Error(); err != closeError { t.Errorf("Expected closeError, got %v", err) } if output := consumer1.count; output != 5 { t.Errorf("Expected 5, got %v", output) } if output := consumer2.count; output != 5 { t.Errorf("Expected 5, got %v", output) } }
func TestBufferBig(t *testing.T) { stream := functional.Slice(functional.Count(), 0, 4) b := NewBuffer(make([]int, 5)) b.Consume(stream) verifyFetched(t, b, 0, 4) }