gde4onos/src/test/java/br/ufes/inf/decisionelement/MathExpressionTreeification...

81 lines
2.9 KiB
Java

/*
* Copyright 2020 Adler Neves.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package br.ufes.inf.decisionelement;
import br.ufes.inf.decisionelement.wheelreinventions.expressionparsing.MathExpressionException;
import br.ufes.inf.decisionelement.wheelreinventions.expressionparsing.MathExpressionParser;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/**
* Tests MathExpression tree-ification capabilities.
*/
public class MathExpressionTreeificationTest {
@Before
public void setUp() {
}
@After
public void tearDown() {
}
@Test
public void basics() throws Exception {
Assert.assertEquals(1, 1);
parsesTo(" ( a + b ) - 1 ",
"((a+b)-1.0)$");
parsesTo("(.5*current_latency)-max_bandwidth/current_bandwidth$bandwidth",
"((0.5*current_latency)-(max_bandwidth/current_bandwidth))$bandwidth");
parsesTo("current_latency$bandwidth$latency",
"current_latency$bandwidth$latency");
parsesTo("-1+2/3*4",
"((0.0-1.0)+(2.0/(3.0*4.0)))$");
parsesTo("0-1+2/3*4",
"((0.0-1.0)+(2.0/(3.0*4.0)))$");
parsesTo("1-2/3*4",
"(1.0-(2.0/(3.0*4.0)))$");
parsesTo("0+1-2/3*4",
"(0.0+(1.0-(2.0/(3.0*4.0))))$");
simplifiesTo("0+1-24/3*4",
"-1.0$");
parsesTo("1 * (-2)",
"(1.0*(0.0-2.0))$");
simplifiesTo("1 * (-2)",
"-2.0$");
failsParsing("1 + -2");
failsParsing("1 2");
failsParsing("1a");
failsParsing("+1");
failsParsing("/1");
failsParsing("*1");
failsParsing("(1");
failsParsing("1)");
parsesTo("1",
"1.0$");
}
void parsesTo(String expression, String expected) throws MathExpressionException {
Assert.assertEquals(expected, MathExpressionParser.makeTree(expression).toString());
}
void simplifiesTo(String expression, String expected) throws MathExpressionException {
Assert.assertEquals(expected, MathExpressionParser.makeTree(expression).getSimplified().toString());
}
void failsParsing(String expression) {
try {
MathExpressionParser.makeTree(expression);
Assert.fail("Expression should have thrown MathExpressionException: " + expression);
} catch (MathExpressionException e) {
}
}
}