Skip to content

Commit b54053f

Browse files
committed
refactor: test cases and add golangci
1 parent e1051df commit b54053f

35 files changed

+902
-246
lines changed

.golangci.yaml

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
linters-settings:
2+
gosimple:
3+
checks: [ "all" ]
4+
govet:
5+
check-shadowing: true
6+
enable-all: true
7+
disable:
8+
- fieldalignment
9+
staticcheck:
10+
checks: [ "all" ]
11+
revive:
12+
ignore-generated-header: true
13+
enable-all-rules: true
14+
rules:
15+
- name: add-constant
16+
disabled: false
17+
arguments:
18+
- maxLitCount: "3"
19+
allowInts: "0,1"
20+
- name: argument-limit
21+
arguments: [ 4 ]
22+
- name: banned-characters
23+
severity: warning
24+
disabled: false
25+
arguments: ["Ω", "Σ", "σ", "ω"]
26+
- name: cognitive-complexity
27+
arguments: [ 9 ]
28+
- name: context-as-argument
29+
arguments:
30+
- allowTypesBefore: "*testing.T"
31+
- name: cyclomatic
32+
arguments: [ 7 ]
33+
- name: defer
34+
arguments:
35+
- [ "call-chain", "loop" ]
36+
- name: file-header
37+
disabled: true
38+
- name: function-result-limit
39+
arguments: [ 3 ] #3 TODO
40+
- name: flag-parameter
41+
disabled: true #4 TODO
42+
- name: function-length
43+
disabled: false
44+
arguments: [ 10, 20 ]
45+
- name: line-length-limit
46+
arguments: [ 120 ]
47+
- name: max-public-structs
48+
arguments: [ 3 ]
49+
- name: string-format
50+
arguments:
51+
- - 'core.WriteError[1].Message'
52+
- '/^([^A-Z]|$)/'
53+
- must not start with a capital letter
54+
- - 'fmt.Errorf[0]'
55+
- '/(^|[^\.!?])$/'
56+
- must not end in punctuation
57+
- - panic
58+
- '/^[^\n]*$/'
59+
- must not contain line breaks
60+
- name: unhandled-error
61+
arguments:
62+
- "fmt.Printf"
63+
stylecheck:
64+
checks: [ "all" ]
65+
gosec:
66+
exclude-generated: true
67+
severity: high
68+
concurrency: 3
69+
gocritic:
70+
enabled-checks:
71+
- hugeParam
72+
- tooManyResultsChecker
73+
- rangeValCopy
74+
- rangeExprCopy
75+
- nestingReduce
76+
- unnamedResult
77+
- truncateCmp
78+
- ruleguard
79+
- elseif
80+
- captLocal
81+
- underef
82+
settings:
83+
captLocal:
84+
paramsOnly: false
85+
elseif:
86+
skipBalanced: false
87+
hugeParam:
88+
sizeThreshold: 256
89+
truncateCmp:
90+
skipArchDependent: false
91+
underef:
92+
skipRecvDeref: false
93+
unnamedResult:
94+
checkExported: true
95+
linters:
96+
disable-all: true
97+
enable:
98+
- errcheck
99+
- gosimple
100+
- govet
101+
- ineffassign
102+
- staticcheck
103+
- unused
104+
- revive
105+
- stylecheck
106+
- gosec
107+
- gocritic
108+
issues:
109+
# See the dedicated "issues" documentation section.
110+
exclude-rules:
111+
- path: _test\.go
112+
text: "add-constant:"
113+
- path: _test\.go
114+
text: "line-length-limit:"
115+
- path: _test\.go
116+
text: "function-length: maximum number of lines"
117+
- path: _test\.go
118+
text: "function-length: maximum number of statements"
119+
- path: '(.*)\/index\.go'
120+
text: "function-length: maximum number of statements"

.vscode/settings.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"go.lintTool": "golangci-lint",
3+
"go.lintFlags": [
4+
"--fast"
5+
]
6+
}

problems/easy/problem001/problem001_test.go

+21-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,35 @@
11
package problem001
22

33
import (
4+
"fmt"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
78
)
89

10+
type TestCase struct {
11+
nums []int
12+
target int
13+
out []int
14+
}
15+
916
func TestTwoSum(t *testing.T) {
1017
a := assert.New(t)
1118

12-
a.ElementsMatch([]int{0, 1}, twoSum([]int{2, 7, 11, 15}, 9))
13-
a.ElementsMatch([]int{1, 2}, twoSum([]int{3, 2, 4}, 6))
14-
a.ElementsMatch([]int{0, 1}, twoSum([]int{3, 3}, 6))
15-
a.ElementsMatch([]int{1, 2}, twoSum([]int{1, 2, 3, 4}, 5))
16-
a.ElementsMatch([]int{2, 4}, twoSum([]int{1, 1, 2, 3, 4}, 6))
17-
a.ElementsMatch([]int{1, 2}, twoSum([]int{-1, -2, -3, -4}, -5))
18-
a.ElementsMatch([]int{0, 1}, twoSum([]int{0, 0, 0}, 0))
19-
a.ElementsMatch([]int{}, twoSum([]int{1, 2, 3, 4}, 10))
19+
testCases := []TestCase{
20+
{nums: []int{2, 7, 11, 15}, target: 9, out: []int{0, 1}},
21+
{nums: []int{3, 2, 4}, target: 6, out: []int{1, 2}},
22+
{nums: []int{3, 3}, target: 6, out: []int{0, 1}},
23+
{nums: []int{1, 2, 3, 4}, target: 5, out: []int{1, 2}},
24+
{nums: []int{1, 1, 2, 3, 4}, target: 6, out: []int{2, 4}},
25+
{nums: []int{-1, -2, -3, -4}, target: -5, out: []int{1, 2}},
26+
{nums: []int{0, 0, 0}, target: 0, out: []int{0, 1}},
27+
{nums: []int{1, 2, 3, 4}, target: 10, out: []int{}},
28+
}
29+
30+
for key, tc := range testCases {
31+
a.ElementsMatch(tc.out, twoSum(tc.nums, tc.target), fmt.Sprintf("TestTwoSum number # %d", key+1))
32+
}
2033
}
2134

2235
func BenchmarkTwoSum(b *testing.B) {

problems/easy/problem009/problem009_test.go

+21-9
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,35 @@
11
package problem009
22

33
import (
4+
"fmt"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
78
)
89

10+
type TestCase struct {
11+
in int
12+
out bool
13+
}
14+
915
func TestIsPalindrome(t *testing.T) {
1016
a := assert.New(t)
1117

12-
a.Equal(true, isPalindrome(121))
13-
a.Equal(false, isPalindrome(10))
14-
a.Equal(false, isPalindrome(-123))
15-
a.Equal(true, isPalindrome(5))
16-
a.Equal(true, isPalindrome(1221), "even number of digits")
17-
a.Equal(false, isPalindrome(1231), "not a palindrome")
18-
a.Equal(true, isPalindrome(1), "single digit")
19-
a.Equal(false, isPalindrome(-1), "negative single digit")
20-
a.Equal(true, isPalindrome(0), "zero")
18+
testCases := []TestCase{
19+
{in: 121, out: true},
20+
{in: 10, out: false},
21+
{in: -123, out: false},
22+
{in: 5, out: true},
23+
{in: 1221, out: true},
24+
{in: 1231, out: false},
25+
{in: 1, out: true},
26+
{in: -1, out: false},
27+
{in: 0, out: true},
28+
}
29+
30+
for key, tc := range testCases {
31+
a.Equal(tc.out, isPalindrome(tc.in), fmt.Sprintf("TestIsPalindrome number # %d", key+1))
32+
}
2133
}
2234

2335
func BenchmarkIsPalindrome(b *testing.B) {

problems/easy/problem013/problem013.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package problem013
22

3+
const multiplier = 2
4+
35
var symbols = map[byte]int{'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
46

57
func romanToInt(s string) int {
68
num := 0
79

810
for i := 0; i < len(s); i++ {
911
if i > 0 && symbols[s[i]] > symbols[s[i-1]] {
10-
num += symbols[s[i]] - 2*symbols[s[i-1]]
12+
num += symbols[s[i]] - multiplier*symbols[s[i-1]]
1113
} else {
1214
num += symbols[s[i]]
1315
}

problems/easy/problem013/problem013_test.go

+24-12
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,38 @@
11
package problem013
22

33
import (
4+
"fmt"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
78
)
89

10+
type TestCase struct {
11+
in string
12+
out int
13+
}
14+
915
func TestRomanToInt(t *testing.T) {
1016
a := assert.New(t)
1117

12-
a.Equal(3, romanToInt("III"))
13-
a.Equal(54, romanToInt("LIV"))
14-
a.Equal(58, romanToInt("LVIII"))
15-
a.Equal(1994, romanToInt("MCMXCIV"))
16-
a.Equal(1, romanToInt("I"))
17-
a.Equal(3999, romanToInt("MMMCMXCIX"))
18-
a.Equal(4, romanToInt("IV"))
19-
a.Equal(9, romanToInt("IX"))
20-
a.Equal(40, romanToInt("XL"))
21-
a.Equal(90, romanToInt("XC"))
22-
a.Equal(400, romanToInt("CD"))
23-
a.Equal(900, romanToInt("CM"))
18+
testCases := []TestCase{
19+
{in: "III", out: 3},
20+
{in: "LIV", out: 54},
21+
{in: "LVIII", out: 58},
22+
{in: "MCMXCIV", out: 1994},
23+
{in: "I", out: 1},
24+
{in: "MMMCMXCIX", out: 3999},
25+
{in: "IV", out: 4},
26+
{in: "IX", out: 9},
27+
{in: "XL", out: 40},
28+
{in: "XC", out: 90},
29+
{in: "CD", out: 400},
30+
{in: "CM", out: 900},
31+
}
32+
33+
for key, tc := range testCases {
34+
a.Equal(tc.out, romanToInt(tc.in), fmt.Sprintf("TestRomanToInt number # %d", key+1))
35+
}
2436
}
2537

2638
func BenchmarkRomanToInt(b *testing.B) {

problems/easy/problem014/problem014.go

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ func longestCommonPrefix(strs []string) string {
99
return strs[0]
1010
}
1111

12+
if len(strs) == 1 {
13+
return strs[0]
14+
}
15+
16+
return findCommonPrefix(strs)
17+
}
18+
19+
func findCommonPrefix(strs []string) string {
1220
prefix := strs[0]
1321

1422
for i := 1; i < len(strs); i++ {

problems/easy/problem014/problem014_test.go

+19-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
11
package problem014
22

33
import (
4+
"fmt"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
78
)
89

10+
type TestCase struct {
11+
in []string
12+
out string
13+
}
14+
915
func TestLongestCommonPrefix(t *testing.T) {
1016
a := assert.New(t)
1117

12-
a.Equal("fl", longestCommonPrefix([]string{"flower", "flow", "flight"}))
13-
a.Equal("", longestCommonPrefix([]string{"dog", "racecar", "car"}))
14-
a.Equal("a", longestCommonPrefix([]string{"abc", "a", "ab"}), "one string is a prefix of the others")
15-
a.Equal("", longestCommonPrefix([]string{"", "b", "c"}), "one string is empty")
16-
a.Equal("abc", longestCommonPrefix([]string{"abc", "abc", "abc"}), "all strings are the same")
17-
a.Equal("", longestCommonPrefix([]string{"abc", "def", "ghi"}), "no common prefix")
18-
a.Equal("", longestCommonPrefix([]string{}), "empty slice")
18+
testCases := []TestCase{
19+
{in: []string{"flower", "flow", "flight"}, out: "fl"},
20+
{in: []string{"dog", "racecar", "car"}, out: ""},
21+
{in: []string{"abc", "a", "ab"}, out: "a"},
22+
{in: []string{"", "b", "c"}, out: ""},
23+
{in: []string{"abc", "abc", "abc"}, out: "abc"},
24+
{in: []string{"abc", "def", "ghi"}, out: ""},
25+
{in: []string{}, out: ""},
26+
}
27+
28+
for key, tc := range testCases {
29+
a.Equal(tc.out, longestCommonPrefix(tc.in), fmt.Sprintf("TestLongestCommonPrefix number # %d", key+1))
30+
}
1931
}
2032

2133
func BenchmarkLongestCommonPrefix(b *testing.B) {

problems/easy/problem020/problem020.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package problem020
22

3-
import (
4-
stack "leetcode/go/utils/stack"
5-
)
3+
import "leetcode/go/utils/stack"
4+
5+
const DIVISOR = 2
66

77
func isValid(s string) bool {
8-
if len(s) == 0 || len(s)%2 == 1 {
8+
if len(s) == 0 || len(s)%DIVISOR == 1 {
99
return false
1010
}
1111

@@ -16,6 +16,10 @@ func isValid(s string) bool {
1616
}
1717
symbolStack := stack.New()
1818

19+
return processSymbols(s, pattern, symbolStack)
20+
}
21+
22+
func processSymbols(s string, pattern map[byte]byte, symbolStack *stack.Stack) bool {
1923
for i := 0; i < len(s); i++ {
2024
if _, ok := pattern[s[i]]; ok {
2125
symbolStack.Push(s[i])

0 commit comments

Comments
 (0)