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

64 lines
2.3 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 tokenization capabilities.
*/
public class MathExpressionTokenizationTest {
@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]");
failsParsing("[1]");
failsParsing("{2}");
failsParsing("~");
failsParsing("a > b");
failsParsing("a < b");
failsParsing("a = b");
failsParsing("cond ? true : false");
failsParsing("true : false");
failsParsing("a | b");
failsParsing("a & b");
failsParsing("a ^ b");
failsParsing("~a");
}
void parsesTo(String expression, String expected) throws MathExpressionException {
Assert.assertEquals(expected, MathExpressionParser.tokenize(expression).toString());
}
void failsParsing(String expression) {
try {
MathExpressionParser.tokenize(expression);
Assert.fail("Expression should have thrown MathExpressionException: " + expression);
} catch (MathExpressionException e) {
}
}
}