Skip to content
Open
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
24 changes: 24 additions & 0 deletions cmp/cmpopts/equate.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,30 @@ func (a timeApproximator) compare(x, y time.Time) bool {
return !x.Add(a.margin).Before(y)
}

// EquateApproxDuration returns a [cmp.Comparer] option that determines two
// [time.Duration] values to be equal if they are within some margin of one
// another. The margin must be non-negative.
func EquateApproxDuration(margin time.Duration) cmp.Option {
if margin < 0 {
panic("margin must be a non-negative number")
}
a := durationApproximator{margin}
return cmp.Comparer(a.compare)
}

type durationApproximator struct {
margin time.Duration
}

func (a durationApproximator) compare(x, y time.Duration) bool {
if x > y {
x, y = y, x
}
// Avoid subtracting durations to prevent overflow when the
// difference is larger than the largest representable duration.
return !(x+a.margin < y)
}

// AnyError is an error that matches any non-nil error.
var AnyError anyError

Expand Down
42 changes: 42 additions & 0 deletions cmp/cmpopts/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,48 @@ func TestOptions(t *testing.T) {
opts: []cmp.Option{EquateApproxTime(3 * time.Second)},
wantEqual: false,
reason: "time difference overflows time.Duration",
}, {
label: "EquateApproxDuration",
x: time.Second,
y: time.Second,
opts: []cmp.Option{EquateApproxDuration(0)},
wantEqual: true,
reason: "equal because durations are identical",
}, {
label: "EquateApproxDuration",
x: time.Duration(0),
y: 3 * time.Second,
opts: []cmp.Option{EquateApproxDuration(3 * time.Second)},
wantEqual: true,
reason: "equal because duration is exactly at the allowed margin",
}, {
label: "EquateApproxDuration",
x: 3 * time.Second,
y: time.Duration(0),
opts: []cmp.Option{EquateApproxDuration(3 * time.Second)},
wantEqual: true,
reason: "equal because duration is exactly at the allowed margin (negative)",
}, {
label: "EquateApproxDuration",
x: 3 * time.Second,
y: time.Duration(0),
opts: []cmp.Option{EquateApproxDuration(3*time.Second - 1)},
wantEqual: false,
reason: "not equal because duration is outside allowed margin",
}, {
label: "EquateApproxDuration",
x: time.Duration(0),
y: 3 * time.Second,
opts: []cmp.Option{EquateApproxDuration(3*time.Second - 1)},
wantEqual: false,
reason: "not equal because duration is outside allowed margin (negative)",
}, {
label: "EquateApproxDuration",
x: time.Duration(math.MaxInt64),
y: time.Duration(math.MinInt64),
opts: []cmp.Option{EquateApproxDuration(3 * time.Second)},
wantEqual: false,
reason: "duration difference overflows time.Duration",
}, {
label: "EquateErrors",
x: nil,
Expand Down
Loading