Example #1
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!")
			}
		}
	}
}
Example #2
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())
	}
}
Example #3
0
func TestEmpty_PopBack(t *testing.T) {
	deque := deque.New()

	for i := 0; i < 3; i++ {
		back := deque.PopBack()
		if back != nil {
			t.Errorf("got: %v, want: <nil>", back)
		}
	}
}
Example #4
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)
		}
	}
}
Example #5
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)
	}
}