From efd0edaa77ec8dd371e139690404c050a60434c7 Mon Sep 17 00:00:00 2001 From: gabriel-logan Date: Sat, 30 Mar 2024 06:53:32 -0300 Subject: [PATCH] test: adding more tests --- packages/typescript/src/arithmeticOp/index.ts | 87 +++++++++++++++++++ .../generateCriticalPointInterval/index.ts | 6 ++ packages/typescript/src/loganmatic.ts | 13 +-- .../typescript/tests/arithmeticOp.test.ts | 54 ++++++++++++ packages/typescript/tests/equations.test.ts | 20 +++++ 5 files changed, 175 insertions(+), 5 deletions(-) create mode 100644 packages/typescript/src/arithmeticOp/index.ts create mode 100644 packages/typescript/src/generateCriticalPointInterval/index.ts create mode 100644 packages/typescript/tests/arithmeticOp.test.ts diff --git a/packages/typescript/src/arithmeticOp/index.ts b/packages/typescript/src/arithmeticOp/index.ts new file mode 100644 index 0000000..b71217a --- /dev/null +++ b/packages/typescript/src/arithmeticOp/index.ts @@ -0,0 +1,87 @@ +/** + * Represents a class for performing arithmetic operations. + */ +export default class ArithmeticOp { + /** + * Calculates the sum of the given numbers. + * @param numbers - The numbers to be summed. + * @returns The sum of the numbers. + */ + protected sum(...numbers: number[]): number { + return numbers.reduce((acc, curr) => acc + curr, 0); + } + + /** + * Calculates the difference between the given numbers. + * @param numbers - The numbers to be subtracted. + * @returns The difference between the numbers. + */ + protected sub(...numbers: number[]): number { + return numbers.reduce((acc, curr) => acc - curr); + } + + /** + * Calculates the product of the given numbers. + * @param numbers - The numbers to be multiplied. + * @returns The product of the numbers. + */ + protected mul(...numbers: number[]): number { + return numbers.reduce((acc, curr) => acc * curr, 1); + } + + /** + * Calculates the division of the given numbers. + * @param numbers - The numbers to be divided. + * @returns The division of the numbers. + */ + protected div(...numbers: number[]): number { + return numbers.reduce((acc, curr) => acc / curr); + } + + /** + * Calculates the modulo of the given numbers. + * @param numbers - The numbers to be modded. + * @returns The modulo of the numbers. + */ + protected mod(...numbers: number[]): number { + return numbers.reduce((acc, curr) => acc % curr); + } + + /** + * Calculates the power of the given base and exponent. + * @param base - The base number. + * @param exponent - The exponent number. + * @returns The power of the base and exponent. + */ + protected power(base: number, exponent: number): number { + let result = 1; + for (let i = 0; i < exponent; i++) { + result *= base; + } + return result; + } + + public getSum() { + return this.sum; + } + + public getSub() { + return this.sub; + } + + public getMul() { + return this.mul; + } + + public getDiv() { + return this.div; + } + + public getMod() { + return this.mod; + } + + public getPower() { + return this.power; + } +} diff --git a/packages/typescript/src/generateCriticalPointInterval/index.ts b/packages/typescript/src/generateCriticalPointInterval/index.ts new file mode 100644 index 0000000..8459a9b --- /dev/null +++ b/packages/typescript/src/generateCriticalPointInterval/index.ts @@ -0,0 +1,6 @@ +export default function generateCriticalPointInterval( + min: number, + max: number, +) { + return Math.random() * (max - min + 1) + min; +} diff --git a/packages/typescript/src/loganmatic.ts b/packages/typescript/src/loganmatic.ts index 27290c1..e1e6c3c 100644 --- a/packages/typescript/src/loganmatic.ts +++ b/packages/typescript/src/loganmatic.ts @@ -1,5 +1,7 @@ // Class that represents a calculator with some basic mathematical functions +import ArithmeticOp from "./arithmeticOp"; +import generateCriticalPointInterval from "./generateCriticalPointInterval"; import { ReturnTypesForEquation, ReturnTypesForEquation2upDegree, @@ -13,11 +15,12 @@ const piValue: string = "3.1415926535897932384626433832795"; * @example - import Mathematics from "loganmatic" * console.log(Mathematics.Pi) */ -class Calculator { +class Calculator extends ArithmeticOp { EulerNumber: number; // Declare EulerNumber property Pi: number; // Declare Pi property constructor() { + super(); this.EulerNumber = this.createEulerNumber(); this.Pi = parseFloat(piValue); } @@ -72,10 +75,6 @@ class Calculator { criticalPoint1 = criticalPoint2 * -1; } - function generateCriticalPointInterval(min: number, max: number) { - return Math.random() * (max - min + 1) + min; - } - let criticalPoint3: number; if (answer1 > answer2) { @@ -787,6 +786,10 @@ class Calculator { ); } } + + public getRufinhoDevice() { + return this.ruffiniDevice; + } } // Create an instance of the Calculator class diff --git a/packages/typescript/tests/arithmeticOp.test.ts b/packages/typescript/tests/arithmeticOp.test.ts new file mode 100644 index 0000000..494ada7 --- /dev/null +++ b/packages/typescript/tests/arithmeticOp.test.ts @@ -0,0 +1,54 @@ +import ArithmeticOp from "../src/arithmeticOp/index"; + +describe("ArithmeticOp", () => { + let arithmeticOp: ArithmeticOp; + + beforeEach(() => { + arithmeticOp = new ArithmeticOp(); + }); + + describe("sum", () => { + it("should calculate the sum of the given numbers", () => { + expect(arithmeticOp.getSum()(1, 2, 3)).toBe(6); + expect(arithmeticOp.getSum()(10, 20, 30, 40)).toBe(100); + }); + }); + + describe("sub", () => { + it("should calculate the difference between the given numbers", () => { + expect(arithmeticOp.getSub()(5, 2)).toBe(3); + expect(arithmeticOp.getSub()(10, 5, 3)).toBe(2); + }); + }); + + describe("mul", () => { + it("should calculate the product of the given numbers", () => { + expect(arithmeticOp.getMul()(2, 3)).toBe(6); + expect(arithmeticOp.getMul()(5, 10, 2)).toBe(100); + }); + }); + + describe("div", () => { + it("should calculate the division of the given numbers", () => { + expect(arithmeticOp.getDiv()(10, 2)).toBe(5); + expect(arithmeticOp.getDiv()(100, 5, 2)).toBe(10); + }); + }); + + describe("mod", () => { + it("should calculate the modulo of the given numbers", () => { + expect(arithmeticOp.getMod()(10, 3)).toBe(1); + expect(arithmeticOp.getMod()(100, 5, 3)).toBe(0); + expect(arithmeticOp.getMod()(100, 54, 6)).toBe(4); + }); + }); + + describe("power", () => { + it("should calculate the power of the given base and exponent", () => { + expect(arithmeticOp.getPower()(2, 3)).toBe(8); + expect(arithmeticOp.getPower()(5, 3)).toBe(125); + expect(arithmeticOp.getPower()(-10, 2)).toBe(100); + expect(arithmeticOp.getPower()(-2, 3)).toBe(-8); + }); + }); +}); diff --git a/packages/typescript/tests/equations.test.ts b/packages/typescript/tests/equations.test.ts index 624f8cf..901a1af 100644 --- a/packages/typescript/tests/equations.test.ts +++ b/packages/typescript/tests/equations.test.ts @@ -1,6 +1,26 @@ import Mathematics from "../src/loganmatic"; +import generateCriticalPointInterval from "../src/generateCriticalPointInterval/"; describe("Calculadora equations", () => { + // loganmatic.test.ts + + const rufineMethod = Mathematics.getRufinhoDevice(); + + test("getRufinhoDevice returns the expected result", () => { + // Substitua 'arg1', 'arg2', etc. pelos argumentos reais que getRufinhoDevice aceita + const result = rufineMethod(1, -6, 11, -6, [1, 3, 2], false); + // Substitua 'expectedResult' pelo resultado que vocĂȘ espera de getRufinhoDevice + expect(result.value).toEqual([1, 3, 2]); + }); + + test("generateCriticalPointInterval returns a value within the expected range", () => { + const min = 1; + const max = 10; + const result = generateCriticalPointInterval(min, max); + expect(result).toBeGreaterThanOrEqual(min); + expect(result).toBeLessThanOrEqual(max); + }); + test("should correctly calculate the root of a linear equation", () => { const equationRoot = Mathematics.linearEquation(2, 3); expect(equationRoot).toEqual({