2019-03-13 04:21:30 +00:00
|
|
|
package lastack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-04-10 17:07:09 +00:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
|
2019-03-13 04:21:30 +00:00
|
|
|
"github.com/emirpasic/gods/stacks/arraystack"
|
|
|
|
|
|
|
|
"github.com/Pallinder/go-randomdata"
|
|
|
|
)
|
|
|
|
|
2019-04-02 10:28:41 +00:00
|
|
|
func TestPush(t *testing.T) {
|
2019-04-10 17:07:09 +00:00
|
|
|
var result string
|
2019-04-02 10:28:41 +00:00
|
|
|
s := New()
|
2019-04-10 17:07:09 +00:00
|
|
|
|
|
|
|
result = spew.Sprint(s.Values())
|
|
|
|
if result != "[]" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
l := []int{10, 7, 3, 4, 5, 15}
|
|
|
|
for _, v := range l {
|
2019-04-02 10:28:41 +00:00
|
|
|
s.Push(v)
|
|
|
|
}
|
2019-03-13 05:57:08 +00:00
|
|
|
|
2019-04-10 17:07:09 +00:00
|
|
|
result = spew.Sprint(s.Values())
|
|
|
|
if result != "[15 5 4 3 7 10]" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
2019-03-13 05:57:08 +00:00
|
|
|
|
2019-04-10 17:07:09 +00:00
|
|
|
if v, ok := s.Peek(); ok {
|
|
|
|
if v != 15 {
|
|
|
|
t.Error(v)
|
|
|
|
}
|
2019-04-02 10:28:41 +00:00
|
|
|
}
|
2019-03-13 05:57:08 +00:00
|
|
|
|
2019-04-10 17:07:09 +00:00
|
|
|
result = spew.Sprint(s.Values())
|
|
|
|
if result != "[15 5 4 3 7 10]" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
2019-04-03 10:24:05 +00:00
|
|
|
|
2019-04-10 17:07:09 +00:00
|
|
|
if v, ok := s.Pop(); ok {
|
|
|
|
if v != 15 {
|
|
|
|
t.Error(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result = spew.Sprint(s.Values())
|
|
|
|
if result != "[5 4 3 7 10]" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
for s.Size() != 1 {
|
|
|
|
s.Pop()
|
|
|
|
}
|
|
|
|
|
|
|
|
result = spew.Sprint(s.Values())
|
|
|
|
if result != "[10]" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := s.Pop(); ok {
|
|
|
|
if v != 10 {
|
|
|
|
t.Error(v)
|
|
|
|
}
|
2019-04-02 10:28:41 +00:00
|
|
|
}
|
2019-03-13 05:57:08 +00:00
|
|
|
|
2019-04-10 17:07:09 +00:00
|
|
|
result = spew.Sprint(s.Values())
|
|
|
|
if result != "[]" {
|
|
|
|
t.Error(result)
|
2019-04-02 10:28:41 +00:00
|
|
|
}
|
2019-04-10 17:07:09 +00:00
|
|
|
|
2019-04-02 10:28:41 +00:00
|
|
|
}
|
2019-03-13 04:21:30 +00:00
|
|
|
|
|
|
|
func BenchmarkGet(b *testing.B) {
|
|
|
|
s := New()
|
|
|
|
b.N = 20000000
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
v := randomdata.Number(0, 65535)
|
|
|
|
s.Push(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
s.Get(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkPush(b *testing.B) {
|
|
|
|
s := New()
|
2019-03-23 19:11:42 +00:00
|
|
|
b.N = 20000000
|
2019-03-13 04:21:30 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
v := randomdata.Number(0, 65535)
|
|
|
|
s.Push(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGodsPush(b *testing.B) {
|
|
|
|
s := arraystack.New()
|
2019-03-23 19:11:42 +00:00
|
|
|
b.N = 2000000
|
2019-03-13 04:21:30 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
v := randomdata.Number(0, 65535)
|
|
|
|
s.Push(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkPop(b *testing.B) {
|
|
|
|
s := New()
|
2019-03-23 19:11:42 +00:00
|
|
|
b.N = 2000000
|
2019-03-13 04:21:30 +00:00
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
v := randomdata.Number(0, 65535)
|
|
|
|
s.Push(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
s.Pop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGodsPop(b *testing.B) {
|
|
|
|
s := arraystack.New()
|
2019-03-23 19:11:42 +00:00
|
|
|
b.N = 2000000
|
2019-03-13 04:21:30 +00:00
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
v := randomdata.Number(0, 65535)
|
|
|
|
s.Push(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
s.Pop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkValues(b *testing.B) {
|
|
|
|
s := New()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
v := randomdata.Number(0, 65535)
|
|
|
|
s.Push(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
s.Values()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGodsValues(b *testing.B) {
|
|
|
|
s := arraystack.New()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
v := randomdata.Number(0, 65535)
|
|
|
|
s.Push(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
s.Values()
|
|
|
|
}
|
|
|
|
}
|