github.com/aergoio/aergo@v1.3.1/state/util_test.go (about)

     1  package state
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestUtilStackBasic(t *testing.T) {
    10  	stk := newStack()
    11  	assert.Equal(t, -1, stk.peek())
    12  	t.Log("stk", stk)
    13  
    14  	stk.push(1)
    15  	assert.Equal(t, 1, stk.peek())
    16  	t.Log("stk", stk)
    17  
    18  	stk.push(2).push(3).push(4)
    19  	stk.push(5)
    20  	t.Log("stk", stk)
    21  
    22  	assert.Equal(t, 5, stk.peek())
    23  	t.Log("stk", stk)
    24  
    25  	assert.Equal(t, 5, stk.pop())
    26  	assert.Equal(t, 4, stk.peek())
    27  	t.Log("stk", stk)
    28  
    29  	stk.push(6)
    30  	t.Log("stk", stk)
    31  
    32  	assert.Equal(t, 6, stk.peek())
    33  	assert.Equal(t, 6, stk.pop())
    34  	assert.Equal(t, 4, stk.pop())
    35  	assert.Equal(t, 3, stk.pop())
    36  	t.Log("stk", stk)
    37  
    38  	assert.Equal(t, 2, stk.peek())
    39  	assert.Equal(t, 2, stk.pop())
    40  	assert.Equal(t, 1, stk.pop())
    41  	t.Log("stk", stk)
    42  
    43  	assert.Equal(t, -1, stk.peek())
    44  	assert.Equal(t, -1, stk.pop())
    45  	assert.Equal(t, -1, stk.pop())
    46  	t.Log("stk", stk)
    47  
    48  	stk.push(7).push(8, 9, 10)
    49  	t.Log("stk", stk)
    50  
    51  	assert.Equal(t, 10, stk.peek())
    52  	assert.Equal(t, 10, stk.pop())
    53  	assert.Equal(t, 9, stk.pop())
    54  	assert.Equal(t, 8, stk.pop())
    55  	t.Log("stk", stk)
    56  }
    57  
    58  func TestUtilStackExport(t *testing.T) {
    59  	stk := newStack()
    60  	stk.push(1, 2, 3, 4, 5)
    61  	t.Log("stk", stk)
    62  
    63  	assert.Equal(t, []int{5, 4, 3, 2, 1}, stk.export())
    64  	t.Log("export", stk.export())
    65  
    66  	assert.Equal(t, []int{1, 2, 3, 4, 5}, []int(*stk))
    67  }
    68  
    69  func TestUtilStackIter(t *testing.T) {
    70  	cmp := newStack().push(1, 2, 3, 4, 5)
    71  	stk := newStack().push(1, 2, 3, 4, 5)
    72  	t.Log("stk", stk)
    73  
    74  	it := stk.iter()
    75  	for v := it(); v >= 0; v = it() {
    76  		assert.Equal(t, cmp.pop(), v)
    77  		t.Log("it", v)
    78  	}
    79  
    80  	it = stk.iter()
    81  	assert.Equal(t, 5, it())
    82  	assert.Equal(t, 4, it())
    83  	assert.Equal(t, 3, it())
    84  	assert.Equal(t, 2, it())
    85  	assert.Equal(t, 1, it())
    86  	assert.Equal(t, -1, it())
    87  	assert.Equal(t, -1, it())
    88  
    89  	assert.Equal(t, 5, stk.peek())
    90  	assert.Equal(t, []int{1, 2, 3, 4, 5}, []int(*stk))
    91  }
    92  
    93  func TestUtilStackGet(t *testing.T) {
    94  	cmp := newStack().push(1, 2, 3, 4, 5)
    95  	stk := newStack().push(1, 2, 3, 4, 5)
    96  	t.Log("stk", stk)
    97  
    98  	for i := stk.last(); i >= 0; i-- {
    99  		assert.Equal(t, cmp.pop(), stk.get(i))
   100  		t.Logf("get(%d) %v", i, stk.get(i))
   101  	}
   102  
   103  	assert.Equal(t, 5, stk.get(4))
   104  	assert.Equal(t, 4, stk.get(3))
   105  	assert.Equal(t, 3, stk.get(2))
   106  	assert.Equal(t, 2, stk.get(1))
   107  	assert.Equal(t, 1, stk.get(0))
   108  	assert.Equal(t, -1, stk.get(-1))
   109  	assert.Equal(t, -1, stk.get(-2))
   110  
   111  	assert.Equal(t, 5, stk.peek())
   112  	assert.Equal(t, []int{1, 2, 3, 4, 5}, []int(*stk))
   113  }