util/cmpver: add Less/LessEq helper funcs

Updates tailscale/corp#17199

Signed-off-by: Paul Scott <paul@tailscale.com>
This commit is contained in:
Paul Scott
2024-02-07 12:05:07 +00:00
committed by Paul Scott
parent d610f8eec0
commit 2fa20e3787
2 changed files with 38 additions and 6 deletions

View File

@@ -163,16 +163,31 @@ func TestCompare(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
got := cmpver.Compare(test.v1, test.v2)
if got != test.want {
t.Errorf("Compare(%v, %v) = %v, want %v", test.v1, test.v2, got, test.want)
t.Errorf("Compare(%q, %q) = %v, want %v", test.v1, test.v2, got, test.want)
}
// Reversing the comparison should reverse the outcome.
got2 := cmpver.Compare(test.v2, test.v1)
if got2 != -test.want {
t.Errorf("Compare(%v, %v) = %v, want %v", test.v2, test.v1, got2, -test.want)
t.Errorf("Compare(%q, %q) = %v, want %v", test.v2, test.v1, got2, -test.want)
}
if got, want := cmpver.Less(test.v1, test.v2), test.want < 0; got != want {
t.Errorf("Less(%q, %q) = %v, want %v", test.v1, test.v2, got, want)
}
if got, want := cmpver.Less(test.v2, test.v1), test.want > 0; got != want {
t.Errorf("Less(%q, %q) = %v, want %v", test.v2, test.v1, got, want)
}
if got, want := cmpver.LessEq(test.v1, test.v2), test.want <= 0; got != want {
t.Errorf("LessEq(%q, %q) = %v, want %v", test.v1, test.v2, got, want)
}
if got, want := cmpver.LessEq(test.v2, test.v1), test.want >= 0; got != want {
t.Errorf("LessEq(%q, %q) = %v, want %v", test.v2, test.v1, got, want)
}
// Check that version comparison does not allocate.
if n := testing.AllocsPerRun(100, func() { cmpver.Compare(test.v1, test.v2) }); n > 0 {
t.Errorf("Compare(%v, %v) got %v allocs per run", test.v1, test.v2, n)
t.Errorf("Compare(%q, %q) got %v allocs per run", test.v1, test.v2, n)
}
})
}