-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqueue_stack_test.go
More file actions
123 lines (100 loc) · 2.4 KB
/
queue_stack_test.go
File metadata and controls
123 lines (100 loc) · 2.4 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package algo_test
import (
"github.com/brnstz/algo"
"fmt"
"io"
"os"
"strconv"
"testing"
)
// A generic interface for queues and stacks so we can use the same
// test code
type AddDel interface {
Add(interface{})
Del() (interface{}, error)
}
func loadIt(ad AddDel, file string, t *testing.T) {
fh, err := os.Open(file)
if err != nil {
t.Fatal(err)
}
defer fh.Close()
// Read curVal as a string. If it's "-", that means delete. Otherwise
// it should be an int to add which we translate using Atoi()
var curVal string
for {
// Scan a string into curVal
_, err = fmt.Fscan(fh, &curVal)
// If EOF, stop reading file. Other errors are fatal.
if err == io.EOF {
break
} else if err != nil {
t.Fatal(err)
}
if curVal == "-" {
// Dash means to delete from our stack/queue
_, err = ad.Del()
if err != nil {
t.Fatal(err)
}
} else {
// Otherwise convert to an int and add to our object.
intVal, err := strconv.Atoi(curVal)
if err != nil {
t.Fatal(err)
}
ad.Add(intVal)
}
}
}
func TestQueue(t *testing.T) {
q := algo.NewQueue()
loadIt(q, "data/numdash.txt", t)
// Check the expected value of size
size := q.Size()
if size != 30714 {
t.Fatalf("Expected size 30714 but got %v", size)
}
// Check expected value on top of queue
peek := q.Peek()
if peek != 1270713267 {
t.Fatalf("Expected 1270713267 on top of queue, but got: %v", peek)
}
// Delete all values from queue, ensure that empty works and that down
// sizing works without a panic.
for !q.IsEmpty() {
q.Dequeue()
}
size = q.Size()
if size != 0 {
t.Fatalf("Expected queue with size 0, but got size: %v", size)
}
}
func TestStack(t *testing.T) {
s := algo.NewStack()
loadIt(s, "data/numdash.txt", t)
// Check the expected value of size
size := s.Size()
if size != 30714 {
t.Fatalf("Expected size 30714 but got %v", size)
}
// Check expected value on top of stack
peek := s.Peek()
if peek != 318299769 {
t.Fatalf("Expected 318299769 on top of queue, but got: %v", peek)
}
// Exhuastively pop all values off stack
for !s.IsEmpty() {
s.Pop()
}
// We should get a nil and error value here
beNil, popErr := s.Pop()
if beNil != nil || popErr != algo.EmptyStack {
t.Fatalf("Expected nil and empty stack error, got %v and %v", beNil, popErr)
}
// We should get size 0 here
size = s.Size()
if size != 0 {
t.Fatalf("Expected queue with size 0, but got size: %v", size)
}
}