trpc.group/trpc-go/trpc-go@v1.0.3/internal/stack/stack_test.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  package stack_test
    15  
    16  import (
    17  	"testing"
    18  
    19  	"trpc.group/trpc-go/trpc-go/internal/stack"
    20  
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  func TestStack(t *testing.T) {
    25  	st := stack.New[struct{}]()
    26  	st.Push(struct{}{})
    27  	require.Equal(t, 1, st.Size())
    28  
    29  	st.Reset()
    30  	require.Equal(t, 0, st.Size())
    31  
    32  	v, ok := st.Peek()
    33  	require.False(t, ok)
    34  	require.Equal(t, struct{}{}, v)
    35  
    36  	v, ok = st.Pop()
    37  	require.False(t, ok)
    38  	require.Equal(t, struct{}{}, v)
    39  
    40  	{
    41  		type foo struct {
    42  			bar string
    43  		}
    44  
    45  		st := stack.New[foo]()
    46  		st.Push(foo{bar: "baz"})
    47  
    48  		v, ok := st.Peek()
    49  		require.True(t, ok)
    50  		require.Equal(t, foo{bar: "baz"}, v)
    51  
    52  		v, ok = st.Pop()
    53  		require.True(t, ok)
    54  		require.Equal(t, foo{bar: "baz"}, v)
    55  
    56  		require.Zero(t, st.Size())
    57  	}
    58  }