-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixTest.java
More file actions
80 lines (69 loc) · 2.74 KB
/
Copy pathMatrixTest.java
File metadata and controls
80 lines (69 loc) · 2.74 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
package test;
import model.ForkJoinMatrixMultiplier;
import model.Matrix;
import model.MatrixUtils;
import model.SequentialMatrixMultiplier;
public class MatrixTest {
public static void main(String[] args) {
System.out.println("\n" + "=".repeat(60));
System.out.println("MATRIX MULTIPLICATION TESTS");
System.out.println("=".repeat(60));
int passed = 0;
int total = 3;
// Test 1: Sequential == Parallel
System.out.print("Test 1 - Sequential vs Parallel (100x100)... ");
Matrix A = MatrixUtils.randomMatrix(100, 100);
Matrix B = MatrixUtils.randomMatrix(100, 100);
Matrix seqResult = new SequentialMatrixMultiplier().multiply(A, B);
Matrix parResult = new ForkJoinMatrixMultiplier(32).multiply(A, B);
if (matricesEqual(seqResult, parResult)) {
System.out.println("PASS");
passed++;
} else {
System.out.println("FAIL");
}
// Test 2: Row × Column
System.out.print("Test 2 - Edge case: Row × Column... ");
Matrix row = new Matrix(new double[][]{{1, 2, 3}});
Matrix col = new Matrix(new double[][]{{4}, {5}, {6}});
Matrix dotResult = new ForkJoinMatrixMultiplier(1).multiply(row, col);
// 1*4 + 2*5 + 3*6 = 32
if (Math.abs(dotResult.get(0, 0) - 32.0) < 1e-9) {
System.out.println("PASS");
passed++;
} else {
System.out.println("FAIL");
}
// Test 3: Dimension validation
System.out.print("Test 3 - Dimension validation... ");
try {
Matrix incompatible = new Matrix(new double[][]{{1, 2}});
new SequentialMatrixMultiplier().multiply(row, incompatible);
System.out.println("FAIL (no exception thrown)");
} catch (IllegalArgumentException e) {
System.out.println("PASS");
passed++;
}
System.out.println("=".repeat(60));
System.out.println("RESULT: " + passed + "/" + total + " tests passed");
if (passed == total) {
System.out.println("STATUS: ALL TESTS PASSED - Ready for benchmarking");
} else {
System.out.println("STATUS: SOME TESTS FAILED - Fix before benchmarking");
}
System.out.println("=".repeat(60) + "\n");
}
private static boolean matricesEqual(Matrix a, Matrix b) {
if (a.getRows() != b.getRows() || a.getCols() != b.getCols()) {
return false;
}
for (int i = 0; i < a.getRows(); i++) {
for (int j = 0; j < a.getCols(); j++) {
if (Math.abs(a.get(i, j) - b.get(i, j)) > 1e-9) {
return false;
}
}
}
return true;
}
}