Skip to content

Commit 344b3f7

Browse files
committed
Lab3: add parameterized unit tests
1 parent dfe6b1f commit 344b3f7

1 file changed

Lines changed: 58 additions & 1 deletion

File tree

lab3/main_test.js

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,61 @@ const {describe, it} = require('node:test');
22
const assert = require('assert');
33
const { Calculator } = require('./main');
44

5-
// TODO: write your tests here
5+
const calc = new Calculator();
6+
7+
describe('Calculator', () => {
8+
// exp() 正確結果
9+
it('exp() should return correct results', () => {
10+
const cases = [
11+
{ param: 0, expected: Math.exp(0) },
12+
{ param: 1, expected: Math.exp(1) },
13+
{ param: -1, expected: Math.exp(-1) },
14+
];
15+
for (const c of cases) {
16+
assert.strictEqual(calc.exp(c.param), c.expected);
17+
}
18+
});
19+
20+
// exp() 錯誤處理 unsupported operand
21+
it('exp() should throw unsupported operand type', () => {
22+
const inputs = [Infinity, -Infinity, NaN];
23+
for (const x of inputs) {
24+
assert.throws(() => calc.exp(x), { message: 'unsupported operand type' });
25+
}
26+
});
27+
28+
// exp() overflow
29+
it('exp() should throw overflow for large number', () => {
30+
assert.throws(() => calc.exp(1000), { message: 'overflow' });
31+
});
32+
33+
// log() 正確結果
34+
it('log() should return correct results', () => {
35+
const cases = [
36+
{ param: 1, expected: Math.log(1) },
37+
{ param: Math.E, expected: Math.log(Math.E) },
38+
{ param: 10, expected: Math.log(10) },
39+
];
40+
for (const c of cases) {
41+
assert.strictEqual(calc.log(c.param), c.expected);
42+
}
43+
});
44+
45+
// log() 錯誤處理 unsupported operand
46+
it('log() should throw unsupported operand type', () => {
47+
const inputs = [Infinity, -Infinity, NaN];
48+
for (const x of inputs) {
49+
assert.throws(() => calc.log(x), { message: 'unsupported operand type' });
50+
}
51+
});
52+
53+
// log() math domain error (1)
54+
it('log() should throw math domain error (1) for 0', () => {
55+
assert.throws(() => calc.log(0), { message: 'math domain error (1)' });
56+
});
57+
58+
// log() math domain error (2)
59+
it('log() should throw math domain error (2) for negative input', () => {
60+
assert.throws(() => calc.log(-3), { message: 'math domain error (2)' });
61+
});
62+
});

0 commit comments

Comments
 (0)