From be24249fc8ba97ccb9b8abe1ebe3c2965a93275e Mon Sep 17 00:00:00 2001 From: Matthew Dawson Date: Thu, 8 May 2014 11:10:38 -0700 Subject: [PATCH] Add tests around handle handling. Ensure handle classes have away to to compare for object equality. Due to the current design of MLArray, this cannot be done per object. So instead the same Objects share the same structure. Eventually this should be fixed. --- .../jmatio/test/MatlabMCOSTest.java | 75 ++++++++++++++++++ .../resources/mcos/handlesingle_multiple.mat | Bin 0 -> 1768 bytes 2 files changed, 75 insertions(+) create mode 100644 src/test/resources/mcos/handlesingle_multiple.mat diff --git a/src/test/java/ca/mjdsystems/jmatio/test/MatlabMCOSTest.java b/src/test/java/ca/mjdsystems/jmatio/test/MatlabMCOSTest.java index 356a258..febc8fb 100644 --- a/src/test/java/ca/mjdsystems/jmatio/test/MatlabMCOSTest.java +++ b/src/test/java/ca/mjdsystems/jmatio/test/MatlabMCOSTest.java @@ -6,6 +6,7 @@ package ca.mjdsystems.jmatio.test; import ca.mjdsystems.jmatio.io.MatFileReader; import ca.mjdsystems.jmatio.types.MLArray; import ca.mjdsystems.jmatio.types.MLChar; +import ca.mjdsystems.jmatio.types.MLInt8; import ca.mjdsystems.jmatio.types.MLObject; import org.junit.Rule; import org.junit.Test; @@ -19,6 +20,7 @@ import java.util.Map; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.CoreMatchers.sameInstance; import static org.junit.Assert.assertThat; /** @@ -147,6 +149,79 @@ public class MatlabMCOSTest { assertThat(obj.getObject().getFieldNames().iterator().next(), is("test_text")); } + @Test + public void testParsingHandleSinglePropertyMultipleMCOS() throws IOException + { + File file = fileFromStream("/mcos/handlesingle_multiple.mat"); + MatFileReader reader = new MatFileReader(file); + Map content = reader.getContent(); + + assertThat(content.size(), is(4)); + + MLObject obj = (MLObject) content.get("obj1"); + assertThat(obj, is(notNullValue())); + + assertThat(obj.getName(), is("obj1")); + assertThat(obj.getClassName(), is("HandleSingle")); + assertThat(((MLObject) content.get("obj3")).getObject(), is(sameInstance(obj.getObject()))); + Collection fields = obj.getObject().getAllFields(); + assertThat(fields.size(), is(1)); + + MLInt8 intField = (MLInt8) fields.toArray()[0]; + assertThat(intField.getSize(), is(1)); + assertThat(intField.get(0).byteValue(), is((byte)25)); + + assertThat(obj.getObject().getFieldNames().iterator().next(), is("myelement")); + + + obj = (MLObject) content.get("obj3"); + assertThat(obj, is(notNullValue())); + + assertThat(obj.getName(), is("obj3")); + assertThat(obj.getClassName(), is("HandleSingle")); + assertThat(((MLObject) content.get("obj1")).getObject(), is(sameInstance(obj.getObject()))); + fields = obj.getObject().getAllFields(); + assertThat(fields.size(), is(1)); + + intField = (MLInt8) fields.toArray()[0]; + assertThat(intField.getSize(), is(1)); + assertThat(intField.get(0).byteValue(), is((byte)25)); + + assertThat(obj.getObject().getFieldNames().iterator().next(), is("myelement")); + + + + + obj = (MLObject) content.get("obj2"); + assertThat(obj, is(notNullValue())); + + assertThat(obj.getName(), is("obj2")); + assertThat(obj.getClassName(), is("HandleSingle")); + assertThat(((MLObject) content.get("obj4")).getObject(), is(sameInstance(obj.getObject()))); + fields = obj.getObject().getAllFields(); + assertThat(fields.size(), is(1)); + + MLChar charField = (MLChar) fields.toArray()[0]; + assertThat(charField.getString(0), is("testing")); + + assertThat(obj.getObject().getFieldNames().iterator().next(), is("myelement")); + + + obj = (MLObject) content.get("obj4"); + assertThat(obj, is(notNullValue())); + + assertThat(obj.getName(), is("obj4")); + assertThat(obj.getClassName(), is("HandleSingle")); + assertThat(((MLObject) content.get("obj2")).getObject(), is(sameInstance(obj.getObject()))); + fields = obj.getObject().getAllFields(); + assertThat(fields.size(), is(1)); + + charField = (MLChar) fields.toArray()[0]; + assertThat(charField.getString(0), is("testing")); + + assertThat(obj.getObject().getFieldNames().iterator().next(), is("myelement")); + } + private File fileFromStream(String location) throws IOException { String outname = location.replace("/", "_"); diff --git a/src/test/resources/mcos/handlesingle_multiple.mat b/src/test/resources/mcos/handlesingle_multiple.mat new file mode 100644 index 0000000000000000000000000000000000000000..e9316c6c59eef689cb6db00fd6e943c62919af2f GIT binary patch literal 1768 zcmd5*!7c+)6n&*AL1I_C_W?*-tw>i&LM4VKny_QULuERZs)e0pKgDNQTKWNd%YK4$ z+8#3*q*l1enLGEsd)~YEzWcmNeXp`XotzEbIT)DJbE|zQ|moM5M%F~z`NNU%c$6pti1Tw$^KuMXj zX`Nl?slBa(!xVh8K-CY=19#X6F9XLo@~eROb96!`0PaUWxderFw*O~dnTFP+N942m z&+;k!RzB+0i2fA+A|IQM?tgl4w;%VZ0!ccs& z?l-J6R`INhK5AkEI33~<`UT*+q<0K~(H^(D@mf=4-Uot%zw<`&!OqKORs14C3jdj_w@>B-uaKK~X*fE9!&hki0ISy5$#- f=N$vo6xK;Q{w}L9#s8`g$vU_