github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/stack_test.go (about)

     1  package jzon
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  func TestStack(t *testing.T) {
    10  	t.Run("empty", func(t *testing.T) {
    11  		s := stackPool.Get().(*stack).init()
    12  		require.Equal(t, stackElementNone, s.top())
    13  		require.Equal(t, stackElementNone, s.pop())
    14  	})
    15  	t.Run("array top", func(t *testing.T) {
    16  		s := stackPool.Get().(*stack).initArray()
    17  		require.Equal(t, stackElementArrayBegin, s.top())
    18  		require.Equal(t, stackElementArray, s.pop())
    19  		require.Equal(t, stackElementNone, s.top())
    20  		require.Equal(t, stackElementNone, s.pop())
    21  	})
    22  	t.Run("object top", func(t *testing.T) {
    23  		s := stackPool.Get().(*stack).initObject()
    24  		require.Equal(t, stackElementObjectBegin, s.top())
    25  		require.Equal(t, stackElementObject, s.pop())
    26  		require.Equal(t, stackElementNone, s.top())
    27  		require.Equal(t, stackElementNone, s.pop())
    28  	})
    29  	t.Run("nested 1", func(t *testing.T) {
    30  		s := stackPool.Get().(*stack).init()
    31  		count := 100
    32  		for i := 0; i < count; i++ {
    33  			if i&1 == 0 {
    34  				s.pushObject()
    35  				require.Equal(t, stackElementObjectBegin, s.top())
    36  			} else {
    37  				s.pushArray()
    38  				require.Equal(t, stackElementArrayBegin, s.top())
    39  			}
    40  		}
    41  		for i := count - 1; i >= 0; i-- {
    42  			if i&1 == 0 {
    43  				require.Equal(t, stackElementObjectBegin, s.top())
    44  				require.Equal(t, stackElementObject, s.pop())
    45  			} else {
    46  				require.Equal(t, stackElementArrayBegin, s.top())
    47  				require.Equal(t, stackElementArray, s.pop())
    48  			}
    49  		}
    50  		require.Equal(t, stackElementNone, s.top())
    51  		require.Equal(t, stackElementNone, s.pop())
    52  	})
    53  }
    54  
    55  func TestStack_Init(t *testing.T) {
    56  	t.Run("object", func(t *testing.T) {
    57  		var s stack
    58  		s.initObject()
    59  		require.Equal(t, stackElementObjectBegin, s.top())
    60  		require.Equal(t, stackElementObject, s.pop())
    61  		require.Equal(t, stackElementNone, s.top())
    62  		require.Equal(t, stackElementNone, s.pop())
    63  	})
    64  	t.Run("array", func(t *testing.T) {
    65  		var s stack
    66  		s.initArray()
    67  		require.Equal(t, stackElementArrayBegin, s.top())
    68  		require.Equal(t, stackElementArray, s.pop())
    69  		require.Equal(t, stackElementNone, s.top())
    70  		require.Equal(t, stackElementNone, s.pop())
    71  	})
    72  }