Skip to content

Commit

Permalink
go/common/version: Expose Less function
Browse files Browse the repository at this point in the history
  • Loading branch information
martintomazic committed Jan 14, 2025
1 parent f494997 commit effb8ba
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions go/common/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func (v Version) Cmp(other Version) int {
return int(v.Patch) - int(other.Patch)
}

// Less checks if the current Version is strictly less than another Version.
func (v Version) Less(other Version) bool {
return v.Cmp(other) < 0
}

// ToU64 returns the version as platform-dependent uint64.
func (v Version) ToU64() uint64 {
return (uint64(v.Major) << 32) | (uint64(v.Minor) << 16) | (uint64(v.Patch))
Expand Down
22 changes: 22 additions & 0 deletions go/common/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,25 @@ func TestProtocolVersionCompatible(t *testing.T) {
require.Equal(t, v.isCompatible, Versions.Compatible(v.versions()), v.msg)
}
}

func TestLess(t *testing.T) {
tests := []struct {
name string
v1 Version
v2 Version
expect bool
}{
{"v1 less than v2", Version{1, 3, 0}, Version{2, 0, 0}, true},
{"v1 equal to v2", Version{1, 0, 0}, Version{1, 0, 0}, false},
{"v1 greater than v2", Version{2, 0, 0}, Version{1, 0, 5}, false},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := tc.v1.Less(tc.v2)
if result != tc.expect {
t.Errorf("Less(%v, %v) = %v, want %v", tc.v1, tc.v2, result, tc.expect)
}
})
}
}

0 comments on commit effb8ba

Please sign in to comment.