From 27a4b8c691ace3822a10162bf2bb0f6475669b5d Mon Sep 17 00:00:00 2001 From: as10968574 Date: Wed, 5 Jun 2024 19:49:15 +0800 Subject: [PATCH] Update main_test.js --- lab3/main_test.js | 57 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/lab3/main_test.js b/lab3/main_test.js index 096fd421..96dbd2f0 100644 --- a/lab3/main_test.js +++ b/lab3/main_test.js @@ -2,4 +2,59 @@ const { describe, it } = require('node:test'); const assert = require('assert'); const { Calculator } = require('./main'); -// TODO: write your tests here +describe('Calculator', () => { + const calculator = new Calculator(); + + describe('exp function', () => { + it('calculates the exponential of a number', async () => { + assert.strictEqual(calculator.exp(1), Math.exp(1)); + }); + + it('throws error on non-finite input', async () => { + assert.throws(() => calculator.exp('a'), { + name: 'Error', + message: 'unsupported operand type' + }); + assert.throws(() => calculator.exp(Infinity), { + name: 'Error', + message: 'unsupported operand type' + }); + }); + + it('handles overflow', async () => { + assert.throws(() => calculator.exp(1000), { + name: 'Error', + message: 'overflow' + }); + }); + }); + + describe('log function', () => { + it('calculates the logarithm of a number', async () => { + assert.strictEqual(calculator.log(Math.E), Math.log(Math.E)); + }); + + it('throws error on non-finite input', async () => { + assert.throws(() => calculator.log('a'), { + name: 'Error', + message: 'unsupported operand type' + }); + assert.throws(() => calculator.log(-1), { + name: 'Error', + message: 'math domain error (1)' + }); + }); + + it('handles domain errors', async () => { + assert.throws(() => calculator.log(0), { + name: 'Error', + message: 'math domain error (1)' + }); + assert.throws(() => calculator.log(null), { + name: 'Error', + message: 'unsupported operand type' + }); + }); + }); +}); +