-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port Multiply matrices tests to new format (#559)
* Use _ instead of - to match \w regex * Port multiply matrices tests * Use mathjs importmap so HTML tests expected values can be determined by mathjs * Inline test arrays and map in expected values * lint fix
- Loading branch information
1 parent
fc076e0
commit bfe6a05
Showing
4 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,13 @@ | |
<meta charset="UTF-8"> | ||
<title>Tests</title> | ||
<link rel="stylesheet" href="../node_modules/htest.dev/htest.css" crossorigin /> | ||
<script type="importmap"> | ||
{ | ||
"imports": { | ||
"mathjs": "https://cdn.jsdelivr.net/npm/[email protected]/+esm" | ||
} | ||
} | ||
</script> | ||
<script src="../node_modules/htest.dev/htest.js" type="module" crossorigin></script> | ||
<script> | ||
let params = new URLSearchParams(location.search); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import * as math from "mathjs"; // Used as test oracle | ||
import multiplyMatrices from "../src/multiply-matrices.js"; | ||
import * as check from "../node_modules/htest.dev/src/check.js"; | ||
|
||
// Used to collect expected results from oracle | ||
function refMultiply (A, B) { | ||
return math.multiply(math.matrix(A), math.matrix(B)).valueOf(); | ||
} | ||
|
||
function testExpected (testObj) { | ||
return { ...testObj, expect: refMultiply(...testObj.args) }; | ||
} | ||
|
||
function expectThrows (testObj) { | ||
let refResult; | ||
try { | ||
refResult = refMultiply(...testObj.args); | ||
} | ||
catch (error) { | ||
refResult = error.message; | ||
} | ||
return { ...testObj, expect: refResult }; | ||
} | ||
|
||
const M_lin_sRGB_to_XYZ = [ | ||
[0.4124564, 0.3575761, 0.1804375], | ||
[0.2126729, 0.7151522, 0.0721750], | ||
[0.0193339, 0.1191920, 0.9503041], | ||
]; | ||
|
||
const M_XYZ_to_lin_sRGB = [ | ||
[3.2404542, -1.5371385, -0.4985314], | ||
[-0.9692660, 1.8760108, 0.0415560], | ||
[0.0556434, -0.2040259, 1.0572252], | ||
]; | ||
|
||
export default { | ||
name: "Matrix multiplication Tests", | ||
run: multiplyMatrices, | ||
check: check.deep(check.proximity({ epsilon: 0.00001 })), | ||
tests: [{ | ||
name: "Basic 3x3 and vectors", | ||
tests: [ | ||
{ | ||
name: "3x3 matrix with vector", | ||
args: [M_lin_sRGB_to_XYZ, [1, .5, 0]], | ||
}, | ||
{ | ||
name: "3x3 matrix with itself", | ||
args: [M_lin_sRGB_to_XYZ, M_lin_sRGB_to_XYZ], | ||
}, | ||
{ | ||
name: "Vector with vector", | ||
skip: true, // multiplyMatrices doesn't return numbers | ||
args: [[1, 2, 3], [1, .5, 0]], | ||
}, | ||
{ | ||
name: "3x3 matrix with vector", | ||
args: [M_XYZ_to_lin_sRGB, [1, .5, 0]], | ||
}, | ||
{ | ||
name: "3x3 matrix with itself", | ||
args: [M_XYZ_to_lin_sRGB, M_XYZ_to_lin_sRGB], | ||
}, | ||
{ | ||
name: "3x3 matrix with other 3x3 matrix", | ||
args: [M_XYZ_to_lin_sRGB, M_lin_sRGB_to_XYZ], | ||
}, | ||
].map(testExpected), | ||
}, | ||
{ | ||
name: "Incorrect data", | ||
description: "These are expected to fail, as multiplyMatrices does not do dimension checking. The point of them is to see how it fails.", | ||
check: (..._args) => { | ||
return true; // Treat these tests as passed | ||
}, | ||
tests: [ | ||
{ | ||
name: "Incompatible dimensions (matrix × matrix)", | ||
args: [[[1], [3]], [[1, 2], [3, 4]]], | ||
}, | ||
{ | ||
name: "Incompatible dimensions (vector × matrix)", | ||
args: [[1, 2, 3], [[1, 2], [3, 4]]], | ||
}, | ||
{ | ||
name: "Different number of elements per row", | ||
args: [[[1, 2], [3, 4, 5]], [[1, 2], [3, 4]]], | ||
}, | ||
{ | ||
name: "Empty vectors", | ||
skip: true, // multiplyMatrices calls length on the undefined first element of the empty array | ||
args: [[], []], | ||
}, | ||
].map(expectThrows), | ||
}], | ||
}; |