Skip to content

[LAB3] 313551010 #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 1, 2025
Merged
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
67 changes: 66 additions & 1 deletion lab3/main_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,69 @@ const {describe, it} = require('node:test');
const assert = require('assert');
const { Calculator } = require('./main');

// TODO: write your tests here
describe('Calculator', () => {
describe('exp', () => {
const calculator = new Calculator();

it('should throw "unsupported operand type" for non-finite numbers', () => {
const invalidInputs = [NaN, Infinity, -Infinity, 'string', null, undefined];
invalidInputs.forEach(input => {
assert.throws(() => calculator.exp(input), Error('unsupported operand type'));
});
});

it('should throw "overflow" for large numbers', () => {
const largeInputs = [1000, 10000, 100000];
largeInputs.forEach(input => {
assert.throws(() => calculator.exp(input), Error('overflow'));
});
});

it('should return correct exp value for valid inputs', () => {
const testCases = [
{ input: 0, expected: 1 },
{ input: 1, expected: Math.exp(1) },
{ input: -1, expected: Math.exp(-1) },
];
testCases.forEach(({ input, expected }) => {
assert.strictEqual(calculator.exp(input), expected);
});
});
});

describe('log', () => {
const calculator = new Calculator();

it('should throw "unsupported operand type" for non-finite numbers', () => {
const invalidInputs = [NaN, Infinity, -Infinity, 'string', null, undefined];
invalidInputs.forEach(input => {
assert.throws(() => calculator.log(input), Error('unsupported operand type'));
});
});

it('should throw "math domain error (1)" for zero', () => {
const invalidInputs = [0];
invalidInputs.forEach(input => {
assert.throws(() => calculator.log(input), Error('math domain error (1)'));
});
});

it('should throw "math domain error (2)" for finite negative numbers', () => {
const invalidInputs = [-1, -5, -100];
invalidInputs.forEach(input => {
assert.throws(() => calculator.log(input), Error('math domain error (2)'));
});
});

it('should return correct log value for valid inputs', () => {
const testCases = [
{ input: 1, expected: 0 },
{ input: Math.E, expected: 1 },
{ input: 10, expected: Math.log(10) },
];
testCases.forEach(({ input, expected }) => {
assert.strictEqual(calculator.log(input), expected);
});
});
});
});
Loading