| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
package net.sourceforge.cobertura.coveragedata; |
| 23 | |
|
| 24 | |
import java.io.Serializable; |
| 25 | |
import java.util.Arrays; |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
public class SwitchData implements BranchCoverageData, Comparable, Serializable, |
| 35 | |
HasBeenInstrumented |
| 36 | |
{ |
| 37 | |
private static final long serialVersionUID = 9; |
| 38 | |
|
| 39 | |
private int switchNumber; |
| 40 | |
|
| 41 | |
private long defaultHits; |
| 42 | |
|
| 43 | |
private long[] hits; |
| 44 | |
|
| 45 | |
private int[] keys; |
| 46 | |
|
| 47 | |
public SwitchData(int switchNumber, int[] keys) |
| 48 | |
{ |
| 49 | |
super(); |
| 50 | |
this.switchNumber = switchNumber; |
| 51 | |
defaultHits = 0; |
| 52 | |
hits = new long[keys.length]; |
| 53 | |
Arrays.fill(hits, 0); |
| 54 | |
this.keys = new int[keys.length]; |
| 55 | |
System.arraycopy(keys, 0, this.keys, 0, keys.length); |
| 56 | |
} |
| 57 | |
|
| 58 | |
public SwitchData(int switchNumber, int min, int max) |
| 59 | |
{ |
| 60 | |
super(); |
| 61 | |
this.switchNumber = switchNumber; |
| 62 | |
defaultHits = 0; |
| 63 | |
hits = new long[max - min + 1]; |
| 64 | |
Arrays.fill(hits, 0); |
| 65 | |
this.keys = new int[max - min + 1]; |
| 66 | |
for (int i = 0; min <= max; keys[i++] = min++); |
| 67 | |
} |
| 68 | |
|
| 69 | |
public SwitchData(int switchNumber) |
| 70 | |
{ |
| 71 | |
this(switchNumber, new int[0]); |
| 72 | |
} |
| 73 | |
|
| 74 | |
public int compareTo(Object o) |
| 75 | |
{ |
| 76 | |
if (!o.getClass().equals(SwitchData.class)) |
| 77 | |
return Integer.MAX_VALUE; |
| 78 | |
return this.switchNumber - ((SwitchData) o).switchNumber; |
| 79 | |
} |
| 80 | |
|
| 81 | |
void touchBranch(int branch) |
| 82 | |
{ |
| 83 | |
if (branch == -1) |
| 84 | |
defaultHits++; |
| 85 | |
else |
| 86 | |
{ |
| 87 | |
if (hits.length <= branch) |
| 88 | |
{ |
| 89 | |
long[] old = hits; |
| 90 | |
hits = new long[branch + 1]; |
| 91 | |
System.arraycopy(old, 0, hits, 0, old.length); |
| 92 | |
Arrays.fill(hits, old.length, hits.length - 1, 0); |
| 93 | |
} |
| 94 | |
hits[branch]++; |
| 95 | |
} |
| 96 | |
} |
| 97 | |
|
| 98 | |
public int getSwitchNumber() |
| 99 | |
{ |
| 100 | |
return this.switchNumber; |
| 101 | |
} |
| 102 | |
|
| 103 | |
public long getHits(int branch) |
| 104 | |
{ |
| 105 | |
if (hits.length > branch) |
| 106 | |
return hits[branch]; |
| 107 | |
return -1; |
| 108 | |
} |
| 109 | |
|
| 110 | |
public long getDefaultHits() |
| 111 | |
{ |
| 112 | |
return defaultHits; |
| 113 | |
} |
| 114 | |
|
| 115 | |
public double getBranchCoverageRate() |
| 116 | |
{ |
| 117 | |
int branches = hits.length + 1; |
| 118 | |
int hit = (defaultHits > 0) ? 1 : 0; |
| 119 | |
for (int i = hits.length - 1; i >= 0; hit += ((hits[i--] > 0) ? 1 : 0)); |
| 120 | |
return ((double) hit) / branches; |
| 121 | |
} |
| 122 | |
|
| 123 | |
public boolean equals(Object obj) |
| 124 | |
{ |
| 125 | |
if (this == obj) |
| 126 | |
return true; |
| 127 | |
if ((obj == null) || !(obj.getClass().equals(this.getClass()))) |
| 128 | |
return false; |
| 129 | |
|
| 130 | |
SwitchData switchData = (SwitchData) obj; |
| 131 | |
return (this.defaultHits == switchData.defaultHits) |
| 132 | |
&& (Arrays.equals(this.hits, switchData.hits)) |
| 133 | |
&& (this.switchNumber == switchData.switchNumber); |
| 134 | |
} |
| 135 | |
|
| 136 | |
public int hashCode() |
| 137 | |
{ |
| 138 | |
return this.switchNumber; |
| 139 | |
} |
| 140 | |
|
| 141 | |
public int getNumberOfCoveredBranches() |
| 142 | |
{ |
| 143 | |
int ret = (defaultHits > 0) ? 1 : 0; |
| 144 | |
for (int i = hits.length -1; i >= 0;i--) |
| 145 | |
{ |
| 146 | |
if (hits[i] > 0) ret++; |
| 147 | |
} |
| 148 | |
return ret; |
| 149 | |
} |
| 150 | |
|
| 151 | |
public int getNumberOfValidBranches() |
| 152 | |
{ |
| 153 | |
return hits.length + 1; |
| 154 | |
} |
| 155 | |
|
| 156 | |
public void merge(BranchCoverageData coverageData) |
| 157 | |
{ |
| 158 | |
SwitchData switchData = (SwitchData) coverageData; |
| 159 | |
defaultHits += switchData.defaultHits; |
| 160 | |
for (int i = Math.min(hits.length, switchData.hits.length) - 1; i >= 0; i--) |
| 161 | |
hits[i] += switchData.hits[i]; |
| 162 | |
if (switchData.hits.length > hits.length) |
| 163 | |
{ |
| 164 | |
long[] old = hits; |
| 165 | |
hits = new long[switchData.hits.length]; |
| 166 | |
System.arraycopy(old, 0, hits, 0, old.length); |
| 167 | |
System.arraycopy(switchData.hits, old.length, hits, old.length, hits.length - old.length); |
| 168 | |
} |
| 169 | |
if ((this.keys.length == 0) && (switchData.keys.length > 0)) |
| 170 | |
this.keys = switchData.keys; |
| 171 | |
} |
| 172 | |
|
| 173 | |
} |