Skip to content

Commit

Permalink
Small change in Statistics display
Browse files Browse the repository at this point in the history
  • Loading branch information
jlink committed Apr 13, 2019
1 parent ea885d9 commit 045352d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
10 changes: 0 additions & 10 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,6 @@
Fail test (instead of ignore it)
https://github.com/jlink/jqwik/issues/54

- Shrink correctly: (Maybe Integer.MAX_VALUE/2, 1)
@Property
boolean sumOfTwoIntegersAlwaysGreaterThanEach(
@ForAll @Positive int positive1, //
@ForAll @Positive int positive2
) {
int sum = positive1 + positive2;
return sum > positive1 && sum > positive2;
}

- Case-based branching with statistical reporting:

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,40 @@ public Map<List<Object>, Integer> getCounts() {
public ReportEntry createReportEntry(String propertyName) {
StringBuilder statistics = new StringBuilder();
int sum = counts.values().stream().mapToInt(aCount -> aCount).sum();
List<StatisticsEntry> statisticsEntries = counts.entrySet().stream() //
.sorted((e1, e2) -> e2.getValue().compareTo(e1.getValue())) //
.filter(entry -> !entry.getKey().equals(Collections.emptyList())) //
.map(entry -> new StatisticsEntry(displayKey(entry.getKey()), entry.getValue() * 100.0 / sum)) //
.collect(Collectors.toList());
List<StatisticsEntry> statisticsEntries =
counts.entrySet().stream()
.sorted(this::compareStatisticsEntries)
.filter(entry -> !entry.getKey().equals(Collections.emptyList()))
.map(entry -> new StatisticsEntry(displayKey(entry.getKey()), entry.getValue() * 100.0 / sum))
.collect(Collectors.toList());
int maxKeyLength = statisticsEntries.stream().mapToInt(entry -> entry.name.length()).max().orElse(0);
boolean fullNumbersOnly = !statisticsEntries.stream().anyMatch(entry -> entry.percentage < 1);

statisticsEntries.stream() //
.forEach(statsEntry -> {
statistics.append(String.format("%n %1$-" + maxKeyLength + "s : %2$s %%", //
statsEntry.name, //
displayPercentage(statsEntry.percentage, fullNumbersOnly)));
});
for (StatisticsEntry statsEntry : statisticsEntries) {
statistics.append(formatEntry(statsEntry, maxKeyLength, fullNumbersOnly));
}

String keyStatistics = String.format("%s for [%s]", KEY_STATISTICS, propertyName);
return ReportEntry.from(keyStatistics, statistics.toString());
}

private String formatEntry(StatisticsEntry statsEntry, int maxKeyLength, boolean fullNumbersOnly) {
return String.format(
"%n %1$-" + maxKeyLength + "s : %2$s %%",
statsEntry.name,
displayPercentage(statsEntry.percentage, fullNumbersOnly)
);
}

private int compareStatisticsEntries(Map.Entry<List<Object>, Integer> e1, Map.Entry<List<Object>, Integer> e2) {
List<Object> k1 = e1.getKey();
List<Object> k2 = e2.getKey();
if (k1.size() != k2.size()) {
return Integer.compare(k1.size(), k2.size());
}
return e2.getValue().compareTo(e1.getValue());
}

private String displayPercentage(double percentage, boolean fullNumbersOnly) {
if (fullNumbersOnly)
return String.valueOf(Math.round(percentage));
Expand All @@ -71,8 +87,8 @@ public void collect(Object... values) {
List<Object> key = Collections.emptyList();
if (values != null) {
key = Arrays.stream(values) //
.filter(Objects::nonNull) //
.collect(Collectors.toList());
.filter(Objects::nonNull) //
.collect(Collectors.toList());
}

int count = counts.computeIfAbsent(key, any -> 0);
Expand Down

0 comments on commit 045352d

Please sign in to comment.