Example #1
0
func TestIterate(t *testing.T) {
	const N = 1000
	deque := deque.New()
	for i := 0; i < N; i++ {
		deque.PushBack(i)
	}

	// iterate from front to back
	i := 0
	for e := deque.Front(); e != nil; e = e.Next() {
		if e.Value != i {
			t.Errorf("got: %d, want: %d", e.Value, i)
		}
		i++
	}

	// iterate from back to front
	i = N - 1
	for e := deque.Back(); e != nil; e = e.Prev() {
		if e.Value != i {
			t.Errorf("got: %d, want: %d", e.Value, i)
		}
		i--
	}
}
Example #2
0
func TestEmpty_iterator(t *testing.T) {
	deque := deque.New()

	front := deque.Front()
	if front != nil {
		t.Errorf("got: %v, want: <nil>", front.Value)
	}

	back := deque.Back()
	if back != nil {
		t.Errorf("got: %v, want: <nil>", back.Value)
	}
}
Example #3
0
func BenchmarkIterate_forward(b *testing.B) {
	const N = 1024
	deque := deque.New()
	for i := 0; i < N; i++ {
		deque.PushBack(i)
	}
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		for it := deque.Front(); it != nil; it = it.Next() {
			_ = it.Value
		}
	}
}
Example #4
0
func ExampleIterator() {
	const N = 10
	deque := deque.New()
	for i := 0; i < N; i++ {
		deque.PushBack(i)
	}

	// iterate from front to back
	for it := deque.Front(); it != nil; it = it.Next() {
		fmt.Printf("%v", it.Value)
	}
	fmt.Println()

	// iterate from back to front
	for it := deque.Back(); it != nil; it = it.Prev() {
		fmt.Printf("%v", it.Value)
	}
	fmt.Println()

	// Output:
	// 0123456789
	// 9876543210
}