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   import static org.junit.jupiter.api.Assertions.assertEquals;
9   
10  import org.junit.jupiter.api.Disabled;
11  import org.junit.jupiter.api.Test;
12  
13  class IfElseStatementsTest extends CoverageTest {
14      IfElseStatements tested;
15  
16      @Test
17      void simpleIf() {
18          tested.simpleIf(true);
19          tested.simpleIf(false);
20  
21          assertLines(21, 25, 4);
22          assertLine(21, 3, 3, 2);
23          assertLine(22, 1, 1, 1);
24          assertLine(25, 1, 1, 2);
25  
26          assertBranchingPoints(21, 2, 2);
27          assertBranchingPoints(22, 0, 0);
28      }
29  
30      @Test
31      void ifAndElse() {
32          tested.ifAndElse(true);
33          tested.ifAndElse(false);
34      }
35  
36      @Test
37      void singleLineIf() {
38          tested.singleLineIf(true);
39          tested.singleLineIf(false);
40  
41          assertLines(48, 51, 2);
42          assertLine(49, 3, 3, 2);
43          assertLine(51, 1, 1, 2);
44      }
45  
46      @Test
47      void singleLineIfAndElse() {
48          tested.singleLineIfAndElse(true);
49          tested.singleLineIfAndElse(false);
50  
51          assertLines(60, 63, 2);
52          assertLine(61, 3, 3, 2, 1, 1);
53          assertLine(63, 1, 1, 2);
54      }
55  
56      @Test
57      void singleLineIfAndElseWhereOnlyTheElseIsExecuted() {
58          tested.anotherSingleLineIfAndElse(false);
59  
60          assertLines(253, 253, 1);
61          assertLine(253, 3, 2, 1, 0, 1);
62      }
63  
64      @Test
65      void singleLineIfAndElseWhereElseIsExecutedMoreTimes() {
66          tested.yetAnotherSingleLineIfAndElse(false);
67          tested.yetAnotherSingleLineIfAndElse(true);
68          tested.yetAnotherSingleLineIfAndElse(false);
69  
70          assertLines(267, 267, 1);
71          assertLine(267, 3, 3, 3, 1, 2);
72      }
73  
74      @Test
75      void ifWithBooleanAndOperator() {
76          tested.ifWithBooleanAndOperator(true, false);
77          tested.ifWithBooleanAndOperator(false, true);
78  
79          assertLines(280, 283, 2);
80          assertLine(280, 5, 4, 2, 1);
81          assertLine(281, 1, 0, 0);
82          assertLine(283, 1, 1, 2);
83      }
84  
85      @Disabled
86      @Test
87      void anotherIfWithBooleanAndOperator() {
88          tested.anotherIfWithBooleanAndOperator(true, true);
89          tested.anotherIfWithBooleanAndOperator(true, false);
90  
91          assertLines(177, 180, 3);
92          assertLine(177, 3, 2, 2, 2, 0);
93          assertLine(178, 1, 1, 1);
94          assertLine(180, 1, 1, 2);
95      }
96  
97      @Test
98      void ifWithBooleanOrOperator() {
99          tested.ifWithBooleanOrOperator(false, false);
100         tested.ifWithBooleanOrOperator(true, true);
101 
102         assertLines(294, 297, 3);
103         assertLine(294, 5, 4, 2, 1);
104         assertLine(295, 1, 1, 1);
105         assertLine(297, 1, 1, 2);
106     }
107 
108     @Test
109     void methodWithFourDifferentPathsAndSimpleLines_exerciseTwoOppositePaths() {
110         tested.methodWithFourDifferentPathsAndSimpleLines(true, 0);
111         tested.methodWithFourDifferentPathsAndSimpleLines(false, 1);
112 
113         // TODO: assertions
114     }
115 
116     @Test
117     void methodWithFourDifferentPathsAndSegmentedLines_exerciseTwoOppositePaths() {
118         tested.methodWithFourDifferentPathsAndSegmentedLines(false, -1);
119         tested.methodWithFourDifferentPathsAndSegmentedLines(true, 1);
120 
121         // TODO: assertions
122     }
123 
124     @Test
125     void ifElseWithComplexBooleanCondition() {
126         tested.ifElseWithComplexBooleanCondition(true, false);
127 
128         // TODO: assertions
129     }
130 
131     @Test
132     void returnInput() {
133         assertEquals(2, tested.returnInput(1, true, false, false));
134         assertEquals(2, tested.returnInput(2, false, false, false));
135         assertEquals(2, tested.returnInput(3, false, true, false));
136         assertEquals(4, tested.returnInput(4, false, false, true));
137         assertEquals(5, tested.returnInput(5, true, true, false));
138         assertEquals(5, tested.returnInput(6, false, true, true));
139         assertEquals(7, tested.returnInput(7, true, true, true));
140         assertEquals(9, tested.returnInput(8, true, false, true));
141     }
142 
143     @Test
144     void nestedIf() {
145         assertEquals(1, tested.nestedIf(false, false));
146         assertEquals(2, tested.nestedIf(true, true));
147 
148         // TODO: assertions
149     }
150 
151     @Test
152     void ifElseWithNestedIf() {
153         assertEquals(1, tested.ifElseWithNestedIf(true, false));
154         assertEquals(2, tested.ifElseWithNestedIf(true, true));
155         assertEquals(3, tested.ifElseWithNestedIf(false, false));
156 
157         // TODO: assertions
158     }
159 
160     @Test
161     void nestedIfElse() {
162         assertEquals(1, tested.nestedIfElse(false, false));
163         assertEquals(2, tested.nestedIfElse(true, true));
164         assertEquals(3, tested.nestedIfElse(true, false));
165         assertEquals(4, tested.nestedIfElse(false, true));
166 
167         // TODO: assertions
168     }
169 
170     @Test
171     void infeasiblePaths() {
172         tested.infeasiblePaths(true);
173         tested.infeasiblePaths(false);
174 
175         // TODO: assertions
176     }
177 
178     @Test
179     void ifSpanningMultipleLines() {
180         tested.ifSpanningMultipleLines(true, 0);
181         tested.ifSpanningMultipleLines(false, -1);
182         tested.ifSpanningMultipleLines(false, 1);
183 
184         assertLines(323, 330, 3);
185         assertLine(323, 5, 5, 3);
186         assertLine(327, 1, 1, 2);
187         assertLine(330, 1, 1, 3);
188     }
189 }