@@ -2,4 +2,63 @@ const {describe, it} = require('node:test');
22const assert = require ( 'assert' ) ;
33const { Calculator } = require ( './main' ) ;
44
5- // TODO: write your tests here
5+
6+ const calc = new Calculator ( ) ;
7+
8+ describe ( 'Calculator' , ( ) => {
9+ // exp() 正確結果
10+ it ( 'exp() should return correct results' , ( ) => {
11+ const cases = [
12+ { param : 0 , expected : Math . exp ( 0 ) } ,
13+ { param : 1 , expected : Math . exp ( 1 ) } ,
14+ { param : - 1 , expected : Math . exp ( - 1 ) } ,
15+ ] ;
16+ for ( const c of cases ) {
17+ assert . strictEqual ( calc . exp ( c . param ) , c . expected ) ;
18+ }
19+ } ) ;
20+
21+ // exp() 錯誤處理 unsupported operand
22+ it ( 'exp() should throw unsupported operand type' , ( ) => {
23+ const inputs = [ Infinity , - Infinity , NaN ] ;
24+ for ( const x of inputs ) {
25+ assert . throws ( ( ) => calc . exp ( x ) , { message : 'unsupported operand type' } ) ;
26+ }
27+ } ) ;
28+
29+ // exp() overflow
30+ it ( 'exp() should throw overflow for large number' , ( ) => {
31+ assert . throws ( ( ) => calc . exp ( 1000 ) , { message : 'overflow' } ) ;
32+ } ) ;
33+
34+ // log() 正確結果
35+ it ( 'log() should return correct results' , ( ) => {
36+ const cases = [
37+ { param : 1 , expected : Math . log ( 1 ) } ,
38+ { param : Math . E , expected : Math . log ( Math . E ) } ,
39+ { param : 10 , expected : Math . log ( 10 ) } ,
40+ ] ;
41+ for ( const c of cases ) {
42+ assert . strictEqual ( calc . log ( c . param ) , c . expected ) ;
43+ }
44+ } ) ;
45+
46+ // log() 錯誤處理 unsupported operand
47+ it ( 'log() should throw unsupported operand type' , ( ) => {
48+ const inputs = [ Infinity , - Infinity , NaN ] ;
49+ for ( const x of inputs ) {
50+ assert . throws ( ( ) => calc . log ( x ) , { message : 'unsupported operand type' } ) ;
51+ }
52+ } ) ;
53+
54+ // log() math domain error (1)
55+ it ( 'log() should throw math domain error (1) for 0' , ( ) => {
56+ assert . throws ( ( ) => calc . log ( 0 ) , { message : 'math domain error (1)' } ) ;
57+ } ) ;
58+
59+ // log() math domain error (2)
60+ it ( 'log() should throw math domain error (2) for negative input' , ( ) => {
61+ assert . throws ( ( ) => calc . log ( - 3 ) , { message : 'math domain error (2)' } ) ;
62+ } ) ;
63+ } ) ;
64+
0 commit comments