You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2019/05/16 19:10:29 UTC
[commons-rng] 07/08: Add option to replace the file prefix in the
APT report.
This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-rng.git
commit fa9e3d5d3140a1d9292a681776397abb45cd5725
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Thu May 16 18:27:18 2019 +0100
Add option to replace the file prefix in the APT report.
---
.../rng/examples/stress/ResultsCommand.java | 85 ++++++++++++++++++----
1 file changed, 70 insertions(+), 15 deletions(-)
diff --git a/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/ResultsCommand.java b/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/ResultsCommand.java
index a884b20..ccee7bb 100644
--- a/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/ResultsCommand.java
+++ b/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/ResultsCommand.java
@@ -42,6 +42,7 @@ import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Callable;
+import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@@ -111,6 +112,13 @@ class ResultsCommand implements Callable<Void> {
description = "Include Dieharder sums test.")
private boolean includeDiehardSums;
+ /** The common file path prefix used when outputting file paths. */
+ @Option(names = {"--path-prefix"},
+ description = {"Common path prefix.",
+ "If specified this will replace the common prefix from all " +
+ "files when the path is output, e.g. for the APT report."})
+ private String pathPrefix = "";
+
/**
* The output mode for the results.
*/
@@ -275,18 +283,6 @@ class ResultsCommand implements Callable<Void> {
void setComplete(boolean complete) {
this.complete = complete;
}
-
- /**
- * Return the Almost-Plain-Text (APT) string representation of this result. This
- * is a link for the relative path to the result file and the failure count.
- *
- * @return the string
- */
- String toAPTString() {
- final String text = "{{{" + getResultFile().getPath() + "}" + getFailureCountString() + "}}";
- // Convert to web-link name separators
- return text.replace('\\', '/');
- }
}
/**
@@ -655,14 +651,36 @@ class ResultsCommand implements Callable<Void> {
* @param results Results.
* @throws IOException Signals that an I/O exception has occurred.
*/
- private static void writeAPT(OutputStream out,
- List<TestResult> results) throws IOException {
+ private void writeAPT(OutputStream out,
+ List<TestResult> results) throws IOException {
// Identify all:
// RandomSources, bit-reversed, test names,
final List<RandomSource> randomSources = getRandomSources(results);
final List<Boolean> bitReversed = getBitReversed(results);
final List<String> testNames = getTestNames(results);
+ // Identify the common path prefix to be replaced
+ final int prefixLength = (pathPrefix.isEmpty()) ? 0 : findCommonPathPrefixLength(results);
+
+ // Create a function to update the file path and then output the failure count
+ // as a link to the file using the APT format.
+ final Function<TestResult, String> toAPTString = result -> {
+ String path = result.getResultFile().getPath();
+ // Remove common path prefix
+ path = path.substring(prefixLength);
+ // Build the APT relative link
+ final StringBuilder sb = new StringBuilder()
+ .append("{{{").append(pathPrefix).append(path).append('}')
+ .append(result.getFailureCountString()).append("}}");
+ // Convert to web-link name separators
+ for (int i = 0; i < sb.length(); i++) {
+ if (sb.charAt(i) == '\\') {
+ sb.setCharAt(i, '/');
+ }
+ }
+ return sb.toString();
+ };
+
// Create columns for RandomSource, bit-reversed, each test name.
// Make bit-reversed column optional if no generators are bit reversed.
final boolean showBitReversedColumn = bitReversed.contains(Boolean.TRUE);
@@ -688,7 +706,7 @@ class ResultsCommand implements Callable<Void> {
for (String testName : testNames) {
final List<TestResult> testResults = getTestResults(results, randomSource, reversed, testName);
writeAPTColumn(output, testResults.stream()
- .map(TestResult::toAPTString)
+ .map(toAPTString)
.collect(Collectors.joining(", ")));
}
output.newLine();
@@ -756,6 +774,43 @@ class ResultsCommand implements Callable<Void> {
}
/**
+ * Find the common path prefix for all result files. This is returned as the length of the
+ * common prefix.
+ *
+ * @param results Results.
+ * @return the length
+ */
+ private static int findCommonPathPrefixLength(List<TestResult> results) {
+ if (results.isEmpty()) {
+ return 0;
+ }
+ // Find the first prefix
+ final String prefix1 = getPathPrefix(results.get(0));
+ int length = prefix1.length();
+ for (int i = 1; i < results.size() && length != 0; i++) {
+ final String prefix2 = getPathPrefix(results.get(i));
+ // Update
+ final int size = Math.min(prefix2.length(), length);
+ length = 0;
+ while (length < size && prefix1.charAt(length) == prefix2.charAt(length)) {
+ length++;
+ }
+ }
+ return length;
+ }
+
+ /**
+ * Gets the path prefix.
+ *
+ * @param testResult Test result.
+ * @return the path prefix (or the empty string)
+ */
+ private static String getPathPrefix(TestResult testResult) {
+ final String parent = testResult.getResultFile().getParent();
+ return parent != null ? parent : "";
+ }
+
+ /**
* Creates the APT header.
*
* @param showBitReversedColumn Set to true to the show bit reversed column.