@@ -2,4 +2,69 @@ 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' , ( ) => {
6
+ describe ( 'exp' , ( ) => {
7
+ const calculator = new Calculator ( ) ;
8
+
9
+ it ( 'should throw "unsupported operand type" for non-finite numbers' , ( ) => {
10
+ const invalidInputs = [ NaN , Infinity , - Infinity , 'string' , null , undefined ] ;
11
+ invalidInputs . forEach ( input => {
12
+ assert . throws ( ( ) => calculator . exp ( input ) , Error ( 'unsupported operand type' ) ) ;
13
+ } ) ;
14
+ } ) ;
15
+
16
+ it ( 'should throw "overflow" for large numbers' , ( ) => {
17
+ const largeInputs = [ 1000 , 10000 , 100000 ] ;
18
+ largeInputs . forEach ( input => {
19
+ assert . throws ( ( ) => calculator . exp ( input ) , Error ( 'overflow' ) ) ;
20
+ } ) ;
21
+ } ) ;
22
+
23
+ it ( 'should return correct exp value for valid inputs' , ( ) => {
24
+ const testCases = [
25
+ { input : 0 , expected : 1 } ,
26
+ { input : 1 , expected : Math . exp ( 1 ) } ,
27
+ { input : - 1 , expected : Math . exp ( - 1 ) } ,
28
+ ] ;
29
+ testCases . forEach ( ( { input, expected } ) => {
30
+ assert . strictEqual ( calculator . exp ( input ) , expected ) ;
31
+ } ) ;
32
+ } ) ;
33
+ } ) ;
34
+
35
+ describe ( 'log' , ( ) => {
36
+ const calculator = new Calculator ( ) ;
37
+
38
+ it ( 'should throw "unsupported operand type" for non-finite numbers' , ( ) => {
39
+ const invalidInputs = [ NaN , Infinity , - Infinity , 'string' , null , undefined ] ;
40
+ invalidInputs . forEach ( input => {
41
+ assert . throws ( ( ) => calculator . log ( input ) , Error ( 'unsupported operand type' ) ) ;
42
+ } ) ;
43
+ } ) ;
44
+
45
+ it ( 'should throw "math domain error (1)" for zero' , ( ) => {
46
+ const invalidInputs = [ 0 ] ;
47
+ invalidInputs . forEach ( input => {
48
+ assert . throws ( ( ) => calculator . log ( input ) , Error ( 'math domain error (1)' ) ) ;
49
+ } ) ;
50
+ } ) ;
51
+
52
+ it ( 'should throw "math domain error (2)" for finite negative numbers' , ( ) => {
53
+ const invalidInputs = [ - 1 , - 5 , - 100 ] ;
54
+ invalidInputs . forEach ( input => {
55
+ assert . throws ( ( ) => calculator . log ( input ) , Error ( 'math domain error (2)' ) ) ;
56
+ } ) ;
57
+ } ) ;
58
+
59
+ it ( 'should return correct log value for valid inputs' , ( ) => {
60
+ const testCases = [
61
+ { input : 1 , expected : 0 } ,
62
+ { input : Math . E , expected : 1 } ,
63
+ { input : 10 , expected : Math . log ( 10 ) } ,
64
+ ] ;
65
+ testCases . forEach ( ( { input, expected } ) => {
66
+ assert . strictEqual ( calculator . log ( input ) , expected ) ;
67
+ } ) ;
68
+ } ) ;
69
+ } ) ;
70
+ } ) ;
0 commit comments