stack_test.go
1 // Copyright (c) 2024-2026 Tencent Zhuque Lab. All rights reserved. 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 // Requirement: Any integration or derivative work must explicitly attribute 16 // Tencent Zhuque Lab (https://github.com/Tencent/AI-Infra-Guard) in its 17 // documentation or user interface, as detailed in the NOTICE file. 18 19 package parser 20 21 import ( 22 "testing" 23 24 "github.com/stretchr/testify/assert" 25 ) 26 27 // TestStack_IsEmpty 测试空栈判断 28 func TestStack_IsEmpty(t *testing.T) { 29 s := NewStack() 30 // 新建的栈应该是空的 31 assert.True(t, s.isEmpty(), "新建栈应为空") 32 } 33 34 // TestStack_PushAndIsEmpty 测试 push 后栈非空 35 func TestStack_PushAndIsEmpty(t *testing.T) { 36 s := NewStack() 37 s.push(1) 38 // push 之后栈不应该为空 39 assert.False(t, s.isEmpty(), "push 元素后栈不应为空") 40 } 41 42 // TestStack_Top 测试 top 方法返回栈顶元素但不弹出 43 func TestStack_Top(t *testing.T) { 44 s := NewStack() 45 s.push("hello") 46 // top 应该返回栈顶元素 47 assert.Equal(t, "hello", s.top(), "top 应返回栈顶元素 'hello'") 48 // top 之后栈仍然非空 49 assert.False(t, s.isEmpty(), "top 不应弹出元素,栈仍非空") 50 } 51 52 // TestStack_TopOnEmpty 测试空栈 top 返回 nil 53 func TestStack_TopOnEmpty(t *testing.T) { 54 s := NewStack() 55 // 空栈 top 应该返回 nil 56 assert.Nil(t, s.top(), "空栈 top 应返回 nil") 57 } 58 59 // TestStack_Pop 测试 pop 方法弹出栈顶元素 60 func TestStack_Pop(t *testing.T) { 61 s := NewStack() 62 s.push(42) 63 val := s.pop() 64 // pop 应返回之前 push 的值 65 assert.Equal(t, 42, val, "pop 应返回 42") 66 // pop 之后栈应为空 67 assert.True(t, s.isEmpty(), "pop 元素后栈应为空") 68 } 69 70 // TestStack_PopOnEmpty 测试空栈 pop 返回 nil 71 func TestStack_PopOnEmpty(t *testing.T) { 72 s := NewStack() 73 // 空栈 pop 应该返回 nil 74 assert.Nil(t, s.pop(), "空栈 pop 应返回 nil") 75 } 76 77 // TestStack_LIFO 测试栈的后进先出顺序 78 func TestStack_LIFO(t *testing.T) { 79 s := NewStack() 80 // 依次压入三个元素 81 s.push(1) 82 s.push(2) 83 s.push(3) 84 85 // 弹出顺序应为 3、2、1(后进先出) 86 assert.Equal(t, 3, s.pop(), "第一次 pop 应返回最后压入的 3") 87 assert.Equal(t, 2, s.pop(), "第二次 pop 应返回 2") 88 assert.Equal(t, 1, s.pop(), "第三次 pop 应返回最先压入的 1") 89 assert.True(t, s.isEmpty(), "所有元素弹出后栈应为空") 90 } 91 92 // TestStack_MultiplePush 测试多次 push 和 top 93 func TestStack_MultiplePush(t *testing.T) { 94 s := NewStack() 95 s.push("a") 96 s.push("b") 97 s.push("c") 98 99 // top 应返回最后压入的元素 100 assert.Equal(t, "c", s.top(), "top 应返回最后压入的 'c'") 101 // pop 后 top 应变为 "b" 102 s.pop() 103 assert.Equal(t, "b", s.top(), "pop 一次后 top 应为 'b'") 104 } 105 106 // TestStack_MixedTypes 测试压入不同类型的元素 107 func TestStack_MixedTypes(t *testing.T) { 108 s := NewStack() 109 s.push("string") 110 s.push(100) 111 s.push(3.14) 112 113 // 按后进先出顺序验证 114 assert.Equal(t, 3.14, s.pop(), "pop 应返回最后压入的 float64 值 3.14") 115 assert.Equal(t, 100, s.pop(), "pop 应返回 int 值 100") 116 assert.Equal(t, "string", s.pop(), "pop 应返回 string 值 'string'") 117 }