Skip to content

Commit

Permalink
Port Multiply matrices tests to new format (#559)
Browse files Browse the repository at this point in the history
* 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
epsilonError committed Aug 9, 2024
1 parent fc076e0 commit bfe6a05
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 1 deletion.
1 change: 1 addition & 0 deletions test/index-fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ let tests = await Promise.all([
"in_gamut",
"parse",
"contrast",
"multiply_matrices",
].map(name => import(`./${name}.js`).then(module => module.default)));


Expand Down
7 changes: 7 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"contrast": "Contrast",
"angles": "Angles",
"adapt": "Chromatic adaptation",
"multiply-matrices": "Matrix multiplication",
"multiply_matrices": "Matrix multiplication",
"hooks": "Hooks"
}
97 changes: 97 additions & 0 deletions test/multiply_matrices.js
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),
}],
};

0 comments on commit bfe6a05

Please sign in to comment.