github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/internal/list2/stack_test.go (about)

     1  // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package list2
    16  
    17  import "testing"
    18  
    19  func TestStack(t *testing.T) {
    20  	stack := NewStack()
    21  	stack.Push(1)
    22  	stack.Push(2)
    23  	stack.Push(3)
    24  	stack.Push(4)
    25  
    26  	length := stack.Len()
    27  	if length != 4 {
    28  		t.Errorf("stack.Len() failed. Got %d, expected 4.", length)
    29  	}
    30  
    31  	value := stack.Peak().(int)
    32  	if value != 4 {
    33  		t.Errorf("stack.Peak() failed. Got %d, expected 4.", value)
    34  	}
    35  
    36  	value = stack.Pop().(int)
    37  	if value != 4 {
    38  		t.Errorf("stack.Pop() failed. Got %d, expected 4.", value)
    39  	}
    40  
    41  	length = stack.Len()
    42  	if length != 3 {
    43  		t.Errorf("stack.Len() failed. Got %d, expected 3.", length)
    44  	}
    45  
    46  	value = stack.Peak().(int)
    47  	if value != 3 {
    48  		t.Errorf("stack.Peak() failed. Got %d, expected 3.", value)
    49  	}
    50  
    51  	value = stack.Pop().(int)
    52  	if value != 3 {
    53  		t.Errorf("stack.Pop() failed. Got %d, expected 3.", value)
    54  	}
    55  
    56  	value = stack.Pop().(int)
    57  	if value != 2 {
    58  		t.Errorf("stack.Pop() failed. Got %d, expected 2.", value)
    59  	}
    60  
    61  	empty := stack.Empty()
    62  	if empty {
    63  		t.Errorf("stack.Empty() failed. Got %v, expected false.", empty)
    64  	}
    65  
    66  	value = stack.Pop().(int)
    67  	if value != 1 {
    68  		t.Errorf("stack.Pop() failed. Got %d, expected 1.", value)
    69  	}
    70  
    71  	empty = stack.Empty()
    72  	if !empty {
    73  		t.Errorf("stack.Empty() failed. Got %v, expected true.", empty)
    74  	}
    75  
    76  	nilValue := stack.Peak()
    77  	if nilValue != nil {
    78  		t.Errorf("stack.Peak() failed. Got %d, expected nil.", nilValue)
    79  	}
    80  
    81  	nilValue = stack.Pop()
    82  	if nilValue != nil {
    83  		t.Errorf("stack.Pop() failed. Got %d, expected nil.", nilValue)
    84  	}
    85  
    86  	length = stack.Len()
    87  	if length != 0 {
    88  		t.Errorf("stack.Len() failed. Got %d, expected 0.", length)
    89  	}
    90  }