From d32f15feb14a19e2f1fc0c125795ef87e8cdfc6a Mon Sep 17 00:00:00 2001 From: reanon <793584285@qq.com> Date: Tue, 29 Nov 2022 01:17:07 +0800 Subject: [PATCH] fix(testcase): modify testcase --- .../go/chapter_stack_and_queue/stack_test.go | 78 +++++++++---------- 1 file changed, 37 insertions(+), 41 deletions(-) diff --git a/codes/go/chapter_stack_and_queue/stack_test.go b/codes/go/chapter_stack_and_queue/stack_test.go index b3cd3da..b546d55 100644 --- a/codes/go/chapter_stack_and_queue/stack_test.go +++ b/codes/go/chapter_stack_and_queue/stack_test.go @@ -6,35 +6,6 @@ package chapter_stack_and_queue 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) { // 初始化栈 stack := NewArrayStack() @@ -65,19 +36,32 @@ func TestArrayStack(t *testing.T) { } func TestLinkedListStack(t *testing.T) { - -} - -// BenchmarkStack 65.02 ns/op in Mac M1 Pro -func BenchmarkStack(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() - } + + // 元素入栈 + 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) } // BenchmarkArrayStack 8 ns/op in Mac M1 Pro @@ -91,3 +75,15 @@ func BenchmarkArrayStack(b *testing.B) { 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() + } +}