1 package gov.nasa.jpf.util; 20 21 import gov.nasa.jpf.Config; 22 import gov.nasa.jpf.jvm.bytecode.Instruction; 23 24 import java.util.Enumeration ; 25 import java.util.Hashtable ; 26 27 34 public class CoverageManager { 35 public static int IFcounter = 0; 36 37 public static int Methodcounter = 0; 38 39 private static String [] StatementIFMap = new String [1]; 40 41 private static int[][] IndexIFMap = new int[1][1]; 42 43 private static String [] StatementMethodMap = new String [1]; 44 45 private static int[][] IndexMethodMap = new int[1][1]; 46 47 private static int lastInstruction = 0; 48 49 private static int lastInstructionGlobal = 0; 50 51 private static Hashtable InstructionMap = new Hashtable (); 52 53 private static Hashtable PathInstructionMap = new Hashtable (); 54 55 private static int lastIncremented = 0; 56 57 private static int lastIncrementedGlobal = 0; 58 59 private static int[][] PathIndexIFMap = new int[1][1]; 60 61 private static boolean isBranchHeuristic; 62 63 private static boolean isPathHeuristic; 64 65 static boolean pathCoverage = false; 66 67 static boolean branchCoverage = false; 68 69 static boolean calcBranchCoverage = false; 70 71 static boolean insnCoverage = false; 72 73 public static boolean init(Config config) { 74 String hc = config.getProperty("search.heuristic.class"); 75 if (hc != null) { 76 if (hc.indexOf("Branch") != -1) 77 isBranchHeuristic = true; 78 if (hc.indexOf("Path") != -1) 79 isPathHeuristic = true; 80 } 81 82 branchCoverage = config.getBoolean("search.branch_coverage"); 83 84 return true; 85 } 86 87 public static void setPathCoverage(boolean b) { 88 pathCoverage = b; 89 } 90 91 public static void setInstructionCoverage(boolean b) { 92 insnCoverage = b; 93 } 94 95 public static void setCalcBranchCoverage(boolean b) { 96 calcBranchCoverage = b; 97 } 98 99 public static int getBranchCount(int th, int index) { 100 return IndexIFMap[th][index]; 101 } 102 103 public static int getGlobalCoverage() { 104 int[] sum = new int[IndexIFMap.length]; 105 int total_sum = 0; 106 int length = IndexIFMap[0].length; int[] results = new int[length]; 110 for (int k = 0; k < length; k++) { 111 results[k] = 0; 112 } 113 114 for (int j = 0; j < IndexIFMap.length; j++) { 115 sum[j] = 0; 116 117 for (int i = 1; i < IndexIFMap[j].length; i++) { results[i] += IndexIFMap[j][i]; 120 121 if (IndexIFMap[j][i] != 0) { 122 sum[j]++; 123 } 124 } 125 } 126 127 for (int l = 0; l < length; l++) { 128 if (results[l] != 0) { 129 total_sum++; 130 } 131 } 132 133 if ((length - 1) == 0) { 134 return 0; 135 } else { 136 return (int) ((100 * total_sum) / (length - 1)); 137 } 138 } 139 140 public static void setIndexIFMap(int[][] newIndexMap) { 141 PathIndexIFMap = (int[][]) newIndexMap.clone(); 142 143 int pl1 = PathIndexIFMap.length; 144 int pl2 = PathIndexIFMap[0].length; 145 int l1 = IndexIFMap.length; 146 int l2 = IndexIFMap[0].length; 147 148 if ((l1 > pl1) || (l2 > pl2)) { 149 int[][] temp1 = new int[l1][l2]; 150 151 for (int i = 0; i < l1; i++) { 152 int[] temp2 = new int[l2]; 153 154 if (i < pl1) { 155 System.arraycopy(PathIndexIFMap[i], 0, temp2, 0, pl2); 156 } 157 158 temp1[i] = temp2; 159 } 160 161 PathIndexIFMap = temp1; 162 } 163 } 164 165 public static int[][] getIndexIFMap() { 166 return (int[][]) PathIndexIFMap.clone(); 167 } 168 169 public static void setInstructionMap(Hashtable iMap) { 170 Hashtable h = new Hashtable (); 171 172 for (Enumeration e = iMap.keys(); e.hasMoreElements();) { 173 Object o = e.nextElement(); 174 h.put(o, ((int[]) iMap.get(o)).clone()); 175 } 176 177 PathInstructionMap = h; 178 } 179 180 public static Hashtable getInstructionMap() { 181 Hashtable h = new Hashtable (); 182 183 for (Enumeration e = PathInstructionMap.keys(); e.hasMoreElements();) { 184 Object o = e.nextElement(); 185 h.put(o, ((int[]) PathInstructionMap.get(o)).clone()); 186 } 187 188 return h; 189 } 190 191 public static int getLastIncremented() { 192 return lastIncremented; 193 } 194 195 public static int getLastIncrementedGlobal() { 196 return lastIncrementedGlobal; 197 } 198 199 public static void setLastIncrements(int i) { 200 lastIncremented = i; 201 lastIncrementedGlobal = i; 202 } 203 204 public static int getLastInstruction() { 205 return lastInstruction; 206 } 207 208 public static int getLastInstructionGlobal() { 209 return lastInstructionGlobal; 210 } 211 212 public static int getMethodCount(int th, int index) { 213 return IndexMethodMap[th][index]; 214 } 215 216 public static int getPathBranchCount(int th, int index) { 217 return PathIndexIFMap[th][index]; 218 } 219 220 public static int getPathCoverage() { 221 int[] sum = new int[PathIndexIFMap.length]; 222 int total_sum = 0; 223 int length = PathIndexIFMap[0].length; int[] results = new int[length]; 227 for (int k = 0; k < length; k++) { 228 results[k] = 0; 229 } 230 231 for (int j = 0; j < PathIndexIFMap.length; j++) { 232 sum[j] = 0; 233 234 for (int i = 1; i < PathIndexIFMap[j].length; i++) { results[i] += PathIndexIFMap[j][i]; 237 238 if (PathIndexIFMap[j][i] != 0) { 239 sum[j]++; 240 } 241 } 242 } 243 244 for (int l = 0; l < length; l++) { 245 if (results[l] != 0) { 246 total_sum++; 247 } 248 } 249 250 if ((length - 1) == 0) { 251 return 0; 252 } else { 253 return (int) ((100 * total_sum) / (length - 1)); 254 } 255 } 256 257 public static int addIF(String name) { 258 if (calcBranchCoverage || branchCoverage || isBranchHeuristic) { 260 if (StatementIFMap.length <= IFcounter) { 261 String [] temp = new String [IFcounter + 1]; 262 System.arraycopy(StatementIFMap, 0, temp, 0, StatementIFMap.length); 263 StatementIFMap = temp; 264 265 for (int i = 0; i < IndexIFMap.length; i++) { 266 int[] temp1 = new int[IFcounter + 1]; 267 System.arraycopy(IndexIFMap[i], 0, temp1, 0, IndexIFMap[i].length); 268 IndexIFMap[i] = temp1; 269 } 270 271 for (int i = 0; i < PathIndexIFMap.length; i++) { 272 int[] temp1 = new int[IFcounter + 1]; 273 System.arraycopy(PathIndexIFMap[i], 0, temp1, 0, 274 PathIndexIFMap[i].length); 275 PathIndexIFMap[i] = temp1; 276 } 277 } 278 279 StatementIFMap[IFcounter] = name; 280 281 for (int i = 0; i < IndexIFMap.length; i++) { 282 IndexIFMap[i][IFcounter] = 0; 283 } 284 285 for (int i = 0; i < PathIndexIFMap.length; i++) { 286 PathIndexIFMap[i][IFcounter] = 0; 287 } 288 289 IFcounter++; 290 } 291 292 return IFcounter - 1; 294 } 295 296 public static int addMethod(String name) { 297 if (StatementMethodMap.length <= Methodcounter) { 298 String [] temp = new String [Methodcounter + 1]; 299 System.arraycopy(StatementMethodMap, 0, temp, 0, 300 StatementMethodMap.length); 301 StatementMethodMap = temp; 302 303 for (int i = 0; i < IndexIFMap.length; i++) { 304 int[] temp1 = new int[Methodcounter + 1]; 305 System.arraycopy(IndexMethodMap[i], 0, temp1, 0, 306 IndexMethodMap[i].length); 307 IndexMethodMap[i] = temp1; 308 } 309 } 310 311 StatementMethodMap[Methodcounter] = name; 312 313 for (int i = 0; i < IndexIFMap.length; i++) { 314 IndexMethodMap[i][Methodcounter] = 0; 315 } 316 317 Methodcounter++; 318 319 return Methodcounter - 1; 320 } 321 322 public static void addThread(int thread) { 323 if (calcBranchCoverage || branchCoverage || isBranchHeuristic) { 324 if (IndexIFMap.length <= thread) { 325 int[][] temp = new int[thread + 1][]; 326 System.arraycopy(IndexIFMap, 0, temp, 0, IndexIFMap.length); 327 IndexIFMap = temp; 328 } 329 330 if (IndexIFMap[thread] == null) { 331 IndexIFMap[thread] = new int[IFcounter]; 332 } 333 334 if (IndexMethodMap.length <= thread) { 335 int[][] temp = new int[thread + 1][]; 336 System.arraycopy(IndexMethodMap, 0, temp, 0, IndexMethodMap.length); 337 IndexMethodMap = temp; 338 } 339 340 if (IndexMethodMap[thread] == null) { 341 IndexMethodMap[thread] = new int[Methodcounter]; 342 } 343 344 if (pathCoverage || isPathHeuristic) { 345 if (PathIndexIFMap.length <= thread) { 346 int[][] temp = new int[thread + 1][]; 347 System.arraycopy(PathIndexIFMap, 0, temp, 0, PathIndexIFMap.length); 348 PathIndexIFMap = temp; 349 } 350 351 if (PathIndexIFMap[thread] == null) { 352 PathIndexIFMap[thread] = new int[IFcounter]; 353 } 354 } 355 } 356 } 357 358 public static void decIFBranch(int th, int index) { 359 IndexIFMap[th][index]--; 360 } 361 362 public static void decMethod(int th, int index) { 363 IndexMethodMap[th][index]--; 364 } 365 366 public static void incIFBranch(int th, int index) { 367 if (calcBranchCoverage || branchCoverage) { 368 if (pathCoverage) { 369 377 lastIncremented = PathIndexIFMap[th][index]; 378 PathIndexIFMap[th][index]++; 379 } 380 381 lastIncrementedGlobal = IndexIFMap[th][index]; 382 383 IndexIFMap[th][index]++; 384 } 385 } 386 387 public static void incInstruction(Instruction pc, int index) { 388 if (insnCoverage) { 389 390 Object old = InstructionMap.get(pc); 391 Object oldPath = PathInstructionMap.get(pc); 392 393 if (old == null) { 394 lastInstructionGlobal = 0; 395 396 int[] threadMap = new int[index + 1]; 397 threadMap[index] = 1; 398 InstructionMap.put(pc, threadMap); 399 } else { 400 int[] threadMap = (int[]) old; 401 402 if (threadMap.length <= index) { 403 int[] temp = new int[index + 1]; 404 System.arraycopy(threadMap, 0, temp, 0, threadMap.length); 405 threadMap = temp; 406 } 407 408 lastInstructionGlobal = threadMap[index]; 409 threadMap[index]++; 410 InstructionMap.put(pc, threadMap); 411 } 412 413 if (pathCoverage) { 414 if (oldPath == null) { 415 lastInstruction = 0; 416 417 int[] threadMap = new int[index + 1]; 418 threadMap[index] = 1; 419 PathInstructionMap.put(pc, threadMap); 420 } else { 421 int[] threadMap = (int[]) oldPath; 422 423 if (threadMap.length <= index) { 424 int[] temp = new int[index + 1]; 425 System.arraycopy(threadMap, 0, temp, 0, threadMap.length); 426 threadMap = temp; 427 } 428 429 lastInstruction = threadMap[index]; 430 threadMap[index]++; 431 PathInstructionMap.put(pc, threadMap); 432 } 433 } 434 } 435 } 436 437 public static void incMethod(int th, int index) { 438 IndexMethodMap[th][index]++; 439 } 440 441 public static void printResults() { 442 if (branchCoverage) { 443 CoverageManager.printIF(); 444 CoverageManager.printCoverageIF(); 445 } 446 } 447 448 public static void printCoverageIF() { 449 int[] sum = new int[IndexIFMap.length]; 450 int total_sum = 0; 451 int length = IndexIFMap[0].length; int[] results = new int[length]; 455 for (int k = 0; k < length; k++) { 456 results[k] = 0; 457 } 458 459 for (int j = 0; j < IndexIFMap.length; j++) { 460 sum[j] = 0; 461 462 for (int i = 1; i < IndexIFMap[j].length; i++) { results[i] += IndexIFMap[j][i]; 465 466 if (IndexIFMap[j][i] != 0) { 467 sum[j]++; 468 } 469 } 470 471 if ((IndexIFMap[j].length - 1) == 0) { 472 System.out.println("Thread " + j + " Branch Coverage 0%"); 473 } else { 474 System.out.println("Thread " + j + " Branch Coverage " 475 + ((100 * sum[j]) / (IndexIFMap[j].length - 1)) + "% (" + sum[j] 476 + "/" + (IndexIFMap[j].length - 1) + ")"); 477 } 478 } 479 480 for (int l = 0; l < length; l++) { 481 if (results[l] != 0) { 482 total_sum++; 483 } 484 } 485 486 if ((length - 1) == 0) { 487 System.out.println("Total Branch Coverage 0%"); 488 } else { 489 System.out.println("Total Branch Coverage " 490 + ((100 * total_sum) / (length - 1)) + "% (" + total_sum + "/" 491 + (length - 1) + ")"); 492 } 493 } 494 495 public static void printIF() { 496 for (int j = 0; j < IndexIFMap.length; j++) { 497 for (int i = 1; i < IndexIFMap[j].length; i++) { System.out.println("Thread " + j + ":" + StatementIFMap[i] + ": " 500 + IndexIFMap[j][i]); 501 } 502 } 503 } 504 505 public static void printPathCoverageIF() { 506 int[] sum = new int[PathIndexIFMap.length]; 507 int total_sum = 0; 508 int length = PathIndexIFMap[0].length; int[] results = new int[length]; 512 for (int k = 0; k < length; k++) { 513 results[k] = 0; 514 } 515 516 for (int j = 0; j < PathIndexIFMap.length; j++) { 517 sum[j] = 0; 518 519 for (int i = 1; i < PathIndexIFMap[j].length; i++) { results[i] += PathIndexIFMap[j][i]; 522 523 if (PathIndexIFMap[j][i] != 0) { 524 sum[j]++; 525 } 526 } 527 528 if ((PathIndexIFMap[j].length - 1) == 0) { 529 System.out.println("Thread " + j + " Branch Coverage 0%"); 530 } else { 531 System.out.println("Thread " + j + " Branch Coverage " 532 + ((100 * sum[j]) / (PathIndexIFMap[j].length - 1)) + "% (" 533 + sum[j] + "/" + (PathIndexIFMap[j].length - 1) + ")"); 534 } 535 } 536 537 for (int l = 0; l < length; l++) { 538 if (results[l] != 0) { 539 total_sum++; 540 } 541 } 542 543 if ((length - 1) == 0) { 544 System.out.println("Total Branch Coverage 0%"); 545 } else { 546 System.out.println("Total Branch Coverage " 547 + ((100 * total_sum) / (length - 1)) + "% (" + total_sum + "/" 548 + (length - 1) + ")"); 549 } 550 } 551 } | Popular Tags |