@@ -2,4 +2,70 @@ 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
+ it ( 'should calculate the exponential function correctly' , ( ) => {
8
+ const calculator = new Calculator ( ) ;
9
+ assert . strictEqual ( calculator . exp ( 0 ) , 1 ) ;
10
+ assert . strictEqual ( calculator . exp ( 1 ) , Math . exp ( 1 ) ) ;
11
+ assert . strictEqual ( calculator . exp ( 2 ) , Math . exp ( 2 ) ) ;
12
+ } ) ;
13
+
14
+ it ( 'should throw an error for unsupported operand type' , ( ) => {
15
+ const calculator = new Calculator ( ) ;
16
+ assert . throws ( ( ) => calculator . exp ( NaN ) , {
17
+ name : 'Error' ,
18
+ message : 'unsupported operand type'
19
+ } ) ;
20
+ assert . throws ( ( ) => calculator . exp ( Infinity ) , {
21
+ name : 'Error' ,
22
+ message : 'unsupported operand type'
23
+ } ) ;
24
+ } ) ;
25
+
26
+ it ( 'should throw an error for overflow' , ( ) => {
27
+ const calculator = new Calculator ( ) ;
28
+ assert . throws ( ( ) => calculator . exp ( 1000 ) , {
29
+ name : 'Error' ,
30
+ message : 'overflow'
31
+ } ) ;
32
+ } ) ;
33
+ } ) ;
34
+
35
+ describe ( 'log' , ( ) => {
36
+
37
+ it ( 'should calculate the natural logarithm correctly' , ( ) => {
38
+ const calculator = new Calculator ( ) ;
39
+ assert . strictEqual ( calculator . log ( 1 ) , 0 ) ;
40
+ assert . strictEqual ( calculator . log ( Math . E ) , 1 ) ;
41
+ } ) ;
42
+
43
+ it ( 'should throw an error for unsupported operand type' , ( ) => {
44
+ const calculator = new Calculator ( ) ;
45
+ assert . throws ( ( ) => calculator . log ( NaN ) , {
46
+ name : 'Error' ,
47
+ message : 'unsupported operand type'
48
+ } ) ;
49
+ assert . throws ( ( ) => calculator . log ( Infinity ) , {
50
+ name : 'Error' ,
51
+ message : 'unsupported operand type'
52
+ } ) ;
53
+ } ) ;
54
+
55
+ it ( 'should throw an error for math domain error (1)' , ( ) => {
56
+ const calculator = new Calculator ( ) ;
57
+ assert . throws ( ( ) => calculator . log ( 0 ) , {
58
+ name : 'Error' ,
59
+ message : 'math domain error (1)'
60
+ } ) ;
61
+ } ) ;
62
+
63
+ it ( 'should throw an error for math domain error (2)' , ( ) => {
64
+ const calculator = new Calculator ( ) ;
65
+ assert . throws ( ( ) => calculator . log ( - 1 ) , {
66
+ name : 'Error' ,
67
+ message : 'math domain error (2)'
68
+ } ) ;
69
+ } ) ;
70
+ } ) ;
71
+ } ) ;
0 commit comments