Skip to content

Commit 4670b5b

Browse files
committed
Add an error margin when comparing floats.
1 parent 34ccaba commit 4670b5b

File tree

2 files changed

+31
-26
lines changed

2 files changed

+31
-26
lines changed

muted-tests.yml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -541,15 +541,6 @@ tests:
541541
- class: org.elasticsearch.xpack.esql.qa.multi_node.EsqlSpecIT
542542
method: test {knn-function.KnnSearchWithKOption SYNC}
543543
issue: https://github.com/elastic/elasticsearch/issues/129512
544-
- class: org.elasticsearch.xpack.logsdb.qa.StandardVersusStandardReindexedIntoLogsDbChallengeRestIT
545-
method: testMatchAllQuery
546-
issue: https://github.com/elastic/elasticsearch/issues/129527
547-
- class: org.elasticsearch.xpack.logsdb.qa.BulkChallengeRestIT
548-
method: testMatchAllQuery
549-
issue: https://github.com/elastic/elasticsearch/issues/129528
550-
- class: org.elasticsearch.xpack.logsdb.qa.StoredSourceLogsDbVersusReindexedLogsDbChallengeRestIT
551-
method: testMatchAllQuery
552-
issue: https://github.com/elastic/elasticsearch/issues/129529
553544
- class: org.elasticsearch.index.engine.ThreadPoolMergeExecutorServiceTests
554545
method: testIORateIsAdjustedForAllRunningMergeTasks
555546
issue: https://github.com/elastic/elasticsearch/issues/129531

test/framework/src/main/java/org/elasticsearch/datageneration/matchers/source/DynamicFieldMatcher.java

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
import java.util.List;
1818
import java.util.Objects;
1919
import java.util.Optional;
20-
import java.util.Set;
2120
import java.util.function.Function;
22-
import java.util.stream.Collectors;
21+
import java.util.function.Supplier;
2322

2423
import static org.elasticsearch.datageneration.matchers.Messages.formatErrorMessage;
2524
import static org.elasticsearch.datageneration.matchers.Messages.prettyPrintCollections;
@@ -62,31 +61,46 @@ public MatchResult match(List<Object> actual, List<Object> expected) {
6261

6362
var normalizedActual = normalizeDoubles(actual);
6463
var normalizedExpected = normalizeDoubles(expected);
64+
Supplier<MatchResult> noMatchSupplier = () -> MatchResult.noMatch(
65+
formatErrorMessage(
66+
actualMappings,
67+
actualSettings,
68+
expectedMappings,
69+
expectedSettings,
70+
"Values of dynamically mapped field containing double values don't match after normalization, normalized "
71+
+ prettyPrintCollections(normalizedActual, normalizedExpected)
72+
)
73+
);
6574

66-
return normalizedActual.equals(normalizedExpected)
67-
? MatchResult.match()
68-
: MatchResult.noMatch(
69-
formatErrorMessage(
70-
actualMappings,
71-
actualSettings,
72-
expectedMappings,
73-
expectedSettings,
74-
"Values of dynamically mapped field containing double values don't match after normalization, normalized "
75-
+ prettyPrintCollections(normalizedActual, normalizedExpected)
76-
)
77-
);
75+
if (normalizedActual.size() != normalizedExpected.size()) {
76+
return noMatchSupplier.get();
77+
}
78+
79+
for (int i = 0; i < normalizedActual.size(); i++) {
80+
if (floatsEquals(normalizedActual.get(i), normalizedExpected.get(i))) {
81+
return noMatchSupplier.get();
82+
}
83+
}
84+
85+
return MatchResult.match();
7886
}
7987

8088
return matchWithGenericMatcher(actual, expected);
8189
}
8290

83-
private static Set<Float> normalizeDoubles(List<Object> values) {
91+
private static List<Float> normalizeDoubles(List<Object> values) {
8492
if (values == null) {
85-
return Set.of();
93+
return List.of();
8694
}
8795

8896
Function<Object, Float> toFloat = (o) -> o instanceof Number n ? n.floatValue() : Float.parseFloat((String) o);
89-
return values.stream().filter(Objects::nonNull).map(toFloat).collect(Collectors.toSet());
97+
98+
// We skip nulls because they trip the pretty print collections.
99+
return values.stream().filter(Objects::nonNull).map(toFloat).toList();
100+
}
101+
102+
private static boolean floatsEquals(Float actual, Float expected) {
103+
return Math.abs(actual - expected) > 1e-8;
90104
}
91105

92106
private MatchResult matchWithGenericMatcher(List<Object> actualValues, List<Object> expectedValues) {

0 commit comments

Comments
 (0)