Yue Gan | af3c412 | 2016-12-05 14:36:02 +0000 | [diff] [blame] | 1 | // Copyright 2016 The Bazel Authors. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.testing.coverage; |
| 16 | |
| 17 | import java.util.Map; |
| 18 | import java.util.Set; |
| 19 | import java.util.TreeMap; |
| 20 | import java.util.TreeSet; |
| 21 | |
| 22 | /** Details of branch coverage information. */ |
| 23 | public class BranchCoverageDetail { |
| 24 | private final Map<Integer, BitField> branchTaken; |
| 25 | private final Map<Integer, Integer> branches; |
| 26 | |
| 27 | public BranchCoverageDetail() { |
| 28 | branchTaken = new TreeMap<Integer, BitField>(); |
| 29 | branches = new TreeMap<Integer, Integer>(); |
| 30 | } |
| 31 | |
| 32 | private BitField getBranchForLine(int line) { |
| 33 | BitField value = branchTaken.get(line); |
| 34 | if (value != null) { |
| 35 | return value; |
| 36 | } |
| 37 | value = new BitField(); |
| 38 | branchTaken.put(line, value); |
| 39 | return value; |
| 40 | } |
| 41 | |
| 42 | /** Returns true if the line has branches. */ |
| 43 | public boolean hasBranches(int line) { |
| 44 | return branches.containsKey(line); |
| 45 | } |
| 46 | |
| 47 | /** Sets the number of branches entry. */ |
| 48 | public void setBranches(int line, int n) { |
| 49 | branches.put(line, n); |
| 50 | } |
| 51 | |
| 52 | /** Gets the number of branches in the line, returns 0 if there is no branch. */ |
| 53 | public int getBranches(int line) { |
| 54 | Integer value = branches.get(line); |
| 55 | if (value == null) { |
| 56 | return 0; |
| 57 | } |
| 58 | return value; |
| 59 | } |
| 60 | |
| 61 | /** Sets the taken bit of the given line for the given branch index. */ |
| 62 | public void setTakenBit(int line, int branchIdx) { |
| 63 | getBranchForLine(line).setBit(branchIdx); |
| 64 | } |
| 65 | |
| 66 | public boolean getTakenBit(int line, int branchIdx) { |
| 67 | return getBranchForLine(line).isBitSet(branchIdx); |
| 68 | } |
| 69 | |
| 70 | /** Calculate executed bit using heuristics. */ |
| 71 | public boolean getExecutedBit(int line) { |
| 72 | // If any of the branch is taken, the branch must have executed. Otherwise assume it is not. |
| 73 | return getBranchForLine(line).any(); |
| 74 | } |
| 75 | |
| 76 | /** Returns line numbers where more than one branch is present. */ |
| 77 | public Set<Integer> linesWithBranches() { |
| 78 | Set<Integer> result = new TreeSet<Integer>(); |
| 79 | for (int i : branches.keySet()) { |
| 80 | if (branches.get(i) > 1) { |
| 81 | result.add(i); |
| 82 | } |
| 83 | } |
| 84 | return result; |
| 85 | } |
| 86 | } |