| 1 | /* |
|---|
| 2 | * This program is free software; you can redistribute it and/or modify |
|---|
| 3 | * it under the terms of the GNU General Public License as published by |
|---|
| 4 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 5 | * (at your option) any later version. |
|---|
| 6 | * |
|---|
| 7 | * This program is distributed in the hope that it will be useful, |
|---|
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 10 | * GNU General Public License for more details. |
|---|
| 11 | * |
|---|
| 12 | * You should have received a copy of the GNU General Public License |
|---|
| 13 | * along with this program; if not, write to the Free Software |
|---|
| 14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 15 | */ |
|---|
| 16 | |
|---|
| 17 | /* |
|---|
| 18 | * Copyright (C) 2008 University of Waikato, Hamilton, New Zealand |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | package weka.core; |
|---|
| 22 | |
|---|
| 23 | import java.util.HashMap; |
|---|
| 24 | import junit.framework.Test; |
|---|
| 25 | import junit.framework.TestCase; |
|---|
| 26 | import junit.framework.TestSuite; |
|---|
| 27 | |
|---|
| 28 | /** |
|---|
| 29 | * Tests MathematicalExpression. Run from the command line with:<p/> |
|---|
| 30 | * java weka.core.MathematicalTest |
|---|
| 31 | * |
|---|
| 32 | * @author mhall (mhall{[at]}pentaho{[dot]}org) |
|---|
| 33 | * @version $Revision: 5563 $ |
|---|
| 34 | */ |
|---|
| 35 | public class MathematicalExpressionTest |
|---|
| 36 | extends TestCase { |
|---|
| 37 | |
|---|
| 38 | /** |
|---|
| 39 | * Constructs the <code>MathematicalExpresionTest</code>. |
|---|
| 40 | * |
|---|
| 41 | * @param name the name of the test class |
|---|
| 42 | */ |
|---|
| 43 | public MathematicalExpressionTest(String name) { |
|---|
| 44 | super(name); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | /** |
|---|
| 48 | * Called by JUnit before each test method. |
|---|
| 49 | * |
|---|
| 50 | * @throws Exception if an error occurs |
|---|
| 51 | */ |
|---|
| 52 | protected void setUp() throws Exception { |
|---|
| 53 | super.setUp(); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | /** Called by JUnit after each test method */ |
|---|
| 57 | protected void tearDown() throws Exception { |
|---|
| 58 | super.tearDown(); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | public static Test suite() { |
|---|
| 62 | return new TestSuite(MathematicalExpressionTest.class); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | protected double getExpressionResult(String expression) throws Exception { |
|---|
| 66 | HashMap symbols = new HashMap(); |
|---|
| 67 | symbols.put("A", new Double(4)); |
|---|
| 68 | symbols.put("B", new Double(2)); |
|---|
| 69 | symbols.put("C", new Double(2)); |
|---|
| 70 | return MathematicalExpression.evaluate(expression, symbols); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | public void testAddSub() throws Exception { |
|---|
| 74 | double result = getExpressionResult("A-B+C"); |
|---|
| 75 | assertEquals(4.0, result); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | public void testOperatorOrder() throws Exception { |
|---|
| 79 | double result = getExpressionResult("A-B*C"); |
|---|
| 80 | assertEquals(0.0, result); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | public void testBrackets() throws Exception { |
|---|
| 84 | double result = getExpressionResult("(A-B)*C"); |
|---|
| 85 | assertEquals(4.0, result); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | public void testExpressionWithConstants() throws Exception { |
|---|
| 89 | double result = getExpressionResult("A-B*(C+5)"); |
|---|
| 90 | assertEquals(-10.0, result); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | public void testExpressionWithFunction() throws Exception { |
|---|
| 94 | double result = getExpressionResult("pow(A,B*1)-C*2"); |
|---|
| 95 | assertEquals(12.0, result); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | public void testExpressionWithIFELSE() throws Exception { |
|---|
| 99 | double result = getExpressionResult("ifelse((C<1000|C>5000),(A+B),C+C)"); |
|---|
| 100 | assertEquals(6.0, result); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | public static void main(String[] args){ |
|---|
| 104 | junit.textui.TestRunner.run(suite()); |
|---|
| 105 | } |
|---|
| 106 | } |
|---|