Skip to content

Commit

Permalink
test: adding more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-logan committed Mar 30, 2024
1 parent ccf8229 commit efd0eda
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 5 deletions.
87 changes: 87 additions & 0 deletions packages/typescript/src/arithmeticOp/index.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default function generateCriticalPointInterval(
min: number,
max: number,
) {
return Math.random() * (max - min + 1) + min;
}
13 changes: 8 additions & 5 deletions packages/typescript/src/loganmatic.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -787,6 +786,10 @@ class Calculator {
);
}
}

public getRufinhoDevice() {
return this.ruffiniDevice;
}
}

// Create an instance of the Calculator class
Expand Down
54 changes: 54 additions & 0 deletions packages/typescript/tests/arithmeticOp.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
20 changes: 20 additions & 0 deletions packages/typescript/tests/equations.test.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand Down

0 comments on commit efd0eda

Please sign in to comment.