Commit be24249f authored by Matthew Dawson's avatar Matthew Dawson
Browse files

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.
parent 0378fc0b
Loading
Loading
Loading
Loading
+75 −0
Original line number Diff line number Diff line
@@ -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<String, MLArray> 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<MLArray> 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("/", "_");
+1.73 KiB

File added.

No diff preview for this file type.