示例#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--
	}
}
示例#2
0
func TestPushPopRandom(t *testing.T) {
	const N = 1000
	deque := deque.New()

	// randomly push items to the front or to the back
	for i := 0; i < N; i++ {
		switch rand.Intn(2) {
		case 0:
			deque.PushFront("f")
		case 1:
			deque.PushBack("b")
		}
	}

	// randomly pop items from the front or from the back
	for i := 0; i < N; i++ {
		switch rand.Intn(2) {
		case 0:
			if deque.PopFront() == nil {
				t.Errorf("deque empty!")
			}
		case 1:
			if deque.PopBack() == nil {
				t.Errorf("deque empty!")
			}
		}
	}
}
示例#3
0
func TestSize(t *testing.T) {
	const N = 10
	deque := deque.New()

	if deque.Size() != 0 {
		t.Errorf("got: %d, want: 0", deque.Size())
	}

	for i := 0; i < N; i++ {
		deque.PushFront(i)
	}
	if deque.Size() != N {
		t.Errorf("got: %d, want: %d", deque.Size(), N)
	}
	for deque.Size() > 0 {
		deque.PopFront()
	}
	if deque.Size() != 0 {
		t.Errorf("got: %d, want: 0", deque.Size())
	}

	for i := 0; i < N; i++ {
		deque.PushBack(i)
	}
	if deque.Size() != N {
		t.Errorf("got: %d, want: %d", deque.Size(), N)
	}
	for deque.Size() > 0 {
		deque.PopBack()
	}
	if deque.Size() != 0 {
		t.Errorf("got: %d, want: 0", deque.Size())
	}
}
示例#4
0
func BenchmarkBackItem(b *testing.B) {
	const N = 16
	deque := deque.New()
	for i := 0; i < N; i++ {
		deque.PushBack(i)
	}
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		_ = deque.BackItem()
	}
}
示例#5
0
func TestPushPeek(t *testing.T) {
	deque := deque.New()

	deque.PushFront("a")
	if deque.FrontItem() != "a" {
		t.Errorf("got: %v, want: a", deque.FrontItem())
	}

	deque.PushBack("z")
	if deque.BackItem() != "z" {
		t.Errorf("got: %v, want: z", deque.BackItem())
	}
}
示例#6
0
func BenchmarkPushPopBack_1000(b *testing.B) {
	for i := 0; i < b.N; i++ {
		deque := deque.New()
		for i := 0; i < 1000; i++ {
			deque.PushBack(i)
		}

		sum := 0
		for i := 0; i < 1000; i++ {
			sum += deque.PopBack().(int)
		}
	}
}
示例#7
0
func BenchmarkIterate_backward(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.Back(); it != nil; it = it.Prev() {
			_ = it.Value
		}
	}
}
示例#8
0
func TestPushPop(t *testing.T) {
	deque := deque.New()

	deque.PushFront("a")
	deque.PushBack("z")

	front := deque.PopFront()
	if front != "a" {
		t.Errorf("got: %v, want: a", front)
	}
	back := deque.PopBack()
	if back != "z" {
		t.Errorf("got: %v, want: z", back)
	}
}
示例#9
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
}