mirror of
https://github.com/dromara/hertzbeat.git
synced 2026-09-17 09:40:58 +00:00
fix(common): parse +Inf/-Inf as real infinity in StrBuffer.parseDouble
StrBuffer.parseDouble returned the raw IEEE-754 bit-pattern constants 0x7FF0000000000000L / 0xFFF0000000000000L widened to double (9.2e18 / -4.5e15) instead of real infinities for the Prometheus text format special values +inf/-inf. OnlineParser.toDouble already maps them to Double.POSITIVE_INFINITY/NEGATIVE_INFINITY; align StrBuffer (used by TextParser) with that behavior and drop the +/-Inf exclusion from the cross-parser test that papered over the divergence. Signed-off-by: zjncs <18910855655@163.com>
This commit is contained in:
+2
-5
@@ -108,11 +108,8 @@ class OnlineParserTest {
|
||||
MetricFamily metricFamily1 = metricFamilyMap1.get(metricFamilyName);
|
||||
Set<Double> metricValueSet = metricFamily2.getMetricList().stream().map(MetricFamily.Metric::getValue).collect(Collectors.toSet());
|
||||
metricFamily1.getMetricList().forEach(metric -> {
|
||||
// this is for something different between two algorithms above, and both of them is current on this parsing behavior.
|
||||
if (!(metric.getValue() == Double.POSITIVE_INFINITY || metric.getValue() == Double.NEGATIVE_INFINITY)) {
|
||||
if (!metricValueSet.contains(metric.getValue())) {
|
||||
fail();
|
||||
}
|
||||
if (!metricValueSet.contains(metric.getValue())) {
|
||||
fail();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -153,9 +153,9 @@ public class StrBuffer {
|
||||
*/
|
||||
public static double parseDouble(String s) {
|
||||
if (POSITIVE_INF.equalsIgnoreCase(s)) {
|
||||
return POSITIVE_INF_VALUE;
|
||||
return Double.POSITIVE_INFINITY;
|
||||
} else if (NEGATIVE_INF.equalsIgnoreCase(s)) {
|
||||
return NEGATIVE_INF_VALUE;
|
||||
return Double.NEGATIVE_INFINITY;
|
||||
}
|
||||
return Double.parseDouble(s);
|
||||
}
|
||||
|
||||
+4
-4
@@ -91,10 +91,10 @@ class StrBufferTest {
|
||||
assertEquals(123.45, buffer.toDouble());
|
||||
|
||||
buffer = new StrBuffer("+inf");
|
||||
assertEquals(POSITIVE_INF_VALUE, buffer.toDouble());
|
||||
assertEquals(Double.POSITIVE_INFINITY, buffer.toDouble());
|
||||
|
||||
buffer = new StrBuffer("-inf");
|
||||
assertEquals(NEGATIVE_INF_VALUE, buffer.toDouble());
|
||||
assertEquals(Double.NEGATIVE_INFINITY, buffer.toDouble());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -144,8 +144,8 @@ class StrBufferTest {
|
||||
void testParseDouble() {
|
||||
|
||||
assertEquals(123.45, StrBuffer.parseDouble("123.45"));
|
||||
assertEquals(POSITIVE_INF_VALUE, StrBuffer.parseDouble("+inf"));
|
||||
assertEquals(NEGATIVE_INF_VALUE, StrBuffer.parseDouble("-inf"));
|
||||
assertEquals(Double.POSITIVE_INFINITY, StrBuffer.parseDouble("+inf"));
|
||||
assertEquals(Double.NEGATIVE_INFINITY, StrBuffer.parseDouble("-inf"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user