1 /******************************************************************************* 2 * Copyright (c) 2009, 2015 Mountainminds GmbH & Co. KG and Contributors 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the Eclipse Public License v1.0 5 * which accompanies this distribution, and is available at 6 * http://www.eclipse.org/legal/epl-v10.html 7 * 8 * Contributors: 9 * Marc R. Hoffmann - initial API and implementation 10 * 11 *******************************************************************************/ 12 package org.jacoco.examples.parser; 13 14 import java.io.IOException; 15 import static org.junit.Assert.*; 16 import org.junit.Test; 17 18 public class ExpressionParserTest { 19 20 @Test 21 public void expression1() throws IOException { 22 assertExpression("2 * 3 + 4", 10); 23 } 24 25 @Test 26 public void expression2() throws IOException { 27 assertExpression("2 + 3 * 4", 14); 28 } 29 30 @Test 31 public void expression3() throws IOException { 32 assertExpression("(2 + 3) * 4", 20); 33 } 34 35 @Test 36 public void expression4() throws IOException { 37 assertExpression("2 * 2 * 2 * 2", 16); 38 } 39 40 @Test 41 public void expression5() throws IOException { 42 assertExpression("1 + 2 + 3 + 4", 10); 43 } 44 45 @Test 46 public void expression6() throws IOException { 47 assertExpression("2 * 3 + 2 * 5", 16); 48 } 49 50 private static void assertExpression(final String expression, 51 final double expected) throws IOException { 52 final ExpressionParser parser = new ExpressionParser(expression); 53 final double actual = parser.parse().evaluate(); 54 assertEquals("expression", expected, actual, 0.0); 55 } 56 57 } 58