Example #1
0
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)
	}
}
Example #2
0
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)
	}
}
Example #3
0
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)
	}
}
Example #4
0
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)
	}
}
Example #5
0
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
}
Example #6
0
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
}
Example #7
0
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")
	}
}
Example #8
0
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)
}
Example #9
0
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.")
		}
	}
}
Example #10
0
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)
}
Example #11
0
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)
}
Example #12
0
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)
}
Example #13
0
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)
}
Example #14
0
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)
}
Example #15
0
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)
}
Example #16
0
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)
}
Example #17
0
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)
}
Example #18
0
func TestPtrBuffer(t *testing.T) {
	stream := functional.Count()
	b := NewPtrBuffer(make([]*int, 5))
	doConsume(t, b, stream, nil)
	verifyPtrFetched(t, b, 0, 5)
}
Example #19
0
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)
}
Example #20
0
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)
}
Example #21
0
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)
}