Skip to content

Commit 37b2829

Browse files
authored
Merge pull request #227 from yungen-lu/lab3
[LAB3] 313551077
2 parents 1e105d4 + 0eba85a commit 37b2829

1 file changed

Lines changed: 68 additions & 1 deletion

File tree

lab3/main_test.js

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

5-
// TODO: write your tests here
5+
describe('Calculator', () => {
6+
describe('exp function', () => {
7+
const expTestCases = [
8+
{ input: 0, expected: 1, description: 'exp of 0' },
9+
{ input: 1, expected: Math.E, description: 'exp of 1' },
10+
{ input: 2, expected: Math.exp(2), description: 'exp of 2' },
11+
{ input: -1, expected: 1/Math.E, description: 'exp of -1' },
12+
{ input: -2, expected: Math.exp(-2), description: 'exp of -2' }
13+
];
14+
15+
expTestCases.forEach(({ input, expected, description }) => {
16+
it(`should calculate ${description}`, () => {
17+
const calculator = new Calculator();
18+
assert.strictEqual(calculator.exp(input), expected);
19+
});
20+
});
21+
22+
const expErrorCases = [
23+
{ input: Infinity, expectedError: 'unsupported operand type', description: 'Infinity' },
24+
{ input: -Infinity, expectedError: 'unsupported operand type', description: '-Infinity' },
25+
{ input: NaN, expectedError: 'unsupported operand type', description: 'NaN' },
26+
{ input: 1000, expectedError: 'overflow', description: 'large value' }
27+
];
28+
29+
expErrorCases.forEach(({ input, expectedError, description }) => {
30+
it(`should throw error for ${description}`, () => {
31+
const calculator = new Calculator();
32+
assert.throws(() => calculator.exp(input), {
33+
name: 'Error',
34+
message: expectedError
35+
});
36+
});
37+
});
38+
});
39+
40+
describe('log function', () => {
41+
const logTestCases = [
42+
{ input: 1, expected: 0, description: 'log of 1' },
43+
{ input: Math.E, expected: 1, description: 'log of e' },
44+
{ input: Math.exp(2), expected: 2, description: 'log of e^2' }
45+
];
46+
47+
logTestCases.forEach(({ input, expected, description }) => {
48+
it(`should calculate ${description}`, () => {
49+
const calculator = new Calculator();
50+
assert.strictEqual(calculator.log(input), expected);
51+
});
52+
});
53+
54+
const logErrorCases = [
55+
{ input: -1, expectedError: 'math domain error (2)', description: 'negative number' },
56+
{ input: 0, expectedError: 'math domain error (1)', description: 'zero' },
57+
{ input: Infinity, expectedError: 'unsupported operand type', description: 'Infinity' },
58+
{ input: -Infinity, expectedError: 'unsupported operand type', description: '-Infinity' },
59+
{ input: NaN, expectedError: 'unsupported operand type', description: 'NaN' }
60+
];
61+
62+
logErrorCases.forEach(({ input, expectedError, description }) => {
63+
it(`should throw error for ${description}`, () => {
64+
const calculator = new Calculator();
65+
assert.throws(() => calculator.log(input), {
66+
name: 'Error',
67+
message: expectedError
68+
});
69+
});
70+
});
71+
});
72+
});

0 commit comments

Comments
 (0)