Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.75 KB | None | 0 0
  1. package games.stendhal.tools.statistics;
  2.  
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.io.PrintWriter;
  7. import java.nio.file.Files;
  8. import java.nio.file.Paths;
  9. import java.util.Arrays;
  10. import java.util.List;
  11.  
  12. import static java.lang.Math.ceil;
  13.  
  14. /**
  15.  * Stores coverage data
  16.  *
  17.  * @author Pihlqvist
  18.  */
  19.  
  20. public class AdHocCoverage {
  21.  
  22.  
  23.     private final String PATH_NAME = "F:\\kth\\reports\\";
  24.     private String FILE_NAME;
  25.  
  26.     private int[] branches;
  27.  
  28.     /**
  29.      * Create a AdHocCoverage object in preparation to store coverage data
  30.      * @param method name of method
  31.      * @param branchAmount total amount of branches in method
  32.      */
  33.     public AdHocCoverage(String method, int branchAmount) {
  34.         FILE_NAME = PATH_NAME + method + ".txt";
  35.         branches = new int[branchAmount];
  36.         Arrays.fill(branches, 0);
  37.  
  38.         try {
  39.             readFile();
  40.         } catch (IOException e) {
  41.             System.err.println(e);
  42.         }
  43.     }
  44.  
  45.     /**
  46.      * Invoked when a branch has been reached, the class will store the id
  47.      *
  48.      * @param branchID
  49.      */
  50.     public void branchReached(int branchID) {
  51.         assert(branchID < branches.length);
  52.         branches[branchID] += 1;
  53.  
  54.         try {
  55.             writeFile();
  56.         } catch (IOException e) {
  57.             System.err.println(e);
  58.         }
  59.     }
  60.  
  61.     /**
  62.      * Calculates the percentile coverage of branches as a float.
  63.      *
  64.      * @return percentile of covered branches
  65.      */
  66.     private double calculateCoverage() {
  67.         int reached = 0;
  68.         for (int i = 2; i < branches.length; i++) {
  69.             if (branches[i] > 0) {
  70.                 reached += 1;
  71.             }
  72.         }
  73.         double coverage = (double) reached/branches.length;
  74.         assert(coverage <= 1);
  75.  
  76.         return coverage;
  77.     }
  78.  
  79.     /**
  80.      * Reads a buffer file and stores the data in class structures
  81.      *
  82.      */
  83.     private void readFile() throws IOException {
  84.         // If file don't exist create one.
  85.         File test = new File(Paths.get(FILE_NAME).toString());
  86.         if (!test.exists()) {
  87.             PrintWriter writer = new PrintWriter(Paths.get(FILE_NAME).toString());
  88.             writer.print("Coverage: ");
  89.             writer.close();
  90.         }
  91.  
  92.         // If file exist, read data from it and store it in class array.
  93.         else {
  94.             List<String> allLines = Files.readAllLines(
  95.                     Paths.get(FILE_NAME));
  96.  
  97.             for (int i = 4; i < allLines.size(); i++) {
  98.                 assert (Integer.valueOf(allLines.get(i)) <= branches.length);
  99.                 branches[Integer.valueOf(allLines.get(i))] += 1;
  100.             }
  101.         }
  102.     }
  103.  
  104.     /**
  105.      * Store data from class structs to a file
  106.      *
  107.      */
  108.     private void writeFile() throws IOException {
  109.         FileWriter fw = new FileWriter(Paths.get(FILE_NAME).toString());
  110.         double coverage = calculateCoverage();
  111.  
  112.         // Write the report summary
  113.         fw.write(String.format("Coverage: %.3f %%", coverage));
  114.         fw.write(System.lineSeparator()); //new line
  115.         fw.write(String.format("Branches: %d", branches.length));
  116.         fw.write(System.lineSeparator()); //new line
  117.         fw.write(String.format("Reached: %d", (int)ceil(branches.length*coverage)+1));
  118.         fw.write(System.lineSeparator()); //new line
  119.         fw.write("#");
  120.         fw.write(System.lineSeparator()); //new line
  121.  
  122.         // List the ID's of branches reached
  123.         for (int i = 0; i < branches.length; i++) {
  124.             if (branches[i] > 0) {
  125.                 fw.write(String.format("%d", i));
  126.                 fw.write(System.lineSeparator()); //new line
  127.             }
  128.         }
  129.         fw.close();
  130.     }
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement