Skip to content

Commit cecf793

Browse files
Test case with struct containing timeseries
The data in the mat file created with: s = struct('test', timeseries(1:5, 1:5, ones(5, 1)))
1 parent cd8809b commit cecf793

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Code licensed under new-style BSD (see LICENSE).
3+
* All code up to tags/original: Copyright (c) 2006, Wojciech Gradkowski
4+
* All code after tags/original: Copyright (c) 2015, DiffPlug
5+
*/
6+
package com.jmatio.io;
7+
8+
import static org.hamcrest.CoreMatchers.*;
9+
import static org.junit.Assert.*;
10+
11+
import java.util.Map;
12+
13+
import org.hamcrest.CoreMatchers;
14+
import org.hamcrest.CustomMatcher;
15+
import org.hamcrest.Matcher;
16+
import org.junit.Test;
17+
import org.junit.runner.RunWith;
18+
import org.junit.runners.JUnit4;
19+
20+
import com.jmatio.types.MLArray;
21+
import com.jmatio.types.MLChar;
22+
import com.jmatio.types.MLDouble;
23+
import com.jmatio.types.MLInt8;
24+
import com.jmatio.types.MLObject;
25+
import com.jmatio.types.MLStructure;
26+
import com.jmatio.types.MLStructureObjectBase;
27+
28+
/**
29+
* This test verifies {@link <a href="https://github.com/diffplug/matfilerw/issues/13">issue #13</a>}.
30+
* <p>
31+
* During tests the introspection of data did not show essential vectors.
32+
* It comes out that the properties are propagated, but their corresponding
33+
* names are not propagated to {@link MLStructureObjectBase#getFieldNames()}.
34+
*
35+
* @author Piotr Smolinski <piotr.smolinski.77@gmail.com>
36+
*/
37+
@RunWith(JUnit4.class)
38+
public class StructureWithTimeseriesTest {
39+
40+
/**
41+
* Tests reading of a timeseries object contained in a struct.
42+
*/
43+
@Test
44+
public void testReadingTimeSeries() throws Exception {
45+
46+
MatFileReader reader = new MatFileReader(
47+
getClass().getResourceAsStream("/timeseries.mat"),
48+
MatFileType.Regular);
49+
50+
Map<String, MLArray> content = reader.getContent();
51+
52+
assertThat(content.get("s"), is(instanceOf(MLStructure.class)));
53+
MLStructure s = (MLStructure) content.get("s");
54+
55+
assertThat(s.getField("test"), is(instanceOf(MLObject.class)));
56+
MLObject test = (MLObject) s.getField("test");
57+
assertThat(test.getClassName(), equalTo("timeseries"));
58+
59+
assertThat(test.getField("Data_"), is(instanceOf(MLDouble.class)));
60+
MLDouble data = (MLDouble) test.getField("Data_");
61+
assertThat(data.getSize(), equalTo(5));
62+
63+
assertThat(test.getField("Quality_"), is(instanceOf(MLInt8.class)));
64+
MLInt8 quality = (MLInt8) test.getField("Quality_");
65+
assertThat(quality.getSize(), equalTo(5));
66+
67+
assertThat(test.getField("Time_"), isMatlabUndefined());
68+
69+
assertThat(test.getField("DataInfo"), not(isMatlabUndefined()));
70+
assertThat(test.getField("DataInfo"), is(instanceOf(MLObject.class)));
71+
MLObject dataInfo = (MLObject) test.getField("DataInfo");
72+
assertThat(dataInfo.getClassName(), equalTo("datametadata"));
73+
74+
assertThat(dataInfo.getField("Interpolation"), is(instanceOf(MLObject.class)));
75+
assertThat(dataInfo.getFieldNames(), hasItem("Interpolation"));
76+
77+
assertThat(test.getField("QualityInfo"), not(isMatlabUndefined()));
78+
assertThat(test.getField("QualityInfo"), is(instanceOf(MLObject.class)));
79+
MLObject qualityInfo = (MLObject) test.getField("QualityInfo");
80+
assertThat(qualityInfo.getClassName(), equalTo("qualmetadata"));
81+
82+
assertThat(qualityInfo.getField("Version"), is(instanceOf(MLDouble.class)));
83+
assertThat(qualityInfo.getFieldNames(), hasItem("Version"));
84+
85+
assertThat(test.getField("TimeInfo"), not(isMatlabUndefined()));
86+
assertThat(test.getField("TimeInfo"), is(instanceOf(MLObject.class)));
87+
MLObject timeInfo = (MLObject) test.getField("TimeInfo");
88+
assertThat(timeInfo.getClassName(), equalTo("timemetadata"));
89+
90+
// apparently this works even when #13 fix is not applied
91+
assertThat(timeInfo.getField("Time_"), is(instanceOf(MLDouble.class)));
92+
// but this fails
93+
assertThat(timeInfo.getFieldNames(), hasItem("Time_"));
94+
MLDouble timeInfoTime = (MLDouble) timeInfo.getField("Time_");
95+
assertThat(timeInfoTime.getSize(), equalTo(5));
96+
97+
assertThat(timeInfo.getField("Units"), is(instanceOf(MLChar.class)));
98+
assertThat(timeInfo.getFieldNames(), hasItem("Units"));
99+
MLChar timeInfoUnits = (MLChar) timeInfo.getField("Units");
100+
assertThat(timeInfoUnits.getString(0), equalTo("seconds"));
101+
102+
}
103+
104+
/**
105+
* Tests if given {@link MLArray} is undefined in sense of JMatIO.
106+
* Actually this should just return null, but in the JMatIO empty
107+
* properties are resolved to empty double vector
108+
*/
109+
private Matcher<MLArray> isMatlabUndefined() {
110+
return CoreMatchers.allOf(
111+
is(instanceOf(MLDouble.class)),
112+
new CustomMatcher<MLArray>("empty double vector") {
113+
@Override
114+
public boolean matches(Object item) {
115+
if (!(item instanceof MLDouble))
116+
return false;
117+
return ((MLDouble) item).getSize() == 0;
118+
}
119+
});
120+
}
121+
122+
}

src/test/resources/timeseries.mat

1.36 KB
Binary file not shown.

0 commit comments

Comments
 (0)