summaryrefslogtreecommitdiff
path: root/stack_test.go
blob: 31e6ff730adc26cfeb3cdcaa4a62de49f2870e76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package stack

import "testing"

func TestInt(t *testing.T) {
	s := NewStack()
	s.Push(1)
	i := s.Pop()
	if i != 1 {
		t.Error("Expected 1, got ", i)
	}
}

func TestComplex(t *testing.T) {
	s := NewStack()
	s.Push(1+1i)
	i := s.Pop()
	if i != 1+1i {
		t.Error("Expected 1+1i, got ", i)
	}
}

func TestMeny(t *testing.T) {
	s := NewStack()
	s.Push(1)
	s.Push(2)
	i := s.Pop()
	if i != 2 {
		t.Error("Expected 2, got ", i)
	}
	i = s.Pop()
	if i != 1 {
		t.Error("Expected 1, got ", i)
	}
}

func TestSwap(t *testing.T) {
	s := NewStack()
	s.Push(1)
	s.Push(2)
	s.Swap()
	if s.Depth() != 2 {
		t.Error("Expected depth of 2")
	}
	a := s.Pop()
	b := s.Pop()
	if s.Depth() != 0 {
		t.Error("Expected depth of 0")
	}
	if a != 1 || b != 2 {
		t.Error("Expected swapped values")
	}
}

func TestInsert(t *testing.T) {
	s := NewStack()
	s.Push(1)
	s.Insert(2)
	if s.Depth() != 2 {
		t.Error("Expected depth of 2")
	}
	a := s.Pop()
	b := s.Pop()
	if s.Depth() != 0 {
		t.Error("Expected depth of 0")
	}
	if a != 1 || b != 2 {
		t.Error("Expected swapped values")
	}
}

func BenchmarkPushPopSingle(b *testing.B) {
	s := NewStack()
	for i := 0; i < b.N; i++ {
		s.Push(1)
		s.Pop()
	}
}

func BenchmarkPushPopAlot(b *testing.B) {
	s := NewStack()
	for i := 0; i < b.N; i++ {
		s.Push(i)
	}
	for i := 0; i < b.N; i++ {
		s.Pop()
	}
}