diff options
author | Arne Juul <arnej@vespa.ai> | 2023-12-14 08:26:50 +0000 |
---|---|---|
committer | Arne Juul <arnej@vespa.ai> | 2023-12-14 08:26:50 +0000 |
commit | 990aab34103e545f17f3e05b02a6b2f7bcdca05c (patch) | |
tree | ebde890e7fd9f58a129cafc36a2fa75846fd55e7 /vespajlib/src/test | |
parent | 1ad5ec5fa814a92fdbf98db14121197023f434f0 (diff) |
Reapply "add parsing of special strings for inf/nan cell values"
This reverts commit d976f82207c09b3215661e1d034ae9a42f28a63d.
Diffstat (limited to 'vespajlib/src/test')
-rw-r--r-- | vespajlib/src/test/java/com/yahoo/tensor/serialization/JsonFormatTestCase.java | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/vespajlib/src/test/java/com/yahoo/tensor/serialization/JsonFormatTestCase.java b/vespajlib/src/test/java/com/yahoo/tensor/serialization/JsonFormatTestCase.java index d95396aca50..52f44f0ac42 100644 --- a/vespajlib/src/test/java/com/yahoo/tensor/serialization/JsonFormatTestCase.java +++ b/vespajlib/src/test/java/com/yahoo/tensor/serialization/JsonFormatTestCase.java @@ -669,6 +669,53 @@ public class JsonFormatTestCase { "{\"type\":\"tensor<float>(x[1])\",\"values\":[0.3333333432674408]}"); } + @Test + public void testSpecialNumberStrings() { + assertEquals(Double.POSITIVE_INFINITY, JsonFormat.decodeNumberString("Infinity"), 0.0); + assertEquals(Double.POSITIVE_INFINITY, JsonFormat.decodeNumberString("+Infinity"), 0.0); + assertEquals(Double.POSITIVE_INFINITY, JsonFormat.decodeNumberString("Inf"), 0.0); + assertEquals(Double.POSITIVE_INFINITY, JsonFormat.decodeNumberString("+Inf"), 0.0); + assertEquals(Double.POSITIVE_INFINITY, JsonFormat.decodeNumberString("infinity"), 0.0); + assertEquals(Double.NEGATIVE_INFINITY, JsonFormat.decodeNumberString("-Infinity"), 0.0); + assertEquals(Double.NEGATIVE_INFINITY, JsonFormat.decodeNumberString("-Inf"), 0.0); + assertEquals(Double.NEGATIVE_INFINITY, JsonFormat.decodeNumberString("-infinity"), 0.0); + assertEquals(Double.NEGATIVE_INFINITY, JsonFormat.decodeNumberString("-inf"), 0.0); + assertEquals(0x7FF8000000000000L, Double.doubleToRawLongBits(JsonFormat.decodeNumberString("nan"))); + assertEquals(0x7FF8000000000000L, Double.doubleToRawLongBits(JsonFormat.decodeNumberString("NaN"))); + assertEquals(0x7FF8000000000000L, Double.doubleToRawLongBits(JsonFormat.decodeNumberString("+NaN"))); + assertEquals(0xFFF8000000000000L, Double.doubleToRawLongBits(JsonFormat.decodeNumberString("-nan"))); + assertEquals(0xFFF8000000000000L, Double.doubleToRawLongBits(JsonFormat.decodeNumberString("-NaN"))); + } + + @Test + public void testWithNanVariants() { + TensorType x3 = TensorType.fromSpec("tensor(x[3])"); + String json = "{\"cells\":[" + + "{\"address\":{\"x\":\"0\"},\"value\":\"nan\"}," + + "{\"address\":{\"x\":\"1\"},\"value\":null}," + + "{\"address\":{\"x\":\"2\"},\"value\":\"+NaN\"}" + + "]}"; + var t = JsonFormat.decode(x3, json.getBytes(StandardCharsets.UTF_8)); + checkThreeNans(t); + json = "['nan', null, '+NaN']"; + t = JsonFormat.decode(x3, json.getBytes(StandardCharsets.UTF_8)); + checkThreeNans(t); + json = "{'type':'tensor(x[3])','values':['nan', null, '+NaN']}"; + t = JsonFormat.decode(x3, json.getBytes(StandardCharsets.UTF_8)); + checkThreeNans(t); + } + + private void checkThreeNans(Tensor t) { + final Double nan = Double.NaN; + int cnt = 0; + for (var iter = t.cellIterator(); iter.hasNext(); ) { + var cell = iter.next(); + assertEquals(nan, cell.getValue()); + ++cnt; + } + assertEquals(3, cnt); + } + private void assertEncodeShortForm(String tensor, String expected) { assertEncodeShortForm(Tensor.from(tensor), expected); } |