1 /* 2 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.scenario.effect.compiler.parser; 27 28 import com.sun.scenario.effect.compiler.JSLParser; 29 import com.sun.scenario.effect.compiler.model.Type; 30 import com.sun.scenario.effect.compiler.model.UnaryOpType; 31 import com.sun.scenario.effect.compiler.tree.LiteralExpr; 32 import com.sun.scenario.effect.compiler.tree.UnaryExpr; 33 import com.sun.scenario.effect.compiler.tree.VariableExpr; 34 import org.antlr.runtime.RecognitionException; 35 import org.junit.Before; 36 import org.junit.Test; 37 import static org.junit.Assert.assertEquals; 38 39 public class UnaryExprTest extends PrimaryExprTest { 40 41 private String primary; 42 43 @Before 44 @Override 45 public void setUp() { 46 super.setUp(); 47 this.primary = primary(); 48 } 49 50 @Test 51 public void negated() throws Exception { 52 UnaryExpr tree = parseTreeFor("!true"); 53 assertEquals(tree.getOp(), UnaryOpType.NOT); 54 assertEquals(((LiteralExpr)tree.getExpr()).getValue(), Boolean.TRUE); 55 } 56 57 @Test 58 public void positive() throws Exception { 59 UnaryExpr tree = parseTreeFor("+72.4"); 60 assertEquals(tree.getOp(), UnaryOpType.PLUS); 61 assertEquals(((LiteralExpr)tree.getExpr()).getValue(), new Float(72.4)); 62 } 63 64 @Test 65 public void negative() throws Exception { 66 UnaryExpr tree = parseTreeFor("-72.4"); 67 assertEquals(tree.getOp(), UnaryOpType.MINUS); 68 assertEquals(((LiteralExpr)tree.getExpr()).getValue(), new Float(72.4)); 69 } 70 71 @Test 72 public void preIncrement() throws Exception { 73 UnaryExpr tree = parseTreeFor("++foo"); 74 assertEquals(tree.getOp(), UnaryOpType.INC); 75 assertEquals(((VariableExpr)tree.getExpr()).getVariable().getName(), "foo"); 76 } 77 78 @Test 79 public void preDecrement() throws Exception { 80 UnaryExpr tree = parseTreeFor("--foo"); 81 assertEquals(tree.getOp(), UnaryOpType.DEC); 82 assertEquals(((VariableExpr)tree.getExpr()).getVariable().getName(), "foo"); 83 } 84 85 @Test 86 public void postIncrement() throws Exception { 87 UnaryExpr tree = parseTreeFor("foo++"); 88 assertEquals(tree.getOp(), UnaryOpType.INC); 89 assertEquals(((VariableExpr)tree.getExpr()).getVariable().getName(), "foo"); 90 } 91 92 @Test 93 public void postDecrement() throws Exception { 94 UnaryExpr tree = parseTreeFor("foo--"); 95 assertEquals(tree.getOp(), UnaryOpType.DEC); 96 assertEquals(((VariableExpr)tree.getExpr()).getVariable().getName(), "foo"); 97 } 98 99 @Test(expected = RecognitionException.class) 100 public void notAUnaryExpression() throws Exception { 101 parseTreeFor("^" + primary); 102 } 103 104 private UnaryExpr parseTreeFor(String text) throws RecognitionException { 105 JSLParser parser = parserOver(text); 106 parser.getSymbolTable().declareVariable("foo", Type.INT, null); 107 parser.getSymbolTable().declareVariable("vec", Type.INT3, null); 108 return (UnaryExpr)parser.unary_expression(); 109 } 110 111 protected String unary() { 112 return "(-" + primary() + ")"; 113 } 114 }