【rich function】CustomVersionCompare append

master
sunwei 4 years ago committed by Kevin Wan
parent babb240cbe
commit 1104363988

@ -35,3 +35,90 @@ func CompareVersions(a, b string) int {
return 0
}
//return 0 if they are equal,and 1 if v1>2,and 2 if v1<v2
func Compare(v1, v2 string) int {
replaceMap := map[string]string{"V": "", "v": "", "-": "."}
for k, v := range replaceMap {
if strings.Contains(v1, k) {
strings.Replace(v1, k, v, -1)
}
if strings.Contains(v2, k) {
strings.Replace(v2, k, v, -1)
}
}
verStr1 := strings.Split(v1, ".")
verStr2 := strings.Split(v2, ".")
ver1 := strSlice2IntSlice(verStr1)
ver2 := strSlice2IntSlice(verStr2)
var shorter int
if len(ver1) > len(ver2) {
shorter = len(ver2)
} else {
shorter = len(ver1)
}
for i := 0; i < shorter; i++ {
if ver1[i] == ver2[i] {
if shorter-1 == i {
if len(ver1) == len(ver2) {
return 0
} else {
if len(ver1) > len(ver2) {
return 1
} else {
return 2
}
}
}
} else if ver1[i] > ver2[i] {
return 1
} else {
return 2
}
}
return -1
}
func strSlice2IntSlice(strs []string) []int64 {
if len(strs) == 0 {
return []int64{}
}
retInt := make([]int64, 0, len(strs))
for _, str := range strs {
i, err := strconv.ParseInt(str, 10, 64)
if err == nil {
retInt = append(retInt, i)
}
}
return retInt
}
//custom operator compare
func CustomCompareVersions(v1, v2, operator string) bool {
com := Compare(v1, v2)
switch operator {
case "==":
if com == 0 {
return true
}
case "<":
if com == 2 {
return true
}
case ">":
if com == 1 {
return true
}
case "<=":
if com == 0 || com == 2 {
return true
}
case ">=":
if com == 0 || com == 1 {
return true
}
}
return false
}

@ -28,3 +28,33 @@ func TestCompareVersions(t *testing.T) {
})
}
}
func TestCustomCompareVersions(t *testing.T) {
cases := []struct {
ver1 string
ver2 string
operator string
out bool
}{
{"1", "1.0.1", ">", false},
{"1", "0.9.9", ">", true},
{"1", "1-0.1", "<", true},
{"1.0.1", "1-0.1", "<", false},
{"1.0.1", "1.0.1", "==", true},
{"1.0.1", "1.0.2", "==", false},
{"1.1-1", "1.0.2", "==", false},
{"1.0.1", "1.0.2", ">=", false},
{"1.0.2", "1.0.2", ">=", true},
{"1.0.3", "1.0.2", ">=", true},
{"1.0.4", "1.0.2", "<=", false},
{"1.0.4", "1.0.6", "<=", true},
{"1.0.4", "1.0.4", "<=", true},
}
for _, each := range cases {
t.Run(each.ver1, func(t *testing.T) {
actual := CustomCompareVersions(each.ver1, each.ver2, each.operator)
assert.Equal(t, each.out, actual)
})
}
}

Loading…
Cancel
Save