Skip to content

Commit 16bb7b7

Browse files
authored
Merge pull request #368 from bookingcom/grzkv/floats_cmp_fix_in_test
Corrected floats comparison in a test to take into account limited precison of floating point numbers.
2 parents 174654c + 5ce6298 commit 16bb7b7

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

pkg/types/types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ var (
284284

285285
type floatEqualityFunc func(a, b float64) bool
286286

287-
func areFloatsApproximatelyEqual(a, b float64) bool {
287+
func AreFloatsApproximatelyEqual(a, b float64) bool {
288288
if a == b {
289289
return true
290290
}
@@ -350,7 +350,7 @@ func mergeMetrics(metrics []Metric, replicaMismatchConfig cfg.RenderReplicaMisma
350350

351351
var equalityFunc floatEqualityFunc
352352
if replicaMismatchConfig.RenderReplicaMismatchApproximateCheck {
353-
equalityFunc = areFloatsApproximatelyEqual
353+
equalityFunc = AreFloatsApproximatelyEqual
354354
}
355355

356356
replicaMatchMode := replicaMismatchConfig.RenderReplicaMatchMode

tests/helper.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,27 @@ func TestMultiReturnEvalExpr(t *testing.T, tt *MultiReturnEvalTestItem) {
277277
if r[0].Name != gg.Name {
278278
t.Errorf("result Name mismatch, got\n%#v,\nwant\n%#v", gg.Name, r[0].Name)
279279
}
280-
if !reflect.DeepEqual(r[0].Values, gg.Values) || !reflect.DeepEqual(r[0].IsAbsent, gg.IsAbsent) ||
280+
281+
// TODO (grzkv) : Reuse in other places when similar operation is done.
282+
areValsEqual := true
283+
if gg.Values == nil && r[0].Values != nil ||
284+
gg.Values != nil && r[0].Values == nil ||
285+
len(gg.Values) != len(r[0].Values) {
286+
areValsEqual = false
287+
} else {
288+
for i := range gg.Values {
289+
if !dataTypes.AreFloatsApproximatelyEqual(gg.Values[i], r[0].Values[i]) {
290+
areValsEqual = false
291+
break
292+
}
293+
}
294+
}
295+
296+
if !areValsEqual || !reflect.DeepEqual(r[0].IsAbsent, gg.IsAbsent) ||
281297
r[0].StartTime != gg.StartTime ||
282298
r[0].StopTime != gg.StopTime ||
283299
r[0].StepTime != gg.StepTime {
284-
t.Errorf("result mismatch, got\n%#v,\nwant\n%#v", gg, r)
300+
t.Errorf("result mismatch, got\n%#v,\nwant\n%#v", gg, r[0])
285301
}
286302
}
287303
}

0 commit comments

Comments
 (0)