Skip to content

Commit

Permalink
WIP: Test NDArray<->JSON en/de-coding
Browse files Browse the repository at this point in the history
The decoding works, but fails the existing shm size check.
Investigation needed.
  • Loading branch information
ctrueden committed Jul 15, 2024
1 parent 2c0866d commit 77fd98f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/apposed/appose/NDArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/apposed/appose/SharedMemory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
49 changes: 38 additions & 11 deletions src/test/java/org/apposed/appose/TypesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand All @@ -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" +
Expand Down Expand Up @@ -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<String, Object> 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());
Expand All @@ -122,10 +135,24 @@ public void testDecode() throws IOException {
assertEquals("\0", data.get("aChar"));
assertEquals(STRING, data.get("aString"));
List<Integer> 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) {
Expand Down

0 comments on commit 77fd98f

Please sign in to comment.