package zerojson
import (
"math/rand"
"testing"
"time"
)
func TestStack(t *testing.T) {
seed := time.Now().UnixNano()
rnd := rand.New(rand.NewSource(seed))
// make sure to bleed into dynamic allocation
max := staticStackSize*64 + rnd.Intn(1000) + 1
// generate the slice of values to push and pop
vals := make([]byte, max)
for i := range vals {
if rnd.Intn(2) == 1 {
vals[i] = '{'
} else {
vals[i] = '['
}
}
t.Logf("seed=%d\nmax=%d\nvalues=%s", seed, max, string(vals))
var s stack
// repeat a couple times, so we push after pop
for count := 0; count < 2; count++ {
for _, v := range vals {
s.push(v)
b := s.peek()
if b != v {
t.Fatalf("peek: want %#U, got %#U", v, b)
}
}
for i := len(vals) - 1; i >= 0; i-- {
v := s.pop()
if v != vals[i] {
t.Fatalf("pop at %d: want %#U, got %#U", i, vals[i], v)
}
if i > 0 {
want := vals[i-1]
for j := 0; j < 2; j++ {
got := s.peek()
if want != got {
t.Fatalf("peek at %d: want %#U, got %#U", i, want, got)
}
}
}
}
if s.depth != 0 {
t.Fatalf("depth should be 0, is %d", s.depth)
}
if got := s.peek(); got != 0 {
t.Fatalf("peek when empty should be 0, got %#U", got)
}
}
}