github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/collection/listx/stack_test.go (about) 1 package listx 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 func TestStack(t *testing.T) { 10 stack := NewStack[int]() 11 for i := 0; i < 10; i++ { 12 stack.Push(i) 13 } 14 assert.Equal(t, 10, stack.Len()) 15 16 got := make([]int, 0) 17 for i := 0; i < 10; i++ { 18 x, ok := stack.Pop() 19 got = append(got, x) 20 assert.True(t, ok) 21 if i > 0 { 22 assert.Equal(t, got[i-1]-1, got[i]) 23 } 24 } 25 26 _, ok := stack.Peek() 27 assert.False(t, ok) 28 }