From a34cf66a0d7db9c1ea3e904924cd09bc2fd753f2 Mon Sep 17 00:00:00 2001 From: ashing Date: Wed, 17 Jul 2024 00:38:48 +0800 Subject: [PATCH] test: add coverage Signed-off-by: ashing --- .../2020/0447/447. Number of Boomerangs.go | 4 +-- .../0447/447. Number of Boomerangs_test.go | 8 ++++++ .../225. Implement Stack using Queues.go | 7 ------ .../225. Implement Stack using Queues_test.go | 25 ++++++++++++++++++- 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/leetcode_91_2/2020/0447/447. Number of Boomerangs.go b/leetcode_91_2/2020/0447/447. Number of Boomerangs.go index eb78905..1100c6c 100644 --- a/leetcode_91_2/2020/0447/447. Number of Boomerangs.go +++ b/leetcode_91_2/2020/0447/447. Number of Boomerangs.go @@ -1,7 +1,5 @@ package _447 -import "fmt" - func numberOfBoomerangs(points [][]int) int { res := 0 for i := 0; i < len(points); i++ { @@ -10,7 +8,7 @@ func numberOfBoomerangs(points [][]int) int { if i != j { d := distanceCalc(points[i], points[j]) v, ok := distance[d] - fmt.Printf("d is %v\n", d) + //fmt.Printf("d is %v\n", d) if ok { // 存在 res += 2 * v // (v+1) * v - [v * (v-1)] = 2 * v distance[d] += 1 diff --git a/leetcode_91_2/2020/0447/447. Number of Boomerangs_test.go b/leetcode_91_2/2020/0447/447. Number of Boomerangs_test.go index 72d6da3..8160cd2 100644 --- a/leetcode_91_2/2020/0447/447. Number of Boomerangs_test.go +++ b/leetcode_91_2/2020/0447/447. Number of Boomerangs_test.go @@ -16,4 +16,12 @@ func TestNumberOfBoomerangs(t *testing.T) { }), "must be 2", ) + assert.Equal(t, 2, + numberOfBoomerangs1([][]int{ + {0, 0}, + {1, 0}, + {2, 0}, + }), + "must be 2", + ) } diff --git a/topic/stack_queue/225. Implement Stack using Queues.go b/topic/stack_queue/225. Implement Stack using Queues.go index 0b02546..efe615e 100644 --- a/topic/stack_queue/225. Implement Stack using Queues.go +++ b/topic/stack_queue/225. Implement Stack using Queues.go @@ -5,13 +5,6 @@ type MyStack struct { Queue2 []int } -func stackConstructor() MyStack { - return MyStack{ - Queue1: make([]int, 0), - Queue2: make([]int, 0), - } -} - func (m *MyStack) Push(x int) { if len(m.Queue2) != 0 { m.Queue2 = append(m.Queue2, x) diff --git a/topic/stack_queue/225. Implement Stack using Queues_test.go b/topic/stack_queue/225. Implement Stack using Queues_test.go index e9341f5..d3b4b90 100644 --- a/topic/stack_queue/225. Implement Stack using Queues_test.go +++ b/topic/stack_queue/225. Implement Stack using Queues_test.go @@ -1,6 +1,10 @@ package stack_queue -import "testing" +import ( + "testing" + + "github.com/bmizerany/assert" +) func TestMyStack_Pop(t *testing.T) { type fields struct { @@ -41,3 +45,22 @@ func TestMyStack_Pop(t *testing.T) { }) } } + +func TestMyStack(t *testing.T) { + m := &MyStack{} + + assert.Equal(t, true, m.Empty()) + // Test when Queue2 is empty + m.Push(1) + if len(m.Queue1) != 1 { + t.Errorf("Expected Queue1 length to be 1, got %d", len(m.Queue1)) + } + assert.Equal(t, false, m.Empty()) + + m.Push(2) + + assert.Equal(t, 2, m.Top()) + assert.Equal(t, 2, m.Pop()) + m.Push(3) + assert.Equal(t, 3, m.Top()) +}