func TestAppendTo2(t *testing.T) { values := []int{1, 2} c := AppendTo(&values) stream := functional.Slice(functional.Count(), 3, 7) doConsume(t, c, stream, nil) stream = functional.Slice(functional.Count(), 7, 11) doConsume(t, c, stream, nil) verifyValues(t, values, 1, 11) if actual := cap(values); actual != 11 { t.Errorf("Expected capacity of 11, got %v", actual) } }
func TestAppendTo(t *testing.T) { var values []int stream := functional.Slice(functional.Count(), 0, 7) doConsume(t, AppendTo(&values), stream, nil) verifyValues(t, values, 0, 7) if actual := cap(values); actual != 15 { t.Errorf("Expected capacity of 15, got %v", actual) } }
func TestGrowingBufferBig(t *testing.T) { stream := functional.Slice(functional.Count(), 0, 4) b := NewGrowingBuffer(intSlice, 5) doConsume(t, b, stream, nil) verifyFetched(t, b, 0, 4) if actual := cap(b.Values().([]int)); actual != 6 { t.Errorf("Expected capacity of 6, got %v", actual) } }
func TestPtrGrowingBuffer2(t *testing.T) { stream := functional.Slice(functional.Count(), 0, 6) b := NewPtrGrowingBuffer(intPtrSlice, 0, intCreater) doConsume(t, b, stream, nil) verifyPtrFetched(t, b, 0, 6) if actual := cap(b.Values().([]*int)); actual != 7 { t.Errorf("Expected capacity of 7, got %v", actual) } }
func ExampleAppendTo() { values := []int{5} stream := functional.Slice(functional.Count(), 0, 2) AppendTo(&values).Consume(stream) for i := range values { fmt.Println(values[i]) } // Output: // 5 // 0 // 1 }
func ExampleAppendPtrsTo() { var values []*int stream := functional.Slice(functional.Count(), 0, 3) AppendPtrsTo(&values, nil).Consume(stream) for i := range values { fmt.Println(*values[i]) } // Output: // 0 // 1 // 2 }
func TestAppendPtrsTo2(t *testing.T) { var values []*int stream := functional.Slice(functional.Count(), 0, 3) var x int // Our creater returns a pointer to the same variable. creater := func() interface{} { return &x } doConsume(t, AppendPtrsTo(&values, creater), stream, nil) // We should have a slice of length 3 with all pointers being the same. if len(values) != 3 || values[0] != values[1] || values[0] != values[2] { t.Error("Failure") } }
func TestPtrGrowingBufferPointersPreserved(t *testing.T) { stream := functional.Slice(functional.Count(), 0, 3) b := NewPtrGrowingBuffer(intPtrSlice, 4, nil) b.Consume(stream) values := b.Values().([]*int) zeroToThree := make([]*int, len(values)) copy(zeroToThree, values) stream = functional.Slice(functional.Count(), 10, 15) b.Consume(stream) values = b.Values().([]*int) tenToFifteen := make([]*int, len(values)) copy(tenToFifteen, values) stream = functional.Slice(functional.Count(), 20, 22) b.Consume(stream) values = b.Values().([]*int) twentyToTwentyTwo := make([]*int, len(values)) copy(twentyToTwentyTwo, values) verifyPtrValues(t, zeroToThree, 0, 3) verifyPtrValues(t, tenToFifteen, 10, 15) verifyPtrValues(t, twentyToTwentyTwo, 20, 22) }
func TestPtrBufferWithCreater(t *testing.T) { stream := functional.Count() aslice := make([]*int, 5) for i := range aslice { aslice[i] = new(int) } origSlice := make([]*int, len(aslice)) copy(origSlice, aslice) b := NewPtrBuffer(aslice) doConsume(t, b, stream, nil) verifyPtrFetched(t, b, 0, 5) for i := range aslice { if origSlice[i] != aslice[i] { t.Fatal("Expect aslice pointers not to change.") } } }
func TestPageBufferSecondPage(t *testing.T) { stream := functional.Count() pb := NewPageBuffer(make([]int, 6), 1) doConsume(t, pb, stream, nil) verifyPageFetched(t, pb, 3, 6, 1, false) }
func TestBufferBig(t *testing.T) { stream := functional.Slice(functional.Count(), 0, 4) b := NewBuffer(make([]int, 5)) doConsume(t, b, stream, nil) verifyFetched(t, b, 0, 4) }
func TestPageBufferParitalFirst(t *testing.T) { stream := functional.Slice(functional.Count(), 0, 1) pb := NewPageBuffer(make([]int, 6), 0) doConsume(t, pb, stream, nil) verifyPageFetched(t, pb, 0, 1, 0, true) }
func TestPageBufferFullSecond(t *testing.T) { stream := functional.Slice(functional.Count(), 0, 6) pb := NewPageBuffer(make([]int, 6), 1) doConsume(t, pb, stream, nil) verifyPageFetched(t, pb, 3, 6, 1, true) }
func TestPageBufferEmptyThirdTooHigh(t *testing.T) { stream := functional.Slice(functional.Count(), 0, 6) pb := NewPageBuffer(make([]int, 6), 3) doConsume(t, pb, stream, nil) verifyPageFetched(t, pb, 3, 6, 1, true) }
func TestPageBufferParitalThirdToHigh(t *testing.T) { stream := functional.Slice(functional.Count(), 0, 7) pb := NewPageBuffer(make([]int, 6), 3) doConsume(t, pb, stream, nil) verifyPageFetched(t, pb, 6, 7, 2, true) }
func TestPageBufferNegativePage(t *testing.T) { stream := functional.Count() pb := NewPageBuffer(make([]int, 6), -1) doConsume(t, pb, stream, nil) verifyPageFetched(t, pb, 0, 3, 0, false) }
func TestPtrGrowingBuffer(t *testing.T) { stream := functional.Slice(functional.Count(), 0, 6) b := NewPtrGrowingBuffer(intPtrSlice, 5, nil) doConsume(t, b, stream, nil) verifyPtrFetched(t, b, 0, 6) }
func TestPtrBuffer(t *testing.T) { stream := functional.Count() b := NewPtrBuffer(make([]*int, 5)) doConsume(t, b, stream, nil) verifyPtrFetched(t, b, 0, 5) }
func TestPtrPageBuffer(t *testing.T) { stream := functional.Count() pb := NewPtrPageBuffer(make([]*int, 6), 0) doConsume(t, pb, stream, nil) verifyPtrPageFetched(t, pb, 0, 3, 0, false) }
func TestPageBufferThirdPage(t *testing.T) { stream := functional.Count() pb := NewPageBuffer(make([]int, 6), 2) doConsume(t, pb, stream, nil) verifyPageFetched(t, pb, 6, 9, 2, false) }
func TestAppendPtrsTo(t *testing.T) { var values []*int stream := functional.Slice(functional.Count(), 0, 7) doConsume(t, AppendPtrsTo(&values, nil), stream, nil) verifyPtrValues(t, values, 0, 7) }