index.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // This test is for node JS
  2. var assert = require('assert');
  3. var a=require("../src/formula_evaluator.js");
  4. describe('Testing Unit', function () {
  5. it('should equal 2 to check a number', function () {
  6. assert.equal(a.lex("2").toPostfix().postfixEval(),2);
  7. });
  8. it('checks a math function', function () {
  9. assert.equal(a.lex("tan(180)").toPostfix().postfixEval(),0);
  10. });
  11. it('checks a parenthesis less function', function () {
  12. assert.equal(a.lex("sin180").toPostfix().postfixEval(),0);
  13. });
  14. it('checks a parenthesis less function after a space', function () {
  15. assert.equal(a.lex("cos 180").toPostfix().postfixEval(),-1);
  16. });
  17. it('checks a parenthesis less function after multiple spaces', function () {
  18. assert.equal(a.lex("cos 180").toPostfix().postfixEval(),-1);
  19. });
  20. it('checks consecutive operator', function () {
  21. assert.equal(a.lex("0+-2").toPostfix().postfixEval(),-2);
  22. });
  23. it('checks ^ operator', function () {
  24. assert.equal(a.lex("2^2").toPostfix().postfixEval(),4);
  25. });
  26. it('checks when * is omitted before parenthesis and after', function () {
  27. assert.equal(a.lex("2(7-4)3").toPostfix().postfixEval(),18);
  28. });
  29. it('checks multiplication and exponential in series', function () {
  30. assert.equal(a.lex("2*7^2").toPostfix().postfixEval(),98);
  31. });
  32. it('checks exponential and multiplication in series', function () {
  33. assert.equal(a.lex("2^5*2").toPostfix().postfixEval(),64);
  34. });
  35. it('-3^2=-9', function () {
  36. assert.equal(a.lex("-3^2").toPostfix().postfixEval(),-9);
  37. });
  38. it('3^2-2^2=5', function () {
  39. assert.equal(a.lex("3^2-2^2").toPostfix().postfixEval(),5);
  40. });
  41. it('formula test', function () {
  42. assert.equal(a.lex("2").toPostfix().formulaEval(),2);
  43. });
  44. it('formula test', function () {
  45. assert.equal(a.lex("sinpi").toPostfix().formulaEval(),"sin(π)");
  46. });
  47. it('formula test', function () {
  48. assert.equal(a.lex("cos pi").toPostfix().formulaEval(),"cos(π)");
  49. });
  50. it('formula test', function () {
  51. assert.equal(a.lex("tan(pi)").toPostfix().formulaEval(),"tan(π)");
  52. });
  53. it('formula test', function () {
  54. assert.equal(a.lex("2(7-4)3").toPostfix().formulaEval(),"(2×(7-4))×3");
  55. });
  56. it('test to check the bug when number contains decimal', function () {
  57. assert.equal(a.lex("int2.3").toPostfix().postfixEval(),"2");
  58. });
  59. it('test to check auto correct of parenthesis mismatch if opening>closing', function () {
  60. assert.equal(a.lex("(2+(3-4").toPostfix().postfixEval(),"1");
  61. });
  62. it('check for negative of a constant', function () {
  63. assert.equal(a.lex("-e").toPostfix().postfixEval(),-Math.E);
  64. });
  65. it('check for constant inside Sigma', function () {
  66. assert.equal(a.lex("Sigma1,3,2",[{type:3,preced:0,ev:"x",show:"x",token:"x"}]).toPostfix().postfixEval({x:2}),6);
  67. });
  68. it('check when arithmetic and n are present inside sigma', function () {
  69. assert.equal(a.lex("Sigma1,2,n").toPostfix().postfixEval(),3);
  70. });
  71. it(' should check when 4C3', function () {
  72. assert.equal(a.lex("4C3").toPostfix().postfixEval(),4);
  73. });
  74. it('check when arithmetic and n are present inside sigma', function () {
  75. assert.equal(a.lex("Sigma1,2,(n*n)").toPostfix().postfixEval(),5);
  76. });
  77. it('check when two parenthesis less functions are consecutive on one parameter', function () {
  78. assert.equal(a.lex("sinint2.5").toPostfix().postfixEval(),a.lex("sin(int(2.5))").toPostfix().postfixEval());
  79. });
  80. it('check eval method with single argument', function () {
  81. assert.equal(a.eval("5*3"),"15");
  82. });
  83. it('check eval method with three argument', function () {
  84. assert.equal(a.eval("mexp*3",[{type:3,show:"mexp",token:"mexp",value:"mexp",preced:0}],{mexp:5}),"15");
  85. });
  86. it('check eval method with two argument when second one is value of constants', function () {
  87. a.addToken([{type:3,show:"mexp",value:"mexp",preced:0,token:"mexp"}]);
  88. assert.equal(a.eval("mexp*3",{mexp:5}),"15");
  89. });
  90. it('check eval method with two argument when second one is value of constants', function () {
  91. a.addToken([{type:0,show:"mexp",value:function(a){return 5*a;},preced:11,token:"mexp"}]);
  92. assert.equal(a.lex("mexp3").toPostfix().postfixEval(),"15");
  93. });
  94. it('check eval method with two argument when second one is token list', function () {
  95. assert.equal(a.eval("mexp(3)",[{type:0,show:"mexp(",value:function(a){return 5*a;},preced:11,token:"mexp"}]),"15");
  96. });
  97. it('Pi', function () {
  98. assert.equal(a.eval("Pi1,5,n"),"120");
  99. });
  100. it('tan5(6+3)', function () {
  101. assert.equal(a.eval("tan5(6+3)"),"1");
  102. });
  103. it('tan(40+5)', function () {
  104. assert.equal(a.eval("tan(40+5)"),"1");
  105. });
  106. it('checks when a 0 is missing in a decimal number', function () {
  107. assert.equal(a.eval("5*.8"),"4");
  108. });
  109. it('checks root function', function () {
  110. assert.equal(a.eval("root4"),"2");
  111. });
  112. it('checks + precedence before number insise parenthesis ', function () {
  113. assert.equal(a.eval("(-2)"),"-2");
  114. });
  115. it('checks multiple allowable operator', function () {
  116. assert.equal(a.eval("2+++-++-+-+3"),"-1");
  117. assert.equal(a.eval("2*+3"),"6");
  118. });
  119. });
  120. describe('These expression will check for types of returned result', function () {
  121. it('should tell to compllete expression', function () {
  122. assert.equal(typeof a.eval('0'), 'number')
  123. });
  124. });
  125. describe('These expression will raise error', function () {
  126. it('should tell to compllete expression', function () {
  127. try{
  128. a.eval("2*")
  129. }
  130. catch(e){
  131. assert.equal(e.message,"complete the expression")
  132. }
  133. });
  134. it('should warn about multiple operators', function () {
  135. try{
  136. a.eval("2**3")
  137. }
  138. catch(e){
  139. assert.equal(e.message,"* is not allowed after *")
  140. }
  141. });
  142. it('should warn about multiple operators', function () {
  143. try{
  144. a.eval("2*Mod*3")
  145. }
  146. catch(e){
  147. assert.equal(e.message,"Mod is not allowed after *")
  148. }
  149. });
  150. it('should warn about operator inside parenthesis', function () {
  151. try{
  152. a.eval("(+)")
  153. }
  154. catch(e){
  155. assert.equal(e.message,") is not allowed after +")
  156. }
  157. });
  158. it('should warn about operator inside parenthesis', function () {
  159. try{
  160. a.eval("(+)")
  161. }
  162. catch(e){
  163. assert.equal(e.message,") is not allowed after +")
  164. }
  165. });
  166. });