From 77fd98f31924a6076c0f15f29f2c01cac5503c56 Mon Sep 17 00:00:00 2001 From: Curtis Rueden Date: Mon, 15 Jul 2024 16:15:33 -0500 Subject: [PATCH] WIP: Test NDArray<->JSON en/de-coding The decoding works, but fails the existing shm size check. Investigation needed. --- src/main/java/org/apposed/appose/NDArray.java | 2 +- .../java/org/apposed/appose/SharedMemory.java | 2 +- .../java/org/apposed/appose/TypesTest.java | 49 ++++++++++++++----- 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/apposed/appose/NDArray.java b/src/main/java/org/apposed/appose/NDArray.java index e86084b..7d3efe9 100644 --- a/src/main/java/org/apposed/appose/NDArray.java +++ b/src/main/java/org/apposed/appose/NDArray.java @@ -115,7 +115,7 @@ public ByteBuffer buffer() { * Release resources ({@code SharedMemory}) associated with this {@code NDArray}. */ @Override - public void close() throws Exception { + public void close() { sharedMemory.close(); } diff --git a/src/main/java/org/apposed/appose/SharedMemory.java b/src/main/java/org/apposed/appose/SharedMemory.java index d68d13b..a9041e9 100644 --- a/src/main/java/org/apposed/appose/SharedMemory.java +++ b/src/main/java/org/apposed/appose/SharedMemory.java @@ -121,7 +121,7 @@ static SharedMemory createOrAttach(String name, boolean create, int size) { * called once (and only once) across all processes which have access * to the shared memory block. */ - default void unlink() throws Exception { + default void unlink() { throw new UnsupportedOperationException(); } diff --git a/src/test/java/org/apposed/appose/TypesTest.java b/src/test/java/org/apposed/appose/TypesTest.java index 4e84954..ddac656 100644 --- a/src/test/java/org/apposed/appose/TypesTest.java +++ b/src/test/java/org/apposed/appose/TypesTest.java @@ -39,8 +39,7 @@ import java.util.Map; import java.util.stream.Collectors; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.*; /** * Tests {@link Types}. @@ -62,8 +61,18 @@ public class TypesTest { "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz" + "~!@#$%^&*()\"," + "\"numbers\":[1,1,2,3,5,8]," + - "\"words\":[\"quick\",\"brown\",\"fox\"]" + - "}"; + "\"words\":[\"quick\",\"brown\",\"fox\"]," + + "\"ndArray\":{" + + "\"appose_type\":\"ndarray\"," + + "\"shm\":{" + + "\"appose_type\":\"shm\"," + + "\"name\":\"SHM_NAME\"," + + "\"size\":4000" + + "}," + + "\"dtype\":\"float32\"," + + "\"shape\":[2,20,25]" + + "}" + + "}"; private static String STRING = "-=[]\\;',./_+{}|:\"<>?" + "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz" + @@ -94,17 +103,21 @@ public void testEncode() { data.put("aString", STRING); data.put("numbers", NUMBERS); data.put("words", WORDS); - String json = Types.encode(data); - assertNotNull(json); - System.out.println(json); - assertEquals(JSON, json); + NDArray.DType dtype = NDArray.DType.FLOAT32; + NDArray.Shape shape = new NDArray.Shape(NDArray.Shape.Order.C_ORDER, 2, 20, 25); + try (NDArray ndArray = new NDArray(dtype, shape)) { + data.put("ndArray", ndArray); + String json = Types.encode(data); + assertNotNull(json); + assertEquals(JSON, generalizeShmName(json)); + } } @Test public void testDecode() throws IOException { Map data = Types.decode(JSON); assertNotNull(data); - assertEquals(18, data.size()); + assertEquals(19, data.size()); assertEquals(123, data.get("posByte")); // NB: decodes back to int assertEquals(-98, data.get("negByte")); // NB: decodes back to int assertEquals(9.876543210123456, bd(data.get("posDouble")).doubleValue()); @@ -122,10 +135,24 @@ public void testDecode() throws IOException { assertEquals("\0", data.get("aChar")); assertEquals(STRING, data.get("aString")); List numbersList = Arrays.stream(NUMBERS) - .boxed() - .collect(Collectors.toList()); + .boxed().collect(Collectors.toList()); assertEquals(numbersList, data.get("numbers")); assertEquals(Arrays.asList(WORDS), data.get("words")); + try (NDArray ndArray = (NDArray) data.get("ndArray")) { + assertSame(NDArray.DType.FLOAT32, ndArray.dType()); + assertEquals(NDArray.Shape.Order.C_ORDER, ndArray.shape().order()); + assertEquals(3, ndArray.shape().length()); + assertEquals(2, ndArray.shape().get(0)); + assertEquals(20, ndArray.shape().get(1)); + assertEquals(25, ndArray.shape().get(2)); + } + } + + private String generalizeShmName(String json) { + return json == null ? null : json.replaceAll( + "(\"shm\":\\{\"appose_type\":\"shm\",\"name\":\").*?\"", + "$1SHM_NAME\"" + ); } private BigDecimal bd(Object posDouble) {