Skip to content

Commit 2a7b2ed

Browse files
authored
Merge pull request #207 from SapphireLinXu/lab3
[LAB3] 312553027
2 parents 2600510 + 526fee9 commit 2a7b2ed

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

lab3/main_test.js

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

55
// TODO: write your tests here
6+
7+
describe('Calculator', () => {
8+
9+
it('should calculate exp correctly', () => {
10+
const testcases = [
11+
[Infinity, 'unsupported operand type'],
12+
[Number.MAX_VALUE, 'overflow'],
13+
[1, Math.exp(1)],
14+
[0, Math.exp(0)],
15+
[-1, Math.exp(-1)]
16+
];
17+
const calculator = new Calculator();
18+
for (const [input, expected] of testcases) {
19+
if (typeof expected === 'string') {
20+
assert.throws(() => calculator.exp(input), Error);
21+
} else {
22+
assert.strictEqual(calculator.exp(input), expected);
23+
}
24+
}
25+
});
26+
27+
it('should calculate log correctly', () => {
28+
const testcases = [
29+
[Infinity, 'unsupported operand type'],
30+
[0, 'unsupported operand type'],
31+
[-1, 'unsupported operand type'],
32+
[1, Math.log(1)],
33+
[100, Math.log(100)],
34+
[Math.E, Math.log(Math.E)]
35+
];
36+
const calculator = new Calculator();
37+
for (const [input, expected] of testcases) {
38+
if (typeof expected === 'string') {
39+
assert.throws(() => calculator.log(input), Error);
40+
} else {
41+
assert.strictEqual(calculator.log(input), expected);
42+
}
43+
}
44+
});
45+
});
46+

0 commit comments

Comments
 (0)