|
1 | 1 | const {describe, it} = require('node:test');
|
2 | 2 | const assert = require('assert');
|
3 | 3 | const { Calculator } = require('./main');
|
4 |
| - |
| 4 | +const test = require('node:test'); |
5 | 5 | // TODO: write your tests here
|
| 6 | +// const { test, describe } = require('node:test'); |
| 7 | +// const assert = require('assert'); |
| 8 | +// const { Calculator } = require('./main'); |
| 9 | + |
| 10 | +describe('Calculator', () => { |
| 11 | + const calculator = new Calculator(); |
| 12 | + |
| 13 | + test('return Math.exp(x) ', () => { |
| 14 | + assert.ok(Math.abs(calculator.exp(1) - Math.exp(1)) < 1e-10); |
| 15 | + assert.ok(Math.abs(calculator.exp(0) - 1) < 1e-10); |
| 16 | + }); |
| 17 | + |
| 18 | + test('exp() throw "unsupported operand type" for non-finite x', () => { |
| 19 | + assert.throws(() => calculator.exp(Infinity), /unsupported operand type/); |
| 20 | + assert.throws(() => calculator.exp(-Infinity), /unsupported operand type/); |
| 21 | + assert.throws(() => calculator.exp(NaN), /unsupported operand type/); |
| 22 | + }); |
| 23 | + |
| 24 | + test('exp() throw "overflow" for large x resulting Infinity', () => { |
| 25 | + assert.throws(() => calculator.exp(10000), /overflow/); |
| 26 | + }); |
| 27 | + |
| 28 | + test('log() return Math.log(x) ', () => { |
| 29 | + assert.ok(Math.abs(calculator.log(1) - 0) < 1e-10); |
| 30 | + assert.ok(Math.abs(calculator.log(Math.E) - 1) < 1e-10); |
| 31 | + }); |
| 32 | + |
| 33 | + test('log() throw "unsupported operand type" for non-finite x', () => { |
| 34 | + assert.throws(() => calculator.log(Infinity), /unsupported operand type/); |
| 35 | + assert.throws(() => calculator.log(-Infinity), /unsupported operand type/); |
| 36 | + assert.throws(() => calculator.log(NaN), /unsupported operand type/); |
| 37 | + }); |
| 38 | + |
| 39 | + test('log() throw "math domain error (1)" when x = 0', () => { |
| 40 | + assert.throws(() => calculator.log(0), /math domain error \(1\)/); |
| 41 | + }); |
| 42 | + |
| 43 | + test('log() throw "math domain error (2)" when x < 0', () => { |
| 44 | + assert.throws(() => calculator.log(-1), /math domain error \(2\)/); |
| 45 | + }); |
| 46 | +}); |
0 commit comments