|
1 | | -const {describe, it} = require('node:test'); |
| 1 | +const { describe, it } = require('node:test'); |
2 | 2 | const assert = require('assert'); |
3 | 3 | const { Calculator } = require('./main'); |
4 | 4 |
|
5 | | -// TODO: write your tests here |
| 5 | +const calculator = new Calculator(); |
| 6 | + |
| 7 | +// exp() Error Cases |
| 8 | +describe('Calculator.exp() Error Cases', () => { |
| 9 | + const errorCases = [ |
| 10 | + { param: [NaN], error: 'unsupported operand type' }, |
| 11 | + { param: [Infinity], error: 'unsupported operand type' }, |
| 12 | + { param: [-Infinity], error: 'unsupported operand type' }, |
| 13 | + { param: [1000], error: 'overflow' } |
| 14 | + ]; |
| 15 | + |
| 16 | + for (const tc of errorCases) { |
| 17 | + it(`should throw "${tc.error}" for exp(${tc.param})`, () => { |
| 18 | + assert.throws(() => calculator.exp.apply(calculator, tc.param), { message: tc.error }); |
| 19 | + }); |
| 20 | + } |
| 21 | +}); |
| 22 | + |
| 23 | +// exp() Success Cases |
| 24 | +describe('Calculator.exp() Success Cases', () => { |
| 25 | + const successCases = [ |
| 26 | + { param: [0], expected: 1 }, |
| 27 | + { param: [1], expected: Math.exp(1) }, |
| 28 | + { param: [-1], expected: Math.exp(-1) } |
| 29 | + ]; |
| 30 | + |
| 31 | + for (const tc of successCases) { |
| 32 | + it(`should return ${tc.expected} for exp(${tc.param})`, () => { |
| 33 | + assert.strictEqual(calculator.exp.apply(calculator, tc.param), tc.expected); |
| 34 | + }); |
| 35 | + } |
| 36 | +}); |
| 37 | + |
| 38 | +// log() Error Cases |
| 39 | +describe('Calculator.log() Error Cases', () => { |
| 40 | + const errorCases = [ |
| 41 | + { param: [NaN], error: 'unsupported operand type' }, |
| 42 | + { param: [Infinity], error: 'unsupported operand type' }, |
| 43 | + { param: [-Infinity], error: 'unsupported operand type' }, |
| 44 | + { param: [0], error: 'math domain error (1)' }, |
| 45 | + { param: [-1], error: 'math domain error (2)' } |
| 46 | + ]; |
| 47 | + |
| 48 | + for (const tc of errorCases) { |
| 49 | + it(`should throw "${tc.error}" for log(${tc.param})`, () => { |
| 50 | + assert.throws(() => calculator.log.apply(calculator, tc.param), { message: tc.error }); |
| 51 | + }); |
| 52 | + } |
| 53 | +}); |
| 54 | + |
| 55 | +// log() Success Cases |
| 56 | +describe('Calculator.log() Success Cases', () => { |
| 57 | + const successCases = [ |
| 58 | + { param: [1], expected: 0 }, |
| 59 | + { param: [Math.E], expected: 1 }, |
| 60 | + { param: [10], expected: Math.log(10) } |
| 61 | + ]; |
| 62 | + |
| 63 | + for (const tc of successCases) { |
| 64 | + it(`should return ${tc.expected} for log(${tc.param})`, () => { |
| 65 | + assert.strictEqual(calculator.log.apply(calculator, tc.param), tc.expected); |
| 66 | + }); |
| 67 | + } |
| 68 | +}); |
0 commit comments