| 1 | package de.uka.ipd.sdq.simucomframework.variables.tests; |
| 2 | |
| 3 | import junit.framework.Assert; |
| 4 | import junit.framework.TestCase; |
| 5 | |
| 6 | import org.apache.log4j.BasicConfigurator; |
| 7 | import org.apache.log4j.ConsoleAppender; |
| 8 | import org.apache.log4j.Logger; |
| 9 | import org.apache.log4j.PatternLayout; |
| 10 | import org.apache.log4j.Priority; |
| 11 | |
| 12 | import de.uka.ipd.sdq.probfunction.math.IProbabilityFunctionFactory; |
| 13 | import de.uka.ipd.sdq.probfunction.math.impl.DefaultRandomGenerator; |
| 14 | import de.uka.ipd.sdq.probfunction.math.impl.ProbabilityFunctionFactoryImpl; |
| 15 | import de.uka.ipd.sdq.simucomframework.SimuComDefaultRandomNumberGenerator; |
| 16 | import de.uka.ipd.sdq.simucomframework.variables.StackContext; |
| 17 | import de.uka.ipd.sdq.simucomframework.variables.cache.StoExCache; |
| 18 | import de.uka.ipd.sdq.simucomframework.variables.functions.MaxDeviationFunction; |
| 19 | import de.uka.ipd.sdq.simucomframework.variables.functions.MaxFunction; |
| 20 | import de.uka.ipd.sdq.simucomframework.variables.functions.MinDeviationFunction; |
| 21 | import de.uka.ipd.sdq.simucomframework.variables.functions.MinFunction; |
| 22 | |
| 23 | public class StoExVisitorTests extends TestCase { |
| 24 | private static Logger logger = |
| 25 | Logger.getLogger(StoExVisitorTests.class.getName()); |
| 26 | |
| 27 | public void setUp() { |
| 28 | IProbabilityFunctionFactory probFunctionFactory = ProbabilityFunctionFactoryImpl.getInstance(); |
| 29 | probFunctionFactory.setRandomGenerator(new SimuComDefaultRandomNumberGenerator(new long[]{1,2,3,4,5,6})); |
| 30 | StoExCache.initialiseStoExCache(probFunctionFactory); |
| 31 | |
| 32 | |
| 33 | PatternLayout myLayout = new PatternLayout("%d{HH:mm:ss,SSS} [%t] %-5p %m [%c]%n"); |
| 34 | ConsoleAppender ca = new ConsoleAppender(myLayout); |
| 35 | ca.setThreshold(Priority.INFO); |
| 36 | BasicConfigurator.resetConfiguration(); |
| 37 | BasicConfigurator.configure(ca); |
| 38 | } |
| 39 | |
| 40 | public void testDoubleOperations() { |
| 41 | logger.debug("Running Double Op Test"); |
| 42 | for (int i = 0; i < 2000; i++){ |
| 43 | double result = (Double)StackContext.evaluateStatic("( ( DoublePDF[ (1.0; 0.02236114450589084) (2.0; 0.04664582832411637) (3.0; 0.10387112286607357) (4.0; 0.1606155325799471) (5.0; 0.20389516710747776) (6.0; 0.18826641019475834) (7.0; 0.11372926184178889) (8.0; 0.06852608800192354) (9.0; 0.02933397451310411) (10.0; 0.017311853811012263) (11.0; 0.009377254147631643) (12.0; 0.008896369319547967) (13.0; 0.004327963452753066) (14.0; 0.005049290694878576) (15.0; 0.0019235393123346958) (16.0; 0.002163981726376533) (17.0; 0.0012022120702091848) ] * 0.04 ) + 0.18 ) * 14"); |
| 44 | Assert.assertTrue("Result must be greater 0, but was "+result,result >= 0); |
| 45 | Assert.assertTrue(result <= 12.1); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | public void testUnaryOperator() { |
| 50 | int result = (Integer)StackContext.evaluateStatic("-4"); |
| 51 | Assert.assertEquals(-4, result); |
| 52 | boolean result2 = (Boolean)StackContext.evaluateStatic("NOT true"); |
| 53 | Assert.assertEquals(result2, false); |
| 54 | } |
| 55 | |
| 56 | public void testPow1() { |
| 57 | double result3 = (Double)StackContext.evaluateStatic("10^3"); |
| 58 | Assert.assertEquals(1000.0, result3); |
| 59 | } |
| 60 | |
| 61 | public void testPow2() { |
| 62 | double result3 = (Double)StackContext.evaluateStatic("10^-3"); |
| 63 | Assert.assertEquals(0.001, result3); |
| 64 | } |
| 65 | |
| 66 | public void testDouble() { |
| 67 | double result3 = (Double)StackContext.evaluateStatic("1.0E-3"); |
| 68 | Assert.assertEquals(0.001, result3); |
| 69 | result3 = (Double)StackContext.evaluateStatic("0.4 + 0.6"); |
| 70 | Assert.assertEquals(1.0, result3); |
| 71 | result3 = (Double)StackContext.evaluateStatic("5 * 10.0"); |
| 72 | Assert.assertEquals(50.0, result3); |
| 73 | result3 = (Double)StackContext.evaluateStatic("20.0 / 10"); |
| 74 | Assert.assertEquals(2.0, result3); |
| 75 | result3 = (Double)StackContext.evaluateStatic("4.0^0.5"); |
| 76 | Assert.assertEquals(2.0, result3); |
| 77 | } |
| 78 | |
| 79 | public void testIntegerDivisionSemantics() { |
| 80 | double i = 25 + 15 * 10 / 1000; |
| 81 | double result3 = (Integer)StackContext.evaluateStatic("25 + 15 * 10 / 1000"); |
| 82 | Assert.assertEquals(result3, i); |
| 83 | |
| 84 | i = 25 + 15 * 10 / 1000.0; |
| 85 | result3 = (Double)StackContext.evaluateStatic("25 + 15 * 10 / 1000.0"); |
| 86 | Assert.assertEquals(result3, i); |
| 87 | Assert.assertEquals(result3, 25.15); |
| 88 | } |
| 89 | |
| 90 | public void testBool() { |
| 91 | boolean result = (Boolean)StackContext.evaluateStatic("true"); |
| 92 | Assert.assertEquals(true, result); |
| 93 | result = (Boolean)StackContext.evaluateStatic("5 > 4"); |
| 94 | Assert.assertEquals(true, result); |
| 95 | result = (Boolean)StackContext.evaluateStatic("NOT (5 <= 4)"); |
| 96 | Assert.assertEquals(true, result); |
| 97 | result = (Boolean)StackContext.evaluateStatic("true AND false"); |
| 98 | Assert.assertEquals(false, result); |
| 99 | result = (Boolean)StackContext.evaluateStatic("true AND true"); |
| 100 | Assert.assertEquals(true, result); |
| 101 | result = (Boolean)StackContext.evaluateStatic("true OR false"); |
| 102 | Assert.assertEquals(true, result); |
| 103 | result = (Boolean)StackContext.evaluateStatic("true XOR true"); |
| 104 | Assert.assertEquals(false, result); |
| 105 | } |
| 106 | |
| 107 | public void testFunctions() { |
| 108 | logger.info("UniDouble"); |
| 109 | for (int i=0; i<2000; i++) { |
| 110 | double result3 = (Double)StackContext.evaluateStatic("UniDouble(1,4)"); |
| 111 | Assert.assertTrue(result3 >= 1 && result3 <= 4); |
| 112 | } |
| 113 | logger.info("UniInt"); |
| 114 | for (int i=0; i<2000; i++) { |
| 115 | int result3 = (Integer)StackContext.evaluateStatic("UniInt(1,4)"); |
| 116 | Assert.assertTrue(result3 >= 1 && result3 <= 4); |
| 117 | } |
| 118 | logger.info("Norm"); |
| 119 | for (int i=0; i<200; i++) { |
| 120 | double result3 = (Double)StackContext.evaluateStatic("Norm(0,1)"); |
| 121 | logger.debug(result3); |
| 122 | Assert.assertTrue(result3 >= -5 && result3 <= 5); |
| 123 | } |
| 124 | logger.info("Exp"); |
| 125 | for (int i=0; i<200; i++) { |
| 126 | double result3 = (Double)StackContext.evaluateStatic("Exp(1)"); |
| 127 | Assert.assertTrue(result3 >= 0); |
| 128 | } |
| 129 | logger.info("Trunc"); |
| 130 | int result4 = (Integer)StackContext.evaluateStatic("Trunc(2.5)"); |
| 131 | Assert.assertEquals(2, result4); |
| 132 | result4 = (Integer)StackContext.evaluateStatic("Trunc(2)"); |
| 133 | Assert.assertEquals(2, result4); |
| 134 | logger.info("Round"); |
| 135 | result4 = (Integer)StackContext.evaluateStatic("Round(2.5)"); |
| 136 | Assert.assertEquals(3, result4); |
| 137 | result4 = (Integer)StackContext.evaluateStatic("Round(3)"); |
| 138 | Assert.assertEquals(3, result4); |
| 139 | logger.info(MinFunction.MIN_FUNCTION_NAME); |
| 140 | Assert.assertEquals(2,StackContext.evaluateStatic("Min(2,5)")); |
| 141 | Assert.assertEquals(2.3,StackContext.evaluateStatic("Min(2.3,5.3)")); |
| 142 | logger.info(MaxFunction.MAX_FUNCTION_NAME); |
| 143 | Assert.assertEquals(5,StackContext.evaluateStatic("Max(2,5)")); |
| 144 | Assert.assertEquals(5.3,StackContext.evaluateStatic("Max(2.3,5.3)")); |
| 145 | logger.info(MinDeviationFunction.MIN_DEVIATION_FUNCTION_NAME); |
| 146 | Assert.assertEquals(5, StackContext.evaluateStatic("MinDeviation(10, 5.0, 0.1)")); |
| 147 | Assert.assertEquals(4, StackContext.evaluateStatic("MinDeviation(10, 5.0, 0.6)")); |
| 148 | Assert.assertEquals(5.0, StackContext.evaluateStatic("MinDeviation(10.0, 5.0, 0.1)")); |
| 149 | Assert.assertEquals(4.0, StackContext.evaluateStatic("MinDeviation(10.0, 5.0, 0.6)")); |
| 150 | logger.info(MaxDeviationFunction.MAX_DEVIATION_FUNCTION_NAME); |
| 151 | Assert.assertEquals(15, StackContext.evaluateStatic("MaxDeviation(10, 5.0, 0.1)")); |
| 152 | Assert.assertEquals(16, StackContext.evaluateStatic("MaxDeviation(10, 5.0, 0.6)")); |
| 153 | Assert.assertEquals(15.0, StackContext.evaluateStatic("MaxDeviation(10.0, 5.0, 0.1)")); |
| 154 | Assert.assertEquals(16.0, StackContext.evaluateStatic("MaxDeviation(10.0, 5.0, 0.6)")); |
| 155 | } |
| 156 | |
| 157 | public void testIfelse() { |
| 158 | int result = (Integer)StackContext.evaluateStatic("true ? 3 : 4"); |
| 159 | Assert.assertEquals(3, result); |
| 160 | result = (Integer)StackContext.evaluateStatic("false ? 3 : 4"); |
| 161 | Assert.assertEquals(4, result); |
| 162 | } |
| 163 | } |