-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrixTestAll.java
552 lines (387 loc) · 13.4 KB
/
matrixTestAll.java
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
import static org.junit.Assert.*;
import org.junit.Test;
public class matrixTestAll {
@Test
public void testConstructor1Invalid1() {
// for constructor1
int invalid_row1 = -1, invalid_col1 = -2;
String expected="0 0 0 \n"+"0 0 0 \n"+"0 0 0 \n";
// test constructor1; invalid inputs
Matrix a = new Matrix(invalid_row1,invalid_col1);
assertEquals(expected,a.toString());
}
@Test
public void testConstructor1Invalid2() {
// for constructor1
int invalid_row2 = 1, invalid_col2 = 0;
String expected="0 0 0 \n";
Matrix b = new Matrix(invalid_row2,invalid_col2);
assertEquals(expected,b.toString());
}
@Test
public void testConstructor1Valid() {
int valid_row = 3, valid_col = 4;
// test constructor1; valid inputs
String expected="0 0 0 0 \n"+"0 0 0 0 \n"+"0 0 0 0 \n";
Matrix d = new Matrix(valid_row,valid_col);
assertEquals(expected,d.toString());
}
@Test
public void testConstructor2() {
// for constructor2
int[][] data = {{1,2,3,4,5},{6,7,8,9,0},{0,0,1,2,3},{0,0,0,4,5}};
Matrix f = new Matrix(data);
String expected= "1 2 3 4 5 \n"+"6 7 8 9 0 \n"+"0 0 1 2 3 \n"+"0 0 0 4 5 \n";
assertEquals(expected,f.toString());
}
@Test
public void testGetElementValid() {
// for constructor2
int[][] data = {{1,2,3,4,5},{6,7,8,9,0},{0,0,1,2,3},{0,0,0,4,5}};
Matrix f = new Matrix(data);
int expected= 9;
assertEquals(expected,f.getElement(1, 3));
}
@Test
public void testGetElementInvalid() {
// for constructor2
int[][] data = {{1,2,3,4,5},{6,7,8,9,0},{0,0,1,2,3},{0,0,0,4,5}};
Matrix f = new Matrix(data);
String actual ="Empty for Now";
try {
int output = f.getElement(10, 2); // invalid indexes
}
catch (IndexOutOfBoundsException e){
actual = e.getMessage();
}
String expected= "Invalid indexes.";
assertEquals(expected,actual);
}
@Test
public void testSetElementValid() {
int value = 100;
int setvalid_row = 0, setvalid_col = 0;
int[][] data = {{1,2,3,4,5},{6,7,8,9,0},{0,0,1,2,3},{0,0,0,4,5}};
Matrix f = new Matrix(data);
boolean check = f.setElement(value,setvalid_row,setvalid_col);
String expected= "100 2 3 4 5 \n"+"6 7 8 9 0 \n"+"0 0 1 2 3 \n"+"0 0 0 4 5 \n";
assertEquals(expected,f.toString());
assertEquals(true,check);
}
@Test
public void testSetElementInvalid() {
int value = 100;
int setinvalid_row = 10, setinvalid_col = 10;
int[][] data = {{1,2,3,4,5},{6,7,8,9,0},{0,0,1,2,3},{0,0,0,4,5}};
Matrix f = new Matrix(data);
boolean check = f.setElement(value,setinvalid_row,setinvalid_col);
String expected= "1 2 3 4 5 \n"+"6 7 8 9 0 \n"+"0 0 1 2 3 \n"+"0 0 0 4 5 \n";
assertEquals(expected,f.toString());
assertEquals(false,check);
}
@Test
public void testCopy() {
int[][] data = {{1,2,3,4,5},{6,7,8,9,0},{0,0,1,2,3},{0,0,0,4,5}};
Matrix origin = new Matrix(data);
Matrix copied = origin.copy(); // make a copy
String expected= "1 2 3 4 5 \n"+"6 7 8 9 0 \n"+"0 0 1 2 3 \n"+"0 0 0 4 5 \n";
assertEquals(expected,copied.toString());
}
@Test
public void testAddToInvalid() {
int[][] add_to_data = {{3,4,5}, {1,2,3}, {0,0,0},{1,2,1}}; // add to 'data' matrix, dimension not match
int[][] data = {{1,2,3,4,5},{6,7,8,9,0},{0,0,1,2,3},{0,0,0,4,5}};
Matrix f = new Matrix(data);
String actual ="Empty for Now";
String expected = "Invalid operation";
Matrix m = new Matrix(add_to_data);
try{
m.addTo(f);
System.out.println(m);
}
catch (ArithmeticException e){
actual = e.getMessage();
}
assertEquals(expected,actual);
}
@Test
public void testAddToValid() {
int[][] add_to_data = {{3,4,5}, {1,2,3}, {0,0,0},{1,2,1}}; // add to 'data' matrix, dimension not match
int[][] add_to_above = {{1,0,-1}, {2,1,0}, {2,2,2},{0,-1,0}}; // add to 'add_to_data' matrix
Matrix m = new Matrix(add_to_data);
Matrix m1 = new Matrix(add_to_above);
String expected = "4 4 4 \n"+"3 3 3 \n"+"2 2 2 \n"+"1 1 1 \n";
try{
m.addTo(m1);
assertEquals(expected,m.toString());
}
catch (ArithmeticException e){
System.out.println(e);
System.out.println("");
fail("supposed to be a valid input");
}
}
@Test
public void testSubMatrixValid() {
int subvalid_row = 3, subvalid_col = 3;
int[][] data = {{1,2,3,4,5},{6,7,8,9,0},{0,0,1,2,3},{0,0,0,4,5}};
Matrix f = new Matrix(data);
String expected= "1 2 3 4 \n"+"6 7 8 9 \n"+"0 0 1 2 \n"+"0 0 0 4 \n";
try{
Matrix y = f.subMatrix(subvalid_row,subvalid_col);
assertEquals(expected,y.toString());
}
catch (ArithmeticException e){
System.out.println(e);
System.out.println("");
fail("supposed to be a valid input");
}
}
@Test
public void testSubMatrixInvalid() {
int subinvalid_row = 3, subinvalid_col = 6;
int[][] data = {{1,2,3,4,5},{6,7,8,9,0},{0,0,1,2,3},{0,0,0,4,5}};
Matrix f = new Matrix(data);
String actual ="Empty for Now";
String expected = "Submatrix not defined";
try{
Matrix y = f.subMatrix(subinvalid_row,subinvalid_col);
}
catch (ArithmeticException e){
actual = e.getMessage();
}
assertEquals(expected,actual);
}
@Test
public void testIsUpperTrTrue() {
//test isUpperTr()
int[][] Upper1 = {{1,4,5},{0,9,8},{0,0,7},{0,0,0}};
Matrix v = new Matrix(Upper1);
boolean expected = true;
assertEquals(expected,v.isUpperTr());
}
@Test
public void testIsUpperTrFalse() {
int[][] notUpper1 = {{1,4,1,4},{0,7,6,5},{0,1,4,9}};
Matrix v = new Matrix(notUpper1);
boolean expected = false;
assertEquals(expected,v.isUpperTr());
}
@Test
public void testIsUpperTrFalseNotSquare() {
int[][] notUpper2 = {{1,4,1},{1,9,1},{0,2,4}}; // not a square matrix
Matrix v = new Matrix(notUpper2);
boolean expected = false;
assertEquals(expected,v.isUpperTr());
}
@Test
public void testSumInvalid() {
int size=4;
Matrix[] arr = new Matrix[size];
for(int i=0; i<size; i++)
arr[i]=new Matrix(2,3);
arr[2]= new Matrix(3,3); // invalid, due to dimension do not match
String actual ="Empty for Now";
String expected = "Invalid operation";
try{
Matrix sum = Matrix.sum(arr);
}
catch (ArithmeticException e){
actual = e.getMessage();
}
assertEquals(expected,actual);
}
@Test
public void testSumValid() {
Matrix[] arr2 = new Matrix[3]; // valid, can change the matrices here
int[][] temp1={{1,-1,2}, {1,0,-2}};
int[][] temp2={{1,0,1}, {0,1,2}};
int[][] temp3={{0,3,-1}, {0,0,1}};
arr2[0] = new Matrix(temp1);
arr2[1] = new Matrix(temp2);
arr2[2] = new Matrix(temp3);
String expected ="2 2 2 \n"+"1 1 1 \n";
try{
Matrix sum = Matrix.sum(arr2);
assertEquals(expected,sum.toString());
}
catch (ArithmeticException e){
System.out.println(e);
System.out.println("");
fail("supposed to be a valid input");
}
}
// Now, test UpperTriangularMatrix
@Test
public void testUpperConstructor1Invalid() {
// test constructor1; invalid input
// for constructor1
int size_invalid = -5;
UpperTriangularMatrix m11 = new UpperTriangularMatrix(size_invalid);
String expected="0 \n";
assertEquals(expected,m11.toString());
}
@Test
public void testUpperConstructor1Valid() {
int size_valid = 4;
UpperTriangularMatrix m11 = new UpperTriangularMatrix(size_valid);
String expected="0 0 0 0 \n"+ "0 0 0 0 \n"+"0 0 0 0 \n"+"0 0 0 0 \n";
assertEquals(expected,m11.toString());
}
@Test
public void testUpperConstructor2Valid() {
int[][] con2_11 = {{1,2,3,4,5},{0,6,7,8,9},{0,0,10,11,12},{0,0,0,13,14},{0,0,0,0,15}};
Matrix con2_1 = new Matrix(con2_11);
UpperTriangularMatrix m3 = new UpperTriangularMatrix(con2_1);
String expected="1 2 3 4 5 \n"+ "0 6 7 8 9 \n"+"0 0 10 11 12 \n"+"0 0 0 13 14 \n"+"0 0 0 0 15 \n";
assertEquals(expected,m3.toString());
}
@Test
public void testUpperConstructor2Invalid() {
int[][] notupp = {{1,0,0},{2,2,0},{3,3,3}};
Matrix not = new Matrix(notupp);
String expected="Not an upper triangular Matrix";
String actual = "Empty for Now";
try{
UpperTriangularMatrix m5 = new UpperTriangularMatrix(not);
}
catch (IllegalArgumentException e){
actual = e.getMessage();
}
assertEquals(expected,actual);
}
@Test
public void testUpperGetDim() {
int[][] con2_11 = {{1,2,3,4,5},{0,6,7,8,9},{0,0,10,11,12},{0,0,0,13,14},{0,0,0,0,15}};
Matrix con2_1 = new Matrix(con2_11);
UpperTriangularMatrix m3 = new UpperTriangularMatrix(con2_1);
int expected=5;
assertEquals(expected,m3.getDim());
}
//test getElement()
@Test
public void testUpperGetElementValid() {
// for constructor2
int[][] con2_11 = {{1,2,3,4,5},{0,6,7,8,9},{0,0,10,11,12},{0,0,0,13,14},{0,0,0,0,15}};
Matrix con2_1 = new Matrix(con2_11);
UpperTriangularMatrix m3 = new UpperTriangularMatrix(con2_1);
int expected= 8;
assertEquals(expected,m3.getElement(1, 3));
}
@Test
public void testUpperGetElementInvalid() {
// for constructor2
int[][] con2_11 = {{1,2,3,4,5},{0,6,7,8,9},{0,0,10,11,12},{0,0,0,13,14},{0,0,0,0,15}};
Matrix con2_1 = new Matrix(con2_11);
UpperTriangularMatrix m3 = new UpperTriangularMatrix(con2_1);
String actual ="Empty for Now";
try {
int output = m3.getElement(10, 2); // invalid indexes
}
catch (IndexOutOfBoundsException e){
actual = e.getMessage();
}
String expected= "Invalid indexes";
assertEquals(expected,actual);
}
@Test
public void testUpperSetElementValid() {
int value = 100;
int setvalid_row = 0, setvalid_col = 0;
int[][] con2_11 = {{1,2,3,4,5},{0,6,7,8,9},{0,0,10,11,12},{0,0,0,13,14},{0,0,0,0,15}};
Matrix con2_1 = new Matrix(con2_11);
UpperTriangularMatrix m3 = new UpperTriangularMatrix(con2_1);
try{
m3.setElement(value,setvalid_row,setvalid_col);
}
catch (IndexOutOfBoundsException e){
System.out.println(e);
System.out.println("");
fail("supposed to be a valid input");
}
String expected="100 2 3 4 5 \n"+ "0 6 7 8 9 \n"+"0 0 10 11 12 \n"+"0 0 0 13 14 \n"+"0 0 0 0 15 \n";
assertEquals(expected,m3.toString());
}
public void testUpperSetElementInvalid() {
int value = 100;
int setinvalid_row = 10, setvalid_col = 0;
int[][] con2_11 = {{1,2,3,4,5},{0,6,7,8,9},{0,0,10,11,12},{0,0,0,13,14},{0,0,0,0,15}};
Matrix con2_1 = new Matrix(con2_11);
String actual ="Empty for Now";
UpperTriangularMatrix m3 = new UpperTriangularMatrix(con2_1);
try{
m3.setElement(value,setinvalid_row,setvalid_col);
}
catch (IndexOutOfBoundsException e){
actual = e.getMessage();
}
String expected= "Invalid index";
assertEquals(expected,actual);
}
@Test
public void testUpperFullMatrix() {
int[][] con2_11 = {{1,2,3,4,5},{0,6,7,8,9},{0,0,10,11,12},{0,0,0,13,14},{0,0,0,0,15}};
Matrix con2_1 = new Matrix(con2_11);
UpperTriangularMatrix m3 = new UpperTriangularMatrix(con2_1);
int expected=1*6*10*13*15;
assertEquals(expected,m3.getDet());
}
@Test
public void testUpperEffSolveValid1() {
// for effSolve
double []eff1 = {11,11,3};
int [][]test1 = {{1,4,2},{0,9,2},{0,0,3}};
Matrix mtest1 = new Matrix(test1); // gives x=[5,1,1]
UpperTriangularMatrix m12 = new UpperTriangularMatrix(mtest1);
double []actual = m12.effSolve(eff1);
for(int i=0;i<eff1.length;i++)
System.out.print(actual[i]+",");
System.out.println("");
double []expected={5,1,1};
for (int indx=0;indx<3;indx++)
assertEquals(expected[indx],actual[indx],0.001);
}
public void testUpperEffSolveValid2() {
// for effSolve
double []eff1 = {11,11,3};
int [][]test1 = {{1,4,2},{0,9,2},{0,0,3}};
Matrix mtest1 = new Matrix(test1); // gives x=[5,1,1]
double []eff2 = {5,0,5.5,1};
int [][]test2 = {{1,1,1,1},{0,2,1,1},{0,0,3,2},{0,0,0,2}};
Matrix mtest2 = new Matrix(test2); // gives x=[4,-1,1.5,0.5]
UpperTriangularMatrix m12 = new UpperTriangularMatrix(mtest2);
double []actual = m12.effSolve(eff2);
double []expected={4,-1,1.5,0.5};
for (int indx=0;indx<4;indx++)
assertEquals(expected[indx],actual[indx],0.001);
}
public void testUpperEffSolveInvalid1() {
int size_valid = 4;
UpperTriangularMatrix m2 = new UpperTriangularMatrix(size_valid);
double []eff2 = {5,0,5.5,1};
String expected="The determinant of the matrix is 0";
String actual="Empty for now";
try{
double []x3 = m2.effSolve(eff2);
}
catch (IllegalArgumentException e){
actual = e.getMessage();
}
assertEquals(expected,actual);
}
public void testUpperEffSolveInvalid2() {
double []eff1 = {11,11,3};
int [][]test2 = {{1,1,1,1},{0,2,1,1},{0,0,3,2},{0,0,0,2}};
Matrix mtest2 = new Matrix(test2); // gives x=[4,-1,1.5,0.5]
String expected="The dimension of the matrix is not defined for operation";
String actual="Empty for now";
UpperTriangularMatrix m12 = new UpperTriangularMatrix(mtest2);
try{
double []x3 = m12.effSolve(eff1);
}
catch (IllegalArgumentException e){
actual = e.getMessage();
}
assertEquals(expected,actual);
}
}