2015-03-05 18:29:57 +00:00
|
|
|
/*
|
2015-03-06 16:10:34 +00:00
|
|
|
Copyright (c) 2015, Emir Pasic
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright notice, this
|
|
|
|
list of conditions and the following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer in the documentation
|
|
|
|
and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2015-03-05 18:29:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package arraystack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2016-06-24 17:57:54 +00:00
|
|
|
func TestStackPush(t *testing.T) {
|
2015-03-05 18:29:57 +00:00
|
|
|
stack := New()
|
|
|
|
if actualValue := stack.Empty(); actualValue != true {
|
|
|
|
t.Errorf("Got %v expected %v", actualValue, true)
|
|
|
|
}
|
|
|
|
stack.Push(1)
|
|
|
|
stack.Push(2)
|
|
|
|
stack.Push(3)
|
|
|
|
|
2015-03-07 17:19:58 +00:00
|
|
|
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]")
|
2015-03-07 17:05:34 +00:00
|
|
|
}
|
2015-03-05 18:29:57 +00:00
|
|
|
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)
|
|
|
|
}
|
2016-06-24 17:57:54 +00:00
|
|
|
if actualValue, ok := stack.Peek(); actualValue != 3 || !ok {
|
|
|
|
t.Errorf("Got %v expected %v", actualValue, 3)
|
|
|
|
}
|
|
|
|
}
|
2015-03-05 18:29:57 +00:00
|
|
|
|
2016-06-24 17:57:54 +00:00
|
|
|
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)
|
2015-03-05 18:29:57 +00:00
|
|
|
if actualValue, ok := stack.Peek(); actualValue != 3 || !ok {
|
|
|
|
t.Errorf("Got %v expected %v", actualValue, 3)
|
|
|
|
}
|
2016-06-24 17:57:54 +00:00
|
|
|
}
|
2015-03-05 18:29:57 +00:00
|
|
|
|
2016-06-24 17:57:54 +00:00
|
|
|
func TestStackPop(t *testing.T) {
|
|
|
|
stack := New()
|
|
|
|
stack.Push(1)
|
|
|
|
stack.Push(2)
|
|
|
|
stack.Push(3)
|
2015-03-05 18:29:57 +00:00
|
|
|
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)
|
|
|
|
}
|
2015-03-07 17:19:58 +00:00
|
|
|
if actualValue := stack.Values(); len(actualValue) != 0 {
|
|
|
|
t.Errorf("Got %v expected %v", actualValue, "[]")
|
2015-03-07 17:05:34 +00:00
|
|
|
}
|
2016-06-22 03:56:09 +00:00
|
|
|
}
|
2015-03-07 17:05:34 +00:00
|
|
|
|
2016-06-25 17:26:08 +00:00
|
|
|
func TestStackIteratorOnEmpty(t *testing.T) {
|
|
|
|
stack := New()
|
|
|
|
it := stack.Iterator()
|
|
|
|
for it.Next() {
|
|
|
|
t.Errorf("Shouldn't iterate on empty stack")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStackIteratorNext(t *testing.T) {
|
2016-06-22 03:56:09 +00:00
|
|
|
stack := New()
|
|
|
|
stack.Push("a")
|
|
|
|
stack.Push("b")
|
|
|
|
stack.Push("c")
|
|
|
|
|
|
|
|
it := stack.Iterator()
|
2016-06-25 17:11:20 +00:00
|
|
|
count := 0
|
2016-06-22 03:56:09 +00:00
|
|
|
for it.Next() {
|
2016-06-25 17:11:20 +00:00
|
|
|
count++
|
2016-06-22 03:56:09 +00:00
|
|
|
index := it.Index()
|
|
|
|
value := it.Value()
|
|
|
|
switch index {
|
|
|
|
case 0:
|
|
|
|
if actualValue, expectedValue := value, "c"; actualValue != expectedValue {
|
|
|
|
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
|
|
|
}
|
|
|
|
case 1:
|
|
|
|
if actualValue, expectedValue := value, "b"; actualValue != expectedValue {
|
|
|
|
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
|
|
|
}
|
|
|
|
case 2:
|
|
|
|
if actualValue, expectedValue := value, "a"; actualValue != expectedValue {
|
|
|
|
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Errorf("Too many")
|
|
|
|
}
|
2016-06-25 17:11:20 +00:00
|
|
|
if actualValue, expectedValue := index, count-1; actualValue != expectedValue {
|
|
|
|
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if actualValue, expectedValue := count, 3; actualValue != expectedValue {
|
|
|
|
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
2016-06-22 03:56:09 +00:00
|
|
|
}
|
2016-06-25 17:26:08 +00:00
|
|
|
}
|
2016-06-25 17:11:20 +00:00
|
|
|
|
2016-06-25 17:26:08 +00:00
|
|
|
func TestStackIteratorPrev(t *testing.T) {
|
|
|
|
stack := New()
|
|
|
|
stack.Push("a")
|
|
|
|
stack.Push("b")
|
|
|
|
stack.Push("c")
|
|
|
|
|
|
|
|
it := stack.Iterator()
|
|
|
|
for it.Next() {
|
|
|
|
}
|
|
|
|
count := 0
|
2016-06-25 17:11:20 +00:00
|
|
|
for it.Prev() {
|
|
|
|
count++
|
|
|
|
index := it.Index()
|
|
|
|
value := it.Value()
|
|
|
|
switch index {
|
|
|
|
case 0:
|
|
|
|
if actualValue, expectedValue := value, "c"; actualValue != expectedValue {
|
|
|
|
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
|
|
|
}
|
|
|
|
case 1:
|
|
|
|
if actualValue, expectedValue := value, "b"; actualValue != expectedValue {
|
|
|
|
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
|
|
|
}
|
|
|
|
case 2:
|
|
|
|
if actualValue, expectedValue := value, "a"; actualValue != expectedValue {
|
|
|
|
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Errorf("Too many")
|
|
|
|
}
|
|
|
|
if actualValue, expectedValue := index, 3-count; actualValue != expectedValue {
|
|
|
|
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if actualValue, expectedValue := count, 3; actualValue != expectedValue {
|
|
|
|
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
|
|
|
}
|
2015-03-05 18:29:57 +00:00
|
|
|
}
|
|
|
|
|
2016-06-26 20:40:49 +00:00
|
|
|
func TestStackIteratorReset(t *testing.T) {
|
2016-06-26 19:44:23 +00:00
|
|
|
stack := New()
|
|
|
|
it := stack.Iterator()
|
|
|
|
it.Reset()
|
|
|
|
stack.Push("a")
|
|
|
|
stack.Push("b")
|
|
|
|
stack.Push("c")
|
|
|
|
for it.Next() {
|
|
|
|
}
|
|
|
|
it.Reset()
|
|
|
|
it.Next()
|
|
|
|
if index, value := it.Index(), it.Value(); index != 0 || value != "c" {
|
|
|
|
t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "c")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-26 20:40:49 +00:00
|
|
|
func TestStackIteratorLast(t *testing.T) {
|
|
|
|
stack := New()
|
|
|
|
it := stack.Iterator()
|
|
|
|
if actualValue, expectedValue := it.Last(), false; actualValue != expectedValue {
|
|
|
|
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
|
|
|
}
|
|
|
|
stack.Push("a")
|
|
|
|
stack.Push("b")
|
|
|
|
stack.Push("c")
|
|
|
|
if actualValue, expectedValue := it.Last(), true; actualValue != expectedValue {
|
|
|
|
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
|
|
|
}
|
|
|
|
if index, value := it.Index(), it.Value(); index != 2 || value != "a" {
|
|
|
|
t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "a")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-24 17:57:54 +00:00
|
|
|
func BenchmarkStack(b *testing.B) {
|
2015-03-05 18:29:57 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
stack := New()
|
|
|
|
for n := 0; n < 1000; n++ {
|
|
|
|
stack.Push(i)
|
|
|
|
}
|
|
|
|
for !stack.Empty() {
|
|
|
|
stack.Pop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|