Skip to content

Commit 85bb5a3

Browse files
committed
add some tests and go fmt
1 parent 29d7574 commit 85bb5a3

File tree

12 files changed

+152
-11
lines changed

12 files changed

+152
-11
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test:
2+
go test ./...

leetcode/3sum.go renamed to leetcode/3sum/3sum.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// https://leetcode.com/problems/3sum/
2-
package main
2+
package _sum
33

44
import (
55
"sort"

leetcode/3sum/3sum_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package _sum
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func Test_threeSum(t *testing.T) {
9+
cases := []struct {
10+
params []int
11+
exp [][]int
12+
}{
13+
{
14+
[]int{-1, 0, 1, 2, -1, -4}, [][]int{{-1, -1, 2}, {-1, 0, 1}},
15+
},
16+
{
17+
[]int{-1, 0, 1, 2, -1, -4}, [][]int{{-1, -1, 2}, {-1, 0, 1}},
18+
},
19+
{
20+
[]int{-1, 0, 1, 2, -1, -4}, [][]int{{-1, -1, 2}, {-1, 0, 1}},
21+
},
22+
}
23+
24+
for idx, tc := range cases {
25+
out := threeSum(tc.params)
26+
if !reflect.DeepEqual(out, tc.exp) {
27+
t.Errorf("case %d error: got %v, expected %v", idx, out, tc.exp)
28+
}
29+
}
30+
}

leetcode/accounts_merge.go renamed to leetcode/accounts_merge/accounts_merge.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// https://leetcode.com/problems/accounts-merge/
2-
package main
2+
package accounts_merge
33

44
import (
55
"sort"
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package accounts_merge
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func Test_accountsMerge(t *testing.T) {
9+
cases := []struct {
10+
params [][]string
11+
exp [][]string
12+
}{
13+
{
14+
[][]string{{"John", "johnsmith@mail.com", "john_newyork@mail.com"}, {"John", "johnsmith@mail.com", "john00@mail.com"}, {"Mary", "mary@mail.com"}, {"John", "johnnybravo@mail.com"}},
15+
[][]string{{"John", "john00@mail.com", "john_newyork@mail.com", "johnsmith@mail.com"}, {"Mary", "mary@mail.com"}, {"John", "johnnybravo@mail.com"}},
16+
},
17+
{
18+
[][]string{{"Gabe", "Gabe0@m.co", "Gabe3@m.co", "Gabe1@m.co"}, {"Kevin", "Kevin3@m.co", "Kevin5@m.co", "Kevin0@m.co"}, {"Ethan", "Ethan5@m.co", "Ethan4@m.co", "Ethan0@m.co"}, {"Hanzo", "Hanzo3@m.co", "Hanzo1@m.co", "Hanzo0@m.co"}, {"Fern", "Fern5@m.co", "Fern1@m.co", "Fern0@m.co"}},
19+
[][]string{{"Ethan", "Ethan0@m.co", "Ethan4@m.co", "Ethan5@m.co"}, {"Gabe", "Gabe0@m.co", "Gabe1@m.co", "Gabe3@m.co"}, {"Hanzo", "Hanzo0@m.co", "Hanzo1@m.co", "Hanzo3@m.co"}, {"Kevin", "Kevin0@m.co", "Kevin3@m.co", "Kevin5@m.co"}, {"Fern", "Fern0@m.co", "Fern1@m.co", "Fern5@m.co"}},
20+
},
21+
}
22+
23+
for idx, tc := range cases {
24+
out := accountsMerge(tc.params)
25+
if !reflect.DeepEqual(out, tc.exp) {
26+
t.Errorf("case %d error: \n-- got: %v\n-- expected: %v", idx, out, tc.exp)
27+
}
28+
}
29+
}

leetcode/add_and_search_word_data_structure_design.go renamed to leetcode/add_and_search_word_data_structure_design/add_and_search_word_data_structure_design.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// https://leetcode.com/problems/add-and-search-word-data-structure-design/
2-
package main
2+
package add_and_search_word_data_structure_design
33

44
type WordDictionary struct {
55
root *Node
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package add_and_search_word_data_structure_design
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestWordDictionary(t *testing.T) {
8+
wd := Constructor()
9+
wd.AddWord("bad")
10+
wd.AddWord("dad")
11+
wd.AddWord("mad")
12+
assert(t, wd.Search("pad") == false)
13+
assert(t, wd.Search("bad"))
14+
assert(t, wd.Search(".ad"))
15+
assert(t, wd.Search("b.."))
16+
}
17+
18+
func assert(t *testing.T, res bool) {
19+
if !res {
20+
t.Errorf("assert fails")
21+
}
22+
}

leetcode/add_binary.go renamed to leetcode/add_binary/add_binary.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// https://leetcode.com/problems/add-binary/
2-
package main
2+
package add_binary
33

44
func addBinary(a string, b string) string {
55
aIdxShift := 0
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package add_binary
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func Test_addBinary(t *testing.T) {
9+
cases := []struct {
10+
params []string
11+
exp string
12+
}{
13+
{
14+
[]string{"11", "1"},
15+
"100",
16+
},
17+
{
18+
[]string{"1010", "1011"},
19+
"10101",
20+
},
21+
}
22+
23+
for idx, tc := range cases {
24+
out := addBinary(tc.params[0], tc.params[1])
25+
if !reflect.DeepEqual(out, tc.exp) {
26+
t.Errorf("case %d error: got: %v, expected: %v", idx, out, tc.exp)
27+
}
28+
}
29+
}

leetcode/add_strings.go renamed to leetcode/add_strings/add_strings.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// https://leetcode.com/problems/add-strings/
2-
package main
2+
package add_strings
33

44
func addStrings(num1 string, num2 string) string {
55
total := make([]byte, max(len(num1), len(num2))+1)

0 commit comments

Comments
 (0)