diff options
author | Lester Solbakken <lesters@oath.com> | 2021-09-02 15:59:15 +0200 |
---|---|---|
committer | Lester Solbakken <lesters@oath.com> | 2021-09-02 15:59:15 +0200 |
commit | 691ddac6701fb0d45b0ef2bbd49e5ab99bcc6c17 (patch) | |
tree | c2982328f7351182e4d7451f4c326563f0b24ca7 /vespajlib/src/test/java/com/yahoo/tensor/serialization | |
parent | 045321f2a1b2d00d33ccf7c64c2708b6e2c94667 (diff) |
Disallow feeding empty indexed tensors
Diffstat (limited to 'vespajlib/src/test/java/com/yahoo/tensor/serialization')
-rw-r--r-- | vespajlib/src/test/java/com/yahoo/tensor/serialization/JsonFormatTestCase.java | 24 |
1 files changed, 24 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 2f1e3be9299..87796501917 100644 --- a/vespajlib/src/test/java/com/yahoo/tensor/serialization/JsonFormatTestCase.java +++ b/vespajlib/src/test/java/com/yahoo/tensor/serialization/JsonFormatTestCase.java @@ -68,6 +68,21 @@ public class JsonFormatTestCase { } @Test + public void testDisallowedEmptyDenseTensor() { + TensorType type = TensorType.fromSpec("tensor(x[3])"); + assertDecodeFails(type, "{\"values\":[]}", "The 'values' array does not contain any values"); + assertDecodeFails(type, "{\"values\":\"\"}", "The 'values' string does not contain any values"); + } + + @Test + public void testDisallowedEmptyMixedTensor() { + TensorType type = TensorType.fromSpec("tensor(x{},y[3])"); + assertDecodeFails(type, "{\"blocks\":{ \"a\": [] } }", "The 'block' value array does not contain any values"); + assertDecodeFails(type, "{\"blocks\":[ {\"address\":{\"x\":\"a\"}, \"values\": [] } ] }", + "The 'block' value array does not contain any values"); + } + + @Test public void testDenseTensorInDenseForm() { Tensor.Builder builder = Tensor.Builder.of(TensorType.fromSpec("tensor(x[2],y[3])")); builder.cell().label("x", 0).label("y", 0).value(2.0); @@ -304,4 +319,13 @@ public class JsonFormatTestCase { assertEquals(expected, new String(json, StandardCharsets.UTF_8)); } + private void assertDecodeFails(TensorType type, String format, String msg) { + try { + Tensor decoded = JsonFormat.decode(type, format.getBytes(StandardCharsets.UTF_8)); + fail("Did not get exception as expected, decoded as: " + decoded); + } catch (IllegalArgumentException e) { + assertEquals(e.getMessage(), msg); + } + } + } |