postfix functions calculations fixes

This commit is contained in:
serso
2011-10-23 20:10:48 +04:00
parent b6742a7c15
commit edc5cb1431
5 changed files with 168 additions and 119 deletions

View File

@@ -42,4 +42,27 @@ public class MathTypeTest {
} catch (IllegalArgumentException e) {
}
}
/* @Test
public void testPostfixFunctionsProcessing() throws Exception {
org.junit.Assert.assertEquals(-1, MathType.getPostfixFunctionStart("5!", 1));
org.junit.Assert.assertEquals(0, MathType.getPostfixFunctionStart("!", 1));
org.junit.Assert.assertEquals(-1, MathType.getPostfixFunctionStart("5.4434234!", 9));
org.junit.Assert.assertEquals(1, MathType.getPostfixFunctionStart("2+5!", 3));
org.junit.Assert.assertEquals(4, MathType.getPostfixFunctionStart("2.23+5.4434234!", 14));
org.junit.Assert.assertEquals(14, MathType.getPostfixFunctionStart("2.23+5.4434234*5!", 16));
org.junit.Assert.assertEquals(14, MathType.getPostfixFunctionStart("2.23+5.4434234*5.1!", 18));
org.junit.Assert.assertEquals(4, MathType.getPostfixFunctionStart("2.23+(5.4434234*5.1)!", 20));
org.junit.Assert.assertEquals(4, MathType.getPostfixFunctionStart("2.23+(5.4434234*(5.1+1))!", 24));
org.junit.Assert.assertEquals(4, MathType.getPostfixFunctionStart("2.23+(5.4434234*sin(5.1+1))!", 27));
org.junit.Assert.assertEquals(0, MathType.getPostfixFunctionStart("sin(5)!", 6));
org.junit.Assert.assertEquals(-1, MathType.getPostfixFunctionStart(")!", ")!".indexOf("!")));
org.junit.Assert.assertEquals(0, MathType.getPostfixFunctionStart("sin(5sin(5sin(5)))!", 18));
org.junit.Assert.assertEquals(2, MathType.getPostfixFunctionStart("2+sin(5sin(5sin(5)))!", 20));
org.junit.Assert.assertEquals(5, MathType.getPostfixFunctionStart("2.23+sin(5.4434234*sin(5.1+1))!", 30));
org.junit.Assert.assertEquals(5, MathType.getPostfixFunctionStart("2.23+sin(5.4434234*sin(5.1E2+e))!", "2.23+sin(5.4434234*sin(5.1E2+e))!".indexOf("!")));
org.junit.Assert.assertEquals(5, MathType.getPostfixFunctionStart("2.23+sin(5.4434234*sin(5.1E2+5 555 555))!", "2.23+sin(5.4434234*sin(5.1E2+5 555 555))!".indexOf("!")));
org.junit.Assert.assertEquals(5, MathType.getPostfixFunctionStart("2.23+sin(5.4434234^sin(5.1E2!+5'555'555))!", "2.23+sin(5.4434234^sin(5.1E2!+5'555'555))!".lastIndexOf("!")));
}*/
}

View File

@@ -107,6 +107,19 @@ public class CalculatorEngineTest {
Assert.assertEquals("11et", cm.evaluate(JsclOperation.numeric, "t11e"));
Assert.assertEquals("11×Infinityt", cm.evaluate(JsclOperation.numeric, "t11∞"));
Assert.assertEquals("-t+t^3", cm.evaluate(JsclOperation.numeric, "t(t-1)(t+1)"));
Assert.assertEquals("0.524", cm.evaluate(JsclOperation.numeric, "30°"));
Assert.assertEquals("0.524", cm.evaluate(JsclOperation.numeric, "(10+20)°"));
Assert.assertEquals("1.047", cm.evaluate(JsclOperation.numeric, "(10+20)°*2"));
try {
Assert.assertEquals("0.278", cm.evaluate(JsclOperation.numeric, "30°^2"));
junit.framework.Assert.fail();
} catch (ParseException e) {
if ( !e.getMessage().equals("Power operation after postfix function is currently unsupported!") ) {
junit.framework.Assert.fail();
}
}
}
@Test
@@ -152,7 +165,7 @@ public class CalculatorEngineTest {
}
@Test
/* @Test
public void testDegrees() throws Exception {
final CalculatorEngine cm = CalculatorEngine.instance;
@@ -163,5 +176,5 @@ public class CalculatorEngineTest {
Assert.assertEquals("0.5", cm.evaluate(JsclOperation.numeric, "sin(30°)"));
Assert.assertEquals("0.524", cm.evaluate(JsclOperation.numeric, "asin(sin(30°))"));
}
}*/
}

View File

@@ -88,38 +88,35 @@ public class ToJsclTextProcessorTest {
}
}
@Test
public void testPostfixFunctionsProcessing() throws Exception {
final ToJsclTextProcessor preprocessor = new ToJsclTextProcessor();
Assert.assertEquals(-1, preprocessor.getPostfixFunctionStart("5!", 0));
Assert.assertEquals(0, preprocessor.getPostfixFunctionStart("!", 0));
Assert.assertEquals(-1, preprocessor.getPostfixFunctionStart("5.4434234!", 8));
Assert.assertEquals(1, preprocessor.getPostfixFunctionStart("2+5!", 2));
Assert.assertEquals(4, preprocessor.getPostfixFunctionStart("2.23+5.4434234!", 13));
Assert.assertEquals(14, preprocessor.getPostfixFunctionStart("2.23+5.4434234*5!", 15));
Assert.assertEquals(14, preprocessor.getPostfixFunctionStart("2.23+5.4434234*5.1!", 17));
Assert.assertEquals(4, preprocessor.getPostfixFunctionStart("2.23+(5.4434234*5.1)!", 19));
Assert.assertEquals(4, preprocessor.getPostfixFunctionStart("2.23+(5.4434234*(5.1+1))!", 23));
Assert.assertEquals(4, preprocessor.getPostfixFunctionStart("2.23+(5.4434234*sin(5.1+1))!", 26));
Assert.assertEquals(0, preprocessor.getPostfixFunctionStart("sin(5)!", 5));
Assert.assertEquals(0, preprocessor.getPostfixFunctionStart("sin(5sin(5sin(5)))!", 17));
Assert.assertEquals(2, preprocessor.getPostfixFunctionStart("2+sin(5sin(5sin(5)))!", 19));
Assert.assertEquals(5, preprocessor.getPostfixFunctionStart("2.23+sin(5.4434234*sin(5.1+1))!", 29));
}
@Test
public void testDegrees() throws Exception {
final ToJsclTextProcessor preprocessor = new ToJsclTextProcessor();
Assert.assertEquals( "", preprocessor.process("").toString());
Assert.assertEquals( "3.141592653589793/180", preprocessor.process("°").toString());
try {
Assert.assertEquals( "3.141592653589793/180", preprocessor.process("°").toString());
} catch (ParseException e) {
if ( !e.getMessage().startsWith("Could not find start of prefix") ){
junit.framework.Assert.fail();
}
}
Assert.assertEquals( "1*3.141592653589793/180", preprocessor.process("").toString());
Assert.assertEquals( "20.0*3.141592653589793/180", preprocessor.process("20.0°").toString());
Assert.assertEquals( "sin(30*3.141592653589793/180)", preprocessor.process("sin(30°)").toString());
Assert.assertEquals( "asin(sin(3.141592653589793/6))*3.141592653589793/180", preprocessor.process("asin(sin(π/6))°").toString());
Assert.assertEquals( "1*3.141592653589793/180*sin(1)", preprocessor.process("1°sin(1)").toString());
try {
Assert.assertEquals( "1*3.141592653589793/180^sin(1)", preprocessor.process("1°^sin(1)").toString());
junit.framework.Assert.fail();
} catch (ParseException e) {
if ( !e.getMessage().equals("Power operation after postfix function is currently unsupported!") ) {
junit.framework.Assert.fail();
}
}
}
@Test
public void testPostfixFunction() throws Exception {
}
}