index.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. assert.equal(
  41. Math.round((a.eval("(4-(2-1)^2)^.5") + Number.EPSILON) * 100) / 100,
  42. Math.round((Math.sqrt(3) + Number.EPSILON) * 100) / 100
  43. )
  44. });
  45. it('formula test', function () {
  46. assert.equal(a.lex("2").toPostfix().formulaEval(),2);
  47. });
  48. it('formula test', function () {
  49. assert.equal(a.lex("sinpi").toPostfix().formulaEval(),"sin(π)");
  50. });
  51. it('formula test', function () {
  52. assert.equal(a.lex("cos pi").toPostfix().formulaEval(),"cos(π)");
  53. });
  54. it('formula test', function () {
  55. assert.equal(a.lex("tan(pi)").toPostfix().formulaEval(),"tan(π)");
  56. });
  57. it('formula test', function () {
  58. assert.equal(a.lex("2(7-4)3").toPostfix().formulaEval(),"(2×(7-4))×3");
  59. });
  60. it('test to check the bug when number contains decimal', function () {
  61. assert.equal(a.lex("int2.3").toPostfix().postfixEval(),"2");
  62. });
  63. it('test to check auto correct of parenthesis mismatch if opening>closing', function () {
  64. assert.equal(a.lex("(2+(3-4").toPostfix().postfixEval(),"1");
  65. });
  66. it('check for negative of a constant', function () {
  67. assert.equal(a.lex("-e").toPostfix().postfixEval(),-Math.E);
  68. });
  69. it('check for constant inside Sigma', function () {
  70. assert.equal(a.lex("Sigma1,3,2",[{type:3,preced:0,ev:"x",show:"x",token:"x"}]).toPostfix().postfixEval({x:2}),6);
  71. });
  72. it('check when arithmetic and n are present inside sigma', function () {
  73. assert.equal(a.lex("Sigma1,2,n").toPostfix().postfixEval(),3);
  74. });
  75. it(' should check when 4C3', function () {
  76. assert.equal(a.lex("4C3").toPostfix().postfixEval(),4);
  77. });
  78. it('check when arithmetic and n are present inside sigma', function () {
  79. assert.equal(a.lex("Sigma1,2,(n*n)").toPostfix().postfixEval(),5);
  80. });
  81. it('check when two parenthesis less functions are consecutive on one parameter', function () {
  82. assert.equal(a.lex("sinint2.5").toPostfix().postfixEval(),a.lex("sin(int(2.5))").toPostfix().postfixEval());
  83. });
  84. it('check eval method with single argument', function () {
  85. assert.equal(a.eval("5*3"),"15");
  86. });
  87. it('check eval method with three argument', function () {
  88. assert.equal(a.eval("mexp*3",[{type:3,show:"mexp",token:"mexp",value:"mexp",preced:0}],{mexp:5}),"15");
  89. });
  90. it('check eval method with two argument when second one is value of constants', function () {
  91. a.addToken([{type:3,show:"mexp",value:"mexp",preced:0,token:"mexp"}]);
  92. assert.equal(a.eval("mexp*3",{mexp:5}),"15");
  93. });
  94. it('check eval method with two argument when second one is value of constants', function () {
  95. a.addToken([{type:0,show:"mexp",value:function(a){return 5*a;},preced:11,token:"mexp"}]);
  96. assert.equal(a.lex("mexp3").toPostfix().postfixEval(),"15");
  97. });
  98. it('check eval method with two argument when second one is token list', function () {
  99. assert.equal(a.eval("mexp(3)",[{type:0,show:"mexp(",value:function(a){return 5*a;},preced:11,token:"mexp"}]),"15");
  100. });
  101. it('Pi', function () {
  102. assert.equal(a.eval("Pi1,5,n"),"120");
  103. });
  104. it('tan5(6+3)', function () {
  105. assert.equal(
  106. Math.round((a.eval("tan45(6+3)") + Number.EPSILON) * 100) / 100,
  107. Math.round((9 + Number.EPSILON) * 100) / 100
  108. );
  109. });
  110. it('tan(40+5)', function () {
  111. assert.equal(a.eval("tan(40+5)"),"1");
  112. });
  113. it('checks when a 0 is missing in a decimal number', function () {
  114. assert.equal(a.eval("5*.8"),"4");
  115. });
  116. it('checks root function', function () {
  117. assert.equal(a.eval("root4"),"2");
  118. assert.equal(
  119. Math.round((a.eval("root(4-1^2)") + Number.EPSILON) * 100) / 100,
  120. Math.round((Math.sqrt(3) + Number.EPSILON) * 100) / 100
  121. )
  122. assert.equal(
  123. Math.round((a.eval("root(4-(2-1)^2)") + Number.EPSILON) * 100) / 100,
  124. Math.round((Math.sqrt(3) + Number.EPSILON) * 100) / 100
  125. )
  126. });
  127. it('checks + precedence before number insise parenthesis ', function () {
  128. assert.equal(a.eval("(-2)"),"-2");
  129. });
  130. it('checks multiple allowable operator', function () {
  131. assert.equal(a.eval("2+++-++-+-+3"),"-1");
  132. assert.equal(a.eval("2*+3"),"6");
  133. });
  134. });
  135. describe('These expression will check for types of returned result', function () {
  136. it('should tell to compllete expression', function () {
  137. assert.equal(typeof a.eval('0'), 'number')
  138. });
  139. });
  140. describe('These expression will raise error', function () {
  141. it('should tell to compllete expression', function () {
  142. try{
  143. a.eval("2*")
  144. }
  145. catch(e){
  146. assert.equal(e.message,"complete the expression")
  147. }
  148. });
  149. it('should warn about multiple operators', function () {
  150. try{
  151. a.eval("2**3")
  152. }
  153. catch(e){
  154. assert.equal(e.message,"* is not allowed after *")
  155. }
  156. });
  157. it('should warn about multiple operators', function () {
  158. try{
  159. a.eval("2*Mod*3")
  160. }
  161. catch(e){
  162. assert.equal(e.message,"Mod is not allowed after *")
  163. }
  164. });
  165. it('should warn about operator inside parenthesis', function () {
  166. try{
  167. a.eval("(+)")
  168. }
  169. catch(e){
  170. assert.equal(e.message,") is not allowed after +")
  171. }
  172. });
  173. it('should warn about operator inside parenthesis', function () {
  174. try{
  175. a.eval("(+)")
  176. }
  177. catch(e){
  178. assert.equal(e.message,") is not allowed after +")
  179. }
  180. });
  181. });
  182. describe('Check autoclose of parenthesis of parser', function () {
  183. it('should tell to compllete expression', function () {
  184. assert.equal(a.eval("((2+3*4"),"14");
  185. });
  186. });
  187. describe('Check autoclose of parenthesis of parser', function () {
  188. it('should tell to compllete expression', function () {
  189. a.addToken([{
  190. type:2,
  191. token:"nroot",
  192. show:"nroot",
  193. value:function(a,b){
  194. return Math.pow(a, 1 / b);
  195. }
  196. }])
  197. assert.equal(a.eval("27nroot3"), 3);
  198. a.addToken([{
  199. type:2,
  200. token:"nrootlongesttoken",
  201. show:"nrootlongesttoken",
  202. value:function(a,b){
  203. return Math.pow(a, 1 / b);
  204. }
  205. }])
  206. assert.equal(a.eval("27nrootlongesttoken3"), 3);
  207. a.addToken([{
  208. type:2,
  209. token:"tokenwithnumber34",
  210. show:"tokenwithnumber34",
  211. value:function(a,b){
  212. return a + b;
  213. }
  214. }])
  215. assert.equal(a.eval("17tokenwithnumber347"), 24);
  216. });
  217. });