|
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 | +describe('Calculator Functionality', () => { |
| 6 | + it('check exp method', () => { |
| 7 | + const testCases = [ |
| 8 | + { input: 2, expected: 7.38905609893065 }, |
| 9 | + { input: 5, expected: 148.4131591025766 }, |
| 10 | + { input: 10, expected: 22026.465794806718 }, |
| 11 | + { input: -1, expected: 0.36787944117144233 }, |
| 12 | + { input: -5, expected: 0.006737946999085467 }, |
| 13 | + { input: 0, expected: 1 } |
| 14 | + ]; |
| 15 | + const calculator = new Calculator(); |
| 16 | + testCases.forEach(({ input, expected }) => { |
| 17 | + const result = calculator.exp(input); |
| 18 | + assert.strictEqual(result, expected); |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + it('check log method', () => { |
| 23 | + const testCases = [ |
| 24 | + { input: 2, expected: 0.6931471805599453 }, |
| 25 | + { input: 5, expected: 1.6094379124341003 }, |
| 26 | + { input: 10, expected: 2.302585092994046 }, |
| 27 | + { input: Math.E, expected: 1 }, |
| 28 | + { input: Math.E * Math.E, expected: 2 }, |
| 29 | + { input: Math.E * Math.E * Math.E, expected: 3 } |
| 30 | + ]; |
| 31 | + const calculator = new Calculator(); |
| 32 | + testCases.forEach(({ input, expected }) => { |
| 33 | + const result = calculator.log(input); |
| 34 | + assert.strictEqual(result, expected); |
| 35 | + }); |
| 36 | + }); |
| 37 | +} |
| 38 | +); |
| 39 | + |
| 40 | +describe('Calculator Error Handling', () => { |
| 41 | + it('check exp handle non-finite numbers', () => { |
| 42 | + const testCases = [ |
| 43 | + { input: NaN, expected: 'unsupported operand type' }, |
| 44 | + { input: Infinity, expected: 'unsupported operand type' }, |
| 45 | + { input: -Infinity, expected: 'unsupported operand type' } |
| 46 | + ]; |
| 47 | + const calculator = new Calculator(); |
| 48 | + testCases.forEach(({ input, expected }) => { |
| 49 | + assert.throws(() => { |
| 50 | + calculator.exp(input); |
| 51 | + }, { |
| 52 | + name: 'Error', |
| 53 | + message: expected |
| 54 | + }); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + it('check exp handle overflow', () => { |
| 59 | + const calculator = new Calculator(); |
| 60 | + assert.throws(() => { |
| 61 | + calculator.exp(1000); |
| 62 | + }, { |
| 63 | + name: 'Error', |
| 64 | + message: 'overflow' |
| 65 | + }); |
| 66 | + } |
| 67 | + ); |
| 68 | + |
| 69 | + it('check log handle non-finite numbers', () => { |
| 70 | + const testCases = [ |
| 71 | + { input: NaN, expected: 'unsupported operand type' }, |
| 72 | + { input: Infinity, expected: 'unsupported operand type' }, |
| 73 | + { input: -Infinity, expected: 'unsupported operand type' } |
| 74 | + ]; |
| 75 | + const calculator = new Calculator(); |
| 76 | + testCases.forEach(({ input, expected }) => { |
| 77 | + assert.throws(() => { |
| 78 | + calculator.log(input); |
| 79 | + }, { |
| 80 | + name: 'Error', |
| 81 | + message: expected |
| 82 | + }); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + it('check log handle 0', () => { |
| 87 | + const calculator = new Calculator(); |
| 88 | + assert.throws(() => { |
| 89 | + calculator.log(0); |
| 90 | + }, { |
| 91 | + name: 'Error', |
| 92 | + message: 'math domain error (1)' |
| 93 | + }); |
| 94 | + } |
| 95 | + ); |
| 96 | + |
| 97 | + it('check log handle negative number', () => { |
| 98 | + const testCases = [ |
| 99 | + { input: -1, expected: 'math domain error (2)' }, |
| 100 | + { input: -2, expected: 'math domain error (2)' }, |
| 101 | + { input: -Math.E, expected: 'math domain error (2)' } |
| 102 | + ]; |
| 103 | + const calculator = new Calculator(); |
| 104 | + testCases.forEach(({ input, expected }) => { |
| 105 | + assert.throws(() => { |
| 106 | + calculator.log(input); |
| 107 | + }, { |
| 108 | + name: 'Error', |
| 109 | + message: expected |
| 110 | + }); |
| 111 | + }); |
| 112 | + }); |
| 113 | +}); |
0 commit comments