View Javadoc
1   /*
2    * MIT License
3    * Copyright (c) 2006-2025 JMockit developers
4    * See LICENSE file for full license text.
5    */
6   package integration.tests;
7   
8   /**
9    * The Class BooleanExpressions.
10   */
11  public final class BooleanExpressions {
12  
13      /**
14       * Eval 1.
15       *
16       * @param x
17       *            the x
18       * @param y
19       *            the y
20       * @param z
21       *            the z
22       *
23       * @return true, if successful
24       */
25      public boolean eval1(boolean x, boolean y, int z) {
26          return x && (y || z > 0);
27      }
28  
29      /**
30       * Eval 2.
31       *
32       * @param x
33       *            the x
34       * @param y
35       *            the y
36       * @param z
37       *            the z
38       *
39       * @return true, if successful
40       */
41      public boolean eval2(boolean x, boolean y, int z) {
42          return x && (y || z > 0);
43      }
44  
45      /**
46       * Eval 3.
47       *
48       * @param x
49       *            the x
50       * @param y
51       *            the y
52       * @param z
53       *            the z
54       *
55       * @return true, if successful
56       */
57      public boolean eval3(boolean x, boolean y, boolean z) {
58          return x && (y || z); // LOAD 1 IFEQ L1, LOAD 2 IFNE L2, LOAD 3 IFEQ L1, [L2 1 GOTO L3], [L1 0 L3 RETURN]
59      }
60  
61      /**
62       * Eval 4.
63       *
64       * @param x
65       *            the x
66       * @param y
67       *            the y
68       * @param z
69       *            the z
70       *
71       * @return true, if successful
72       */
73      public boolean eval4(boolean x, boolean y, boolean z) {
74          return x && (!y || z);
75      }
76  
77      /**
78       * Eval 5.
79       *
80       * @param a
81       *            the a
82       * @param b
83       *            the b
84       * @param c
85       *            the c
86       *
87       * @return true, if successful
88       */
89      public boolean eval5(boolean a, boolean b, boolean c) {
90          if (a) {
91              return true;
92          }
93          if (b || c) {
94              return false;
95          }
96  
97          return !c;
98      }
99  
100     /**
101      * Checks if is same type ignoring auto boxing.
102      *
103      * @param firstType
104      *            the first type
105      * @param secondType
106      *            the second type
107      *
108      * @return true, if is same type ignoring auto boxing
109      */
110     static boolean isSameTypeIgnoringAutoBoxing(Class<?> firstType, Class<?> secondType) {
111         return firstType == secondType || firstType.isPrimitive() && isWrapperOfPrimitiveType(firstType, secondType)
112                 || secondType.isPrimitive() && isWrapperOfPrimitiveType(secondType, firstType);
113     }
114 
115     /**
116      * Checks if is wrapper of primitive type.
117      *
118      * @param primitiveType
119      *            the primitive type
120      * @param otherType
121      *            the other type
122      *
123      * @return true, if is wrapper of primitive type
124      */
125     static boolean isWrapperOfPrimitiveType(Class<?> primitiveType, Class<?> otherType) {
126         return primitiveType == int.class && otherType == Integer.class
127                 || primitiveType == long.class && otherType == Long.class
128                 || primitiveType == double.class && otherType == Double.class
129                 || primitiveType == float.class && otherType == Float.class
130                 || primitiveType == boolean.class && otherType == Boolean.class;
131     }
132 
133     /**
134      * Simply returns input.
135      *
136      * @param b
137      *            the b
138      *
139      * @return true, if successful
140      */
141     public boolean simplyReturnsInput(boolean b) {
142         return b;
143     }
144 
145     /**
146      * Returns negated input.
147      *
148      * @param b
149      *            the b
150      *
151      * @return true, if successful
152      */
153     public boolean returnsNegatedInput(boolean b) {
154         return !b; // LOAD 1 IFNE L1, 1 GOTO L2, L1 0 L2 RETURN
155     }
156 
157     /**
158      * Returns trivial result from input after if else.
159      *
160      * @param b
161      *            the b
162      * @param i
163      *            the i
164      *
165      * @return true, if successful
166      */
167     public boolean returnsTrivialResultFromInputAfterIfElse(boolean b, int i) {
168         String s;
169 
170         if (b) {
171             s = "one";
172         } else {
173             s = "two";
174         }
175 
176         return i != 0; // LOAD 2 IFEQ L1, 1 GOTO L2, L1 0 L2 RETURN
177     }
178 
179     /**
180      * Returns result previously computed from input.
181      *
182      * @param b
183      *            the b
184      * @param i
185      *            the i
186      *
187      * @return true, if successful
188      */
189     public boolean returnsResultPreviouslyComputedFromInput(boolean b, int i) {
190         String s = b ? "a" : "b";
191         boolean res;
192 
193         if (i != 0) {
194             res = true;
195         } else {
196             res = false;
197             System.out.checkError();
198         }
199 
200         return res;
201     }
202 
203     /**
204      * Method with too many conditions for path analysis.
205      *
206      * @param i
207      *            the i
208      * @param j
209      *            the j
210      * @param b
211      *            the b
212      *
213      * @return true, if successful
214      */
215     public boolean methodWithTooManyConditionsForPathAnalysis(int i, int j, boolean b) {
216         if (i > 0 && j < 5 || (b ? i > 1 : j > 5) || (i <= 3 || j >= 4) && b) {
217             return i + j == 3 == b;
218         }
219         if (i < 0 || j < 0) {
220             return i < j;
221         }
222 
223         return b;
224     }
225 
226     /**
227      * Returns negated input from local variable.
228      *
229      * @param b
230      *            the b
231      *
232      * @return true, if successful
233      */
234     public boolean returnsNegatedInputFromLocalVariable(boolean b) {
235         return !b; // LOAD 1 IFNE L1, [1 GOTO L2], [L1 0], L2 STORE 2, L3 LOAD 2 RETURN
236     }
237 }