Skip to content

Commit 8772820

Browse files
committed
Finish lab3
1 parent afcb770 commit 8772820

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

lab3/main_test.js

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

55
// TODO: write your tests here
6+
7+
const { mock } = require('node:test');
8+
9+
describe('Calculator', () => {
10+
it('should throw an error on non-numeric input when calculate exponential of a number', () => {
11+
const calculator = new Calculator();
12+
const testcases = [
13+
{ params: 'a', expected: Error('unsupported operand type') },
14+
{ params: '123abc', expected: Error('unsupported operand type') },
15+
{ params: '1', expected: Error('unsupported operand type') },
16+
{ params: '0', expected: Error('unsupported operand type') },
17+
{ params: true, expected: Error('unsupported operand type') },
18+
{ params: false, expected: Error('unsupported operand type') },
19+
{ params: NaN, expected: Error('unsupported operand type') },
20+
{ params: Infinity, expected: Error('unsupported operand type') },
21+
{ params: -Infinity, expected: Error('unsupported operand type') },
22+
]
23+
for (const testcase of testcases) {
24+
assert.throws(() => calculator.exp(testcase.params), testcase.expected);
25+
}
26+
});
27+
it('show throw an error on overflow when calculate exponential of a number', () => {
28+
const calculator = new Calculator();
29+
mock.method(Math, 'exp', () => Infinity);
30+
assert.throws(() => calculator.exp(1), Error('overflow'));
31+
mock.reset();
32+
});
33+
it('should return the exponential of a number', () => {
34+
const calculator = new Calculator();
35+
36+
function getRndInteger(min, max) {
37+
return Math.floor(Math.random() * (max - min + 1)) + min;
38+
}
39+
40+
for (let i = 0; i < 20; i++) {
41+
const number = getRndInteger(-100, 100);
42+
assert.strictEqual(calculator.exp(number), Math.exp(number));
43+
}
44+
});
45+
it('should throw an error on non-numeric input when calculate logarithm of a number', () => {
46+
const calculator = new Calculator();
47+
const testcases = [
48+
{ params: 'a', expected: Error('unsupported operand type') },
49+
{ params: '123abc', expected: Error('unsupported operand type') },
50+
{ params: '1', expected: Error('unsupported operand type') },
51+
{ params: '0', expected: Error('unsupported operand type') },
52+
{ params: true, expected: Error('unsupported operand type') },
53+
{ params: false, expected: Error('unsupported operand type') },
54+
{ params: NaN, expected: Error('unsupported operand type') },
55+
{ params: Infinity, expected: Error('unsupported operand type') },
56+
{ params: -Infinity, expected: Error('unsupported operand type') },
57+
]
58+
for (const testcase of testcases) {
59+
assert.throws(() => calculator.log(testcase.params), testcase.expected);
60+
}
61+
});
62+
it('show throw an error on math domain error when calculate logarithm of a number', () => {
63+
const calculator = new Calculator();
64+
65+
mock.method(Math, 'log', () => -Infinity);
66+
assert.throws(() => calculator.log(1), Error('math domain error (1)'));
67+
mock.reset();
68+
69+
mock.method(Math, 'log', () => NaN);
70+
assert.throws(() => calculator.log(1), Error('math domain error (2)'));
71+
mock.reset();
72+
});
73+
it('should return the logarithm of a number', () => {
74+
const calculator = new Calculator();
75+
76+
function getRndInteger(min, max) {
77+
return Math.floor(Math.random() * (max - min + 1)) + min;
78+
}
79+
80+
for (let i = 0; i < 20; i++) {
81+
const number = getRndInteger(1, 1000);
82+
assert.strictEqual(calculator.log(number), Math.log(number));
83+
}
84+
});
85+
});

0 commit comments

Comments
 (0)