1 // =============================================================================
2 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 // Copyright (C) 2008 - INRIA - Vincent Couvert <vincent.couvert@inria.fr>
5 // This file is distributed under the same license as the Scilab package.
6 // =============================================================================
8 // <-- CLI SHELL MODE -->
10 // This Scilab script loads MAT-files to test the MATIO module
15 // Loads all MAT-files
18 binFormats = ["-v4";"-v6";"-v7"];
19 for formatIndex = 1:size(binFormats, "*")
20 matFiles = gsort(listfiles("SCI/modules/matio/tests/unit_tests/*" + binFormats(formatIndex) + ".mat"), "r", "i");
21 for fileIndex=1:size(matFiles, "*")
22 disp(fileparts(matFiles(fileIndex), "fname") + fileparts(matFiles(fileIndex), "extension"));
23 loadmatfile(matFiles(fileIndex));
27 // TESTS FOR CELL ARRAYS
29 if binFormats(formatIndex) <> "-v4" then // Cell arrays can not be stored in Matlab 4 binary files
31 assert_checkequal(emptycell , cell());
32 assert_checkequal(scalarcell , makecell([1 1], 1.23));
33 assert_checkequal(rowcell , makecell([1 3], "abc", [1.23+7.89*%i 4.56-1.23*%i 7.89+4.56*%i], 1.23));
34 assert_checkequal(colcell , makecell([3 1], [1.23+7.89*%i 4.56-1.23*%i 7.89+4.56*%i], "abc", 1.23));
35 assert_checkequal(arraycell , makecell([2 3], "a", [1.23+7.89*%i 4.56-1.23*%i 7.89+4.56*%i], int8(1), "abc", 1.23, eye(100,100)));
36 assert_checkequal(stringcell , makecell([2 3], "abc", "def", "ghi", "jkl", "mno", "pqr"));
38 clear emptycell scalarcell rowcell colcell arraycell stringcell
42 // TESTS FOR STRUCTURE ARRAYS
44 if binFormats(formatIndex) <> "-v4" then // Struct arrays can not be stored in Matlab 4 binary files
46 sciemptystruct = struct();
47 assert_checkequal(emptystruct , sciemptystruct);
49 sciscalarstruct = struct("f1", 10, "ftwo", "Hello", "field3", int8(12));
50 assert_checkequal(scalarstruct , sciscalarstruct);
52 scirowstruct = struct("field1", 10, "field2", "Hello", "field3", int8(12));
53 scirowstruct(1,2).field1 = "test";
54 scirowstruct(1,2).field2 = eye(10, 10);
55 scirowstruct(1,3).field2 = "a field contents";
56 scirowstruct(1,3).field3 = 1.23+4.56*%i;
57 assert_checkequal(rowstruct , scirowstruct);
59 scicolstruct = struct("name", 10, "phone", "Hello", "address", int8(12));
60 scicolstruct(2,1).name = "test";
61 scicolstruct(2,1).phone = eye(10, 10);
62 scicolstruct(3,1).phone = "a field contents";
63 scicolstruct(3,1).address = 1.23+4.56*%i;
64 assert_checkequal(colstruct , scicolstruct);
66 sciarraystruct = struct("field1", 10, "field2", "Hello", "field3", int8(12));
67 sciarraystruct(1,2).field1 = "test";
68 sciarraystruct(1,2).field2 = eye(10, 10);
69 sciarraystruct(1,3).field2 = "a field contents";
70 sciarraystruct(1,3).field3 = 1.23+4.56*%i;
71 sciarraystruct(2,1).name = "test";
72 sciarraystruct(2,1).phone = eye(10, 10);
73 sciarraystruct(3,1).phone = "a field contents";
74 sciarraystruct(3,1).address = 1.23+4.56*%i;
75 assert_checkequal(arraystruct , sciarraystruct);
78 // TESTS FOR STRUCTURE ARRAYS SPARSE
80 if binFormats(formatIndex) == "-v7" then
81 assert_checkequal(structure.test,sparse([1 0; 2 0; 0 3]));
85 clear emptystruct scalarstruct rowstruct colstruct arraystruct
86 clear sciemptystruct sciscalarstruct scirowstruct scicolstruct sciarraystruct
98 // TESTS FOR CHARACTER ARRAYS
101 assert_checkequal(emptystringmatrix , "");
102 assert_checkequal(stringscalar , "a");
103 assert_checkequal(stringrowvector , ["abc"]);
104 assert_checkequal(stringcolvector , ["a";"b";"c"]);
105 assert_checkequal(stringmatrix , ["abc";"def"]);
107 clear emptystringmatrix stringscalar stringrowvector stringcolvector stringmatrix
110 // TESTS FOR DOUBLE PRECISION ARRAYS
113 assert_checkequal(emptydoublematrix , []);
115 assert_checkequal(realdoublescalar , 1.23);
116 assert_checkequal(cplxdoublescalar , 1.23 + 4.56*%i);
118 assert_checkequal(realdoublerowvector , [1.23 -4.56 7.89]);
119 assert_checkequal(cplxdoublerowvector , [1.23+7.89*%i 4.56-1.23*%i 7.89+4.56*%i]);
121 assert_checkequal(realdoublecolvector , [1.23;-4.56;7.89]);
122 assert_checkequal(cplxdoublecolvector , [1.23+7.89*%i;4.56-1.23*%i;7.89+4.56*%i]);
124 assert_checkequal(realdoublematrix , [1.23 -4.56 7.89;9.87 6.54 -3.21]);
125 assert_checkequal(cplxdoublematrix , [1.23+7.89*%i 4.56-1.23*%i 7.89+4.56*%i;9.87+3.21*%i 6.54+9.87*%i 3.21-6.54*%i]);
127 clear emptydoublematrix realdoublescalar cplxdoublescalar realdoublerowvector cplxdoublerowvector realdoublecolvector cplxdoublecolvector realdoublematrix cplxdoublematrix
130 // TESTS FOR SINGLE PRECISION ARRAYS
136 // TESTS FOR SPARSE MATRICES
139 if binFormats(formatIndex) <> "-v4" then // Integers can not be stored in Matlab 4 binary files
140 assert_checkequal(emptysparse , []);
142 assert_checkequal(realscalarsparse , sparse(1.23));
143 //assert_checkequal(cplxscalarsparse , sparse(1.23 + 4.56i));
145 assert_checkequal(realrowvectorsparse , sparse([1.23 0 7.89]));
146 //assert_checkequal(cplxrowvectorsparse , sparse([1.23+7.89i 0 7.89+4.56i]));
148 assert_checkequal(realcolvectorsparse , sparse([1.23;0;7.89]));
149 //assert_checkequal(cplxcolvectorsparse , sparse([1.23+7.89i;;7.89+4.56i]));
151 assert_checkequal(realmatrixsparse , sparse([1.23 0 7.89;0 6.54 -3.21]));
152 //assert_checkequal(cplxmatrixsparse , sparse([1.23+7.89i 0 7.89+4.56i;0 6.54+9.87i 3.21-6.54i]));
154 if binFormats(formatIndex) <> "-v4" then // Integers can not be stored in Matlab 4 binary files
157 // TESTS FOR 8-BITS SIGNED INTEGERS
160 assert_checkequal(emptyint8matrix , int8([]));
161 assert_checkequal(int8scalar , int8(1));
162 assert_checkequal(int8rowvector , int8([1 -4 7]));
163 assert_checkequal(int8colvector , int8([1;-4;7]));
164 assert_checkequal(int8matrix , int8([1 -4 7;-9 6 -3]));
166 clear emptyint8matrix int8scalar int8rowvector int8colvector int8matrix
169 // TESTS FOR 16-BITS SIGNED INTEGERS
172 assert_checkequal(emptyint16matrix , int16([]));
173 assert_checkequal(int16scalar , int16(1));
174 assert_checkequal(int16rowvector , int16([1 -4 7]));
175 assert_checkequal(int16colvector , int16([1;-4;7]));
176 assert_checkequal(int16matrix , int16([1 -4 7;-9 6 -3]));
178 clear emptyint16matrix int16scalar int16rowvector int16colvector int16matrix
181 // TESTS FOR 32-BITS SIGNED INTEGERS
184 assert_checkequal(emptyint32matrix , int32([]));
185 assert_checkequal(int32scalar , int32(1));
186 assert_checkequal(int32rowvector , int32([1 -4 7]));
187 assert_checkequal(int32colvector , int32([1;-4;7]));
188 assert_checkequal(int32matrix , int32([1 -4 7;-9 6 -3]));
190 clear emptyint32matrix int32scalar int32rowvector int32colvector int32matrix
192 // TODO: int64 tests ?
195 // TESTS FOR 8-BITS UNSIGNED INTEGERS
198 assert_checkequal(emptyuint8matrix , uint8([]));
199 assert_checkequal(uint8scalar , uint8(1));
200 //assert_checkequal(uint8rowvector , uint8([1 -4 7]));
201 assert_checkequal(uint8rowvector , uint8([1 0 7]));
202 //assert_checkequal(uint8colvector , uint8([1;-4;7]));
203 assert_checkequal(uint8colvector , uint8([1;0;7]));
204 //assert_checkequal(uint8matrix , uint8([1 -4 7;-9 6 -3]));
205 assert_checkequal(uint8matrix , uint8([1 0 7;0 6 0]));
207 clear emptyuint8matrix uint8scalar uint8rowvector uint8colvector uint8matrix
210 // TESTS FOR 16-BITS UNSIGNED INTEGERS
213 assert_checkequal(emptyuint16matrix , uint16([]));
214 assert_checkequal(uint16scalar , uint16(1));
215 //assert_checkequal(uint16rowvector , uint16([1 -4 7]));
216 assert_checkequal(uint16rowvector , uint16([1 0 7]));
217 //assert_checkequal(uint16colvector , uint16([1;-4;7]));
218 assert_checkequal(uint16colvector , uint16([1;0;7]));
219 //assert_checkequal(uint16matrix , uint16([1 -4 7;-9 6 -3]));
220 assert_checkequal(uint16matrix , uint16([1 0 7;0 6 0]));
222 clear emptyuint16matrix uint16scalar uint16rowvector uint16colvector uint16matrix
225 // TESTS FOR 32-BITS UNSIGNED INTEGERS
228 assert_checkequal(emptyuint32matrix , uint32([]));
229 assert_checkequal(uint32scalar , uint32(1));
230 //assert_checkequal(uint32rowvector , uint32([1 -4 7]));
231 assert_checkequal(uint32rowvector , uint32([1 0 7]));
232 //assert_checkequal(uint32colvector , uint32([1;-4;7]));
233 assert_checkequal(uint32colvector , uint32([1;0;7]));
234 //assert_checkequal(uint32matrix , uint32([1 -4 7;-9 6 -3]));
235 assert_checkequal(uint32matrix , uint32([1 0 7;0 6 0]));
237 clear emptyuint32matrix uint32scalar uint32rowvector uint32colvector uint32matrix
239 // TODO: uint64 tests ?
243 // TESTS FOR ND-ARRAYS
245 assert_checkequal(emptyNDarray , []);
246 assert_checkequal(realdoubleNDarray , matrix(1:24, 2, 3, 4));
247 assert_checkequal(complexdoubleNDarray , matrix((1:24) + (1:24)*%i, 2, 3, 4));
249 clear emptyNDarray realdoubleNDarray complexdoubleNDarray
256 assert_checkequal(eye100x100 , eye(100,100));
260 // TODO: read all variables from a single file ?