fix(testcase): modify testcase

This commit is contained in:
reanon 2022-11-29 01:17:07 +08:00
parent bed8b07b0b
commit d32f15feb1

View File

@ -6,35 +6,6 @@ package chapter_stack_and_queue
import "testing" import "testing"
func TestStack(t *testing.T) {
// 初始化栈
stack := NewLinkedListStack()
// 元素入栈
stack.Push(1)
stack.Push(2)
stack.Push(3)
stack.Push(4)
stack.Push(5)
t.Log("栈 stack = ", stack.toString())
// 访问栈顶元素
peek := stack.Peek()
t.Log("栈顶元素 peek = ", peek)
// 元素出栈
pop := stack.Pop()
t.Log("出栈元素 pop = ", pop, ", 出栈后 stack =", stack.toString())
// 获取栈的长度
size := stack.Size()
t.Log("栈的长度 size = ", size)
// 判断是否为空
isEmpty := stack.IsEmpty()
t.Log("栈是否为空 = ", isEmpty)
}
func TestArrayStack(t *testing.T) { func TestArrayStack(t *testing.T) {
// 初始化栈 // 初始化栈
stack := NewArrayStack() stack := NewArrayStack()
@ -65,19 +36,32 @@ func TestArrayStack(t *testing.T) {
} }
func TestLinkedListStack(t *testing.T) { func TestLinkedListStack(t *testing.T) {
// 初始化栈
}
// BenchmarkStack 65.02 ns/op in Mac M1 Pro
func BenchmarkStack(b *testing.B) {
stack := NewLinkedListStack() stack := NewLinkedListStack()
// use b.N for looping
for i := 0; i < b.N; i++ { // 元素入栈
stack.Push(777) stack.Push(1)
} stack.Push(2)
for i := 0; i < b.N; i++ { stack.Push(3)
stack.Pop() stack.Push(4)
} stack.Push(5)
t.Log("栈 stack = ", stack.toString())
// 访问栈顶元素
peek := stack.Peek()
t.Log("栈顶元素 peek = ", peek)
// 元素出栈
pop := stack.Pop()
t.Log("出栈元素 pop = ", pop, ", 出栈后 stack =", stack.toString())
// 获取栈的长度
size := stack.Size()
t.Log("栈的长度 size = ", size)
// 判断是否为空
isEmpty := stack.IsEmpty()
t.Log("栈是否为空 = ", isEmpty)
} }
// BenchmarkArrayStack 8 ns/op in Mac M1 Pro // BenchmarkArrayStack 8 ns/op in Mac M1 Pro
@ -91,3 +75,15 @@ func BenchmarkArrayStack(b *testing.B) {
stack.Pop() stack.Pop()
} }
} }
// BenchmarkLinkedListStack 65.02 ns/op in Mac M1 Pro
func BenchmarkLinkedListStack(b *testing.B) {
stack := NewLinkedListStack()
// use b.N for looping
for i := 0; i < b.N; i++ {
stack.Push(777)
}
for i := 0; i < b.N; i++ {
stack.Pop()
}
}