| 1 | package de.uka.ipd.sdq.pcm.stochasticexpressions.tests; |
| 2 | |
| 3 | import junit.framework.Assert; |
| 4 | import junit.framework.TestCase; |
| 5 | import org.antlr.runtime.ANTLRStringStream; |
| 6 | import org.antlr.runtime.CommonTokenStream; |
| 7 | import org.antlr.runtime.RecognitionException; |
| 8 | |
| 9 | import de.uka.ipd.sdq.pcm.stochasticexpressions.parser.PCMStoExLexer; |
| 10 | import de.uka.ipd.sdq.pcm.stochasticexpressions.parser.PCMStoExParser; |
| 11 | import de.uka.ipd.sdq.stoex.Expression; |
| 12 | import de.uka.ipd.sdq.stoex.FunctionLiteral; |
| 13 | import de.uka.ipd.sdq.stoex.IfElseExpression; |
| 14 | import de.uka.ipd.sdq.stoex.analyser.visitors.ExpressionInferTypeVisitor; |
| 15 | import de.uka.ipd.sdq.stoex.analyser.visitors.TypeEnum; |
| 16 | |
| 17 | public class TypeInferTests extends TestCase { |
| 18 | |
| 19 | public void testEnums() throws RecognitionException{ |
| 20 | ExpressionInferTypeVisitor visitor = new ExpressionInferTypeVisitor(); |
| 21 | Expression expression = parser("\"blah\""); |
| 22 | infer(expression,visitor); |
| 23 | Assert.assertEquals(TypeEnum.ENUM, visitor.getType(expression)); |
| 24 | visitor = new ExpressionInferTypeVisitor(); |
| 25 | expression = parser("EnumPMF[(\"blah\";0.4)(\"blub\";0.6)]"); |
| 26 | infer(expression,visitor); |
| 27 | Assert.assertEquals(TypeEnum.ENUM_PMF, visitor.getType(expression)); |
| 28 | } |
| 29 | |
| 30 | public void testIntPMF() throws RecognitionException{ |
| 31 | ExpressionInferTypeVisitor visitor = new ExpressionInferTypeVisitor(); |
| 32 | Expression expression = parser("IntPMF[(1;0.2)(2;0.4)]"); |
| 33 | infer(expression,visitor); |
| 34 | Assert.assertEquals(TypeEnum.INT_PMF, visitor.getType(expression)); |
| 35 | } |
| 36 | |
| 37 | public void testNegativeDoublePDF() throws RecognitionException{ |
| 38 | ExpressionInferTypeVisitor visitor = new ExpressionInferTypeVisitor(); |
| 39 | Expression expression = parser("DoublePDF[(-1.0;0.2)(2.0;0.4)]"); |
| 40 | infer(expression,visitor); |
| 41 | Assert.assertEquals(TypeEnum.DOUBLE_PDF, visitor.getType(expression)); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | public void testDoubleAnyCompare() throws RecognitionException{ |
| 46 | ExpressionInferTypeVisitor visitor = new ExpressionInferTypeVisitor(); |
| 47 | Expression expression = parser("192.0 / file.STRUCTURE < 1"); |
| 48 | infer(expression,visitor); |
| 49 | Assert.assertEquals(TypeEnum.BOOL_PMF, visitor.getType(expression)); |
| 50 | } |
| 51 | |
| 52 | public void testDoubleAnyCompareIfElse() throws RecognitionException{ |
| 53 | ExpressionInferTypeVisitor visitor = new ExpressionInferTypeVisitor(); |
| 54 | IfElseExpression expression = (IfElseExpression) parser("192.0 / file.STRUCTURE < 1 ? 5 : 5"); |
| 55 | infer(expression,visitor); |
| 56 | Assert.assertEquals(TypeEnum.ANY, visitor.getType(expression)); |
| 57 | Assert.assertEquals(TypeEnum.BOOL_PMF, visitor.getType(expression.getConditionExpression())); |
| 58 | } |
| 59 | |
| 60 | public void testTypeInfererBool() throws RecognitionException { |
| 61 | Assert.assertTrue(infer("true AND false") == TypeEnum.BOOL); |
| 62 | Assert.assertTrue(infer("true OR false") == TypeEnum.BOOL); |
| 63 | Assert.assertTrue(infer("128.0 < 192 OR false") == TypeEnum.BOOL); |
| 64 | Assert.assertTrue(infer("NOT(128.0 < 192 OR false)") == TypeEnum.BOOL); |
| 65 | Assert.assertTrue(infer("true ? 1 : 5") == TypeEnum.ANY); |
| 66 | } |
| 67 | |
| 68 | public void testsubInfer() throws RecognitionException { |
| 69 | ExpressionInferTypeVisitor visitor = new ExpressionInferTypeVisitor(); |
| 70 | IfElseExpression expression = (IfElseExpression) parser("true ? 1 : 5.5"); |
| 71 | infer(expression,visitor); |
| 72 | Assert.assertEquals(visitor.getType(expression.getConditionExpression()),TypeEnum.BOOL); |
| 73 | Assert.assertEquals(TypeEnum.INT,visitor.getType(expression.getIfExpression())); |
| 74 | Assert.assertEquals(TypeEnum.DOUBLE,visitor.getType(expression.getElseExpression())); |
| 75 | } |
| 76 | |
| 77 | public void testSubFuncInfer() throws RecognitionException { |
| 78 | ExpressionInferTypeVisitor visitor = new ExpressionInferTypeVisitor(); |
| 79 | FunctionLiteral expression = (FunctionLiteral) parser("Trunc(2.5)"); |
| 80 | infer(expression,visitor); |
| 81 | Assert.assertEquals(TypeEnum.INT_PMF,visitor.getType(expression)); |
| 82 | Assert.assertEquals(TypeEnum.DOUBLE,visitor.getType(expression.getParameters_FunctionLiteral().get(0))); |
| 83 | } |
| 84 | |
| 85 | public void testVariables() throws RecognitionException { |
| 86 | ExpressionInferTypeVisitor visitor = new ExpressionInferTypeVisitor(); |
| 87 | Expression expression = parser("file.TYPE"); |
| 88 | infer(expression,visitor); |
| 89 | Assert.assertEquals(TypeEnum.ANY_PMF,visitor.getType(expression)); |
| 90 | } |
| 91 | |
| 92 | public void testParenthesis() throws RecognitionException { |
| 93 | ExpressionInferTypeVisitor visitor = new ExpressionInferTypeVisitor(); |
| 94 | Expression expression = parser("file.BYTESIZE * ( file.TYPE / 192 )"); |
| 95 | infer(expression,visitor); |
| 96 | Assert.assertEquals(TypeEnum.ANY_PMF,visitor.getType(expression)); |
| 97 | } |
| 98 | |
| 99 | public void testTenaryOp() throws RecognitionException { |
| 100 | ExpressionInferTypeVisitor visitor = new ExpressionInferTypeVisitor(); |
| 101 | IfElseExpression expression = (IfElseExpression) parser("file.TYPE > 192 ? file.BYTESIZE * ( file.TYPE / 192 ) : file.BYTESIZE"); |
| 102 | infer(expression,visitor); |
| 103 | } |
| 104 | |
| 105 | private TypeEnum infer(String expression) throws RecognitionException{ |
| 106 | return infer(expression, new ExpressionInferTypeVisitor()); |
| 107 | } |
| 108 | |
| 109 | private TypeEnum infer(String expression, ExpressionInferTypeVisitor typeInferer) throws RecognitionException{ |
| 110 | Expression formula = parser(expression); |
| 111 | typeInferer.doSwitch(formula); |
| 112 | |
| 113 | return typeInferer.getType(formula); |
| 114 | } |
| 115 | |
| 116 | private TypeEnum infer(Expression expression, ExpressionInferTypeVisitor typeInferer) throws RecognitionException{ |
| 117 | Expression formula = expression; |
| 118 | typeInferer.doSwitch(formula); |
| 119 | |
| 120 | return typeInferer.getType(formula); |
| 121 | } |
| 122 | |
| 123 | private Expression parser(String expression) throws RecognitionException { |
| 124 | PCMStoExLexer lexer = new PCMStoExLexer( |
| 125 | new ANTLRStringStream(expression)); |
| 126 | Expression formula = null; |
| 127 | formula = new PCMStoExParser(new CommonTokenStream(lexer)).expression(); |
| 128 | return formula; |
| 129 | } |
| 130 | } |