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 124 125 126 127 128 129 130
|
// Tideland Go Library - Collections - Stacks - Unit Tests
//
// Copyright (C) 2015-2017 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
package collections_test
//--------------------
// IMPORTS
//--------------------
import (
"testing"
"github.com/tideland/golib/audit"
"github.com/tideland/golib/collections"
)
//--------------------
// TESTS
//--------------------
// TestStackPushPop tests the core stack methods.
func TestStackPushPop(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
// Start with an empty stack.
sa := collections.NewStack()
assert.Length(sa, 0)
sa.Push("foo")
sa.Push(4711)
assert.Length(sa, 2)
sa.Push()
assert.Length(sa, 2)
sa.Push(false, 8.15)
assert.Length(sa, 4)
v, err := sa.Peek()
assert.Nil(err)
assert.Equal(v, 8.15)
v, err = sa.Pop()
assert.Nil(err)
assert.Equal(v, 8.15)
assert.Length(sa, 3)
// Start with a filled stack.
sb := collections.NewStack("a", true, 4711)
assert.Length(sb, 3)
v, err = sb.Pop()
assert.Nil(err)
assert.Equal(v, 4711)
assert.Length(sb, 2)
v, err = sb.Pop()
assert.Nil(err)
assert.Equal(v, true)
assert.Length(sb, 1)
v, err = sb.Pop()
assert.Nil(err)
assert.Equal(v, "a")
assert.Length(sb, 0)
// Popping the last one returns an error.
_, err = sb.Pop()
assert.ErrorMatch(err, ".*collection is empty")
// And now deflate the first one.
sa.Deflate()
assert.Length(sa, 0)
}
// TestStackAll tests the retrieval of all values.
func TestStackAll(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
s := collections.NewStack(1, "b", 3.0, true)
all := s.All()
assert.Equal(all, []interface{}{1, "b", 3.0, true})
all = s.AllReverse()
assert.Equal(all, []interface{}{true, 3.0, "b", 1})
}
// TestStringStackPushPop tests the core string stack methods.
func TestStringStackPushPop(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
// Start with an empty stack.
sa := collections.NewStringStack()
assert.Length(sa, 0)
sa.Push("foo")
sa.Push("bar")
assert.Length(sa, 2)
sa.Push()
assert.Length(sa, 2)
sa.Push("baz", "yadda")
assert.Length(sa, 4)
v, err := sa.Peek()
assert.Nil(err)
assert.Equal(v, "yadda")
v, err = sa.Pop()
assert.Nil(err)
assert.Equal(v, "yadda")
assert.Length(sa, 3)
// Start with a filled stack.
sb := collections.NewStringStack("a", "b", "c")
assert.Length(sb, 3)
v, err = sb.Pop()
assert.Nil(err)
assert.Equal(v, "c")
assert.Length(sb, 2)
v, err = sb.Pop()
assert.Nil(err)
assert.Equal(v, "b")
assert.Length(sb, 1)
v, err = sb.Pop()
assert.Nil(err)
assert.Equal(v, "a")
assert.Length(sb, 0)
// Popping the last one returns an error.
_, err = sb.Pop()
assert.ErrorMatch(err, ".*collection is empty")
// And now deflate the first one.
sa.Deflate()
assert.Length(sa, 0)
}
// EOF
|