Пример #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!")
			}
		}
	}
}
Пример #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())
	}
}
Пример #3
0
func TestClear(t *testing.T) {
	const N = 10
	deque := deque.New()

	for i := 0; i < N; i++ {
		deque.PushFront(i)
	}
	deque.Clear()
	if deque.Size() != 0 {
		t.Errorf("got: %d, want: 0", deque.Size())
	}
}
Пример #4
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())
	}
}
Пример #5
0
func BenchmarkPushPopFront_1000(b *testing.B) {
	for i := 0; i < b.N; i++ {
		deque := deque.New()
		for i := 0; i < 1000; i++ {
			deque.PushFront(i)
		}

		sum := 0
		for i := 0; i < 1000; i++ {
			sum += deque.PopFront().(int)
		}
	}
}
Пример #6
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)
	}
}