From fe7fb7b07be0d40225ce67289b6cafb87e7ea1e5 Mon Sep 17 00:00:00 2001 From: Emir Pasic Date: Fri, 24 Jun 2016 19:57:54 +0200 Subject: [PATCH] - refactor stacks' tests --- stacks/arraystack/arraystack_test.go | 37 +++++++++++-------- .../linkedliststack/linkedliststack_test.go | 36 ++++++++++-------- 2 files changed, 42 insertions(+), 31 deletions(-) diff --git a/stacks/arraystack/arraystack_test.go b/stacks/arraystack/arraystack_test.go index 65fed89..42d7d6a 100644 --- a/stacks/arraystack/arraystack_test.go +++ b/stacks/arraystack/arraystack_test.go @@ -30,15 +30,11 @@ import ( "testing" ) -func TestArrayStack(t *testing.T) { - +func TestStackPush(t *testing.T) { stack := New() - if actualValue := stack.Empty(); actualValue != true { t.Errorf("Got %v expected %v", actualValue, true) } - - // insertions stack.Push(1) stack.Push(2) stack.Push(3) @@ -46,47 +42,57 @@ func TestArrayStack(t *testing.T) { if actualValue := stack.Values(); actualValue[0].(int) != 3 || actualValue[1].(int) != 2 || actualValue[2].(int) != 1 { t.Errorf("Got %v expected %v", actualValue, "[3,2,1]") } - if actualValue := stack.Empty(); actualValue != false { t.Errorf("Got %v expected %v", actualValue, false) } - if actualValue := stack.Size(); actualValue != 3 { t.Errorf("Got %v expected %v", actualValue, 3) } + if actualValue, ok := stack.Peek(); actualValue != 3 || !ok { + t.Errorf("Got %v expected %v", actualValue, 3) + } +} +func TestStackPeek(t *testing.T) { + stack := New() + if actualValue, ok := stack.Peek(); actualValue != nil || ok { + t.Errorf("Got %v expected %v", actualValue, nil) + } + stack.Push(1) + stack.Push(2) + stack.Push(3) if actualValue, ok := stack.Peek(); actualValue != 3 || !ok { t.Errorf("Got %v expected %v", actualValue, 3) } +} +func TestStackPop(t *testing.T) { + stack := New() + stack.Push(1) + stack.Push(2) + stack.Push(3) stack.Pop() - if actualValue, ok := stack.Peek(); actualValue != 2 || !ok { t.Errorf("Got %v expected %v", actualValue, 2) } - if actualValue, ok := stack.Pop(); actualValue != 2 || !ok { t.Errorf("Got %v expected %v", actualValue, 2) } - if actualValue, ok := stack.Pop(); actualValue != 1 || !ok { t.Errorf("Got %v expected %v", actualValue, 1) } - if actualValue, ok := stack.Pop(); actualValue != nil || ok { t.Errorf("Got %v expected %v", actualValue, nil) } - if actualValue := stack.Empty(); actualValue != true { t.Errorf("Got %v expected %v", actualValue, true) } - if actualValue := stack.Values(); len(actualValue) != 0 { t.Errorf("Got %v expected %v", actualValue, "[]") } } -func TestArrayStackIterator(t *testing.T) { +func TestStackIterator(t *testing.T) { stack := New() stack.Push("a") stack.Push("b") @@ -121,7 +127,7 @@ func TestArrayStackIterator(t *testing.T) { } } -func BenchmarkArrayStack(b *testing.B) { +func BenchmarkStack(b *testing.B) { for i := 0; i < b.N; i++ { stack := New() for n := 0; n < 1000; n++ { @@ -131,5 +137,4 @@ func BenchmarkArrayStack(b *testing.B) { stack.Pop() } } - } diff --git a/stacks/linkedliststack/linkedliststack_test.go b/stacks/linkedliststack/linkedliststack_test.go index 2e082ff..c9b097a 100644 --- a/stacks/linkedliststack/linkedliststack_test.go +++ b/stacks/linkedliststack/linkedliststack_test.go @@ -30,15 +30,11 @@ import ( "testing" ) -func TestLinkedListStack(t *testing.T) { - +func TestStackPush(t *testing.T) { stack := New() - if actualValue := stack.Empty(); actualValue != true { t.Errorf("Got %v expected %v", actualValue, true) } - - // insertions stack.Push(1) stack.Push(2) stack.Push(3) @@ -46,47 +42,57 @@ func TestLinkedListStack(t *testing.T) { if actualValue := stack.Values(); actualValue[0].(int) != 3 || actualValue[1].(int) != 2 || actualValue[2].(int) != 1 { t.Errorf("Got %v expected %v", actualValue, "[3,2,1]") } - if actualValue := stack.Empty(); actualValue != false { t.Errorf("Got %v expected %v", actualValue, false) } - if actualValue := stack.Size(); actualValue != 3 { t.Errorf("Got %v expected %v", actualValue, 3) } + if actualValue, ok := stack.Peek(); actualValue != 3 || !ok { + t.Errorf("Got %v expected %v", actualValue, 3) + } +} +func TestStackPeek(t *testing.T) { + stack := New() + if actualValue, ok := stack.Peek(); actualValue != nil || ok { + t.Errorf("Got %v expected %v", actualValue, nil) + } + stack.Push(1) + stack.Push(2) + stack.Push(3) if actualValue, ok := stack.Peek(); actualValue != 3 || !ok { t.Errorf("Got %v expected %v", actualValue, 3) } +} +func TestStackPop(t *testing.T) { + stack := New() + stack.Push(1) + stack.Push(2) + stack.Push(3) stack.Pop() - if actualValue, ok := stack.Peek(); actualValue != 2 || !ok { t.Errorf("Got %v expected %v", actualValue, 2) } - if actualValue, ok := stack.Pop(); actualValue != 2 || !ok { t.Errorf("Got %v expected %v", actualValue, 2) } - if actualValue, ok := stack.Pop(); actualValue != 1 || !ok { t.Errorf("Got %v expected %v", actualValue, 1) } - if actualValue, ok := stack.Pop(); actualValue != nil || ok { t.Errorf("Got %v expected %v", actualValue, nil) } - if actualValue := stack.Empty(); actualValue != true { t.Errorf("Got %v expected %v", actualValue, true) } - if actualValue := stack.Values(); len(actualValue) != 0 { t.Errorf("Got %v expected %v", actualValue, "[]") } } -func TestLinkedListStackIterator(t *testing.T) { +func TestStackIterator(t *testing.T) { stack := New() stack.Push("a") stack.Push("b") @@ -121,7 +127,7 @@ func TestLinkedListStackIterator(t *testing.T) { } } -func BenchmarkLinkedListStack(b *testing.B) { +func BenchmarkStack(b *testing.B) { for i := 0; i < b.N; i++ { stack := New() for n := 0; n < 1000; n++ {