2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010 - DIGITEO - Sylvestre LEDRU
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
12 package org.scilab.tests.modules.types;
14 import org.testng.annotations.*;
16 import java.util.Arrays;
18 import org.scilab.modules.types.ScilabType;
19 import org.scilab.modules.types.ScilabDouble;
20 import org.scilab.modules.types.ScilabInteger;
21 import org.scilab.modules.types.ScilabIntegerTypeEnum;
22 import org.scilab.modules.types.ScilabBoolean;
23 import org.scilab.modules.types.ScilabString;
24 import org.scilab.modules.types.ScilabList;
25 import org.scilab.modules.types.ScilabTList;
26 import org.scilab.modules.types.ScilabMList;
28 public class testEquals {
31 public void compareDoubleTest() throws NullPointerException {
32 double [][]a={{21.2, 22.0, 42.0, 39.0},{23.2, 24.0, 44.0, 40.0}};
33 double [][]b={{21.2, 22.0, 42.0, 39.0},{23.2, 24.0, 44.0, 40.0}};
34 double [][]c={{42,43},{21,22}};
35 ScilabDouble aMatrix = new ScilabDouble(a);
36 ScilabDouble bMatrix = new ScilabDouble(b);
37 ScilabDouble cMatrix = new ScilabDouble(c);
38 assert aMatrix.equals(bMatrix) == true;
39 assert bMatrix.equals(aMatrix) == true;
40 assert cMatrix.equals(aMatrix) == false;
41 assert Arrays.deepEquals(aMatrix.getRealPart(), bMatrix.getRealPart()) == true;
42 assert aMatrix.toString().equals("[21.2, 22.0, 42.0, 39.0 ; 23.2, 24.0, 44.0, 40.0]") == true;
43 assert cMatrix.toString().equals("[42.0, 43.0 ; 21.0, 22.0]") == true;
47 public void doubleMatrixTests() {
48 double [][]a={{21.2, 22.0, 42.0, 39.0},{23.2, 24.0, 44.0, 40.0}};
49 ScilabDouble emptyMatrix = new ScilabDouble();
50 assert emptyMatrix.getHeight() == 0;
51 assert emptyMatrix.getWidth() == 0;
52 assert emptyMatrix.isEmpty() == true;
53 assert emptyMatrix.toString().equals("[]") == true;
54 emptyMatrix.setRealPart(a);
55 emptyMatrix.setImaginaryPart(a);
57 ScilabBoolean aMatrix = new ScilabBoolean(true);
58 assert aMatrix.equals(emptyMatrix) == false;
59 assert emptyMatrix.equals(aMatrix) == false;
64 public void compareDoubleComplexTest() throws NullPointerException {
65 double [][]a={{21.2, 22.0, 42.0, 39.0},{23.2, 24.0, 44.0, 40.0}};
66 double [][]aImg={{210.2, 220.0, 420.0, 390.0},{230.2, 240.0, 440.0, 400.0}};
67 double [][]b={{21.2, 22.0, 42.0, 39.0},{23.2, 24.0, 44.0, 40.0}};
68 double [][]bImg={{210.2, 220.0, 420.0, 390.0},{230.2, 240.0, 440.0, 400.0}};
69 double [][]c={{42,43},{21,22}};
70 double [][]cImg={{420,430},{210,220}};
71 ScilabDouble aMatrix = new ScilabDouble(a, aImg);
72 ScilabDouble bMatrix = new ScilabDouble(b, bImg);
73 ScilabDouble cMatrix = new ScilabDouble(c, cImg);
74 assert aMatrix.equals(bMatrix) == true;
75 assert bMatrix.equals(aMatrix) == true;
76 assert cMatrix.equals(aMatrix) == false;
77 assert Arrays.deepEquals(aMatrix.getRealPart(), bMatrix.getRealPart()) == true;
78 assert Arrays.deepEquals(aMatrix.getImaginaryPart(), bMatrix.getImaginaryPart()) == true;
79 assert aMatrix.toString().equals("[21.2 + 210.2 * %i, 22.0 + 220.0 * %i, 42.0 + 420.0 * %i, 39.0 + 390.0 * %i ; 23.2 + 230.2 * %i, 24.0 + 240.0 * %i, 44.0 + 440.0 * %i, 40.0 + 400.0 * %i]") == true;
80 assert cMatrix.toString().equals("[42.0 + 420.0 * %i, 43.0 + 430.0 * %i ; 21.0 + 210.0 * %i, 22.0 + 220.0 * %i]") == true;
82 double [] result = new double[]{21.2, 23.2, 22.0, 24.0, 42.0, 44.0, 39.0, 40.0, 210.2, 230.2, 220.0, 240.0, 420.0, 440.0, 390.0, 400.0};
83 assert aMatrix.getSerializedComplexMatrix().length == result.length;
84 assert Arrays.equals(aMatrix.getSerializedComplexMatrix(),result) == true;
85 ScilabDouble scalarComplex = new ScilabDouble(1,2);
86 assert aMatrix.equals(scalarComplex) == false;
87 assert aMatrix.equals(bMatrix) == true;
92 public void compareInteger8Test() throws NullPointerException {
93 byte [][]a={{32,42,41}, {12,13,32}};
94 byte [][]b={{32,42,41}, {12,13,32}};
95 byte [][]c={{42,43},{21,22}};
96 ScilabInteger aMatrix = new ScilabInteger(a, true);
97 ScilabInteger bMatrix = new ScilabInteger(b, true);
98 ScilabInteger cMatrix = new ScilabInteger(c, true);
99 ScilabInteger dMatrix = new ScilabInteger(c, false);
100 assert aMatrix.equals(bMatrix) == true;
101 assert bMatrix.equals(aMatrix) == true;
102 assert cMatrix.equals(aMatrix) == false;
103 assert cMatrix.equals(dMatrix) == true;
104 assert Arrays.deepEquals(aMatrix.getData(), bMatrix.getData()) == true;
105 assert aMatrix.toString().equals("uint8([32, 42, 41 ; 12, 13, 32])") == true;
106 assert cMatrix.toString().equals("uint8([42, 43 ; 21, 22])") == true;
107 assert dMatrix.toString().equals("int8([42, 43 ; 21, 22])") == true;
108 assert aMatrix.getDataAsByte().length == a.length;
109 assert aMatrix.getDataAsByte()[0].length == a[0].length;
110 byte [][]d = aMatrix.getDataAsByte();
111 long [][]d2 = aMatrix.getData();
112 assert d[0][0] == 32;
113 assert d2[0][0] == 32;
114 assert Arrays.deepEquals(a,d);
115 assert aMatrix.getPrec() == ScilabIntegerTypeEnum.sci_uint8;
116 assert dMatrix.getPrec() == ScilabIntegerTypeEnum.sci_int8;
117 assert aMatrix.isUnsigned() == true;
118 assert dMatrix.isUnsigned() == false;
119 assert ScilabInteger.convertOldType("TYPE8", true) == ScilabIntegerTypeEnum.sci_uint8;
120 assert ScilabInteger.convertOldType("TYPE8", false) == ScilabIntegerTypeEnum.sci_int8;
125 public void integer8MatrixTests() {
126 byte [][]a={{21, 22, 42, 39},{23, 24, 44, 40}};
127 ScilabInteger emptyMatrix = new ScilabInteger();
128 assert emptyMatrix.getHeight() == 0;
129 assert emptyMatrix.getWidth() == 0;
130 assert emptyMatrix.isEmpty() == true;
131 assert emptyMatrix.toString().equals("int([])") == true;
132 emptyMatrix.setData(a, true);
134 ScilabBoolean aMatrix = new ScilabBoolean(true);
135 assert aMatrix.equals(emptyMatrix) == false;
136 assert emptyMatrix.equals(aMatrix) == false;
137 ScilabInteger scalarInteger = new ScilabInteger((byte)2);
142 public void compareInteger16Test() throws NullPointerException {
143 short [][]a={{32,42,41}, {12,13,32}};
144 short [][]b={{32,42,41}, {12,13,32}};
145 short [][]c={{42,43},{21,22}};
146 ScilabInteger aMatrix = new ScilabInteger(a, true);
147 ScilabInteger bMatrix = new ScilabInteger(b, true);
148 ScilabInteger cMatrix = new ScilabInteger(c, true);
149 ScilabInteger dMatrix = new ScilabInteger(c, false);
150 assert aMatrix.equals(bMatrix) == true;
151 assert bMatrix.equals(aMatrix) == true;
152 assert cMatrix.equals(aMatrix) == false;
153 assert cMatrix.equals(dMatrix) == true;
154 assert Arrays.deepEquals(aMatrix.getData(), bMatrix.getData()) == true;
155 assert aMatrix.toString().equals("uint16([32, 42, 41 ; 12, 13, 32])") == true;
156 assert cMatrix.toString().equals("uint16([42, 43 ; 21, 22])") == true;
157 assert dMatrix.toString().equals("int16([42, 43 ; 21, 22])") == true;
158 assert aMatrix.getDataAsShort().length == a.length;
159 assert aMatrix.getDataAsShort()[0].length == a[0].length;
160 short [][]d = aMatrix.getDataAsShort();
161 long [][]d2 = aMatrix.getData();
162 assert d[0][0] == 32;
163 assert d2[0][0] == 32;
164 assert Arrays.deepEquals(a,d);
165 assert aMatrix.getPrec() == ScilabIntegerTypeEnum.sci_uint16;
166 assert dMatrix.getPrec() == ScilabIntegerTypeEnum.sci_int16;
167 assert aMatrix.isUnsigned() == true;
168 assert dMatrix.isUnsigned() == false;
169 assert ScilabInteger.convertOldType("TYPE16", true) == ScilabIntegerTypeEnum.sci_uint16;
170 assert ScilabInteger.convertOldType("TYPE16", false) == ScilabIntegerTypeEnum.sci_int16;
174 public void integer16MatrixTests() {
175 short [][]a={{21, 22, 42, 39},{23, 24, 44, 40}};
176 ScilabInteger emptyMatrix = new ScilabInteger();
177 emptyMatrix.setData(a, true);
179 ScilabBoolean aMatrix = new ScilabBoolean(true);
180 assert aMatrix.equals(emptyMatrix) == false;
181 assert emptyMatrix.equals(aMatrix) == false;
182 ScilabInteger scalarInteger = new ScilabInteger((short)2);
186 public void compareInteger32Test() throws NullPointerException {
187 int [][]a={{32,42,41}, {12,13,32}};
188 int [][]b={{32,42,41}, {12,13,32}};
189 int [][]c={{42,43},{21,22}};
190 ScilabInteger aMatrix = new ScilabInteger(a, true);
191 ScilabInteger bMatrix = new ScilabInteger(b, true);
192 ScilabInteger cMatrix = new ScilabInteger(c, true);
193 ScilabInteger dMatrix = new ScilabInteger(c, false);
194 assert aMatrix.equals(bMatrix) == true;
195 assert bMatrix.equals(aMatrix) == true;
196 assert cMatrix.equals(aMatrix) == false;
197 assert cMatrix.equals(dMatrix) == true;
198 assert Arrays.deepEquals(aMatrix.getData(), bMatrix.getData()) == true;
199 assert aMatrix.toString().equals("uint32([32, 42, 41 ; 12, 13, 32])") == true;
200 assert cMatrix.toString().equals("uint32([42, 43 ; 21, 22])") == true;
201 assert dMatrix.toString().equals("int32([42, 43 ; 21, 22])") == true;
202 assert aMatrix.getDataAsInt().length == a.length;
203 assert aMatrix.getDataAsInt()[0].length == a[0].length;
204 int [][]d = aMatrix.getDataAsInt();
205 long [][]d2 = aMatrix.getData();
206 assert d[0][0] == 32;
207 assert d2[0][0] == 32;
208 assert Arrays.deepEquals(a,d);
209 assert aMatrix.getPrec() == ScilabIntegerTypeEnum.sci_uint32;
210 assert dMatrix.getPrec() == ScilabIntegerTypeEnum.sci_int32;
211 assert aMatrix.isUnsigned() == true;
212 assert dMatrix.isUnsigned() == false;
213 assert ScilabInteger.convertOldType("TYPE32", true) == ScilabIntegerTypeEnum.sci_uint32;
214 assert ScilabInteger.convertOldType("TYPE32", false) == ScilabIntegerTypeEnum.sci_int32;
218 public void integer32MatrixTests() {
219 int [][]a={{21, 22, 42, 39},{23, 24, 44, 40}};
220 ScilabInteger emptyMatrix = new ScilabInteger();
221 emptyMatrix.setData(a, true);
223 ScilabBoolean aMatrix = new ScilabBoolean(true);
224 assert aMatrix.equals(emptyMatrix) == false;
225 assert emptyMatrix.equals(aMatrix) == false;
226 ScilabInteger scalarInteger = new ScilabInteger((int)2);
230 public void compareInteger64Test() throws NullPointerException {
231 long [][]a={{32,42,41}, {12,13,32}};
232 long [][]b={{32,42,41}, {12,13,32}};
233 long [][]c={{42,43},{21,22}};
234 ScilabInteger aMatrix = new ScilabInteger(a, true);
235 ScilabInteger bMatrix = new ScilabInteger(b, true);
236 ScilabInteger cMatrix = new ScilabInteger(c, true);
237 ScilabInteger dMatrix = new ScilabInteger(c, false);
238 assert aMatrix.equals(bMatrix) == true;
239 assert bMatrix.equals(aMatrix) == true;
240 assert cMatrix.equals(aMatrix) == false;
241 assert cMatrix.equals(dMatrix) == true;
242 assert Arrays.deepEquals(aMatrix.getData(), bMatrix.getData()) == true;
243 assert aMatrix.toString().equals("uint64([32, 42, 41 ; 12, 13, 32])") == true;
244 assert cMatrix.toString().equals("uint64([42, 43 ; 21, 22])") == true;
245 assert dMatrix.toString().equals("int64([42, 43 ; 21, 22])") == true;
246 assert aMatrix.getDataAsLong().length == a.length;
247 assert aMatrix.getDataAsLong()[0].length == a[0].length;
248 long [][]d = aMatrix.getDataAsLong();
249 long [][]d2 = aMatrix.getData();
250 assert d[0][0] == 32;
251 assert d2[0][0] == 32;
252 assert Arrays.deepEquals(a,d);
253 assert aMatrix.getPrec() == ScilabIntegerTypeEnum.sci_uint64;
254 assert dMatrix.getPrec() == ScilabIntegerTypeEnum.sci_int64;
255 assert aMatrix.isUnsigned() == true;
256 assert dMatrix.isUnsigned() == false;
257 assert ScilabInteger.convertOldType("TYPE64", true) == ScilabIntegerTypeEnum.sci_uint64;
258 assert ScilabInteger.convertOldType("TYPE64", false) == ScilabIntegerTypeEnum.sci_int64;
262 public void integer64MatrixTests() {
263 long [][]a={{21, 22, 42, 39},{23, 24, 44, 40}};
264 ScilabInteger emptyMatrix = new ScilabInteger();
265 emptyMatrix.setData(a, true);
267 ScilabBoolean aMatrix = new ScilabBoolean(true);
268 assert aMatrix.equals(emptyMatrix) == false;
269 assert emptyMatrix.equals(aMatrix) == false;
270 ScilabInteger scalarInteger = new ScilabInteger((long)2);
275 public void compareBooleanTest() throws NullPointerException {
276 boolean [][]a={{true,false,true}, {true,true,true}};
277 boolean [][]b={{true,false,true}, {true,true,true}};
278 boolean [][]c={{true,false},{false,true}};
279 ScilabBoolean aMatrix = new ScilabBoolean(a);
280 ScilabBoolean bMatrix = new ScilabBoolean(b);
281 ScilabBoolean cMatrix = new ScilabBoolean(c);
282 assert aMatrix.equals(bMatrix) == true;
283 assert bMatrix.equals(aMatrix) == true;
284 assert cMatrix.equals(aMatrix) == false;
285 assert Arrays.deepEquals(aMatrix.getData(), bMatrix.getData()) == true;
286 assert aMatrix.toString().equals("[%t, %f, %t ; %t, %t, %t]") == true;
287 assert cMatrix.toString().equals("[%t, %f ; %f, %t]") == true;
291 public void booleanMatrixTests() {
292 boolean [][]a={{true,false,true}, {true,true,true}};
293 ScilabBoolean emptyMatrix = new ScilabBoolean();
294 assert emptyMatrix.getHeight() == 0;
295 assert emptyMatrix.getWidth() == 0;
296 assert emptyMatrix.isEmpty() == true;
297 assert emptyMatrix.toString().equals("[]") == true;
298 emptyMatrix.setData(a);
299 ScilabDouble aMatrix = new ScilabDouble(2);
300 assert aMatrix.equals(emptyMatrix) == false;
301 assert emptyMatrix.equals(aMatrix) == false;
305 public void compareStringTest() throws NullPointerException {
306 String [][]a={{"This","is","my","string"},{"and","I want to", "compare"," them"}};
307 String [][]b={{"This","is","my","string"},{"and","I want to", "compare"," them"}};
308 String [][]c={{"Wrong","string"},{"right","string"}};
309 ScilabString aMatrix = new ScilabString(a);
310 ScilabString bMatrix = new ScilabString(b);
311 ScilabString cMatrix = new ScilabString(c);
312 assert aMatrix.equals(bMatrix) == true;
313 assert bMatrix.equals(aMatrix) == true;
314 assert cMatrix.equals(aMatrix) == false;
315 assert Arrays.deepEquals(aMatrix.getData(), bMatrix.getData()) == true;
316 assert aMatrix.toString().equals("[\"This\", \"is\", \"my\", \"string\" ; \"and\", \"I want to\", \"compare\", \" them\"]") == true;
317 assert cMatrix.toString().equals("[\"Wrong\", \"string\" ; \"right\", \"string\"]") == true;
321 @Test(expectedExceptions = IllegalArgumentException.class)
322 public void stringMatrixTests() {
323 String [][]a={{"This","is","my","string"},{"and","I want to", "compare"," them"}};
324 ScilabString emptyMatrix = new ScilabString();
325 assert emptyMatrix.getHeight() == 0;
326 assert emptyMatrix.getWidth() == 0;
327 assert emptyMatrix.isEmpty() == true;
328 assert emptyMatrix.toString().equals("[]") == true;
329 emptyMatrix.setData(a);
330 ScilabDouble aMatrix = new ScilabDouble(2);
331 assert aMatrix.equals(emptyMatrix) == false;
332 assert emptyMatrix.equals(aMatrix) == false;
333 String []b={"This","is","my","string"};
334 ScilabString vectorString = new ScilabString(b);
336 ScilabString emptyString = new ScilabString(c);
337 ScilabString nullString = new ScilabString((String)null);
342 public void compareListTest() throws NullPointerException {
343 ScilabList data = new ScilabList();
344 assert data.getHeight() == 0;
345 assert data.getWidth() == 0;
346 assert data.toString().equals("list()");
347 data.add(new ScilabDouble(2));
348 assert data.getHeight() == 1;
349 assert data.getWidth() == 1;
350 data.add(new ScilabDouble(51));
351 assert data.getHeight() == 1;
352 assert data.getWidth() == 2;
353 data.add(new ScilabString("hello"));
354 ScilabList data2 = new ScilabList();
355 data2.add(new ScilabDouble(2));
356 data2.add(new ScilabDouble(51));
357 data2.add(new ScilabString("hello"));
358 ScilabList data3 = new ScilabList();
359 data3.add(new ScilabDouble(2));
360 data3.add(new ScilabDouble(42));
361 data3.add(new ScilabBoolean(true));
362 data3.add(new ScilabString("hello"));
363 assert data.equals(data2) == true;
364 assert data.equals(data3) == false;
365 assert data.toString().equals("list([2.0], [51.0], [\"hello\"])") == true;
366 assert data3.toString().equals("list([2.0], [42.0], [%t], [\"hello\"])") == true;
368 ScilabList data4 = new ScilabList(data3);
369 assert data4.equals(data3) == true;
375 public void compareTListTest() throws NullPointerException {
376 ScilabTList data = new ScilabTList();
377 assert data.getHeight() == 0;
378 assert data.getWidth() == 0;
379 assert data.toString().equals("tlist()");
380 data.add(new ScilabString("hello"));
381 assert data.getHeight() == 1;
382 assert data.getWidth() == 1;
383 data.add(new ScilabDouble(2));
384 assert data.getHeight() == 1;
385 assert data.getWidth() == 2;
386 data.add(new ScilabDouble(51));
387 assert data.getHeight() == 1;
388 assert data.getWidth() == 3;
389 ScilabTList data2 = new ScilabTList();
390 data2.add(new ScilabString("hello"));
391 data2.add(new ScilabDouble(2));
392 data2.add(new ScilabDouble(51));
393 ScilabTList data3 = new ScilabTList();
394 data3.add(new ScilabString("hello"));
395 data3.add(new ScilabDouble(2));
396 data3.add(new ScilabDouble(42));
397 data3.add(new ScilabBoolean(true));
398 assert data.equals(data2) == true;
399 assert data.equals(data3) == false;
401 assert data.toString().equals("tlist([\"hello\"], [2.0], [51.0])") == true;
402 assert data3.toString().equals("tlist([\"hello\"], [2.0], [42.0], [%t])") == true;
403 String []b={"a","b","c"};
405 ScilabTList tlist = new ScilabTList(b);
406 tlist.add(new ScilabDouble(2));
407 tlist.add(new ScilabDouble(3));
408 assert tlist.toString().equals("tlist([\"a\", \"b\", \"c\"], [2.0], [3.0])") == true;
409 ScilabTList tlistFromAnOther = new ScilabTList(b, data2);
410 assert tlistFromAnOther.getHeight() == 1;
411 assert tlistFromAnOther.getWidth() == 4;
412 assert tlistFromAnOther.toString().equals("tlist([\"a\", \"b\", \"c\"], [\"hello\"], [2.0], [51.0])");
417 public void compareMListTest() throws NullPointerException {
418 ScilabMList data = new ScilabMList();
419 assert data.getHeight() == 0;
420 assert data.getWidth() == 0;
421 assert data.toString().equals("mlist()");
422 data.add(new ScilabString("hello"));
423 assert data.getHeight() == 1;
424 assert data.getWidth() == 1;
425 data.add(new ScilabDouble(2));
426 assert data.getHeight() == 1;
427 assert data.getWidth() == 2;
428 data.add(new ScilabDouble(51));
429 assert data.getHeight() == 1;
430 assert data.getWidth() == 3;
431 ScilabMList data2 = new ScilabMList();
432 data2.add(new ScilabString("hello"));
433 data2.add(new ScilabDouble(2));
434 data2.add(new ScilabDouble(51));
435 ScilabMList data3 = new ScilabMList();
436 data3.add(new ScilabString("hello"));
437 data3.add(new ScilabDouble(2));
438 data3.add(new ScilabDouble(42));
439 data3.add(new ScilabBoolean(true));
440 assert data.equals(data2) == true;
441 assert data.equals(data3) == false;
443 assert data.toString().equals("mlist([\"hello\"], [2.0], [51.0])") == true;
444 assert data3.toString().equals("mlist([\"hello\"], [2.0], [42.0], [%t])") == true;
445 String []b={"a","b","c"};
447 ScilabMList mlist = new ScilabMList(b);
448 mlist.add(new ScilabDouble(2));
449 mlist.add(new ScilabDouble(3));
450 assert mlist.toString().equals("mlist([\"a\", \"b\", \"c\"], [2.0], [3.0])") == true;
451 ScilabMList mlistFromAnOther = new ScilabMList(b, data2);
452 assert mlistFromAnOther.getHeight() == 1;
453 assert mlistFromAnOther.getWidth() == 4;
454 assert mlistFromAnOther.toString().equals("mlist([\"a\", \"b\", \"c\"], [\"hello\"], [2.0], [51.0])");