github.com/searKing/golang/go@v1.2.117/container/stack/stack_test.go (about) 1 // Copyright 2020 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package stack 6 7 import ( 8 "reflect" 9 "testing" 10 ) 11 12 func TestStack_Push(t *testing.T) { 13 s := New() 14 var input int = 0 15 ele := s.Push(input) 16 e, ok := ele.Value.(int) 17 if !ok { 18 t.Errorf("type must be %s", reflect.TypeOf(input).String()) 19 } 20 if e != input { 21 t.Errorf("value must be %v", input) 22 } 23 if s.Len() != 1 { 24 t.Errorf("len must be %v", 1) 25 } 26 } 27 28 func TestStack_Pop(t *testing.T) { 29 s := New() 30 inputs := []int{2, 1, 0} 31 for _, input := range inputs { 32 s.Push(input) 33 } 34 for idx := 0; idx < len(inputs); idx++ { 35 ele := s.Pop() 36 e, ok := ele.Value.(int) 37 if !ok { 38 t.Errorf("type must be %s", reflect.TypeOf(inputs[len(inputs)-idx-1]).String()) 39 } 40 if e != idx { 41 t.Errorf("value must be %v", idx) 42 } 43 if s.Len() != len(inputs)-idx-1 { 44 t.Errorf("len is %v must be %v", s.Len(), len(inputs)-idx) 45 } 46 } 47 48 } 49 func TestStack_Peek(t *testing.T) { 50 s := New() 51 var input int = 0 52 s.Push(input) 53 ele := s.Peek() 54 e, ok := ele.Value.(int) 55 if !ok { 56 t.Errorf("return type must be %s", reflect.TypeOf(input).String()) 57 } 58 if e != input { 59 t.Errorf("value must be %v", input) 60 } 61 if s.Len() != 1 { 62 t.Errorf("len must be %v", 1) 63 } 64 }