[LcovMerger] Make int fields of SourceFileCoverage be dynamically computed.
RELNOTES: None.
PiperOrigin-RevId: 203264765
diff --git a/src/test/shell/bazel/bazel_coverage_test.sh b/src/test/shell/bazel/bazel_coverage_test.sh
index 17f1f7f..41a51a6 100755
--- a/src/test/shell/bazel/bazel_coverage_test.sh
+++ b/src/test/shell/bazel/bazel_coverage_test.sh
@@ -209,21 +209,24 @@
FN:6,com/example/Collatz::getCollatzFinal (I)I
FNDA:0,com/example/Collatz::<init> ()V
FNDA:1,com/example/Collatz::getCollatzFinal (I)I
-FNF:0
-FNH:0
+FNF:2
+FNH:1
BA:6,2
BA:9,2
+BRF:2
+BRH:2
DA:3,0
DA:6,3
DA:7,2
DA:9,4
DA:10,5
DA:12,7
-LH:0
-LF:0
+LH:5
+LF:6
end_of_record
EOF
+ diff result.dat "$coverage_file_path" >> $TEST_log
if ! cmp result.dat $coverage_file_path; then
fail "Coverage output file is different with expected"
fi
@@ -302,21 +305,23 @@
FN:6,com/example/Collatz::getCollatzFinal (I)I
FNDA:0,com/example/Collatz::<init> ()V
FNDA:1,com/example/Collatz::getCollatzFinal (I)I
-FNF:0
-FNH:0
+FNF:2
+FNH:1
BA:6,2
BA:9,2
+BRF:2
+BRH:2
DA:3,0
DA:6,3
DA:7,2
DA:9,4
DA:10,5
DA:12,7
-LH:0
-LF:0
+LH:5
+LF:6
end_of_record
EOF
-
+ diff result.dat "$coverage_file_path" >> $TEST_log
cmp result.dat "$coverage_file_path" || fail "Coverage output file is different than the expected file"
}
diff --git a/tools/test/LcovMerger/java/com/google/devtools/lcovmerger/LcovParser.java b/tools/test/LcovMerger/java/com/google/devtools/lcovmerger/LcovParser.java
index a5cd7a0..b441b87 100644
--- a/tools/test/LcovMerger/java/com/google/devtools/lcovmerger/LcovParser.java
+++ b/tools/test/LcovMerger/java/com/google/devtools/lcovmerger/LcovParser.java
@@ -208,7 +208,7 @@
}
try {
int nrFunctionsFound = Integer.parseInt(lineContent);
- currentSourceFileCoverage.nrFunctionsFound(nrFunctionsFound);
+ assert currentSourceFileCoverage.nrFunctionsFound() == nrFunctionsFound;
} catch (NumberFormatException e) {
logger.log(Level.WARNING,
"Tracefile contains invalid number of functions on FNF line " + line);
@@ -226,7 +226,7 @@
}
try {
int nrFunctionsHit = Integer.parseInt(lineContent);
- currentSourceFileCoverage.nrFunctionsHit(nrFunctionsHit);
+ assert currentSourceFileCoverage.nrFunctionsHit() == nrFunctionsHit;
} catch (NumberFormatException e) {
logger.log(Level.WARNING,
"Tracefile contains invalid number of functions hit on FNH line " + line);
@@ -314,7 +314,7 @@
}
try {
int nrBranchesFound = Integer.parseInt(lineContent);
- currentSourceFileCoverage.nrBranchesFound(nrBranchesFound);
+ assert currentSourceFileCoverage.nrBranchesFound() == nrBranchesFound;
} catch (NumberFormatException e) {
logger.log(Level.WARNING,
"Tracefile contains invalid number of branches in BRDA line " + line);
@@ -332,7 +332,7 @@
}
try {
int nrBranchesHit = Integer.parseInt(lineContent);
- currentSourceFileCoverage.nrBranchesHit(nrBranchesHit);
+ assert currentSourceFileCoverage.nrBranchesHit() == nrBranchesHit;
} catch (NumberFormatException e) {
logger.log(Level.WARNING,
"Tracefile contains invalid number of branches hit in BRH line " + line);
@@ -382,7 +382,7 @@
}
try {
int nrLines = Integer.parseInt(lineContent);
- currentSourceFileCoverage.nrOfLinesWithNonZeroExecution(nrLines);
+ assert currentSourceFileCoverage.nrOfLinesWithNonZeroExecution() == nrLines;
} catch (NumberFormatException e) {
logger.log(Level.WARNING, "Tracefile contains an invalid number on LHL line " + line);
return false;
@@ -399,7 +399,7 @@
}
try {
int nrLines = Integer.parseInt(lineContent);
- currentSourceFileCoverage.nrOfInstrumentedLines(nrLines);
+ assert currentSourceFileCoverage.nrOfInstrumentedLines() == nrLines;
} catch (NumberFormatException e) {
logger.log(Level.WARNING, "Tracefile contains an invalid number on LF line " + line);
return false;
diff --git a/tools/test/LcovMerger/java/com/google/devtools/lcovmerger/SourceFileCoverage.java b/tools/test/LcovMerger/java/com/google/devtools/lcovmerger/SourceFileCoverage.java
index 3e293a7..0cdb1a6 100644
--- a/tools/test/LcovMerger/java/com/google/devtools/lcovmerger/SourceFileCoverage.java
+++ b/tools/test/LcovMerger/java/com/google/devtools/lcovmerger/SourceFileCoverage.java
@@ -34,13 +34,6 @@
private final TreeMap<Integer, BranchCoverage> branches; // line number to branch
private final TreeMap<Integer, LineCoverage> lines; // line number to line execution
- private int nrFunctionsFound;
- private int nrFunctionsHit;
- private int nrBranchesFound;
- private int nrBranchesHit;
- private int nrOfLinesWithNonZeroExecution;
- private int nrOfInstrumentedLines;
-
SourceFileCoverage(String sourcefile) {
this.sourceFileName = sourcefile;
this.functionsExecution = new TreeMap<>();
@@ -61,13 +54,6 @@
this.functionsExecution.putAll(other.functionsExecution);
this.branches.putAll(other.branches);
this.lines.putAll(other.lines);
-
- this.nrFunctionsFound = other.nrFunctionsFound;
- this.nrFunctionsHit = other.nrFunctionsHit;
- this.nrBranchesFound = other.nrBranchesFound;
- this.nrBranchesHit = other.nrBranchesHit;
- this.nrOfLinesWithNonZeroExecution = other.nrOfLinesWithNonZeroExecution;
- this.nrOfInstrumentedLines = other.nrOfInstrumentedLines;
}
/*
@@ -169,13 +155,6 @@
merged.addAllFunctionsExecution(mergeFunctionsExecution(source1, source2));
merged.addAllBranches(mergeBranches(source1, source2));
merged.addAllLines(mergeLines(source1, source2));
-
- merged.nrBranchesHit(getNumberOfBranchesHit(merged));
- merged.nrOfLinesWithNonZeroExecution(getNumberOfExecutedLines(merged));
- merged.nrFunctionsFound(merged.lineNumbers.size());
- merged.nrFunctionsHit(merged.functionsExecution.size());
- merged.nrBranchesFound(merged.branches.size());
- merged.nrOfInstrumentedLines(merged.lines.size());
return merged;
}
@@ -184,51 +163,29 @@
}
int nrFunctionsFound() {
- return nrFunctionsFound;
- }
-
- void nrFunctionsFound(int nrFunctionsFound) {
- this.nrFunctionsFound = nrFunctionsFound;
+ return functionsExecution.size();
}
int nrFunctionsHit() {
- return nrFunctionsHit;
- }
-
- void nrFunctionsHit(int nrFunctionsHit) {
- this.nrFunctionsHit = nrFunctionsHit;
+ return (int) functionsExecution.entrySet().stream()
+ .filter(function -> function.getValue() > 0)
+ .count();
}
int nrBranchesFound() {
- return nrBranchesFound;
- }
-
- void nrBranchesFound(int nrBranchesFound) {
- this.nrBranchesFound = nrBranchesFound;
+ return branches.size();
}
int nrBranchesHit() {
- return nrBranchesHit;
- }
-
- void nrBranchesHit(int nrBranchesHit) {
- this.nrBranchesHit = nrBranchesHit;
+ return getNumberOfBranchesHit(this);
}
int nrOfLinesWithNonZeroExecution() {
- return nrOfLinesWithNonZeroExecution;
- }
-
- void nrOfLinesWithNonZeroExecution(int nrOfLinesWithNonZeroExecution) {
- this.nrOfLinesWithNonZeroExecution = nrOfLinesWithNonZeroExecution;
+ return getNumberOfExecutedLines(this);
}
int nrOfInstrumentedLines() {
- return nrOfInstrumentedLines;
- }
-
- void nrOfInstrumentedLines(int nrOfInstrumentedLines) {
- this.nrOfInstrumentedLines = nrOfInstrumentedLines;
+ return this.lines.size();
}
Collection<LineCoverage> getAllLineExecution() {
diff --git a/tools/test/LcovMerger/javatests/com/google/devtools/lcovmerger/LcovMergerTestUtils.java b/tools/test/LcovMerger/javatests/com/google/devtools/lcovmerger/LcovMergerTestUtils.java
index abaed31..eac604a 100644
--- a/tools/test/LcovMerger/javatests/com/google/devtools/lcovmerger/LcovMergerTestUtils.java
+++ b/tools/test/LcovMerger/javatests/com/google/devtools/lcovmerger/LcovMergerTestUtils.java
@@ -244,9 +244,6 @@
sourceFile.addLineNumber(FUNC_3, FUNC_3_LINE_NR);
sourceFile.addFunctionExecution(FUNC_3, FUNC_3_NR_EXECUTED_LINES_TRACEFILE1);
- sourceFile.nrFunctionsFound(NR_FUNCTIONS_FOUND);
- sourceFile.nrFunctionsHit(NR_FUNCTIONS_HIT_TRACEFILE1);
-
for (int line = FUNC_1_LINE_NR; line < MAX_LINES_IN_FILE; line++) {
if (lineExecutionCount[line] >= 0) {
@@ -256,9 +253,6 @@
}
}
- sourceFile.nrOfLinesWithNonZeroExecution(NR_LINES_HIT_TRACEFILE1);
- sourceFile.nrOfInstrumentedLines(NR_LINES_FOUND);
-
return sourceFile;
}
@@ -275,10 +269,6 @@
sourceFileCoverage.addLineNumber(FUNC_3, FUNC_3_LINE_NR);
sourceFileCoverage.addFunctionExecution(FUNC_3, FUNC_3_NR_EXECUTED_LINES_TRACEFILE2);
- sourceFileCoverage.nrFunctionsFound(NR_FUNCTIONS_FOUND);
- sourceFileCoverage.nrFunctionsHit(NR_FUNCTIONS_HIT_TRACEFILE2);
-
-
for (int line = FUNC_1_LINE_NR; line < MAX_LINES_IN_FILE; line++) {
if (lineExecutionCount[line] >= 0) {
sourceFileCoverage.addLine(
@@ -286,10 +276,6 @@
line, lineExecutionCount[line], null));
}
}
-
- sourceFileCoverage.nrOfLinesWithNonZeroExecution(NR_LINES_HIT_TRACEFILE2);
- sourceFileCoverage.nrOfInstrumentedLines(NR_LINES_FOUND);
-
return sourceFileCoverage;
}