/lucene/expressions/src/test/org/apache/lucene/expressions/js/TestJavascriptFunction.java

https://github.com/artash/lucene-solr · Java · 268 lines · 224 code · 28 blank · 16 comment · 0 complexity · 4bd22e4dc93311c66d6c04e266c71a0b MD5 · raw file

  1. package org.apache.lucene.expressions.js;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. import org.apache.lucene.expressions.Expression;
  19. import org.apache.lucene.util.LuceneTestCase;
  20. public class TestJavascriptFunction extends LuceneTestCase {
  21. private static double DELTA = 0.0000001;
  22. private void assertEvaluatesTo(String expression, double expected) throws Exception {
  23. Expression evaluator = JavascriptCompiler.compile(expression);
  24. double actual = evaluator.evaluate(0, null);
  25. assertEquals(expected, actual, DELTA);
  26. }
  27. public void testAbsMethod() throws Exception {
  28. assertEvaluatesTo("abs(0)", 0);
  29. assertEvaluatesTo("abs(119)", 119);
  30. assertEvaluatesTo("abs(119)", 119);
  31. assertEvaluatesTo("abs(1)", 1);
  32. assertEvaluatesTo("abs(-1)", 1);
  33. }
  34. public void testAcosMethod() throws Exception {
  35. assertEvaluatesTo("acos(-1)", Math.PI);
  36. assertEvaluatesTo("acos(-0.8660254)", Math.PI*5/6);
  37. assertEvaluatesTo("acos(-0.7071068)", Math.PI*3/4);
  38. assertEvaluatesTo("acos(-0.5)", Math.PI*2/3);
  39. assertEvaluatesTo("acos(0)", Math.PI/2);
  40. assertEvaluatesTo("acos(0.5)", Math.PI/3);
  41. assertEvaluatesTo("acos(0.7071068)", Math.PI/4);
  42. assertEvaluatesTo("acos(0.8660254)", Math.PI/6);
  43. assertEvaluatesTo("acos(1)", 0);
  44. }
  45. public void testAcoshMethod() throws Exception {
  46. assertEvaluatesTo("acosh(1)", 0);
  47. assertEvaluatesTo("acosh(2.5)", 1.5667992369724109);
  48. assertEvaluatesTo("acosh(1234567.89)", 14.719378760739708);
  49. }
  50. public void testAsinMethod() throws Exception {
  51. assertEvaluatesTo("asin(-1)", -Math.PI/2);
  52. assertEvaluatesTo("asin(-0.8660254)", -Math.PI/3);
  53. assertEvaluatesTo("asin(-0.7071068)", -Math.PI/4);
  54. assertEvaluatesTo("asin(-0.5)", -Math.PI/6);
  55. assertEvaluatesTo("asin(0)", 0);
  56. assertEvaluatesTo("asin(0.5)", Math.PI/6);
  57. assertEvaluatesTo("asin(0.7071068)", Math.PI/4);
  58. assertEvaluatesTo("asin(0.8660254)", Math.PI/3);
  59. assertEvaluatesTo("asin(1)", Math.PI/2);
  60. }
  61. public void testAsinhMethod() throws Exception {
  62. assertEvaluatesTo("asinh(-1234567.89)", -14.719378760740035);
  63. assertEvaluatesTo("asinh(-2.5)", -1.6472311463710958);
  64. assertEvaluatesTo("asinh(-1)", -0.8813735870195429);
  65. assertEvaluatesTo("asinh(0)", 0);
  66. assertEvaluatesTo("asinh(1)", 0.8813735870195429);
  67. assertEvaluatesTo("asinh(2.5)", 1.6472311463710958);
  68. assertEvaluatesTo("asinh(1234567.89)", 14.719378760740035);
  69. }
  70. public void testAtanMethod() throws Exception {
  71. assertEvaluatesTo("atan(-1.732050808)", -Math.PI/3);
  72. assertEvaluatesTo("atan(-1)", -Math.PI/4);
  73. assertEvaluatesTo("atan(-0.577350269)", -Math.PI/6);
  74. assertEvaluatesTo("atan(0)", 0);
  75. assertEvaluatesTo("atan(0.577350269)", Math.PI/6);
  76. assertEvaluatesTo("atan(1)", Math.PI/4);
  77. assertEvaluatesTo("atan(1.732050808)", Math.PI/3);
  78. }
  79. public void testAtan2Method() throws Exception {
  80. assertEvaluatesTo("atan2(+0,+0)", +0.0);
  81. assertEvaluatesTo("atan2(+0,-0)", +Math.PI);
  82. assertEvaluatesTo("atan2(-0,+0)", -0.0);
  83. assertEvaluatesTo("atan2(-0,-0)", -Math.PI);
  84. assertEvaluatesTo("atan2(2,2)", Math.PI/4);
  85. assertEvaluatesTo("atan2(-2,2)", -Math.PI/4);
  86. assertEvaluatesTo("atan2(2,-2)", Math.PI*3/4);
  87. assertEvaluatesTo("atan2(-2,-2)", -Math.PI*3/4);
  88. }
  89. public void testAtanhMethod() throws Exception {
  90. assertEvaluatesTo("atanh(-1)", Double.NEGATIVE_INFINITY);
  91. assertEvaluatesTo("atanh(-0.5)", -0.5493061443340549);
  92. assertEvaluatesTo("atanh(0)", 0);
  93. assertEvaluatesTo("atanh(0.5)", 0.5493061443340549);
  94. assertEvaluatesTo("atanh(1)", Double.POSITIVE_INFINITY);
  95. }
  96. public void testCeilMethod() throws Exception {
  97. assertEvaluatesTo("ceil(0)", 0);
  98. assertEvaluatesTo("ceil(0.1)", 1);
  99. assertEvaluatesTo("ceil(0.9)", 1);
  100. assertEvaluatesTo("ceil(25.2)", 26);
  101. assertEvaluatesTo("ceil(-0.1)", 0);
  102. assertEvaluatesTo("ceil(-0.9)", 0);
  103. assertEvaluatesTo("ceil(-1.1)", -1);
  104. }
  105. public void testCosMethod() throws Exception {
  106. assertEvaluatesTo("cos(0)", 1);
  107. assertEvaluatesTo("cos(" + Math.PI/2 + ")", 0);
  108. assertEvaluatesTo("cos(" + -Math.PI/2 + ")", 0);
  109. assertEvaluatesTo("cos(" + Math.PI/4 + ")", 0.7071068);
  110. assertEvaluatesTo("cos(" + -Math.PI/4 + ")", 0.7071068);
  111. assertEvaluatesTo("cos(" + Math.PI*2/3 + ")",-0.5);
  112. assertEvaluatesTo("cos(" + -Math.PI*2/3 + ")", -0.5);
  113. assertEvaluatesTo("cos(" + Math.PI/6 + ")", 0.8660254);
  114. assertEvaluatesTo("cos(" + -Math.PI/6 + ")", 0.8660254);
  115. }
  116. public void testCoshMethod() throws Exception {
  117. assertEvaluatesTo("cosh(0)", 1);
  118. assertEvaluatesTo("cosh(-1)", 1.5430806348152437);
  119. assertEvaluatesTo("cosh(1)", 1.5430806348152437);
  120. assertEvaluatesTo("cosh(-0.5)", 1.1276259652063807);
  121. assertEvaluatesTo("cosh(0.5)", 1.1276259652063807);
  122. assertEvaluatesTo("cosh(-12.3456789)", 114982.09728671524);
  123. assertEvaluatesTo("cosh(12.3456789)", 114982.09728671524);
  124. }
  125. public void testExpMethod() throws Exception {
  126. assertEvaluatesTo("exp(0)", 1);
  127. assertEvaluatesTo("exp(-1)", 0.36787944117);
  128. assertEvaluatesTo("exp(1)", 2.71828182846);
  129. assertEvaluatesTo("exp(-0.5)", 0.60653065971);
  130. assertEvaluatesTo("exp(0.5)", 1.6487212707);
  131. assertEvaluatesTo("exp(-12.3456789)", 0.0000043485);
  132. assertEvaluatesTo("exp(12.3456789)", 229964.194569);
  133. }
  134. public void testFloorMethod() throws Exception {
  135. assertEvaluatesTo("floor(0)", 0);
  136. assertEvaluatesTo("floor(0.1)", 0);
  137. assertEvaluatesTo("floor(0.9)", 0);
  138. assertEvaluatesTo("floor(25.2)", 25);
  139. assertEvaluatesTo("floor(-0.1)", -1);
  140. assertEvaluatesTo("floor(-0.9)", -1);
  141. assertEvaluatesTo("floor(-1.1)", -2);
  142. }
  143. public void testHaversinMethod() throws Exception {
  144. assertEvaluatesTo("haversin(40.7143528,-74.0059731,40.759011,-73.9844722)", 5.284299568309);
  145. }
  146. public void testLnMethod() throws Exception {
  147. assertEvaluatesTo("ln(0)", Double.NEGATIVE_INFINITY);
  148. assertEvaluatesTo("ln(" + Math.E + ")", 1);
  149. assertEvaluatesTo("ln(-1)", Double.NaN);
  150. assertEvaluatesTo("ln(1)", 0);
  151. assertEvaluatesTo("ln(0.5)", -0.69314718056);
  152. assertEvaluatesTo("ln(12.3456789)", 2.51330611521);
  153. }
  154. public void testLog10Method() throws Exception {
  155. assertEvaluatesTo("log10(0)", Double.NEGATIVE_INFINITY);
  156. assertEvaluatesTo("log10(1)", 0);
  157. assertEvaluatesTo("log10(-1)", Double.NaN);
  158. assertEvaluatesTo("log10(0.5)", -0.3010299956639812);
  159. assertEvaluatesTo("log10(12.3456789)", 1.0915149771692705);
  160. }
  161. public void testLognMethod() throws Exception {
  162. assertEvaluatesTo("logn(2, 0)", Double.NEGATIVE_INFINITY);
  163. assertEvaluatesTo("logn(2, 1)", 0);
  164. assertEvaluatesTo("logn(2, -1)", Double.NaN);
  165. assertEvaluatesTo("logn(2, 0.5)", -1);
  166. assertEvaluatesTo("logn(2, 12.3456789)", 3.6259342686489378);
  167. assertEvaluatesTo("logn(2.5, 0)", Double.NEGATIVE_INFINITY);
  168. assertEvaluatesTo("logn(2.5, 1)", 0);
  169. assertEvaluatesTo("logn(2.5, -1)", Double.NaN);
  170. assertEvaluatesTo("logn(2.5, 0.5)", -0.75647079736603);
  171. assertEvaluatesTo("logn(2.5, 12.3456789)", 2.7429133874016745);
  172. }
  173. public void testMaxMethod() throws Exception {
  174. assertEvaluatesTo("max(0, 0)", 0);
  175. assertEvaluatesTo("max(1, 0)", 1);
  176. assertEvaluatesTo("max(0, -1)", 0);
  177. assertEvaluatesTo("max(-1, 0)", 0);
  178. assertEvaluatesTo("max(25, 23)", 25);
  179. }
  180. public void testMinMethod() throws Exception {
  181. assertEvaluatesTo("min(0, 0)", 0);
  182. assertEvaluatesTo("min(1, 0)", 0);
  183. assertEvaluatesTo("min(0, -1)", -1);
  184. assertEvaluatesTo("min(-1, 0)", -1);
  185. assertEvaluatesTo("min(25, 23)", 23);
  186. }
  187. public void testPowMethod() throws Exception {
  188. assertEvaluatesTo("pow(0, 0)", 1);
  189. assertEvaluatesTo("pow(0.1, 2)", 0.01);
  190. assertEvaluatesTo("pow(0.9, -1)", 1.1111111111111112);
  191. assertEvaluatesTo("pow(2.2, -2.5)", 0.13929749224447147);
  192. assertEvaluatesTo("pow(5, 3)", 125);
  193. assertEvaluatesTo("pow(-0.9, 5)", -0.59049);
  194. assertEvaluatesTo("pow(-1.1, 2)", 1.21);
  195. }
  196. public void testSinMethod() throws Exception {
  197. assertEvaluatesTo("sin(0)", 0);
  198. assertEvaluatesTo("sin(" + Math.PI/2 + ")", 1);
  199. assertEvaluatesTo("sin(" + -Math.PI/2 + ")", -1);
  200. assertEvaluatesTo("sin(" + Math.PI/4 + ")", 0.7071068);
  201. assertEvaluatesTo("sin(" + -Math.PI/4 + ")", -0.7071068);
  202. assertEvaluatesTo("sin(" + Math.PI*2/3 + ")", 0.8660254);
  203. assertEvaluatesTo("sin(" + -Math.PI*2/3 + ")", -0.8660254);
  204. assertEvaluatesTo("sin(" + Math.PI/6 + ")", 0.5);
  205. assertEvaluatesTo("sin(" + -Math.PI/6 + ")", -0.5);
  206. }
  207. public void testSinhMethod() throws Exception {
  208. assertEvaluatesTo("sinh(0)", 0);
  209. assertEvaluatesTo("sinh(-1)", -1.1752011936438014);
  210. assertEvaluatesTo("sinh(1)", 1.1752011936438014);
  211. assertEvaluatesTo("sinh(-0.5)", -0.52109530549);
  212. assertEvaluatesTo("sinh(0.5)", 0.52109530549);
  213. assertEvaluatesTo("sinh(-12.3456789)", -114982.09728236674);
  214. assertEvaluatesTo("sinh(12.3456789)", 114982.09728236674);
  215. }
  216. public void testSqrtMethod() throws Exception {
  217. assertEvaluatesTo("sqrt(0)", 0);
  218. assertEvaluatesTo("sqrt(-1)", Double.NaN);
  219. assertEvaluatesTo("sqrt(0.49)", 0.7);
  220. assertEvaluatesTo("sqrt(49)", 7);
  221. }
  222. public void testTanMethod() throws Exception {
  223. assertEvaluatesTo("tan(0)", 0);
  224. assertEvaluatesTo("tan(-1)", -1.55740772465);
  225. assertEvaluatesTo("tan(1)", 1.55740772465);
  226. assertEvaluatesTo("tan(-0.5)", -0.54630248984);
  227. assertEvaluatesTo("tan(0.5)", 0.54630248984);
  228. assertEvaluatesTo("tan(-1.3)", -3.60210244797);
  229. assertEvaluatesTo("tan(1.3)", 3.60210244797);
  230. }
  231. public void testTanhMethod() throws Exception {
  232. assertEvaluatesTo("tanh(0)", 0);
  233. assertEvaluatesTo("tanh(-1)", -0.76159415595);
  234. assertEvaluatesTo("tanh(1)", 0.76159415595);
  235. assertEvaluatesTo("tanh(-0.5)", -0.46211715726);
  236. assertEvaluatesTo("tanh(0.5)", 0.46211715726);
  237. assertEvaluatesTo("tanh(-12.3456789)", -0.99999999996);
  238. assertEvaluatesTo("tanh(12.3456789)", 0.99999999996);
  239. }
  240. }