Skip to content

Commit b6fbff4

Browse files
committed
add tests
1 parent 877bdd9 commit b6fbff4

File tree

39 files changed

+879
-85
lines changed

39 files changed

+879
-85
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
language: go
22

33
go:
4-
- 1.11.x
54
- 1.12.x
65

76
script:

src/same-tree/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_isSameTree(t *testing.T) {
6+
type args struct {
7+
p *TreeNode
8+
q *TreeNode
9+
}
10+
tests := []struct {
11+
name string
12+
args args
13+
want bool
14+
}{
15+
{
16+
"1",
17+
args{
18+
&TreeNode{1, nil, nil},
19+
&TreeNode{1, nil, nil},
20+
},
21+
true,
22+
},
23+
}
24+
for _, tt := range tests {
25+
t.Run(tt.name, func(t *testing.T) {
26+
if got := isSameTree(tt.args.p, tt.args.q); got != tt.want {
27+
t.Errorf("isSameTree() = %v, want %v", got, tt.want)
28+
}
29+
})
30+
}
31+
}
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_search(t *testing.T) {
6+
type args struct {
7+
nums []int
8+
target int
9+
}
10+
tests := []struct {
11+
name string
12+
args args
13+
want int
14+
}{
15+
{
16+
"1",
17+
args{[]int{4, 5, 6, 7, 0, 1, 2}, 1},
18+
5,
19+
},
20+
}
21+
for _, tt := range tests {
22+
t.Run(tt.name, func(t *testing.T) {
23+
if got := search(tt.args.nums, tt.args.target); got != tt.want {
24+
t.Errorf("search() = %v, want %v", got, tt.want)
25+
}
26+
})
27+
}
28+
}

src/search-insert-position/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{1, 3, 5, 6}
7-
target := 5
8-
fmt.Println(searchInsert(nums, target))
9-
}
10-
115
func searchInsert(nums []int, target int) int {
126
if len(nums) == 0 {
137
return -1
@@ -46,3 +40,9 @@ func searchInsert(nums []int, target int) int {
4640

4741
return start + 1
4842
}
43+
44+
func main() {
45+
nums := []int{1, 3, 5, 6}
46+
target := 5
47+
fmt.Println(searchInsert(nums, target))
48+
}
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_searchInsert(t *testing.T) {
6+
type args struct {
7+
nums []int
8+
target int
9+
}
10+
tests := []struct {
11+
name string
12+
args args
13+
want int
14+
}{
15+
{
16+
"1",
17+
args{[]int{1, 3, 5, 6}, 5},
18+
2,
19+
},
20+
}
21+
for _, tt := range tests {
22+
t.Run(tt.name, func(t *testing.T) {
23+
if got := searchInsert(tt.args.nums, tt.args.target); got != tt.want {
24+
t.Errorf("searchInsert() = %v, want %v", got, tt.want)
25+
}
26+
})
27+
}
28+
}

src/single-number-ii/solution_test.go

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_singleNumber(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{[]int{0, 1, 0, 1, 0, 1, 99}},
17+
99,
18+
},
19+
}
20+
for _, tt := range tests {
21+
t.Run(tt.name, func(t *testing.T) {
22+
if got := singleNumber(tt.args.nums); got != tt.want {
23+
t.Errorf("singleNumber() = %v, want %v", got, tt.want)
24+
}
25+
})
26+
}
27+
}

src/single-number/solution_test.go

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_singleNumber(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{[]int{4, 1, 2, 1, 2}},
17+
4,
18+
},
19+
}
20+
for _, tt := range tests {
21+
t.Run(tt.name, func(t *testing.T) {
22+
if got := singleNumber(tt.args.nums); got != tt.want {
23+
t.Errorf("singleNumber() = %v, want %v", got, tt.want)
24+
}
25+
})
26+
}
27+
}

src/sort-array-by-parity/solution.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ func sortArrayByParity(A []int) []int {
1919
}
2020

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

src/sort-characters-by-frequency/solution.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func frequencySort(s string) string {
2828
freq[s[i]]++
2929
}
3030

31-
// strs := [ttt ee r]
31+
//strs := []string{"ttt", "ee", "r"}
3232
var strs []string
3333
for i := range freq {
3434
if freq[i] == 0 {
@@ -54,5 +54,5 @@ func frequencySort(s string) string {
5454
}
5555

5656
func main() {
57-
fmt.Println(frequencySort("tttree"))
57+
fmt.Printf("%#v", frequencySort("tttree"))
5858
}
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_frequencySort(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{"tttree"},
17+
"ttteer",
18+
},
19+
}
20+
for _, tt := range tests {
21+
t.Run(tt.name, func(t *testing.T) {
22+
if got := frequencySort(tt.args.s); got != tt.want {
23+
t.Errorf("frequencySort() = %v, want %v", got, tt.want)
24+
}
25+
})
26+
}
27+
}

src/sqrtx/solution_test.go

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_mySqrt(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{9},
17+
3,
18+
},
19+
}
20+
for _, tt := range tests {
21+
t.Run(tt.name, func(t *testing.T) {
22+
if got := mySqrt(tt.args.x); got != tt.want {
23+
t.Errorf("mySqrt() = %v, want %v", got, tt.want)
24+
}
25+
})
26+
}
27+
}

src/subsets/solution.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ func generateSubsets(nums []int, idx int, curr *[]int, result *[][]int) {
2424

2525
func main() {
2626
nums := []int{1, 2, 3}
27-
fmt.Println(subsets(nums))
27+
fmt.Printf("%#v", subsets(nums))
2828
}

src/subsets/solution_test.go

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

src/sudoku-solver/solution.go

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)