1 /******************************************************************************* 2 * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the Eclipse Public License v1.0 5 * which accompanies this distribution, and is available at 6 * http://www.eclipse.org/legal/epl-v10.html 7 * 8 * Contributors: 9 * Marc R. Hoffmann - initial API and implementation 10 * 11 *******************************************************************************/ 12 package org.jacoco.core.test.validation.targets; 13 14 import static org.jacoco.core.test.validation.targets.Stubs.f; 15 import static org.jacoco.core.test.validation.targets.Stubs.i2; 16 import static org.jacoco.core.test.validation.targets.Stubs.nop; 17 import static org.jacoco.core.test.validation.targets.Stubs.t; 18 19 import java.util.Collections; 20 21 /** 22 * This target exercises a set of common Java control structures. 23 */ 24 public class Target01 { 25 26 public static void main(String[] args) { 27 28 unconditionalExecution(); 29 missedIfBlock(); 30 executedIfBlock(); 31 missedWhileBlock(); 32 alwaysExecutedWhileBlock(); 33 executedWhileBlock(); 34 executedDoWhileBlock(); 35 missedForBlock(); 36 executedForBlock(); 37 missedForEachBlock(); 38 executedForEachBlock(); 39 tableSwitchWithHit(); 40 continuedTableSwitchWithHit(); 41 tableSwitchWithoutHit(); 42 lookupSwitchWithHit(); 43 continuedLookupSwitchWithHit(); 44 lookupSwitchWithoutHit(); 45 breakStatement(); 46 continueStatement(); 47 conditionalReturn(); 48 implicitReturn(); 49 explicitReturn(); 50 51 } 52 53 private static void unconditionalExecution() { 54 55 nop(); // $line-unconditional$ 56 57 } 58 59 private static void missedIfBlock() { 60 61 if (f()) { // $line-iffalse$ 62 nop(); // $line-missedif$ 63 } else { 64 nop(); // $line-executedelse$ 65 } 66 67 } 68 69 private static void executedIfBlock() { 70 71 if (t()) { // $line-iftrue$ 72 nop(); // $line-executedif$ 73 } else { 74 nop(); // $line-missedelse$ 75 } 76 77 } 78 79 private static void missedWhileBlock() { 80 81 while (f()) { // $line-whilefalse$ 82 nop(); // $line-missedwhile$ 83 } 84 85 } 86 87 private static void alwaysExecutedWhileBlock() { 88 89 while (t()) { // $line-whiletrue$ 90 if (t()) { 91 break; 92 } 93 } 94 95 } 96 97 private static void executedWhileBlock() { 98 99 int i = 0; 100 while (i++ < 3) { // $line-whiletruefalse$ 101 nop(); // $line-executedwhile$ 102 } 103 104 } 105 106 private static void executedDoWhileBlock() { 107 108 do { 109 nop(); // $line-executeddowhile$ 110 } while (f()); // $line-executeddowhilefalse$ 111 112 } 113 114 private static void missedForBlock() { 115 116 for (nop(); f(); nop()) { // $line-missedforincrementer$ 117 nop(); // $line-missedfor$ 118 } 119 120 } 121 122 private static void executedForBlock() { 123 124 for (int j = 0; j < 1; j++) { // $line-executedforincrementer$ 125 nop(); // $line-executedfor$ 126 } 127 128 } 129 130 private static void missedForEachBlock() { 131 132 for (Object o : Collections.emptyList()) { // $line-missedforeachincrementer$ 133 nop(o); // $line-missedforeach$ 134 } 135 136 } 137 138 private static void executedForEachBlock() { 139 140 for (Object o : Collections.singleton(new Object())) { // $line-executedforeachincrementer$ 141 nop(o); // $line-executedforeach$ 142 } 143 144 } 145 146 private static void tableSwitchWithHit() { 147 148 switch (i2()) { // $line-tswitch1$ 149 case 1: 150 nop(); // $line-tswitch1case1$ 151 break; 152 case 2: 153 nop(); // $line-tswitch1case2$ 154 break; 155 case 3: 156 nop(); // $line-tswitch1case3$ 157 break; 158 default: 159 nop(); // $line-tswitch1default$ 160 break; 161 } 162 163 } 164 165 private static void continuedTableSwitchWithHit() { 166 167 switch (i2()) { // $line-tswitch2$ 168 case 1: 169 nop(); // $line-tswitch2case1$ 170 case 2: 171 nop(); // $line-tswitch2case2$ 172 case 3: 173 nop(); // $line-tswitch2case3$ 174 default: 175 nop(); // $line-tswitch2default$ 176 } 177 178 } 179 180 private static void tableSwitchWithoutHit() { 181 182 switch (i2()) { // $line-tswitch3$ 183 case 3: 184 nop(); // $line-tswitch3case1$ 185 break; 186 case 4: 187 nop(); // $line-tswitch3case2$ 188 break; 189 case 5: 190 nop(); // $line-tswitch3case3$ 191 break; 192 default: 193 nop(); // $line-tswitch3default$ 194 break; 195 } 196 197 } 198 199 private static void lookupSwitchWithHit() { 200 201 switch (i2()) { // $line-lswitch1$ 202 case -123: 203 nop(); // $line-lswitch1case1$ 204 break; 205 case 2: 206 nop(); // $line-lswitch1case2$ 207 break; 208 case 456: 209 nop(); // $line-lswitch1case3$ 210 break; 211 default: 212 nop(); // $line-lswitch1default$ 213 break; 214 } 215 216 } 217 218 private static void continuedLookupSwitchWithHit() { 219 220 switch (i2()) { // $line-lswitch2$ 221 case -123: 222 nop(); // $line-lswitch2case1$ 223 case 2: 224 nop(); // $line-lswitch2case2$ 225 case 456: 226 nop(); // $line-lswitch2case3$ 227 default: 228 nop(); // $line-lswitch2default$ 229 } 230 231 } 232 233 private static void lookupSwitchWithoutHit() { 234 235 switch (i2()) { // $line-lswitch3$ 236 case -123: 237 nop(); // $line-lswitch3case1$ 238 break; 239 case 456: 240 nop(); // $line-lswitch3case2$ 241 break; 242 case 789: 243 nop(); // $line-lswitch3case3$ 244 break; 245 default: 246 nop(); // $line-lswitch3default$ 247 break; 248 } 249 250 } 251 252 private static void breakStatement() { 253 254 while (true) { 255 if (t()) { 256 break; // $line-executedbreak$ 257 } 258 nop(); // $line-missedafterbreak$ 259 } 260 261 } 262 263 private static void continueStatement() { 264 265 for (int j = 0; j < 1; j++) { 266 if (t()) { 267 continue; // $line-executedcontinue$ 268 } 269 nop(); // $line-missedaftercontinue$ 270 } 271 272 } 273 274 private static void conditionalReturn() { 275 276 if (t()) { 277 return; // $line-conditionalreturn$ 278 } 279 nop(); // $line-afterconditionalreturn$ 280 281 } 282 283 private static void implicitReturn() { 284 285 } // $line-implicitreturn$ 286 287 private static void explicitReturn() { 288 289 return; // $line-explicitreturn$ 290 291 } // $line-afterexplicitreturn$ 292 293 } 294