github.com/jackc/puddle/v2@v2.2.2-0.20240301145809-72b022bcfc59/internal/genstack/gen_stack_test.go (about)

     1  package genstack
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  func requirePopEmpty[T any](t testing.TB, s *GenStack[T]) {
    10  	v, ok := s.Pop()
    11  	require.False(t, ok)
    12  	require.Zero(t, v)
    13  }
    14  
    15  func requirePop[T any](t testing.TB, s *GenStack[T], expected T) {
    16  	v, ok := s.Pop()
    17  	require.True(t, ok)
    18  	require.Equal(t, expected, v)
    19  }
    20  
    21  func TestGenStack_Empty(t *testing.T) {
    22  	s := NewGenStack[int]()
    23  	requirePopEmpty(t, s)
    24  }
    25  
    26  func TestGenStack_SingleGen(t *testing.T) {
    27  	r := require.New(t)
    28  	s := NewGenStack[int]()
    29  
    30  	s.Push(1)
    31  	s.Push(2)
    32  	r.Equal(2, s.Len())
    33  
    34  	requirePop(t, s, 2)
    35  	requirePop(t, s, 1)
    36  	requirePopEmpty(t, s)
    37  }
    38  
    39  func TestGenStack_TwoGen(t *testing.T) {
    40  	r := require.New(t)
    41  	s := NewGenStack[int]()
    42  
    43  	s.Push(3)
    44  	s.Push(4)
    45  	s.Push(5)
    46  	r.Equal(3, s.Len())
    47  	s.NextGen()
    48  	r.Equal(3, s.Len())
    49  	s.Push(6)
    50  	s.Push(7)
    51  	r.Equal(5, s.Len())
    52  
    53  	requirePop(t, s, 5)
    54  	requirePop(t, s, 4)
    55  	requirePop(t, s, 3)
    56  	requirePop(t, s, 7)
    57  	requirePop(t, s, 6)
    58  	requirePopEmpty(t, s)
    59  }
    60  
    61  func TestGenStack_MuptiGen(t *testing.T) {
    62  	r := require.New(t)
    63  	s := NewGenStack[int]()
    64  
    65  	s.Push(10)
    66  	s.Push(11)
    67  	s.Push(12)
    68  	r.Equal(3, s.Len())
    69  	s.NextGen()
    70  	r.Equal(3, s.Len())
    71  	s.Push(13)
    72  	s.Push(14)
    73  	r.Equal(5, s.Len())
    74  	s.NextGen()
    75  	r.Equal(5, s.Len())
    76  	s.Push(15)
    77  	s.Push(16)
    78  	s.Push(17)
    79  	r.Equal(8, s.Len())
    80  
    81  	requirePop(t, s, 12)
    82  	requirePop(t, s, 11)
    83  	requirePop(t, s, 10)
    84  	requirePop(t, s, 14)
    85  	requirePop(t, s, 13)
    86  	requirePop(t, s, 17)
    87  	requirePop(t, s, 16)
    88  	requirePop(t, s, 15)
    89  	requirePopEmpty(t, s)
    90  }