Skip to content
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

[LAB3] 313553005 #204

Open
wants to merge 7 commits into
base: 313553005
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
lab3 with 100% coverage
nanlioniya committed Mar 20, 2025
commit 9a70133a84992da36c4c67b88eaf49153aa30f3b
60 changes: 60 additions & 0 deletions lab3/main_test.js
Original file line number Diff line number Diff line change
@@ -3,3 +3,63 @@ const assert = require('assert');
const { Calculator } = require('./main');

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

describe('exp(x)', () => {
// 測試正常情況
const normalCases = [
{ input: 0, expected: 1 },
{ input: 1, expected: Math.E },
{ input: -1, expected: 1/Math.E }
];

normalCases.forEach(({ input, expected }) => {
it(`should correctly calculate exp(${input})`, () => {
assert.strictEqual(calculator.exp(input), expected);
});
});

// 測試錯誤情況
const errorCases = [
{ input: Infinity, error: 'unsupported operand type' },
{ input: NaN, error: 'unsupported operand type' },
{ input: 1000, error: 'overflow' } // 大數會造成溢位
];

errorCases.forEach(({ input, error }) => {
it(`should throw error for exp(${input})`, () => {
assert.throws(() => calculator.exp(input), { message: error });
});
});
});

describe('log(x)', () => {
// 測試正常情況
const normalCases = [
{ input: 1, expected: 0 },
{ input: Math.E, expected: 1 },
{ input: 10, expected: Math.log(10) }
];

normalCases.forEach(({ input, expected }) => {
it(`should correctly calculate log(${input})`, () => {
assert.strictEqual(calculator.log(input), expected);
});
});

// 測試錯誤情況
const errorCases = [
{ input: Infinity, error: 'unsupported operand type' },
{ input: NaN, error: 'unsupported operand type' },
{ input: 0, error: 'math domain error (1)' },
{ input: -1, error: 'math domain error (2)' }
];

errorCases.forEach(({ input, error }) => {
it(`should throw error for log(${input})`, () => {
assert.throws(() => calculator.log(input), { message: error });
});
});
});
});