Skip to content

Commit

Permalink
Issue #75: Added tests but was unable to reproduce the error
Browse files Browse the repository at this point in the history
  • Loading branch information
fasseg committed Jan 30, 2017
1 parent 115238b commit e977f19
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/test/java/net/objecthunter/exp4j/ExpressionBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2720,4 +2720,30 @@ public void testSignum() {
.build();
assertEquals(-1, e.evaluate(), 0d);
}

@Test
public void testCustomPercent() {
Function percentage = new Function("percentage", 2) {
@Override
public double apply(double... args) {
double val = args[0];
double percent = args[1];
if (percent < 0) {
return val - val * Math.abs(percent)/100d;
} else {
return val - val * percent/100d;
}
}
};

Expression e = new ExpressionBuilder("percentage(1000,-10)")
.function(percentage)
.build();
assertEquals(0d, 900, e.evaluate());

e = new ExpressionBuilder("percentage(1000,12)")
.function(percentage)
.build();
assertEquals(0d, 1000d*0.12d, e.evaluate());
}
}
148 changes: 148 additions & 0 deletions src/test/java/net/objecthunter/exp4j/ExpressionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
package net.objecthunter.exp4j;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import net.objecthunter.exp4j.function.Functions;
import net.objecthunter.exp4j.operator.Operator;
import net.objecthunter.exp4j.operator.Operators;
import net.objecthunter.exp4j.tokenizer.*;

Expand Down Expand Up @@ -62,6 +65,150 @@ public void testGetVariableNames1() throws Exception{
assertEquals(2, exp.getVariableNames().size());
}

@Test
public void testFactorial() throws Exception {
Operator factorial = new Operator("!", 1, true, Operator.PRECEDENCE_POWER + 1) {

@Override
public double apply(double... args) {
final int arg = (int) args[0];
if ((double) arg != args[0]) {
throw new IllegalArgumentException("Operand for factorial has to be an integer");
}
if (arg < 0) {
throw new IllegalArgumentException("The operand of the factorial can not be less than zero");
}
double result = 1;
for (int i = 1; i <= arg; i++) {
result *= i;
}
return result;
}
};

Expression e = new ExpressionBuilder("2!+3!")
.operator(factorial)
.build();
assertEquals(0d, 8d, e.evaluate());

e = new ExpressionBuilder("3!-2!")
.operator(factorial)
.build();
assertEquals(0d, 4d, e.evaluate());

e = new ExpressionBuilder("3!")
.operator(factorial)
.build();
assertEquals(6, e.evaluate(), 0);

e = new ExpressionBuilder("3!!")
.operator(factorial)
.build();
assertEquals(720, e.evaluate(), 0);

e = new ExpressionBuilder("4 + 3!")
.operator(factorial)
.build();
assertEquals(10, e.evaluate(), 0);

e = new ExpressionBuilder("3! * 2")
.operator(factorial)
.build();
assertEquals(12, e.evaluate(), 0);

e = new ExpressionBuilder("3!")
.operator(factorial)
.build();
assertTrue(e.validate().isValid());
assertEquals(6, e.evaluate(), 0);

e = new ExpressionBuilder("3!!")
.operator(factorial)
.build();
assertTrue(e.validate().isValid());
assertEquals(720, e.evaluate(), 0);

e = new ExpressionBuilder("4 + 3!")
.operator(factorial)
.build();
assertTrue(e.validate().isValid());
assertEquals(10, e.evaluate(), 0);

e = new ExpressionBuilder("3! * 2")
.operator(factorial)
.build();
assertTrue(e.validate().isValid());
assertEquals(12, e.evaluate(), 0);

e = new ExpressionBuilder("2 * 3!")
.operator(factorial)
.build();
assertTrue(e.validate().isValid());
assertEquals(12, e.evaluate(), 0);

e = new ExpressionBuilder("4 + (3!)")
.operator(factorial)
.build();
assertTrue(e.validate().isValid());
assertEquals(10, e.evaluate(), 0);

e = new ExpressionBuilder("4 + 3! + 2 * 6")
.operator(factorial)
.build();
assertTrue(e.validate().isValid());
assertEquals(22, e.evaluate(), 0);
}

@Test(expected = IllegalArgumentException.class)
public void testOperatorFactorial2() throws Exception {
Operator factorial = new Operator("!", 1, true, Operator.PRECEDENCE_POWER + 1) {

@Override
public double apply(double... args) {
final int arg = (int) args[0];
if ((double) arg != args[0]) {
throw new IllegalArgumentException("Operand for factorial has to be an integer");
}
if (arg < 0) {
throw new IllegalArgumentException("The operand of the factorial can not be less than zero");
}
double result = 1;
for (int i = 1; i <= arg; i++) {
result *= i;
}
return result;
}
};

Expression e = new ExpressionBuilder("!3").build();
assertFalse(e.validate().isValid());
}

@Test (expected = IllegalArgumentException.class)
public void testInvalidFactorial2() {
Operator factorial = new Operator("!", 1, true, Operator.PRECEDENCE_POWER + 1) {

@Override
public double apply(double... args) {
final int arg = (int) args[0];
if ((double) arg != args[0]) {
throw new IllegalArgumentException("Operand for factorial has to be an integer");
}
if (arg < 0) {
throw new IllegalArgumentException("The operand of the factorial can not be less than zero");
}
double result = 1;
for (int i = 1; i <= arg; i++) {
result *= i;
}
return result;
}
};

Expression e = new ExpressionBuilder("!!3").build();
assertFalse(e.validate().isValid());
}

@Test
@Ignore
// If Expression should be threads safe this test must pass
Expand All @@ -87,3 +234,4 @@ public void run() {
}
}
}

0 comments on commit e977f19

Please sign in to comment.