Skip to content

Commit 4b54fde

Browse files
authored
Merge pull request #413 from Yyrff/lab3
[LAB3] 511558014
2 parents 30ef88b + 6deb93d commit 4b54fde

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

lab3/main_test.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,70 @@ const assert = require('assert');
33
const { Calculator } = require('./main');
44

55
// TODO: write your tests here
6+
7+
describe('Calculator', function() {
8+
const calculator = new Calculator();
9+
10+
// 定義針對 exp 功能的測試組合
11+
describe('#exp()', function() {
12+
// 針對預期正確的參數測試
13+
const testCasesExp = [
14+
{ input: 1.618, expected: Math.exp(1.618) }, // 黃金分割率
15+
{ input: 0, expected: Math.exp(0) },
16+
{ input: -1, expected: Math.exp(-1) }
17+
];
18+
19+
// for 進行測試
20+
testCasesExp.forEach(({ input, expected }) => {
21+
it(`should correctly calculate exp(${input})`, function() {
22+
assert.strictEqual(calculator.exp(input), expected);
23+
});
24+
});
25+
26+
// 用 RE 針對預期錯誤的參數測試
27+
const errorCasesExp = [
28+
{ input: 'string', message: /unsupported operand type/ },
29+
{ input: Infinity, message: /unsupported operand type/ },
30+
{ input: 123456789, message: /overflow/ }
31+
];
32+
33+
// for 進行測試
34+
errorCasesExp.forEach(({ input, message }) => {
35+
it(`should throw error for exp(${input})`, function() {
36+
assert.throws(() => calculator.exp(input), message);
37+
});
38+
});
39+
});
40+
41+
// 定義針對 log 功能的測試組合
42+
describe('#log()', function() {
43+
// 針對預期正確的參數測試
44+
const testCasesLog = [
45+
{ input: Math.E, expected: 1 },
46+
{ input: 1, expected: Math.log(1) },
47+
{ input: 3.141592653, expected: Math.log(3.141592653) } // π
48+
];
49+
50+
// for 進行測試
51+
testCasesLog.forEach(({ input, expected }) => {
52+
it(`should correctly calculate log(${input})`, function() {
53+
assert.strictEqual(calculator.log(input), expected);
54+
});
55+
});
56+
57+
// 用 RE 針對預期錯誤的參數測試
58+
const errorCasesLog = [
59+
{ input: 'string', message: /unsupported operand type/ },
60+
{ input: 0, message: /math domain error \(1\)/ },
61+
{ input: -1, message: /math domain error \(2\)/ }
62+
];
63+
64+
// for 進行測試
65+
errorCasesLog.forEach(({ input, message }) => {
66+
it(`should throw error for log(${input})`, function() {
67+
assert.throws(() => calculator.log(input), message);
68+
});
69+
});
70+
});
71+
});
72+

0 commit comments

Comments
 (0)