Beispiel #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)
	}
}
Beispiel #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)
	}
}
Beispiel #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)
	}
}
Beispiel #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)
	}
}
Beispiel #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
}
Beispiel #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
}
Beispiel #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")
	}
}
Beispiel #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)
}
Beispiel #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.")
		}
	}
}
Beispiel #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)
}
Beispiel #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)
}
Beispiel #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)
}
Beispiel #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)
}
Beispiel #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)
}
Beispiel #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)
}
Beispiel #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)
}
Beispiel #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)
}
Beispiel #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)
}
Beispiel #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)
}
Beispiel #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)
}
Beispiel #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)
}