~mna/zerojson

ref: 9815e26416ae791fd399d7f857f99c0e9cb22a3f zerojson/zerojson_test.go -rw-r--r-- 1.2 KiB View raw
                                                                                
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
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)
		}
	}
}