Skip to content

Commit fc84ea1

Browse files
committed
add tests
1 parent 428b7c6 commit fc84ea1

File tree

17 files changed

+332
-17
lines changed

17 files changed

+332
-17
lines changed

src/power-of-two/solution_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import "testing"
4+
5+
func Test_isPowerOfTwo(t *testing.T) {
6+
type args struct {
7+
n int
8+
}
9+
tests := []struct {
10+
name string
11+
args args
12+
want bool
13+
}{
14+
{
15+
"1",
16+
args{
17+
8,
18+
},
19+
true,
20+
},
21+
}
22+
for _, tt := range tests {
23+
t.Run(tt.name, func(t *testing.T) {
24+
if got := isPowerOfTwo(tt.args.n); got != tt.want {
25+
t.Errorf("isPowerOfTwo() = %v, want %v", got, tt.want)
26+
}
27+
})
28+
}
29+
}

src/powx-n/solution.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ func myPow(x float64, n int) float64 {
1616
}
1717

1818
func main() {
19-
x := 2.1
19+
x := 2.0
2020
fmt.Println(myPow(x, 3))
2121
}

src/powx-n/solution_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import "testing"
4+
5+
func Test_myPow(t *testing.T) {
6+
type args struct {
7+
x float64
8+
n int
9+
}
10+
tests := []struct {
11+
name string
12+
args args
13+
want float64
14+
}{
15+
{
16+
"1",
17+
args{2.0, 3},
18+
8,
19+
},
20+
}
21+
for _, tt := range tests {
22+
t.Run(tt.name, func(t *testing.T) {
23+
if got := myPow(tt.args.x, tt.args.n); got != tt.want {
24+
t.Errorf("myPow() = %v, want %v", got, tt.want)
25+
}
26+
})
27+
}
28+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestNumArray_SumRange(t *testing.T) {
8+
type fields struct {
9+
sums []int
10+
}
11+
type args struct {
12+
i int
13+
j int
14+
}
15+
tests := []struct {
16+
name string
17+
fields fields
18+
args args
19+
want int
20+
}{
21+
{
22+
"1",
23+
fields{
24+
[]int{-2, 0, 3, -5, 2, -1},
25+
},
26+
args{
27+
2, 5,
28+
},
29+
-1,
30+
},
31+
}
32+
for _, tt := range tests {
33+
t.Run(tt.name, func(t *testing.T) {
34+
na := Constructor(tt.fields.sums)
35+
if got := na.SumRange(tt.args.i, tt.args.j); got != tt.want {
36+
t.Errorf("NumArray.SumRange() = %v, want %v", got, tt.want)
37+
}
38+
})
39+
}
40+
}

src/remove-duplicates-from-sorted-array/solution.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@ package main
22

33
import "fmt"
44

5-
func main() {
6-
nums := []int{1, 2, 3, 4, 4, 5}
7-
fmt.Println(removeDuplicates(nums))
8-
}
9-
105
func removeDuplicates(nums []int) int {
116
if len(nums) == 0 {
127
return 0
@@ -20,3 +15,8 @@ func removeDuplicates(nums []int) int {
2015
}
2116
return i + 1
2217
}
18+
19+
func main() {
20+
nums := []int{1, 2, 3, 4, 4, 5}
21+
fmt.Println(removeDuplicates(nums))
22+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import "testing"
4+
5+
func Test_removeDuplicates(t *testing.T) {
6+
type args struct {
7+
nums []int
8+
}
9+
tests := []struct {
10+
name string
11+
args args
12+
want int
13+
}{
14+
{
15+
"1",
16+
args{
17+
[]int{1, 2, 3, 4, 4, 5},
18+
},
19+
5,
20+
},
21+
}
22+
for _, tt := range tests {
23+
t.Run(tt.name, func(t *testing.T) {
24+
if got := removeDuplicates(tt.args.nums); got != tt.want {
25+
t.Errorf("removeDuplicates() = %v, want %v", got, tt.want)
26+
}
27+
})
28+
}
29+
}

src/remove-element/solution.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@ package main
22

33
import "fmt"
44

5-
func main() {
6-
nums := []int{3, 2, 2, 3}
7-
val := 3
8-
fmt.Println(removeElement(nums, val))
9-
}
10-
115
func removeElement(nums []int, val int) int {
126
i := 0
137
for j := range nums {
@@ -18,3 +12,9 @@ func removeElement(nums []int, val int) int {
1812
}
1913
return i
2014
}
15+
16+
func main() {
17+
nums := []int{3, 2, 2, 3}
18+
val := 3
19+
fmt.Println(removeElement(nums, val))
20+
}

src/remove-element/solution_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import "testing"
4+
5+
func Test_removeElement(t *testing.T) {
6+
type args struct {
7+
nums []int
8+
val int
9+
}
10+
tests := []struct {
11+
name string
12+
args args
13+
want int
14+
}{
15+
{
16+
"1",
17+
args{
18+
[]int{3, 2, 2, 3},
19+
3,
20+
},
21+
2,
22+
},
23+
}
24+
for _, tt := range tests {
25+
t.Run(tt.name, func(t *testing.T) {
26+
if got := removeElement(tt.args.nums, tt.args.val); got != tt.want {
27+
t.Errorf("removeElement() = %v, want %v", got, tt.want)
28+
}
29+
})
30+
}
31+
}

src/remove-nth-node-from-end-of-list/solution.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,8 @@ func removeNthFromEnd(head *ListNode, n int) *ListNode {
3333

3434
func main() {
3535
head := &ListNode{1, &ListNode{2, &ListNode{3, &ListNode{4, &ListNode{5, nil}}}}}
36-
fmt.Println(removeNthFromEnd(head, 2))
36+
removeNthFromEnd(head, 2)
37+
for root := head; root != nil; root = root.Next {
38+
fmt.Println(root.Val)
39+
}
3740
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func Test_removeNthFromEnd(t *testing.T) {
9+
type args struct {
10+
head *ListNode
11+
n int
12+
}
13+
tests := []struct {
14+
name string
15+
args args
16+
want *ListNode
17+
}{
18+
{
19+
"1",
20+
args{
21+
&ListNode{1, &ListNode{2, &ListNode{3, &ListNode{4, &ListNode{5, nil}}}}},
22+
2,
23+
},
24+
&ListNode{1, &ListNode{2, &ListNode{3, &ListNode{5, nil}}}},
25+
},
26+
}
27+
for _, tt := range tests {
28+
t.Run(tt.name, func(t *testing.T) {
29+
if got := removeNthFromEnd(tt.args.head, tt.args.n); !reflect.DeepEqual(got, tt.want) {
30+
t.Errorf("removeNthFromEnd() = %v, want %v", got, tt.want)
31+
}
32+
})
33+
}
34+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import "testing"
4+
5+
func Test_removeOuterParentheses(t *testing.T) {
6+
type args struct {
7+
S string
8+
}
9+
tests := []struct {
10+
name string
11+
args args
12+
want string
13+
}{
14+
{
15+
"1",
16+
args{"(()())(())(()(()))"},
17+
"()()()()(())",
18+
},
19+
}
20+
for _, tt := range tests {
21+
t.Run(tt.name, func(t *testing.T) {
22+
if got := removeOuterParentheses(tt.args.S); got != tt.want {
23+
t.Errorf("removeOuterParentheses() = %v, want %v", got, tt.want)
24+
}
25+
})
26+
}
27+
}

src/reverse-integer/solution.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func reverse(x int) int {
2323

2424
func main() {
2525
result := reverse(-210)
26-
fmt.Println(reverse(result))
26+
fmt.Println(result)
2727
fmt.Println(2<<31 - 1)
2828
fmt.Println(2147483651)
2929
}

src/reverse-integer/solution_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import "testing"
4+
5+
func Test_reverse(t *testing.T) {
6+
type args struct {
7+
x int
8+
}
9+
tests := []struct {
10+
name string
11+
args args
12+
want int
13+
}{
14+
{
15+
"1",
16+
args{
17+
-210,
18+
},
19+
-12,
20+
},
21+
}
22+
for _, tt := range tests {
23+
t.Run(tt.name, func(t *testing.T) {
24+
if got := reverse(tt.args.x); got != tt.want {
25+
t.Errorf("reverse() = %v, want %v", got, tt.want)
26+
}
27+
})
28+
}
29+
}

src/reverse-linked-list/solution.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ func reverseList(head *ListNode) *ListNode {
2121
}
2222

2323
func main() {
24-
head := ListNode{1, &ListNode{2, &ListNode{3, &ListNode{4, &ListNode{5, nil}}}}}
25-
fmt.Println(reverseList(&head))
24+
head := &ListNode{1, &ListNode{2, &ListNode{3, &ListNode{4, &ListNode{5, nil}}}}}
25+
fmt.Println(reverseList(head))
2626
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func Test_reverseList(t *testing.T) {
9+
type args struct {
10+
head *ListNode
11+
}
12+
tests := []struct {
13+
name string
14+
args args
15+
want *ListNode
16+
}{
17+
{
18+
"1",
19+
args{
20+
&ListNode{1, &ListNode{2, &ListNode{3, &ListNode{4, &ListNode{5, nil}}}}},
21+
},
22+
&ListNode{5, &ListNode{4, &ListNode{3, &ListNode{2, &ListNode{1, nil}}}}},
23+
},
24+
}
25+
for _, tt := range tests {
26+
t.Run(tt.name, func(t *testing.T) {
27+
if got := reverseList(tt.args.head); !reflect.DeepEqual(got, tt.want) {
28+
t.Errorf("reverseList() = %v, want %v", got, tt.want)
29+
}
30+
})
31+
}
32+
}

src/reverse-string/solution.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ func reverseString(s []byte) {
1111
func main() {
1212
s := []byte{'a', 'b', 'd', 'e'}
1313
reverseString(s)
14-
fmt.Println(s)
14+
fmt.Printf("%#v", s)
1515
}

0 commit comments

Comments
 (0)