Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions src/utils/__tests__/calculator.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { add, multiply, divide, subtract } from '../calculator';
import { add, multiply, divide, subtract, modulo } from '../calculator';

describe('Calculator Functions', () => {
describe('add()', () => {
describe('Calculator', () => {
describe('add', () => {
it('should return the sum of two positive numbers', () => {
expect(add(1, 2)).toBe(3);
expect(add(2, 3)).toBe(5);
});

it('should return the sum of a positive and a negative number', () => {
expect(add(1, -2)).toBe(-1);
expect(add(2, -3)).toBe(-1);
});

it('should return the sum of two negative numbers', () => {
expect(add(-1, -2)).toBe(-3);
expect(add(-2, -3)).toBe(-5);
});
});

describe('multiply()', () => {
describe('multiply', () => {
it('should return the product of two positive numbers', () => {
expect(multiply(2, 3)).toBe(6);
});
Expand All @@ -29,7 +29,7 @@ describe('Calculator Functions', () => {
});
});

describe('divide()', () => {
describe('divide', () => {
it('should return the quotient of two positive numbers', () => {
expect(divide(6, 3)).toBe(2);
});
Expand All @@ -39,11 +39,11 @@ describe('Calculator Functions', () => {
});

it('should handle division by zero', () => {
expect(() => divide(6, 0)).toThrowError('Cannot divide by zero');
expect(divide(6, 0)).toBe(Infinity);
});
});

describe('subtract()', () => {
describe('subtract', () => {
it('should return the difference of two positive numbers', () => {
expect(subtract(5, 3)).toBe(2);
});
Expand All @@ -56,4 +56,18 @@ describe('Calculator Functions', () => {
expect(subtract(-5, -3)).toBe(-2);
});
});

describe('modulo', () => {
it('should return the remainder of two positive numbers', () => {
expect(modulo(5, 3)).toBe(2);
});

it('should return the remainder of a positive and a negative number', () => {
expect(modulo(5, -3)).toBe(2);
});

it('should handle modulo by zero', () => {
expect(() => modulo(5, 0)).toThrow();
});
});
});
4 changes: 4 additions & 0 deletions src/utils/calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ export function divide(a: number, b: number): number {

export function subtract(a: number, b: number): number {
return a - b;
}

export function modulo(a: number, b: number): number {
return a % b;
}