001: /*
002: * Cobertura - http://cobertura.sourceforge.net/
003: *
004: * Copyright (C) 2006 Jiri Mares
005: *
006: * Cobertura is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published
008: * by the Free Software Foundation; either version 2 of the License,
009: * or (at your option) any later version.
010: *
011: * Cobertura is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with Cobertura; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: */
021:
022: package net.sourceforge.cobertura.coveragedata;
023:
024: import java.io.Serializable;
025: import java.util.Arrays;
026:
027: /**
028: * <p>
029: * This class implements HasBeenInstrumented so that when cobertura instruments
030: * itself, it will omit this class. It does this to avoid an infinite recursion
031: * problem because instrumented classes make use of this class.
032: * </p>
033: */
034: public class SwitchData implements BranchCoverageData, Comparable,
035: Serializable, HasBeenInstrumented {
036: private static final long serialVersionUID = 9;
037:
038: private int switchNumber;
039:
040: private long defaultHits;
041:
042: private long[] hits;
043:
044: private int[] keys;
045:
046: public SwitchData(int switchNumber, int[] keys) {
047: super ();
048: this .switchNumber = switchNumber;
049: defaultHits = 0;
050: hits = new long[keys.length];
051: Arrays.fill(hits, 0);
052: this .keys = new int[keys.length];
053: System.arraycopy(keys, 0, this .keys, 0, keys.length);
054: }
055:
056: public SwitchData(int switchNumber, int min, int max) {
057: super ();
058: this .switchNumber = switchNumber;
059: defaultHits = 0;
060: hits = new long[max - min + 1];
061: Arrays.fill(hits, 0);
062: this .keys = new int[max - min + 1];
063: for (int i = 0; min <= max; keys[i++] = min++)
064: ;
065: }
066:
067: public SwitchData(int switchNumber) {
068: this (switchNumber, new int[0]);
069: }
070:
071: public int compareTo(Object o) {
072: if (!o.getClass().equals(SwitchData.class))
073: return Integer.MAX_VALUE;
074: return this .switchNumber - ((SwitchData) o).switchNumber;
075: }
076:
077: void touchBranch(int branch) {
078: if (branch == -1)
079: defaultHits++;
080: else {
081: if (hits.length <= branch) {
082: long[] old = hits;
083: hits = new long[branch + 1];
084: System.arraycopy(old, 0, hits, 0, old.length);
085: Arrays.fill(hits, old.length, hits.length - 1, 0);
086: }
087: hits[branch]++;
088: }
089: }
090:
091: public int getSwitchNumber() {
092: return this .switchNumber;
093: }
094:
095: public long getHits(int branch) {
096: if (hits.length > branch)
097: return hits[branch];
098: return -1;
099: }
100:
101: public long getDefaultHits() {
102: return defaultHits;
103: }
104:
105: public double getBranchCoverageRate() {
106: int branches = hits.length + 1;
107: int hit = (defaultHits > 0) ? 1 : 0;
108: for (int i = hits.length - 1; i >= 0; hit += ((hits[i--] > 0) ? 1
109: : 0))
110: ;
111: return ((double) hit) / branches;
112: }
113:
114: public boolean equals(Object obj) {
115: if (this == obj)
116: return true;
117: if ((obj == null) || !(obj.getClass().equals(this .getClass())))
118: return false;
119:
120: SwitchData switchData = (SwitchData) obj;
121: return (this .defaultHits == switchData.defaultHits)
122: && (Arrays.equals(this .hits, switchData.hits))
123: && (this .switchNumber == switchData.switchNumber);
124: }
125:
126: public int hashCode() {
127: return this .switchNumber;
128: }
129:
130: public int getNumberOfCoveredBranches() {
131: int ret = (defaultHits > 0) ? 1 : 0;
132: for (int i = hits.length - 1; i >= 0; i--) {
133: if (hits[i] > 0)
134: ret++;
135: }
136: return ret;
137: }
138:
139: public int getNumberOfValidBranches() {
140: return hits.length + 1;
141: }
142:
143: public void merge(BranchCoverageData coverageData) {
144: SwitchData switchData = (SwitchData) coverageData;
145: defaultHits += switchData.defaultHits;
146: for (int i = Math.min(hits.length, switchData.hits.length) - 1; i >= 0; i--)
147: hits[i] += switchData.hits[i];
148: if (switchData.hits.length > hits.length) {
149: long[] old = hits;
150: hits = new long[switchData.hits.length];
151: System.arraycopy(old, 0, hits, 0, old.length);
152: System.arraycopy(switchData.hits, old.length, hits,
153: old.length, hits.length - old.length);
154: }
155: if ((this .keys.length == 0) && (switchData.keys.length > 0))
156: this.keys = switchData.keys;
157: }
158:
159: }
|