-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestMatlabEngine.java
More file actions
1785 lines (1438 loc) · 74.9 KB
/
TestMatlabEngine.java
File metadata and controls
1785 lines (1438 loc) · 74.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package io.lambdacloud.test;
import static io.lambdacloud.MatlabEngine.exec;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Type;
import Jama.Matrix;
import io.lambdacloud.BytecodeSupport;
import io.lambdacloud.MatlabEngine;
import io.lambdacloud.node.VariableNode;
import io.lambdacloud.node.matrix.MatrixAccessNode;
import io.lambdacloud.util.CSList;
import io.lambdacloud.util.LogicalArray;
import io.lambdacloud.util.ObjectArray;
import io.lambdacloud.util.Struct;
import static org.objectweb.asm.Opcodes.*;
/**
* The type of an optional function parameter is set to null. All the null type of function parameters
* will be ignored when generating bytecode. Two problems are solved:
* (1) A function call with less parameters than the declared number of parameters of a function;
* (2) The optional parameter in the expression of the function body can be ignored/skipped if the parameter appears
* in a call of a function.
* Question (TODO):
* exec("function R=myProd(m, n), if nargin==1, r=m*m; else r=m*n; end end myProd(3);")
* m*n cannot be compiled if the type of n is null
* Bugs fixed:
* <1> exec("function r=fib(n)\n if n<=1\n r=1; else r=fib(n-1)+fib(n-2); end\n end\n fib(42)")
* two times assignement for 'r=fib(n-1)+fib(n-2);' due to two types of 'r' (e.g. I and D)
*
*
* TODO
* multi-assign:
* [A B;C] = D;
*
* TODO
* **Add parent node for each node when it is constructed
* ** nargout nargout(fx)
* Finish test for assignment with comma-separated list on right hand side
* Multi-variable assignement test
* Integer matrix: X = randi(imax,classname) returns a pseudorandom integer where classname specifies the data type.
* classname can be 'single', 'double', 'int8', 'uint8', 'int16', 'uint16', 'int32', or 'uint32'.
* ***[varargout{1:max(nargout,1)}]=F(varargin{:}) in (function varargout = gallery(matname,varargin))
* **try catch end
* **classdef
* **Support inf and -inf
* Support sparse(m,n) - sparse matrix
*
* ***Use updateTree() to transform MatrixAccessNode to CellAccessNode
* Construct CellAccessNode directly instead of using updateTree()?? e.g. A={1,2,3}; a(1,2)
* Default type of an undefined variable is funcCall or ArrayAccess in MatlabTreeBuilder?
* Support auto increment of ObjectArray and Jama.Matrix???
*
*
* DONE:
* Function def body need StatementNode
* Bugfix: Look up function names in BytecodeSupport and Math
* Use a StatementNode to contain all nodes in the stack
* Support 'end' in expression, e.g. A(end-1)
* Logical array support
* Logical indexing
* I = find(A < 9)
* A(B) uses logical indexing, whereas A(I) uses linear indexing
* A(2<A<9), since it evaluates to A(2<A | A<9).
* A(A>5)=100
* Implement 'switch...case...otherwise' grammar and code generation (DONE)
* Add operator +,-,*,/ support for Object type (in cell) (DONE) (Autoboxing unboxing?)
* Remove value form VariableNode (DONE)
* Fix conflict for math.sin(x) and math.sin Struct (DONE)
* CSList as function arguments (DONE)
* Bootstrap function for object arguments (DONE)
* Special functions, likeL false(), true(), nvararg (DONE)
* Delete: new StringConcatNode(v1,v2)
* Fix operator precedence
*
* @author yueming.liu
*
*/
public class TestMatlabEngine {
public static void main(String[] args){
exec("function r=fib(n)\n if n<=1\n r=1L; else r=fib(n-1)+fib(n-2); end\n end\n tic; fib(46)\n toc");
// assertEqual(exec("if( any([0 0 1]) ), 1 else 2 end"), 1);
//
// assertEqual(exec("any(1)"), true);
// assertEqual(exec("any(1L)"), true);
// assertEqual(exec("any(1.0)"), true);
// assertEqual(exec("any([1 2 3; 4 5 6])"), getVector(new double[]{1,1,1}).transpose());
// assertEqual(exec("any([0 1 0; 1 0 0])"), getVector(new double[]{1,1,0}).transpose());
// assertEqual(exec("any([0 1 0])"), getVector(new double[]{1}).transpose());
// assertEqual(exec("any([0 0 0])"), getVector(new double[]{0}).transpose());
//
// //Need better test case
// //exec("[a b]=cellfun(@sum, {1, 2.0, [0 1 2; 3 4 5]}, {[1 2; 3 4],[1 2], 3L}); [x1 x2 x3]=a{:}; x1\nx2\nx3");
//
// exec("a=cellfun(@size, {1, 2.0, [0 1 2; 3 4 5]}); [x1 x2 x3]=a{:}; x1\nx2\nx3");
// exec("[a]=cellfun(@size, {1, 2.0, [0 1 2; 3 4 5]}); [x1 x2 x3]=a{:}; x1\nx2\nx3");
// exec("cellfun(@size, {1, 2.0, [0 1 2; 3 4 5]})");
//
// testCellfun();
//
// test2();
}
public static void testCellfun() {
ObjectArray o = new ObjectArray(3,1);
o.set(0, 1);
o.set(1, 2.0);
o.set(2, getMatrix(new double[][]{{0,1,2},{3,4,5}}));
CSList ret = BytecodeSupport.cellfun("size", new ObjectArray[]{o});
BytecodeSupport.println(ret);
}
public static void test2() {
assertEqual(exec("function [c d]=myfun(a, b)\n c=a+b\n d=a-b\n end\n myfun(10,100)"),
new CSList(new Object[]{110,-90}));
assertEqual(exec("function myfun(a, b)\n c=a+b\n d=a-b\n [c d]\n end\n myfun(10,100)"),
getVector(110,-90).transpose());
assertEqual(exec("function a=fun(x), if x<0, return end a=x*x; end fun(1)"), 1);
//r has type null, the code will not be generated for r
assertEqual(exec("function r=fun(a), if a<0, return 0 end a+1; end fun(-1)"), 0);
exec("function [x y varargout] = fun(a,b,c), x=a; y=b; varargout={c,100,'aaa'}; varargout end [xx yy zz aa bb]=fun(10,20,30); xx\nyy\nzz\naa\nbb");
exec("function [x y varargout] = fun(a,b,c), x=a; y=b; varargout(1)=c; varargout end [xx yy zz]=fun(10,20,30); xx\nyy\nzz");
exec("function [A B]=myfun(), A=[1 2; 3 4]; B=[5 6; 7 8]; end [A B]=myfun()\n A\n B\n");
exec("function [x y z] = fun(a,b,c), x=a; y=b; z=c; end [xx yy zz]=fun(10,20,30); xx\nyy\nzz");
exec("function [x y z] = fun(a,b,c), x=a; y=b+c; z=nargout end [xx]=fun(10,20,30); xx"); //nargout should be 1
exec("[a,b,c]=[1,2,3]; a\nb\nc");
exec("v(1:2)={':'}; x=[1 2 3; 4 5 6]; v\n v(2)=3:-1:1; x(v{:})");
exec("v={0,0}; v(1:2)={':'}; x=[1 2 3; 4 5 6]; v\n v(2)=3:-1:1; x(v{:})");
exec("function fun(a,b,varargin), varargin{:}\n size(varargin) end fun(1,2,{3,4},5);");
exec("function fun(a,b,varargin), varargin{:}\n size(varargin) end fun(1,2,{3,4});");
exec("function fun(a,b,varargin), varargin{:}\n end fun(1,2,3,4);");
exec("function fun(a,b,c), c\n end fun(1,2,3);");
//bugfix: Return type is changed after calling updateTree()
//Make sure getType() return the correct type even after calling updateTree()
exec("C = {'one', 'two', 'three'; 1, 2, 3}; C(1:2,1:2)");
assertEqual(exec("a=[1, 2; 3, 4]; a(2, 2)"), 4.0);
exec("a={1,2,3}");
exec("a={1,2,3}; a(1,2)\n a{1,2}");
exec("a={1,2,3}; a(1,2)=10");
assertEqual(exec("math.sin(1.0);"), Math.sin(1.0));
assertEqual(exec("math.cos(1.0);"), Math.cos(1.0));
// //flipdim.m
// % Create the index that will transform x.
// v(1:ndims(x)) = {':'};
// % Flip dimension dim.
// v{dim} = dimsize:-1:1;
// % Index with v.
// y = x(v{:});
exec("v={0,0}; v(1:2)={':'}; v{:}");
exec("v={0,0}; v(1:2)={':'}; x=[1 2 3; 4 5 6]; x(v{:})");
exec("v={0,0}; v(1:2)={':'}; x=[1 2 3; 4 5 6]; v(2)=3:-1:1; x(v{:})");
//Use an array without initialization
exec("v(1:2)={':'}");
exec("v(2,3)=10");
exec("v(2,3)=10; v(1,1)=5; v(3,3)=9");
exec("a=1\n a=2\n a=3");
test();
}
public static void test() {
//Type changing assignment tests:
//The following code is generated for this test in updateTree() of AssignNode
//The whole logic is replaced by updateTree()
//int aI=1;
//double aD=1.0 //shadow variable for a with double type
//int a_flag_I = 0 //generated flag
//for(int i=1; i<=5; i++) {
// if(a_flag_I==0) { aD=aI+1.1; a_flag_I=1; }
// else { aD=aD+1.1 };
//}
assertEqual(exec("a=1; for i=1:5, a=a+1.1; end a"), 6.5);
assertEqual(exec("a=1; i=1; while i<= 5, a=a+1.1; i+=1; end a"), 6.5);
//exec("a=1; a=a+1.2; a");
// exec("res1=1; for i=1:5, res1 = (res1~=0); end res1"); //res1=1; for i=1:5, res1 = (res1~=0); [res1=0;] end res1
//// exec("res1=1; res1~=0");
//// exec("res1=1; res2 = (res1~=0); res2");
// exec("res1=1; res1 = (res1~=0); res1"); //false???
// exec("[varargout{1:max(nargout,1)}]");
// exec("[varargout{1:max(nargout,1)}]=F(varargin{:});");
//nargs = nargin(matname);
//[classname,varargin{1:end-1}] //end-1
exec("a=[2 3]; a(end-1)");
exec("A = 1:5; B = cumsum(A)");
exec("A=[1 2 3; 4 5 6; 7 8 9]; cumsum(A)");
exec("A=[1 2 0 5 7]; class(A)");
exec("A=[1 2 0 5 7]; diff(A)");
exec("A=[1 2 0; 3 0 4]; diff(A)");
exec("A=[1 2 0; 3 0 4; 0 5 6]; [i j] = find(A); sort(i-j)");
exec("function [x y z] = fun(a,b,c), x=a; y=b+c; z=nargout end [xx]=fun(10,20,30); xx"); //nargout should be 1
exec("function [x y z] = fun(a,b,c), x=a; y=b+c; z=nargout end [xx yy zz]=fun(10,20,30); xx\n yy\n zz");
exec("function [x y] = fun(a,b,c), x=a; y=b+c; nargout end fun(1,2,3)");
exec("a=[10]; a(1,1)=0; a(1)=1; a");
assertEqual(exec("a={1,2,3}; isempty(a{1})"), false);
exec("a='abc'; switch a\n case 'aaa'\n 100\n case 'bbb'\n 200\n case 'abc'\n 300\n otherwise\n 500\n end");
exec("a=1; switch a, case 1\n 1+2; 100\n case 2\n 200; 4+5\n otherwise\n 300; 2+3\n end");
exec("a=2; switch a, case 1\n 1+2; 100\n case 2\n 200; 4+5\n otherwise\n 300; 2+3\n end");
exec("a=3; switch a, case 1\n 1+2; 100\n case 2\n 200; 4+5\n otherwise\n 300; 2+3\n end");
assertEqual(exec("a=1; b=0; switch a, case 2, b=200; case 1, b=100; otherwise b=500; end b"),100);
assertEqual(exec("a=2; b=0; switch a, case 2, b=200; case 1, b=100; otherwise b=500; end b"),200);
assertEqual(exec("a=3; b=0; switch a, case 2, b=200; case 1, b=100; otherwise b=500; end b"),500);
assertEqual(exec("a='abc'; b=0; switch a\n case 'aaa'\n b=100\n case 'bbb'\n b=200\n case 'abc'\n b=300\n otherwise\n b=500\n end b"), 300);
testBasic();
testBasic2();
testBasic3();
testEnd();
testLineContinue();
testBuildinFunc2();
testCellArray();
testVariableNode();
testPrint();
testComment();
testEndIndex();
testNArgin();
testShaddowVariables();
testBuildinFunc();
testFunction();
testMatrixInit();
testMatrixAssign();
testMatrixAccess();
testOptionalParamters();
testMisc();
testString();
testCommaSeparatedList();
testCellObjectOperation();
testSpecialFunctions();
testObjectArgumentFunctionCalls();
testLogicalArray();
testSingleQuote();
testBitOperation();
}
public static void testObjectArgumentFunctionCalls() {
//Test object aruguments function calls
exec("a={1,2}; a(1)");
exec("function fun(a,b), a+b; end a={1,2}; fun(a{1},a{2})");
assertEqual(exec("function r=fib(n), if n<=1, r=1; else r=fib(n-1)+fib(n-2); end end a={4}; fib(a{1})"),5);
exec("function fun(a,b), a+b; end a={1,2}; fun(a{:})");
exec("math.max(1,2)");
exec("a={1,2}; java.lang.Math.max(a{:})");
}
public static void testSpecialFunctions() {
//Test special functions
assertEqual(exec("true(3)"),new LogicalArray(new Object[][]{{true, true, true},{true, true, true},{true, true, true}}));
assertEqual(exec("false(3)"),new LogicalArray(new Object[][]{{false, false, false},{false, false, false},{false, false, false}}));
exec("function fun(a,b), a+b; end nargin('fun')");
}
public static void testCellObjectOperation() {
exec("C = {'one', 'two', 'three'; 1, 2, 3}; C{1}+C{1,2}");
exec("C = {'one', 'two', 'three'; 1, 2, 3}; C{2}+C{4}");
exec("C = {[1 2; 3 4], [10 20; 30 40]}; C{1,1}+C{1,2}");
exec("C = {[1 2; 3 4], 5}; C{1,1}+C{1,2}");
exec("C = {[1 2; 3 4], 5}; C{1,2}+C{1,1}");
exec("C = {'one', 'two', 'three'; 1, 2, 3}; C{2}-C{4}");
exec("C = {[1 2; 3 4], [10 20; 30 40]}; C{1,1}-C{1,2}");
exec("C = {[1 2; 3 4], 5}; C{1,1}-C{1,2}");
exec("C = {[1 2; 3 4], 5}; C{1,2}-C{1,1}");
exec("C = {'one', 'two', 'three'; 1, 2, 3}; C{2}*C{4}");
exec("C = {[1 2; 3 4], [10 20; 30 40]}; C{1,1}*C{1,2}");
exec("C = {[1 2; 3 4], 5}; C{1,1}*C{1,2}");
exec("C = {[1 2; 3 4], 5}; C{1,2}*C{1,1}");
exec("C = {'one', 'two', 'three'; 1, 2, 3}; C{2}/C{4}");
exec("C = {[1 2; 3 4], [10 20; 30 40]}; C{1,1}/C{1,2}");
exec("C = {[1 2; 3 4], 5}; C{1,1}/C{1,2}");
exec("C = {[1 2; 3 4], 5}; C{1,2}/C{1,1}");
}
public static void testLogicalArray() {
assertEqual(exec("A=[1 2 3; 4 5 6]; A==3"),
new LogicalArray(new Object[][]{{false, false, true},{false, false, false}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; A==3L"),
new LogicalArray(new Object[][]{{false, false, true},{false, false, false}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; A==3.0"),
new LogicalArray(new Object[][]{{false, false, true},{false, false, false}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; 3==A"),
new LogicalArray(new Object[][]{{false, false, true},{false, false, false}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; 3L==A"),
new LogicalArray(new Object[][]{{false, false, true},{false, false, false}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; 3.0==A"),
new LogicalArray(new Object[][]{{false, false, true},{false, false, false}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; A!=3"),
new LogicalArray(new Object[][]{{true, true, false},{true, true, true}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; A!=3L"),
new LogicalArray(new Object[][]{{true, true, false},{true, true, true}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; A!=3.0"),
new LogicalArray(new Object[][]{{true, true, false},{true, true, true}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; 3!=A"),
new LogicalArray(new Object[][]{{true, true, false},{true, true, true}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; 3L!=A"),
new LogicalArray(new Object[][]{{true, true, false},{true, true, true}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; 3.0!=A"),
new LogicalArray(new Object[][]{{true, true, false},{true, true, true}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; A>3"),
new LogicalArray(new Object[][]{{false, false, false},{true, true, true}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; A>3L"),
new LogicalArray(new Object[][]{{false, false, false},{true, true, true}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; A>3.0"),
new LogicalArray(new Object[][]{{false, false, false},{true, true, true}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; 3>A"),
new LogicalArray(new Object[][]{{true, true, true},{false, false, false}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; 3L>A"),
new LogicalArray(new Object[][]{{true, true, true},{false, false, false}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; 3.0>A"),
new LogicalArray(new Object[][]{{true, true, true},{false, false, false}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; A<3"),
new LogicalArray(new Object[][]{{true, true, false},{false, false, false}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; A<3L"),
new LogicalArray(new Object[][]{{true, true, false},{false, false, false}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; A<3.0"),
new LogicalArray(new Object[][]{{true, true, false},{false, false, false}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; 3<A"),
new LogicalArray(new Object[][]{{false, false, true},{true, true, true}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; 3L<A"),
new LogicalArray(new Object[][]{{false, false, true},{true, true, true}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; 3.0<A"),
new LogicalArray(new Object[][]{{false, false, true},{true, true, true}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; A>=3"),
new LogicalArray(new Object[][]{{false, false, true},{true, true, true}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; A>=3L"),
new LogicalArray(new Object[][]{{false, false, true},{true, true, true}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; A>=3.0"),
new LogicalArray(new Object[][]{{false, false, true},{true, true, true}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; 3>=A"),
new LogicalArray(new Object[][]{{true, true, false},{false, false, false}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; 3L>=A"),
new LogicalArray(new Object[][]{{true, true, false},{false, false, false}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; 3.0>=A"),
new LogicalArray(new Object[][]{{true, true, false},{false, false, false}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; A<=3"),
new LogicalArray(new Object[][]{{true, true, true},{false, false, false}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; A<=3L"),
new LogicalArray(new Object[][]{{true, true, true},{false, false, false}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; A<=3.0"),
new LogicalArray(new Object[][]{{true, true, true},{false, false, false}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; 3<=A"),
new LogicalArray(new Object[][]{{false, false, false},{true, true, true}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; 3L<=A"),
new LogicalArray(new Object[][]{{false, false, false},{true, true, true}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; 3.0<=A"),
new LogicalArray(new Object[][]{{false, false, false},{true, true, true}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; B=[3 1 1; 6 3 3]; A>B"),
new LogicalArray(new Object[][]{{false, true, true},{false, true, true}}));
assertEqual(exec("A=[1 2 3; 4 5 6]; B=[3 1 1; 6 3 3]; A(A>B)"), getVector(2,3,5,6));
}
public static void testSingleQuote() {
//String s = "a=[1 2 3 4]; b=a'''; b";
//String s = "a=[1 2 3 4]; 'bbb'";
//String s = "'bbb'";
//String s = "'bbb'+'ccc'";
//String s = "'b\\'bb'+'ccc'";
//String s = "cidx = (0:m-1)'; t = t.';";
// s = s.replaceAll("\\\\'", "#1#");
// s = s.replaceAll("([^a-zA-Z_)'])(')([^']*)(')", "$1`$3`");
// s = s.replaceAll("^(')([^']*)(')", "`$2`");
// s = s.replaceAll("#1#", "\\`");
// System.out.println(s);
// exec(s);
exec("a=[1 2 3]; a'''");
//exec("a=[1 2 3]; `ddd`");
exec("a=[1 2 3]; 'ddd'");
exec("a=[1 2 3]; \"dd\\\"d\"");
exec("'aaa'");
exec("\"aaa\"");
//exec("'aaa\"");
//exec("a=[1 2 3]; b=[4 5 6]; a.'; b.'");
exec("a=[1 2 3]; b=[4 5 6]; a'\n b'");
}
public static void testBitOperation() {
exec("1 & 2");
exec("1 | 2");
exec("2 ^ 3"); //!!!!!
exec("2>1 & 2<3");
exec("2>1 & 2==3");
exec("2>1 | 2==3");
//exec("2>1 ^ 2==3");
exec("[1 2; 3 4].^[2 2; 3 3]");
exec("[1 2; 3 4]^[2 2; 3 3]");
}
public static void testEnd() {
// //[classname,varargin{1:end-1}] //end-1
// exec("a=[2 3]; a(end-1)");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{:}={1,2,3,4;5,6,7,8}");
exec("a=[2 3]; a(:)");
exec("a=[2 3]; a(end)");
////exec("a=[2 3]; y = zeros(a(1),a(2))");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{end-1}");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{1:end}={1,2,3,4,5,6,7,8}");
//exec("1");
exec("function A=myfun(), A=[1 2; 3 4]; B=[5 6; 7 8]; end myfun()");
exec("function R=myZeros(m, n), if nargin==1, R=zeros(m, m); else R=zeros(m,n); end end; myZeros(3)");
exec("A=[1 2 3; 4 5 6]; A(end)");
exec("A=[1 2 3; 4 5 6]; A(end-1)");
exec("A=[1 2 3; 4 5 6]; A(end-2)");
exec("A=[1 2 3; 4 5 6]; A(end,end)");
exec("A=[1 2 3; 4 5 6]; A(end,end-2)");
exec("A=[1 2 3; 4 5 6]; A(1:end)");
exec("A=[1 2 3; 4 5 6]; A(1:end,1)");
exec("A=[1 2 3; 4 5 6]; A(2,1:end)");
exec("A=[1 2 3; 4 5 6]; A(2,1:end-2)");
exec("A=[1 2 3; 4 5 6]; A(1:end-2+1,1)");
assertEqual(exec("A=[10 20 30; 40 50 60]; A(end:-2:1)=[1 3 5];"),
getMatrix(new double[][]{{10, 20, 30},{5,3,1}}));
}
public static void testLineContinue() {
exec("a = ...\n 1; a");
exec("a = ...\r 1; a");
exec("a = ... \n 1; a");
}
public static void testBuildinFunc2() {
assertEqual(exec("A=[1 2 3; 4 5 6; 7 7 8]; inv(A);"), new Jama.Matrix(new double[][]{
{1,2,3}, {4,5,6}, {7,7,8}
}).inverse());
assertEqual(exec("n=5; d = ones(n,2); sum(d)"), new Jama.Matrix(new double[][]{
{5,5}
}));
assertEqual(exec("X = [0 1 2; 3 4 5]; sum(X, 1)"), new Jama.Matrix(new double[][]{
{3,5,7}
}));
assertEqual(exec("X = [0 1 2; 3 4 5]; sum(X, 2)"), new Jama.Matrix(new double[][]{
{3},{12}
}));
assertEqual(exec("X = [4 9; 16 25]; sqrt(X)"), new Jama.Matrix(new double[][]{
{2,3},{4,5}
}));
assertEqual(exec("X = [4 9; 16 25]; ismatrix(X)"), true);
//TODO: fix this: assertEqual(exec("X = {[4 9; 16 25],'aaa'}; X=X{1}; ismatrix(X)"), true);
assertEqual(exec("X = {[4 9; 16 25],'aaa'}; Y=X{1}; ismatrix(Y)"), true);
exec("diag([1 2; 3 4])");
exec("function t = trace(A)\n t = full(sum(diag(A))); end trace([1 2; 3 4])");
}
public static void testCommaSeparatedList() {
exec("car.monitor.mpg=35");
assertEqual(exec("math.sin(1.0);"), Math.sin(1.0));
exec("C = {'one', 'two', 'three'; 1, 2, 3}; [a b c]=C{1:3}; a\n b\n c\n");
exec("C = {'one', 'two', 'three'; 1, 2, 3}; C{1:3}");
exec("C = {'one', 'two', 'three'; 1, 2, 3}; a=C{1:3}; a");
assertEqual(exec("sum=0.0; for i=[10 20 30], sum+=i; end sum;"), 60.0);
exec("function [A B]=myfun(), A=1; B=2; end [a b]=myfun(); a\n b");
exec("function [A B]=myfun(), A=1; B=2; end myfun()");
exec("C = {'one', 'two', 'three'; 1, 2, 3}; a=C{2,3}; a");
exec("C = {'one', 'two', 'three'; 1, 2, 3}; a=C{1:3}; a");
exec("C = [1, 2, 3]; [a b c]=C(1:3)'; a\n b\n c\n");
exec("C = [1, 2, 3]; [a b c]=C(:)'; a\n b\n c\n");
exec("C = [1, 2, 3]; [a b c]=C; a\n b\n c\n");
//exec("C = {'one', 'two', 'three'; 1, 2, 3}; [a b c]=C{1:3}"); //string type?
exec("C = {'one', 'two', 'three'; 1, 2, 3}; C{1:3}");
exec("C = {'one', 'two', 'three'; 1, 2, 3}; C{1}+C{1,2}");
exec("C = {'one', 'two', 'three'; 1, 2, 3}; C");
exec("C = {'one', 'two', 'three'; 1, 2, 3}; C(1:2,1:2)");
exec("C = {'one', 'two', 'three'; 1, 2, 3}; C(1,1:3)");
exec("C = {'one', 'two', 'three'; 1, 2, 3}; C{1:2,1:2}");
exec("C = {'one', 'two', 'three'; 1, 2, 3}; C{1,1:3}");
exec("C = {'one', 'two', 'three'; 1, 2, 3}; C(1,1:3) = {'first','second','third'}");
//error: invalid assignment to cs-list outside multiple assignment
// exec("a={1,3.0,'abc'}; a{1}=100");
// exec("a={1,3.0,'abc'}; a{2}=100");
// exec("a={1,3.0,'abc'}; a{3}=100");
exec("a={1,3.0,'abc'}; a(1)=100");
exec("a={1,3.0,'abc'}; a(2)=100");
exec("a={1,3.0,'abc'}; a(3)=100");
exec("a={1,3.0,'abc'}; a(1,3)=100");
// exec("a={1,3.0,'abc'}; a{1,3}=100");
// exec("a={1,3.0,'abc'}; a{1:3}");
exec("a={1,3.0,'abc'}; a(1:3)");
exec("a={1,3.0,'abc'}; a(3)");
exec("a={1,3.0,'abc'}; a{3}");
exec("a={1,3.0,'abc'}; a{1:3}");
exec("a={1,3.0,'abc'}; a(1:3)");
System.out.println(exec("a.b=[1 2; 3 4]; a.c=2; a.d=false"));
exec("a.b=[1 2; 3 4]; a.c=2; a.b+a.c");
//exec("car.monitor.mpg=35.0; car.monitor.miles=2000; car.monitor.mpg+car.monitor.miles");
//exec("car.monitor.mpg=35.0; car.monitor.miles=2000; car");
exec("car.monitor.mpg=35.0; car.monitor.mpg+2");
exec("car.monitor.mpg=35L; car.monitor.mpg+2");
exec("car.monitor.mpg=false; car.monitor.mpg || 2>1");
exec("car.monitor.mpg=35; car.monitor.mpg+2");
exec("car.monitor.mpg=[1 2;3 4]; car.monitor.mpg");
exec("car.monitor.mpg=[1 2;3 4]; car.monitor.mpg+2");
exec("car.monitor.mpg=35; car.monitor.mpg");
exec("car.monitor.mpg=35; car.monitor");
exec("car.monitor.mpg.a.b.c=35");
exec("car.monitor.mpg=35");
// Struct s = new Struct();
// //s.put("mpg", new Jama.Matrix(3,3));
// s.put("mpg", 35);
// Struct ss = new Struct();
// ss.put("monitor", s);
// exec("car.monitor.mpg", new Object[]{ss});
// exec("car.mpg", new Object[]{s});
// exec("car", new Object[]{s});
}
public static void testCellArray() {
// exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; for i=1:8, a{i} end");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{1:end}={1,2,3,4,5,6,7,8}");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{1:2,1:3}={1,2,3;4,5,6}");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{1:2,1:3:4}={1,2;3,4}");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{1:2,end:-1:1}={1,2,3,4;5,6,7,8}");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{[1 2],[1 3 4]}={1,2,3;4,5,6}");
//// //exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{end:1}=100");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{end:-1:1}={1,2,3,4,5,6,7,8}");
// exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{2,2}=100");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{2:4}={1,2,3}");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{1:2:8}={1,2,3,4}");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{2:2:8}={1,2,3,4}");
// exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{3}={100}");
// exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{5}={100}");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{:}={1,2,3,4;5,6,7,8}");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{1:end}=100");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{1:2,1:3}=100");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{1:2,1:3:4}=100");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{1:2,end:-1:1}=100");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{[1 2],[1 3 4]}=100");
// //exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{end:1}=100");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{end:-1:1}=100");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{2,2}=100");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{2:4}=100");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{1:2:8}=100");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{2:2:8}=100");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{3}=100");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{5}=100");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{:}=100");
exec("a=[1 2 3 4 5; 10 20 30 40 50]; a([1 2],[1 3 4])");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{1:end}");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{1:2,1:3}");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{1:2,1:3:4}");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{1:2,end:-1:1}");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{[1 2],[1 3 4]}");
//exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{end:1}");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{end:-1:1}");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{2,2}");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{2:4}");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{2:2:8}");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{3}");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{5}");
exec("a={10,20,30,'ttt'; 1L,3.5,true,false}; a{:}");
exec("{ 30, 'ttt' }");
exec("{ {10, 20}, {30, 'ttt'} }");
exec("{ {10, 20, 30, 'ttt'}; {1L, 3.5}, {true, false} }");
exec("{ {10, 20}, {30, 'ttt'}, {1L, 3.5}, {true, false} }");
exec("{ {10; 20}, {30; 'ttt'}, {1L; 3.5}, {true; false} }");
exec("{10; 20; 30; 'ttt'; 1L; 3.5; true; false}");
exec("{10,20; 30,'ttt'; 1L,3.5; true,false}");
exec("{10,20,30,'ttt'; 1L,3.5,true,false}");
exec("{10,20,30,'ttt',1L,3.5,true}");
exec("{10,20,30,'ttt',1L}");
//exec("a=[1,2,3]; a{1:2}");
//assertEqual(exec("a=[1,2,3]; a{1}=10"), null);
//assertEqual(exec("a{1:2}(0)"), null);
//assertEqual(exec("a[0]{1:2}"), null);
//assertEqual(exec("a(0){1}"), null);
//assertEqual(exec("a{1:2}{0}"), null);
//assertEqual(exec("a.b{1:2}{0}"), null);
//assertEqual(exec("a.b(1:2)(0)"), null);
//assertEqual(exec("aa.bb(1:2).cc(0)"), null);
//assertEqual(exec("dd(1:2,3,4)"), null);
//assertEqual(exec("a=[1,2,3,4]; a(3);"), 3.0);
//assertEqual(exec("math.sin(1.0);"), Math.sin(1.0));
//assertEqual(exec("rem(3.0,2.0);"), 1.0);
//assertEqual(exec("mod(3.0,2.0);"), 1.0);
//assertEqual(exec("a", getMap("a",1)), 1);
}
public static void testMisc() {
assertEqual(exec("function r=fib(n)\n if n<=1\n r=1; else r=fib(n-1)+fib(n-2); end\n end\n fib(4)"),5);
assertEqual(exec("function c=fun(a, b), if nargin < 2, return; end c=a+b; end fun(1);"), null);
//Test logical constant
assertEqual(exec("if true, 1; else 2; end"), 1);
assertEqual(exec("a=1; a-=[1 2;3 4]; a"), getMatrix(new double[][]{{0,-1},{-2,-3}}));
assertEqual(exec("a=1; a+=[1 2;3 4]; a=1.0; a"),1.0);
assertEqual(exec("A=[1 2; 3 4]; A*=[1 2; 1 3]; A"), getMatrix(new double[][]{{3,8},{7,18}}));
// //BUgfix: Two calls of 'r=fib(n-1)+fib(n-2)' for shadow variable of r.
exec("function r=fib(n)\n if n<=1\n r=1; else r=fib(n-1)+fib(n-2); end\n end\n fib(29)");
exec("function fib(n)\n if n<=1\n 1; else fib(n-1)+fib(n-2); end\n end\n fib(29)");
//test local, paramter, func call and array accesses
//assertEqual(MatlabEngine.exec("if n<=1; r=5;r;1\n else 5; 6+7\n 100\n 9; 8; end",new int[]{8}), 8);
// double[][] array3 = {{1,2,3,4,5,6},{11,22,33,44,55,66}};
// Matrix C = new Matrix(array3);
// assertEqual(exec("C( : , : )", getMap("C",C)), C);
// assertEqual(exec("a&&b", new Object[]{true, false}), false);
//Exception in thread "main" java.lang.VerifyError: get long/double overflows locals
//assertEqual(exec("r = -((n-j+1)*r*(n+j-1))/(j-1)^2; r", new double[]{1,2,3}),Double.NEGATIVE_INFINITY);
//assertEqual(exec("isnumeric(c) && isnumeric(r)", getMap("c",1,"r",2)), true);
//assertEqual(exec("if mod(i,2)==0; a+=1; else b+=1; end i\n a\n b\n",
// new int[]{4,10,0}),10);//a=4,b=10,i=0
// assertEqual(exec("A=[10 20 30; 40 50 60]; A(end:-2:1)=[1 3 5];"),
// getMatrix(new double[][]{{10, 20, 30},{5,3,1}}));
// assertEqual(exec("function fib(r, n); A=[1 1; 1 0]; if n<1; r; else r=A*r; fib(r, n-1); end end fib([1 1]', 5)"),
// new Jama.Matrix(new double[]{13,8},2));
//func_def_return is for 'function HERE=fun(a,b) ... end'
assertEqual(exec("function fun(a,b), return a+b end fun(1,2)"),3);
}
public static void testString() {
assertEqual(exec("'aaa'+a;", new Object[]{"bbb"}),"aaabbb");
// //assertEqual(exec("'aaa'+a", new Object[]{5}),"aaabbb");
assertEqual(exec("function fun(a,b), a+b; end fun('aaa','bbb');"),"aaabbb");
assertEqual(exec("function fun(a,b), a+b; end fun('aaa');"),null);
assertEqual(exec("function fun(a,b), a+b; end fun(3,5);"),8);
assertEqual(exec("'aaa' + 'bbb'"), "aaabbb");
assertEqual(exec("'aaa' == 'bbb'"), false);
assertEqual(exec("'aaa' != 'bbb'"), true);
}
public static void testOptionalParamters() {
// //If the caller only pass one argument m, what's the type of n in the call of zeros(m,n)?
assertEqual(exec("function R=myZeros(m, n), if nargin==1, R=zeros(m, m); else R=zeros(m,n); end end myZeros(3)"),
getMatrix(new double[3][3]));
assertEqual(exec("function R=myZeros(m, n), if nargin==1, R=zeros(m, m); else R=zeros(m,n); end end myZeros(3,2)"),
getMatrix(new double[3][2]));
assertEqual(exec("function R=myZeros(m, n), if nargin==1, R=zeros(m, m); else R=zeros(m,1+n); end end myZeros(3)"),
getMatrix(new double[3][3]));
assertEqual(exec("function R=myZeros(m, n), if nargin==1, R=zeros(m, m); else R=zeros(m,1+n); end end myZeros(3,1)"),
getMatrix(new double[3][2]));
assertEqual(exec("function R=myZeros(m, n), if nargin==1, R=zeros(m, m); else R=zeros(m,n); end end myZeros(3);"),
getMatrix(new double[3][3]));
assertEqual(exec("function fun(a, b), a+b; end fun(1,2)"), 3);
assertEqual(exec("function fun(a, b), a+b; end fun(1)"), null);
assertEqual(exec("function fun(a, b), a-b; end fun(1)"), null);
assertEqual(exec("function fun(a, b), a*b; end fun(1)"), null);
assertEqual(exec("function fun(a, b), a/b; end fun(1)"), null);
//java.lang.NoSuchMethodException: io.lambdacloud.BytecodeSupport.mod(int)
//assertEqual(exec("function fun(a, b), mod(a,b); end fun(1)"), null);
assertEqual(exec("function fun(a, b), a+=b; a; end fun(1)"), 1);
assertEqual(exec("function fun(a, b), a+=b; a; end fun(1,2)"), 3);
assertEqual(exec("function fun(a, b), a+=b; end fun(1,2)"), 3);
assertEqual(exec("function fun(a, b), a+=b; end fun(1)"), null);
assertEqual(exec("function fun(a, b), a-=b; end fun(1)"), null);
assertEqual(exec("function fun(a, b), a*=b; end fun(1)"), null);
assertEqual(exec("function fun(a, b), a/=b; end fun(1)"), null);
//assertEqual(exec("function fun(a, b), a++; end fun(1)"), null);
//assertEqual(exec("function fun(a, b), a--; end fun(1)"), null);
assertEqual(exec("function fun(a, b), a=b; end fun(1,2)"), 2);
assertEqual(exec("function fun(a, b), a=b; end fun(1)"), null);
// //optional parameter in function call
// assertEqual(exec("range(5)"), 5);
// //assertEqual(exec("range(1:5)"), 5);
// assertEqual(exec("function fun(a, b, c), range(a:c:b); end fun(1,10,2)"),
// getVector(1,3,5,7,9).transpose());
assertEqual(exec("function fun(a, b, c), range(a:c:b); end fun(1,5)"),
getVector(1,2,3,4,5).transpose());
assertEqual(exec("function fun(a, b), a==b; end fun(1,2)"), false);
assertEqual(exec("function fun(a, b), a==b; end fun(1)"), null);
assertEqual(exec("function fun(a, b), a>b; end fun(1)"), null);
assertEqual(exec("function fun(a, b), a>=b; end fun(1)"), null);
assertEqual(exec("function fun(a, b), a<b; end fun(1)"), null);
assertEqual(exec("function fun(a, b), a<=b; end fun(1)"), null);
assertEqual(exec("function fun(a, b), a and b; end fun(1)"), null);
assertEqual(exec("function fun(a, b), a && b; end fun(1)"), null);
assertEqual(exec("function fun(a, b), a or b; end fun(1)"), null);
assertEqual(exec("function fun(a, b), a || b; end fun(1)"), null);
assertEqual(exec("function fun(a, b), a != b; end fun(1)"), null);
assertEqual(exec("function fun(a, b), a ~= b; end fun(1)"), null);
//optional parameter in if/for/while condition
assertEqual(exec("function fun(a,b,c,d,e), [a,b,c,d,e]; end fun(1,2,3,4,5)"), getVector(1,2,3,4,5).transpose());
assertEqual(exec("function fun(a,b,c,d,e), [a,b,c,d,e]; end fun(1)"), getVector(1).transpose());
assertEqual(exec("function fun(a,b,c,d,e), [a,b,c,d,e]; end fun(1,2)"), getVector(1,2).transpose());
assertEqual(exec("function fun(a,b,c,d,e), [a,b,c,d,e]; end fun(1,2,3)"), getVector(1,2,3).transpose());
assertEqual(exec("function fun(a,b,c,d,e), [a,b,c,d,e]; end fun(1,2,3,4)"), getVector(1,2,3,4).transpose());
// //how to compile m*n if the type of n is null ?
assertEqual(exec("function r=myProd(m, n), if nargin==1, r=m*m; else r=m*n; end end myProd(3);"),9);
}
public static void testVariableNode() {
VariableNode v = new VariableNode();
v.setType(Type.DOUBLE_TYPE);
v.setLVTIndex(Type.DOUBLE_TYPE, 1);
v.setType(Type.getType("D"));
v.setLVTIndex("D", 2);
assertEqual(v.getLVTIndex(),2);
v.setType(Type.DOUBLE_TYPE);
assertEqual(v.getLVTIndex(),2);
}
public static void testMatrixInit() {
assertEqual(exec("[1 2 3];"), getVector(1,2,3).transpose());
assertEqual(exec("[1,2,3];"), getVector(1,2,3).transpose());
assertEqual(exec("[1, 2, 3];"), getVector(1,2,3).transpose());
assertEqual(exec("[1 2 3];"), getVector(1,2,3).transpose());
assertEqual(exec("[1, 2 , 3];"), getVector(1,2,3).transpose());
assertEqual(exec("a=1; b=2; c=3; d=4; [a b; c d];"), getMatrix(new double[][]{{1,2},{3,4}}));
assertEqual(exec("A=[1 2; 3 4]; B=[5 6; 7 8]; [A B];"),
getMatrix(new double[][] {
{1.00, 2.00, 5.00, 6.00},
{3.00, 4.00, 7.00, 8.00} }));
assertEqual(exec("A=[1 2; 3 4]; B=[5 6; 7 8]; [A, B];"),
getMatrix(new double[][] {
{1.00, 2.00, 5.00, 6.00},
{3.00, 4.00, 7.00, 8.00} }));
assertEqual(exec("A=[1 2; 3 4]; B=[5 6; 7 8]; [A; B];"),
getMatrix(new double[][] {
{1.00, 2.00},
{3.00, 4.00},
{5.00, 6.00},
{7.00, 8.00},
}));
//exec("A=[1 2; 3 4]; B=[5 6 7 8]; [A B]"); //pad 0???
assertEqual(exec("A=[1 2; 3 4]; B=[5 6; 7 8]; C=[11 22 33 44; 55 66 77 88]; [[A, B]; C];"),
getMatrix(new double[][] {
{ 1.00, 2.00, 5.00, 6.00},
{ 3.00, 4.00, 7.00, 8.00},
{11.00, 22.00, 33.00, 44.00},
{55.00, 66.00, 77.00, 88.00} }));
assertEqual(exec("A=[1 2; 3 4]; B=[5 6; 7 8]; C=[11 22 33 44; 55 66 77 88]; [A, B; C];"),
getMatrix(new double[][] {
{ 1.00, 2.00, 5.00, 6.00},
{ 3.00, 4.00, 7.00, 8.00},
{11.00, 22.00, 33.00, 44.00},
{55.00, 66.00, 77.00, 88.00} }));
assertEqual(exec("A=[1 2; 3 4]; B=[5 6; 7 8]; C=[11 22 33 44; 55 66 77 88]; [[A; B],C'];"),
getMatrix(new double[][] {
{1.00, 2.00, 11.00, 55.00},
{3.00, 4.00, 22.00, 66.00},
{5.00, 6.00, 33.00, 77.00},
{7.00, 8.00, 44.00, 88.00} }));
}
/**
* [A, B] = [C, D]
* [m, n] = size(A)
*/
public static void testMultiAssign() {
// exec("[a b]=[2 3]; a");
// exec("[a b]=[2 3]; b");
// exec("A=[1 2; 3 4]; B=[5 6 7 8]; [C D]=[A B]; C\n D\n [C D]=[2 3]; C\n D\n");
exec("A=[1 2; 3 4]; B=[5 6 7 8]; [C D]=[A B]; C\n D\n");
// exec("function [A B]=myfun(), A=[1 2; 3 4]; B=[5 6; 7 8]; end myfun()");
exec("function [A B]=myfun(), A=[1 2; 3 4]; B=[5 6; 7 8]; end [A B]=myfun()\n A\n B\n");
// exec("function [A B]=myfun(), A=[1 2; 3 4]; B=[5 6; 7 8]; end A=myfun() ");
// exec("function [A B]=myfun(), A=[1 2; 3 4]; B=[5 6; 7 8]; end [A]=myfun() ");
// exec("A=[1 2; 3 4]; B=[5 6 7 8]; [C D]=[A B]; C\n D\n [C D]=[2 3]; C\n D\n");
exec("A=[1 2; 3 4]; B=[5 6 7 8]; [C D]=[A B]; C\n D\n");
}
/**
* A(:)=[1 2 3]
* A(:,1)=[1 2 3]'
*/
public static void testMatrixAssign() { //ExprArrayAssign
// exec("[a b]=[2 3]; a");
// exec("[a b]=[2 3]; b");
}
/**
* A(1,2)
* A(:)
* A(1:3,1:2:5)
*/
public static void testMatrixAccess() {
double[][] array = {{1.,2.,3},{4.,5.,6.},{7.,8.,10.}};
double[][] array3 = {{1,2,3,4,5,6},{11,22,33,44,55,66}};
Matrix A = new Matrix(array);
Matrix C = new Matrix(array3);
Matrix M = new Matrix(new double[][]{{1,2},{3,4}});
Matrix D = new Matrix(
new double[]{1,2,3,4,5,6,7,8,9,10,
11,12,13,14,15,16,17,18,19,20,
21,22,23,24,25,26,27,28,29,30},
10).transpose();
if(MatrixAccessNode.INDEX_BASE == 1) {
assertEqual(exec("A=[1 2 3 4 5 6 7 8 9 10; 11 12 13 14 15 16 17 18 19 20; 21 22 23 24 25 26 27 28 29 30]; A(1, 1:2)"),
D.getMatrix(0, 0, 0, 1));
assertEqual(exec("C ( 2, 2:3:6 )", getMap("C",C)), C.getMatrix(1,1, new int[]{1,4}));
assertEqual(exec("C(1, 1:2:6)", getMap("C",C)), C.getMatrix(0,0, new int[]{0,2,4}));
assertEqual(exec("C(:, 1:2:6)", getMap("C",C)), C.getMatrix(0,1, new int[]{0,2,4}));
} else {
assertEqual(exec("A=[1 2 3 4 5 6 7 8 9 10; 11 12 13 14 15 16 17 18 19 20; 21 22 23 24 25 26 27 28 29 30]; A(0, 0:1)"),
D.getMatrix(0, 0, 0, 1));
assertEqual(exec("C ( 1, 1:3:5 )", getMap("C",C)), C.getMatrix(1,1, new int[]{1,4}));
assertEqual(exec("C(0, 0:2:5)", getMap("C",C)), C.getMatrix(0,0, new int[]{0,2,4}));
assertEqual(exec("C(:, 0:2:5)", getMap("C",C)), C.getMatrix(0,1, new int[]{0,2,4}));
}
if(MatrixAccessNode.INDEX_BASE == 1) {
assertEqual(exec("A(:,2:3)", getMap("A",A)), A.getMatrix(0,2,1,2));
assertEqual(exec("A=[1 2;3 4]\n A(:,1)"), getVector(1,3));
assertEqual(exec("A=[1 2;3 4]\n A(2,:)"), getVector(3,4).transpose());
assertEqual(exec("A=[1 2;3 4]\n A(:,:)"), M);
} else {
assertEqual(exec("A(:,1:2)", getMap("A",A)), A.getMatrix(0,2,1,2));
assertEqual(exec("A=[1 2;3 4]\n A(:,0)"), getVector(1,3));
assertEqual(exec("A=[1 2;3 4]\n A(1,:)"), getVector(3,4).transpose());
assertEqual(exec("A=[1 2;3 4]\n A(:,:)"), M);
}
}
public static void testShaddowVariables() {
assertEqual(exec("sum=0; sum2=0.0; if true, sum2=sum+0.1; end sum2;"), 0.1);
//assertEqual(exec("sum=0; if true, sum2=sum+0.1; end sum2;"), 0.1);
assertEqual(exec("sum=0; if true, sum=sum+1; end sum;"), 1);
assertEqual(exec("sum=0; if true, sum=sum+0.1; end sum;"), 0.1);
assertEqual(exec("sum=0; if true, sum+=0.1; end sum;"), 0.1);
//assertEqual(exec("sum=0.0; if true, sum2=0.1; end print(sum2);"), 0.1);
//bugfix: double type sum (sumD) may be non-initialized
assertEqual(exec("sum=0.0; if true, sum+=0.1; end sum;"), 0.1);
//assertEqual(exec("a=0.0; if true, sum+=0.1; end sum"), 0.1);
assertEqual(exec("sum=0; sum=sum+0.1; "), 0.1);
assertEqual(exec("sum=0; sum+=3; sum;"), 3);
assertEqual(exec("sum=0; sum+=3; sum+=0.1; sum;"), 3.1);
assertEqual(exec("sum=0; for i=[1 2 3], sum+=i; end sum+=1;"), 7.0);
assertEqual(exec("sum=0; for i=[1 2 3], sum+=i; end sum;"), 6.0);
assertEqual(exec("sum=0.0; for i=[1 2 3], sum=sum+i; end sum;"), 6.0);//only sumD
assertEqual(exec("sum=0; for i=[1 2 3], sum=6; sum=sum+i; end sum;"), 9.0);
assertEqual(exec("sum=0; for i=[1 2 3], sum=sum+i; end sum;"), 6.0);
}
public static void testEndIndex() {
assertEqual(exec("A=[10 20 30; 40 50 60; 70 80 90]; A(2:end, 2:end)=[500 600; 800 900];"),
getMatrix(new double[][]{{10, 20, 30},{40,500,600},{70,800,900}}));
assertEqual(exec("A=[10 20 30; 40 50 60]; A(end:-2:1)=[1 3 5];"),
getMatrix(new double[][]{{10, 20, 30},{5,3,1}}));
assertEqual(exec("A=[10 20 30; 40 50 60]; A(end:-1:1)=[1 2 3 4 5 6];"),
getMatrix(new double[][]{{6,4,2},{5,3,1}}));
assertEqual(exec("A=[10 20 30; 40 50 60]; A(1:3)=[1 2 3];"),
getMatrix(new double[][]{{1,3,30},{2,50,60}}));
assertEqual(exec("A=[10 20 30; 40 50 60]; A(1:6)=[1 2 3 4 5 6];"),
getMatrix(new double[][]{{1, 3, 5},{2,4,6}}));
assertEqual(exec("A=[10 20 30; 40 50 60]; A(4)=500;"),
getMatrix(new double[][]{{10, 20, 30},{40,500,60}}));
/**
* 1 2 3
* 4 5 6
* 7 8 9
* 10 11 12
* 13 14 15
*/
assertEqual(exec("A=[1 2 3; 4 5 6; 7 8 9; 10 11 12; 13 14 15]; A(end:-1:1);"),
getVector(15,12,9,6,3,14,11,8,5,2,13,10,7,4,1));
assertEqual(exec("A=[1 2 3; 4 5 6; 7 8 9; 10 11 12; 13 14 15]; idx=1:2:10; A(idx);"),
getVector(1,7,13,5,11));
assertEqual(exec("A=[1 2 3; 4 5 6; 7 8 9; 10 11 12; 13 14 15]; A(:);"),
getVector(1,4,7,10,13,2,5,8,11,14,3,6,9,12,15));
assertEqual(exec("A=[1 2 3; 4 5 6; 7 8 9; 10 11 12; 13 14 15]; A(4:end,2:end);"),
getMatrix(new double[][]{{11,12},{14,15}}));
assertEqual(exec("A=[10 20 30; 40 50 60]; A(4:6);"),
getVector(50,30,60));
assertEqual(exec("A=[10 20 30; 40 50 60]; A(4);"), 50.0);
assertEqual(exec("A=[10 20 30; 40 50 60]; A(4:end);"),
getVector(50,30,60));
assertEqual(exec("A=[10 20 30; 40 50 60]; A(1:3);"),
getVector(10,40,20));