From f9fff4feb28350dafc400daaf6049ea7d1527f47 Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Tue, 8 Oct 2019 15:09:40 +0200 Subject: Single sparse dimension short form --- .../tensor/serialization/JsonFormatTestCase.java | 33 ++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'vespajlib/src/test/java/com') 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 16f92289504..81de8a9db4c 100644 --- a/vespajlib/src/test/java/com/yahoo/tensor/serialization/JsonFormatTestCase.java +++ b/vespajlib/src/test/java/com/yahoo/tensor/serialization/JsonFormatTestCase.java @@ -31,6 +31,21 @@ public class JsonFormatTestCase { assertEquals(tensor, decoded); } + @Test + public void testSingleSparseDimensionShortForm() { + Tensor.Builder builder = Tensor.Builder.of(TensorType.fromSpec("tensor(x{})")); + builder.cell().label("x", "a").value(2.0); + builder.cell().label("x", "c").value(3.0); + Tensor expected = builder.build(); + + String json= "{\"cells\":{" + + "\"a\":2.0," + + "\"c\":3.0" + + "}}"; + Tensor decoded = JsonFormat.decode(expected.type(), json.getBytes(StandardCharsets.UTF_8)); + assertEquals(expected, decoded); + } + @Test public void testDenseTensor() { Tensor.Builder builder = Tensor.Builder.of(TensorType.fromSpec("tensor(x[2],y[2])")); @@ -84,6 +99,24 @@ public class JsonFormatTestCase { assertEquals(expected, decoded); } + @Test + public void testMixedTensorInMixedFormWithSingleSparseDimensionShortForm() { + Tensor.Builder builder = Tensor.Builder.of(TensorType.fromSpec("tensor(x{},y[3])")); + builder.cell().label("x", 0).label("y", 0).value(2.0); + builder.cell().label("x", 0).label("y", 1).value(3.0); + builder.cell().label("x", 0).label("y", 2).value(4.0); + builder.cell().label("x", 1).label("y", 0).value(5.0); + builder.cell().label("x", 1).label("y", 1).value(6.0); + builder.cell().label("x", 1).label("y", 2).value(7.0); + Tensor expected = builder.build(); + String mixedJson = "{\"blocks\":{" + + "\"0\":[2.0,3.0,4.0]," + + "\"1\":[5.0,6.0,7.0]" + + "}}"; + Tensor decoded = JsonFormat.decode(expected.type(), mixedJson.getBytes(StandardCharsets.UTF_8)); + assertEquals(expected, decoded); + } + @Test public void testTooManyCells() { TensorType x2 = TensorType.fromSpec("tensor(x[2])"); -- cgit v1.2.3