Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ func BenchmarkCompareWithScheme(b *testing.B) {
{name: "Gentoo", a: "1.2.3_rc1", other: "1.2.3", scheme: "gentoo"},
{name: "GentooRevision", a: "1.2.3a_p1-r2", other: "1.2.3a_p2-r1", scheme: "gentoo"},
{name: "APK", a: "1.2.3_rc1", other: "1.2.3", scheme: "apk"},
{name: "IntDot", a: "1.2.3", other: "1.2.4", scheme: "intdot"},
{name: "IntDotUneven", a: "1.2.3.4.5.6.7.8.9", other: "1.2.3.4.5.6.7.8", scheme: "intdot"},
}
for _, tt := range tests {
b.Run(tt.name, func(b *testing.B) {
Expand Down
3 changes: 3 additions & 0 deletions ecosystem_extra_parity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ func TestEcosystemComparatorsDoNotAllocate(t *testing.T) {
{name: "APK", compare: compareAPK, a: "1.2.3_rc1", b: "1.2.3"},
{name: "APK hash and revision", compare: compareAPK, a: "1.2.3a_p1~abc-r2", b: "1.2.3a_p1~abd-r1"},
{name: "APK invalid", compare: compareAPK, a: "1.2.3_bogus", b: "1.2.3"},
{name: "IntDot", compare: compareIntDot, a: "1.2.3", b: "1.2.4"},
{name: "IntDot uneven", compare: compareIntDot, a: "1.2.3.4.5.6.7.8.9", b: "1.2.3.4.5.6.7.8"},
{name: "IntDot suffix", compare: compareIntDot, a: "10.0.1-rc1", b: "9.9.9"},
}

for _, tt := range tests {
Expand Down
34 changes: 34 additions & 0 deletions intdot_parity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package vers

import "testing"

func intDotCorpus() []string {
return append(parityCorpus(),
"01.002", "1.02", "1.2.0", "1.2.00", "0001", "000", "0.0.0.0",
"99999999999999999999", "99999999999999999999.1", "100000000000000000000",
"18446744073709551615", "18446744073709551616",
"1...", "...1", "1.2.", "1.2..", "..1..",
"1.2.3.4.5.6.7.8.9", "1.2.3.4.5.6.7.9", "1.2.3.4.5.6.7.8.9.0",
"1.2a3", "1.2-3.4", "v1.2", " 1.2", "1.2 ", "1,2", "1.2\x00.3",
"١.٢", "1.٢",
)
}

func TestCompareIntDotMatchesReference(t *testing.T) {
assertParity(t, "compareIntDot", intDotCorpus(), compareIntDot, refCompareIntDot)
}

func TestCompareIntDotMatchesReferenceOnFixtures(t *testing.T) {
assertParity(t, "compareIntDot", fixtureVersions(t), compareIntDot, refCompareIntDot)
}

func FuzzCompareIntDotMatchesReference(f *testing.F) {
for _, v := range []string{"", ".", "1.", ".1", "1..2", "01.002", "1.2.3", "1.2.3.4.5.6.7.8.9", "1.2a", "99999999999999999999"} {
f.Add(v, "1.2.3")
}
f.Fuzz(func(t *testing.T, a, b string) {
if got, want := compareIntDot(a, b), refCompareIntDot(a, b); got != want {
t.Fatalf("compareIntDot(%q, %q) = %d, reference implementation returns %d", a, b, got, want)
}
})
}
37 changes: 37 additions & 0 deletions intdot_reference_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package vers

// This file is a mechanically renamed copy of intDotPrefix and compareIntDot
// from before compareIntDot stopped splitting both versions into slices. It
// exists so the differential tests in intdot_parity_test.go can assert that the
// allocation-free rewrite orders every input exactly the way the previous code
// did. Do not edit it to fix a behavior difference; fix the real
// implementation instead.

import "strings"

func refIntDotPrefix(s string) string {
for i := 0; i < len(s); i++ {
if s[i] != '.' && !isASCIIDigit(s[i]) {
return s[:i]
}
}
return s
}

func refCompareIntDot(a, b string) int {
a, b = refIntDotPrefix(a), refIntDotPrefix(b)
pa, pb := strings.Split(a, "."), strings.Split(b, ".")
for i := 0; i < len(pa) || i < len(pb); i++ {
var va, vb string
if i < len(pa) {
va = pa[i]
}
if i < len(pb) {
vb = pb[i]
}
if c := cmpNumStr(va, vb); c != 0 {
return c
}
}
return 0
}
17 changes: 7 additions & 10 deletions schemes.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,20 +622,17 @@ func intDotPrefix(s string) string {

func compareIntDot(a, b string) int {
a, b = intDotPrefix(a), intDotPrefix(b)
pa, pb := strings.Split(a, "."), strings.Split(b, ".")
for i := 0; i < len(pa) || i < len(pb); i++ {
var va, vb string
if i < len(pa) {
va = pa[i]
}
if i < len(pb) {
vb = pb[i]
}
for {
va, restA, moreA := nextDotPart(a)
vb, restB, moreB := nextDotPart(b)
if c := cmpNumStr(va, vb); c != 0 {
return c
}
if !moreA && !moreB {
return 0
}
a, b = restA, restB
}
return 0
}

func compareOpenSSL(a, b string) int {
Expand Down