update readme

This commit is contained in:
Andrew Gillis 2018-04-28 20:35:17 -04:00
parent e0df3df127
commit 3771637d65
3 changed files with 56 additions and 2 deletions

View file

@ -49,7 +49,7 @@ func main() {
// Print: hello bar world // Print: hello bar world
for i := 0; i < q.Len(); i++ { for i := 0; i < q.Len(); i++ {
fmt.Print(q.PeekAt(i), " ") fmt.Print(q.Peek(i), " ")
} }
fmt.Println() fmt.Println()
} }

View file

@ -346,6 +346,16 @@ func TestInsert(t *testing.T) {
if q.Peek(5) != "x" { if q.Peek(5) != "x" {
t.Error("expected x at position 5") t.Error("expected x at position 5")
} }
q.Insert(0, "b")
if q.Front() != "b" {
t.Error("expected b inserted at front")
}
q.Insert(q.Len(), "e")
if q.Back() != "e" {
t.Error("expected e inserted at back")
}
} }
func TestRemove(t *testing.T) { func TestRemove(t *testing.T) {
@ -375,6 +385,14 @@ func TestRemove(t *testing.T) {
if q.Peek(4) != "G" { if q.Peek(4) != "G" {
t.Error("expected G at position 4") t.Error("expected G at position 4")
} }
if q.Remove(0) != "A" {
t.Error("expected to remove A from front")
}
if q.Remove(q.Len()-1) != "G" {
t.Error("expected to remove G from back")
}
} }
func TestPeekOutOfRangePanics(t *testing.T) { func TestPeekOutOfRangePanics(t *testing.T) {
@ -444,6 +462,42 @@ func TestPopBackOutOfRangePanics(t *testing.T) {
}) })
} }
func TestInsertOutOfRangePanics(t *testing.T) {
var q Deque
assertPanics(t, "should panic when inserting out of range", func() {
q.Insert(1, "X")
})
q.PushBack("A")
assertPanics(t, "should panic when inserting at negative index", func() {
q.Insert(-1, "Y")
})
assertPanics(t, "should panic when inserting out of range", func() {
q.Insert(2, "B")
})
}
func TestRemoveOutOfRangePanics(t *testing.T) {
var q Deque
assertPanics(t, "should panic when removing from empty queue", func() {
q.Remove(0)
})
q.PushBack("A")
assertPanics(t, "should panic when removing at negative index", func() {
q.Remove(-1)
})
assertPanics(t, "should panic when removing out of range", func() {
q.Remove(1)
})
}
func assertPanics(t *testing.T, name string, f func()) { func assertPanics(t *testing.T, name string, f func()) {
defer func() { defer func() {
if r := recover(); r == nil { if r := recover(); r == nil {

View file

@ -4,7 +4,7 @@ set -e
echo "" > coverage.txt echo "" > coverage.txt
for d in $(go list ./... | grep -v vendor); do for d in $(go list ./... | grep -v vendor); do
go test -race -coverprofile=profile.out -covermode=atomic $d go test -coverprofile=profile.out -covermode=atomic $d
if [ -f profile.out ]; then if [ -f profile.out ]; then
cat profile.out >> coverage.txt cat profile.out >> coverage.txt
rm profile.out rm profile.out