test: super 包中新增版本比较相关的测试用例

This commit is contained in:
kercylan98 2023-12-05 12:07:50 +08:00
parent f060af2b7d
commit 52c92c8844
3 changed files with 88 additions and 5 deletions

View File

@ -0,0 +1,18 @@
package super_test
import (
"github.com/kercylan98/minotaur/utils/super"
"testing"
)
func BenchmarkCompareVersion(b *testing.B) {
for i := 0; i < b.N; i++ {
super.CompareVersion("vfe2faf.d2ad5.dd3", "afe2faf.d2ad5.dd2")
}
}
func BenchmarkOldVersion(b *testing.B) {
for i := 0; i < b.N; i++ {
super.OldVersion("vfe2faf.d2ad5.dd3", "vda2faf.d2ad5.dd2")
}
}

View File

@ -0,0 +1,18 @@
package super_test
import (
"fmt"
"github.com/kercylan98/minotaur/utils/super"
)
func ExampleCompareVersion() {
result := super.CompareVersion("1.2.3", "1.2.2")
fmt.Println(result)
// Output: 1
}
func ExampleOldVersion() {
result := super.OldVersion("1.2.3", "1.2.2")
fmt.Println(result)
// Output: true
}

View File

@ -5,9 +5,56 @@ import (
"testing"
)
func TestCompareVersion(t *testing.T) {
t.Log(super.CompareVersion("1", "2"), -1)
t.Log(super.CompareVersion("1", "vv2"), -1)
t.Log(super.CompareVersion("1", "vv2.3.1"), -1)
t.Log(super.CompareVersion("11", "vv2.3.1"), 1)
func TestOldVersion(t *testing.T) {
testCases := []struct {
version1 string
version2 string
want bool
}{
{"1.2.3", "1.2.2", true},
{"1.2.1", "1.2.2", false},
{"1.2.3", "1.2.3", false},
{"v1.2.3", "v1.2.2", true},
{"v1.2.3", "v1.2.4", false},
{"v1.2.3", "1.2.3", false},
{"vxx2faf.d2ad5.dd3", "gga2faf.d2ad5.dd2", true},
{"awd2faf.d2ad4.dd3", "vsd2faf.d2ad5.dd3", false},
{"vxd2faf.d2ad5.dd3", "qdq2faf.d2ad5.dd3", false},
{"1.2.3", "vdafe2faf.d2ad5.dd3", false},
{"v1.2.3", "vdafe2faf.d2ad5.dd3", false},
}
for _, tc := range testCases {
got := super.OldVersion(tc.version1, tc.version2)
if got != tc.want {
t.Errorf("OldVersion(%q, %q) = %v; want %v", tc.version1, tc.version2, got, tc.want)
}
}
}
func TestCompareVersion(t *testing.T) {
testCases := []struct {
version1 string
version2 string
want int
}{
{"1.2.3", "1.2.2", 1},
{"1.2.1", "1.2.2", -1},
{"1.2.3", "1.2.3", 0},
{"v1.2.3", "v1.2.2", 1},
{"v1.2.3", "v1.2.4", -1},
{"v1.2.3", "1.2.3", 0},
{"vde2faf.d2ad5.dd3", "e2faf.d2ad5.dd2", 1},
{"vde2faf.d2ad4.dd3", "vde2faf.d2ad5.dd3", -1},
{"vfe2faf.d2ad5.dd3", "ve2faf.d2ad5.dd3", 0},
{"1.2.3", "vdafe2faf.d2ad5.dd3", -1},
{"v1.2.3", "vdafe2faf.d2ad5.dd3", -1},
}
for _, tc := range testCases {
got := super.CompareVersion(tc.version1, tc.version2)
if got != tc.want {
t.Errorf("CompareVersion(%q, %q) = %v; want %v", tc.version1, tc.version2, got, tc.want)
}
}
}