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.Level; |
9 | import org.apache.log4j.Logger; |
10 | import org.apache.log4j.PatternLayout; |
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.variables.StackContext; |
16 | import de.uka.ipd.sdq.simucomframework.variables.cache.StoExCache; |
17 | import de.uka.ipd.sdq.simucomframework.variables.exceptions.StochasticExpressionEvaluationFailedException; |
18 | import de.uka.ipd.sdq.simucomframework.variables.stackframe.SimulatedStackframe; |
19 | import de.uka.ipd.sdq.simucomframework.variables.stoexvisitor.VariableMode; |
20 | |
21 | public class StoExVisitorVariablesTest extends TestCase { |
22 | /** Logger for this class. */ |
23 | @SuppressWarnings("unused") |
24 | private static Logger logger = |
25 | Logger.getLogger(StoExVisitorTests.class.getName()); |
26 | private SimulatedStackframe<Object> stackFrame; |
27 | |
28 | public void setUp() { |
29 | PatternLayout myLayout = new PatternLayout("%d{HH:mm:ss,SSS} [%t] %-5p %m [%c]%n"); |
30 | ConsoleAppender ca = new ConsoleAppender(myLayout); |
31 | ca.setThreshold(Level.INFO); |
32 | BasicConfigurator.resetConfiguration(); |
33 | BasicConfigurator.configure(ca); |
34 | |
35 | IProbabilityFunctionFactory probFunctionFactory = ProbabilityFunctionFactoryImpl.getInstance(); |
36 | probFunctionFactory.setRandomGenerator(new DefaultRandomGenerator()); |
37 | StoExCache.initialiseStoExCache(probFunctionFactory); |
38 | stackFrame = new SimulatedStackframe<Object>(); |
39 | stackFrame.addValue("anInt.BYTESIZE", 10); |
40 | stackFrame.addValue("anBoolean.VALUE", false); |
41 | stackFrame.addValue("anDouble.VALUE", 10.0); |
42 | } |
43 | |
44 | public void testStackEvaluate() { |
45 | int i = (Integer)StackContext.evaluateStatic("anInt.BYTESIZE", stackFrame); |
46 | Assert.assertEquals(i, 10); |
47 | boolean b = (Boolean)StackContext.evaluateStatic("anBoolean.VALUE", stackFrame); |
48 | Assert.assertEquals(b, false); |
49 | double d = (Double)StackContext.evaluateStatic("anDouble.VALUE", stackFrame); |
50 | Assert.assertEquals(d, 10.0); |
51 | } |
52 | |
53 | public void testStackEvaluateAutoTypeConversion() { |
54 | // prepare stack with test values |
55 | SimulatedStackframe<Object> stackFrame = new SimulatedStackframe<Object>(); |
56 | String CHAR_VALUE = "c.VALUE"; |
57 | String BYTE_VALUE = "b.VALUE"; |
58 | String SHORT_VALUE = "s.VALUE"; |
59 | String INTEGER_VALUE = "i.VALUE"; |
60 | String LONG_VALUE = "l.VALUE"; |
61 | String FLOAT_VALUE = "f.VALUE"; |
62 | String DOUBLE_VALUE = "d.VALUE"; |
63 | char c = 14; |
64 | stackFrame.addValue(CHAR_VALUE, c); |
65 | byte b = 15; |
66 | stackFrame.addValue(BYTE_VALUE, b); |
67 | short s = 16; |
68 | stackFrame.addValue(SHORT_VALUE, s); |
69 | int i = 17; |
70 | stackFrame.addValue(INTEGER_VALUE, i); |
71 | long l = 18; |
72 | stackFrame.addValue(LONG_VALUE, l); |
73 | float f = 19; |
74 | stackFrame.addValue(FLOAT_VALUE, f); |
75 | double d = 20; |
76 | stackFrame.addValue(DOUBLE_VALUE, d); |
77 | // test |
78 | // byte/char |
79 | Assert.assertEquals(Character.valueOf(c), StackContext.evaluateStatic(CHAR_VALUE, Character.class, stackFrame)); |
80 | Assert.assertEquals(Byte.valueOf(b), StackContext.evaluateStatic(BYTE_VALUE, Byte.class, stackFrame)); |
81 | // short |
82 | Assert.assertEquals((Short) Byte.valueOf(b).shortValue(), |
83 | StackContext.evaluateStatic(BYTE_VALUE, Short.class, stackFrame)); |
84 | Assert.assertEquals((Short) Byte.valueOf((byte)Character.valueOf(c).charValue()).shortValue(), |
85 | StackContext.evaluateStatic(CHAR_VALUE, Short.class, stackFrame)); |
86 | // int |
87 | Assert.assertEquals((Integer) Byte.valueOf(b).intValue(), |
88 | StackContext.evaluateStatic(BYTE_VALUE, Integer.class, stackFrame)); |
89 | Assert.assertEquals((Integer) Byte.valueOf((byte)Character.valueOf(c).charValue()).intValue(), |
90 | StackContext.evaluateStatic(CHAR_VALUE, Integer.class, stackFrame)); |
91 | Assert.assertEquals((Integer) Short.valueOf(s).intValue(), |
92 | StackContext.evaluateStatic(SHORT_VALUE, Integer.class, stackFrame)); |
93 | // long |
94 | Assert.assertEquals((Long) Byte.valueOf(b).longValue(), |
95 | StackContext.evaluateStatic(BYTE_VALUE, Long.class, stackFrame)); |
96 | Assert.assertEquals((Long) Byte.valueOf((byte)Character.valueOf(c).charValue()).longValue(), |
97 | StackContext.evaluateStatic(CHAR_VALUE, Long.class, stackFrame)); |
98 | Assert.assertEquals((Long) Short.valueOf(s).longValue(), |
99 | StackContext.evaluateStatic(SHORT_VALUE, Long.class, stackFrame)); |
100 | Assert.assertEquals((Long) Integer.valueOf(i).longValue(), |
101 | StackContext.evaluateStatic(INTEGER_VALUE, Long.class, stackFrame)); |
102 | // float |
103 | Assert.assertEquals(Byte.valueOf(b).floatValue(), |
104 | StackContext.evaluateStatic(BYTE_VALUE, Float.class, stackFrame)); |
105 | Assert.assertEquals(Byte.valueOf((byte)Character.valueOf(c).charValue()).floatValue(), |
106 | StackContext.evaluateStatic(CHAR_VALUE, Float.class, stackFrame)); |
107 | Assert.assertEquals(Short.valueOf(s).floatValue(), |
108 | StackContext.evaluateStatic(SHORT_VALUE, Float.class, stackFrame)); |
109 | Assert.assertEquals(Integer.valueOf(i).floatValue(), |
110 | StackContext.evaluateStatic(INTEGER_VALUE, Float.class, stackFrame)); |
111 | Assert.assertEquals(Long.valueOf(l).floatValue(), |
112 | StackContext.evaluateStatic(LONG_VALUE, Float.class, stackFrame)); |
113 | // double |
114 | Assert.assertEquals(Byte.valueOf(b).doubleValue(), |
115 | StackContext.evaluateStatic(BYTE_VALUE, Double.class, stackFrame)); |
116 | Assert.assertEquals(Byte.valueOf((byte)Character.valueOf(c).charValue()).doubleValue(), |
117 | StackContext.evaluateStatic(CHAR_VALUE, Double.class, stackFrame)); |
118 | Assert.assertEquals(Short.valueOf(s).doubleValue(), |
119 | StackContext.evaluateStatic(SHORT_VALUE, Double.class, stackFrame)); |
120 | Assert.assertEquals(Integer.valueOf(i).doubleValue(), |
121 | StackContext.evaluateStatic(INTEGER_VALUE, Double.class, stackFrame)); |
122 | Assert.assertEquals(Long.valueOf(l).doubleValue(), |
123 | StackContext.evaluateStatic(LONG_VALUE, Double.class, stackFrame)); |
124 | Assert.assertEquals(Float.valueOf(f).doubleValue(), |
125 | StackContext.evaluateStatic(FLOAT_VALUE, Double.class, stackFrame)); |
126 | } |
127 | |
128 | public void testInvalidAccess() { |
129 | try { |
130 | StackContext.evaluateStatic("sssdffg.VALUE", stackFrame); |
131 | } catch (Exception ex){ |
132 | Assert.assertEquals(StochasticExpressionEvaluationFailedException.class, ex.getClass()); |
133 | return; |
134 | } |
135 | Assert.fail("Parser error expected, but did not occur"); |
136 | } |
137 | |
138 | public void testPowVar() { |
139 | double result3 = (Double)StackContext.evaluateStatic("10^(anInt.BYTESIZE)",stackFrame); |
140 | Assert.assertEquals(Math.pow(10, 10), result3); |
141 | } |
142 | |
143 | public void testReturnDefault() { |
144 | int bs = (Integer)StackContext.evaluateStatic("a.BYTESIZE", stackFrame, VariableMode.RETURN_DEFAULT_ON_NOT_FOUND); |
145 | Assert.assertEquals(bs, 0); |
146 | } |
147 | |
148 | } |