Skip to content

Commit

Permalink
feat: add testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikitosiki committed Feb 11, 2024
1 parent 61fccfa commit f226928
Show file tree
Hide file tree
Showing 8 changed files with 1,987 additions and 20 deletions.
8 changes: 8 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ module.exports = {
{ allowConstantExport: true },
],
},
overrides: [
{
"files": ["tests/**/*"],
"env": {
"jest": true
}
}
]
}
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
export default {
preset: 'ts-jest',
testEnvironment: 'node',
};
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
"preview": "vite preview",
"test": "jest"
},
"dependencies": {
"@react-stately/data": "^3.11.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/jest": "^29.5.12",
"@types/react": "^18.2.43",
"@types/react-dom": "^18.2.17",
"@typescript-eslint/eslint-plugin": "^6.14.0",
Expand All @@ -24,10 +26,12 @@
"eslint": "^8.55.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.5",
"jest": "^29.7.0",
"postcss": "^8.4.35",
"prettier": "^3.2.5",
"prettier-plugin-tailwindcss": "^0.5.11",
"tailwindcss": "^3.4.1",
"ts-jest": "^29.1.2",
"typescript": "^5.2.2",
"vite": "^5.0.8"
}
Expand Down
54 changes: 54 additions & 0 deletions src/tests/inverseMatrix.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import inverseMatrix from "../functions/inverseMatrix";
import { roundedMatrix } from "../functions/utils";
import { toStrictEqual2Array } from "./toStrictEqualArray";

test("inverse_matrix_test-1", () => {
const matrix = [
[5, -3, 7],
[-1, 4, 3],
[6, -2, 5],
];

const inverse = [
[-0.28, -0.011, 0.398],
[-0.247, 0.183, 0.237],
[0.237, 0.086, -0.183],
];

const result = roundedMatrix(inverseMatrix(matrix) ?? []);
expect(toStrictEqual2Array(result, inverse)).toBe(true);
});

test("inverse_matrix_test-2", () => {
const matrix = [
[6, 2, 5],
[-3, 4, -1],
[1, 4, 3],
];

const inverse = [
[0.5, 0.437, -0.687],
[0.25, 0.406, -0.281],
[-0.5, -0.687, 0.937],
];

const result = roundedMatrix(inverseMatrix(matrix) ?? []);
expect(toStrictEqual2Array(result, inverse)).toBe(true);
});

test("inverse_matrix_test-3", () => {
const matrix = [
[2, -1, 3],
[-1, 2, 2],
[1, 1, 1],
];

const inverse = [
[0, -0.333, 0.667],
[-0.25, 0.083, 0.583],
[0.25, 0.25, -0.25],
];

const result = roundedMatrix(inverseMatrix(matrix) ?? []);
expect(toStrictEqual2Array(result, inverse)).toBe(true);
});
80 changes: 80 additions & 0 deletions src/tests/matrixRank.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import calculateMatrixRank from "../functions/matrixRank";

test("matrix_rank_test-1", () => {
const matrix = [
[1, 2, 3, 4],
[2, 4, 6, 8],
];

const rank = 1;

const result = calculateMatrixRank(matrix);
expect(rank).toBe(result);
});

test("matrix_rank_test-2", () => {
const matrix = [
[2, 5, 4],
[-3, 1, -2],
[-1, 6, 2]
];

const rank = 2;

const result = calculateMatrixRank(matrix);
expect(rank).toBe(result);
});

test("matrix_rank_test-3", () => {
const matrix = [
[1, 2],
[3, 6],
[5, 10],
[4, 8]
];

const rank = 1;

const result = calculateMatrixRank(matrix);
expect(rank).toBe(result);
});

test("matrix_rank_test-4", () => {
const matrix = [
[6, 2, 5],
[-3, 4, -1],
[1, 4, 3],
];

const rank = 3;

const result = calculateMatrixRank(matrix);
expect(rank).toBe(result);
});

test("matrix_rank_test-5", () => {
const matrix = [
[-1, 5, 4],
[-2, 7, 5],
[3, 4, 1],
];

const rank = 3;

const result = calculateMatrixRank(matrix);
expect(rank).toBe(result);
});

test("matrix_rank_test-6", () => {
const matrix = [
[1, 2, 3, 4],
[-2, 5, -1, 3],
[2, 4, 6, 8],
[-1, 7, 2, 7]
];

const rank = 2;

const result = calculateMatrixRank(matrix);
expect(rank).toBe(result);
});
47 changes: 47 additions & 0 deletions src/tests/solveLinearSystem.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import solveLinearSystem from "../functions/solveLinearSystem";
import { toStrictEqual1Array } from "./toStrictEqualArray";

test("solve_linear_system_test-1", () => {
const matrix = [
[5, -3, 7],
[-1, 4, 3],
[6, -2, 5],
];

const constants = [13, 13, 12];

const output = [1, 2, 2];

const result = solveLinearSystem(matrix, constants);
expect(toStrictEqual1Array(output, result ?? [])).toBe(true);
});

test("solve_linear_system_test-2", () => {
const matrix = [
[6, 2, 5],
[-3, 4, -1],
[1, 4, 3],
];

const constants = [1, 6, 6];

const output = [-1, 1, 1];

const result = solveLinearSystem(matrix, constants);
expect(toStrictEqual1Array(output, result ?? [])).toBe(true);
});

test("solve_linear_system_test-3", () => {
const matrix = [
[-2, -1, -2],
[4, -2, 1],
[1, 3, -5],
];

const constants = [1, 5, 3];

const output = [1, -1, -1];

const result = solveLinearSystem(matrix, constants);
expect(toStrictEqual1Array(output, result ?? [])).toBe(true);
});
40 changes: 40 additions & 0 deletions src/tests/toStrictEqualArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// export function toStrictEqual2Array(value: number[][], correct: number[][]) {
// expect(value.length).toBe(correct.length);
// for (let index = 0; index < correct.length; index++)
// expect(value[index].length).toBe(correct[index].length);

// for (let i = 0; i < correct.length; i++)
// for (let j = 0; j < correct[i].length; j++)
// expect(correct[i][j]).toBeCloseTo(correct[i][j]);
// }

export function toStrictEqual1Array(
value: number[],
correct: number[],
): boolean {
if (value.length != correct.length) return false;

for (let i = 0; i < correct.length; i++)
if (!areEqual(value[i], correct[i])) return false;

return true;
}

export function toStrictEqual2Array(
value: number[][],
correct: number[][],
): boolean {
if (value.length != correct.length) return false;
for (let index = 0; index < correct.length; index++)
if (value[index].length != correct[index].length) return false;

for (let i = 0; i < correct.length; i++)
for (let j = 0; j < correct[i].length; j++)
if (!areEqual(value[i][j], correct[i][j])) return false;

return true;
}

function areEqual(number1: number, number2: number): boolean {
return Math.abs(number1 - number2) < 0.002;
}
Loading

0 comments on commit f226928

Please sign in to comment.