Skip to content

Commit 5d39153

Browse files
authored
Merge pull request #597 from as10968574/lab3
[LAB3] 510558017
2 parents f930783 + d44a17c commit 5d39153

File tree

1 file changed

+67
-1
lines changed

1 file changed

+67
-1
lines changed

lab3/main_test.js

+67-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,70 @@ 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', () => {
7+
it('should calculate the exponential function correctly', () => {
8+
const calculator = new Calculator();
9+
assert.strictEqual(calculator.exp(0), 1);
10+
assert.strictEqual(calculator.exp(1), Math.exp(1));
11+
assert.strictEqual(calculator.exp(2), Math.exp(2));
12+
});
13+
14+
it('should throw an error for unsupported operand type', () => {
15+
const calculator = new Calculator();
16+
assert.throws(() => calculator.exp(NaN), {
17+
name: 'Error',
18+
message: 'unsupported operand type'
19+
});
20+
assert.throws(() => calculator.exp(Infinity), {
21+
name: 'Error',
22+
message: 'unsupported operand type'
23+
});
24+
});
25+
26+
it('should throw an error for overflow', () => {
27+
const calculator = new Calculator();
28+
assert.throws(() => calculator.exp(1000), {
29+
name: 'Error',
30+
message: 'overflow'
31+
});
32+
});
33+
});
34+
35+
describe('log', () => {
36+
37+
it('should calculate the natural logarithm correctly', () => {
38+
const calculator = new Calculator();
39+
assert.strictEqual(calculator.log(1), 0);
40+
assert.strictEqual(calculator.log(Math.E), 1);
41+
});
42+
43+
it('should throw an error for unsupported operand type', () => {
44+
const calculator = new Calculator();
45+
assert.throws(() => calculator.log(NaN), {
46+
name: 'Error',
47+
message: 'unsupported operand type'
48+
});
49+
assert.throws(() => calculator.log(Infinity), {
50+
name: 'Error',
51+
message: 'unsupported operand type'
52+
});
53+
});
54+
55+
it('should throw an error for math domain error (1)', () => {
56+
const calculator = new Calculator();
57+
assert.throws(() => calculator.log(0), {
58+
name: 'Error',
59+
message: 'math domain error (1)'
60+
});
61+
});
62+
63+
it('should throw an error for math domain error (2)', () => {
64+
const calculator = new Calculator();
65+
assert.throws(() => calculator.log(-1), {
66+
name: 'Error',
67+
message: 'math domain error (2)'
68+
});
69+
});
70+
});
71+
});

0 commit comments

Comments
 (0)