Skip to content

Commit

Permalink
tidying
Browse files Browse the repository at this point in the history
  • Loading branch information
pholser committed Nov 21, 2020
1 parent e150c73 commit 6896278
Show file tree
Hide file tree
Showing 36 changed files with 357 additions and 197 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,12 @@ public Instant nextInstant(Instant min, Instant max) {
if (comparison == 0)
return min;

long[] next = nextSecondsAndNanos(
min.getEpochSecond(),
min.getNano(),
max.getEpochSecond(),
max.getNano());
long[] next =
nextSecondsAndNanos(
min.getEpochSecond(),
min.getNano(),
max.getEpochSecond(),
max.getNano());

return Instant.ofEpochSecond(next[0], next[1]);
}
Expand All @@ -337,11 +338,12 @@ public Duration nextDuration(Duration min, Duration max) {
if (comparison == 0)
return min;

long[] next = nextSecondsAndNanos(
min.getSeconds(),
min.getNano(),
max.getSeconds(),
max.getNano());
long[] next =
nextSecondsAndNanos(
min.getSeconds(),
min.getNano(),
max.getSeconds(),
max.getNano());

return Duration.ofSeconds(next[0], next[1]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ a copy of this software and associated documentation files (the

/**
* Base class for generators of decimal types, such as {@code double} and
* {@link BigDecimal}. All numbers are converted to/from BigDecimal for processing.
* {@link BigDecimal}. All numbers are converted to/from BigDecimal for
* processing.
*
* @param <T> type of values this generator produces
*/
Expand All @@ -55,7 +56,10 @@ protected DecimalGenerator(List<Class<T>> types) {
super(types);
}

@Override public List<T> doShrink(SourceOfRandomness random, T largestGeneric) {
@Override public List<T> doShrink(
SourceOfRandomness random,
T largestGeneric) {

if (largestGeneric.equals(leastMagnitude()))
return emptyList();

Expand All @@ -72,7 +76,8 @@ protected DecimalGenerator(List<Class<T>> types) {
// Try your luck by testing the smallest possible value
results.add(leastMagnitude());

// Try values between smallest and largest, with smaller and smaller increments as we approach the largest
// Try values between smallest and largest, with smaller and smaller
// increments as we approach the largest

// Integrals are considered easier than decimals
results.addAll(shrunkenIntegrals(largest, least));
Expand Down Expand Up @@ -108,32 +113,38 @@ private List<T> decimalsFrom(Stream<BigDecimal> stream) {
}

/**
* @return a function converting a value of the base type into a {@link BigDecimal}.
* @return a function converting a value of the base type into a
* {@link BigDecimal}
*/
protected abstract Function<T, BigDecimal> widen();

/**
* @return a function converting a {@link BigDecimal} into the equivalent value in the base type.
* @return a function converting a {@link BigDecimal} into the equivalent
* value in the base type
*/
protected abstract Function<BigDecimal, T> narrow();

/**
* @return a predicate checking whether its input is in the configured range.
* @return a predicate checking whether its input is in the configured
* range
*/
protected abstract Predicate<T> inRange();

/**
* @return the lowest magnitude number, respecting the configured range. The ideal shrink value is always this value (i.e. this value cannot be shrunk any further).
* @return the lowest magnitude number, respecting the configured range.
* The ideal shrink value is always this value (i.e. this value cannot
* be shrunk any further).
*/
protected abstract T leastMagnitude();

/**
* @return whether the given number is negative or not.
* @return whether the given number is negative or not
*/
protected abstract boolean negative(T target);

/**
* Used when shrinking negative numbers to add the positive equivalent value at the top of shrinks list.
* Used when shrinking negative numbers to add the positive equivalent
* value at the top of shrinks list.
*
* @param target always a negative number
* @return the positive equivalent to target
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ a copy of this software and associated documentation files (the

/**
* Base class for generators of integral types, such as {@code int} and
* {@link BigInteger}. All numbers are converted to/from BigInteger for processing.
* {@link BigInteger}. All numbers are converted to/from BigInteger for
* processing.
*
* @param <T> type of values this generator produces
*/
public abstract class IntegralGenerator<T extends Number> extends Generator<T> {
public abstract class IntegralGenerator<T extends Number>
extends Generator<T> {

protected IntegralGenerator(Class<T> type) {
super(singletonList(type));
}
Expand All @@ -66,8 +69,10 @@ protected IntegralGenerator(List<Class<T>> types) {
// Try your luck by testing the smallest possible value
results.add(leastMagnitude());

// Try values between smallest and largest, with smaller and smaller increments as we approach the largest
results.addAll(stream(
// Try values between smallest and largest, with smaller and smaller
// increments as we approach the largest
results.addAll(
stream(
halvingIntegral(
// We work with BigInteger, so convert all inputs
widen().apply(larger),
Expand All @@ -84,34 +89,40 @@ protected IntegralGenerator(List<Class<T>> types) {
}

/**
* @return a function converting a value of the base type into a {@link BigInteger}.
* @return a function converting a value of the base type into a
* {@link BigInteger}
*/
protected Function<T, BigInteger> widen() {
return n -> BigInteger.valueOf(n.longValue());
}

/**
* @return a function converting a {@link BigInteger} into the equivalent value in the base type.
* @return a function converting a {@link BigInteger} into the equivalent
* value in the base type
*/
protected abstract Function<BigInteger, T> narrow();

/**
* @return a predicate checking whether its input is in the configured range.
* @return a predicate checking whether its input is in the configured
* range
*/
protected abstract Predicate<T> inRange();

/**
* @return the lowest magnitude number, respecting the configured range. The ideal shrink value is always this value (i.e. this value cannot be shrunk any further).
* @return the lowest magnitude number, respecting the configured range.
* The ideal shrink value is always this value (i.e. this value cannot
* be shrunk any further).
*/
protected abstract T leastMagnitude();

/**
* @return whether the given number is negative or not.
* @return whether the given number is negative or not
*/
protected abstract boolean negative(T target);

/**
* Used when shrinking negative numbers to add the positive equivalent value at the top of shrinks list.
* Used when shrinking negative numbers to add the positive equivalent
* value at the top of shrinks list.
*
* @param target always a negative number
* @return the positive equivalent to target
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ protected AbstractStringGenerator() {
super(String.class);
}

@Override public String generate(SourceOfRandomness random, GenerationStatus status) {
@Override public String generate(
SourceOfRandomness random,
GenerationStatus status) {

int[] codePoints = new int[status.size()];

for (int i = 0; i < codePoints.length; ++i)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,14 @@ public ByteGenerator() {
* @param range annotation that gives the range's constraints
*/
public void configure(InRange range) {
min = range.min().isEmpty() ? range.minByte() : Byte.parseByte(range.min());
max = range.max().isEmpty() ? range.maxByte() : Byte.parseByte(range.max());
min =
range.min().isEmpty()
? range.minByte()
: Byte.parseByte(range.min());
max =
range.max().isEmpty()
? range.maxByte()
: Byte.parseByte(range.max());
}

@Override public Byte generate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,31 @@ class CodePointShrink implements Shrink<Integer> {
this.filter = filter;
}

@Override public List<Integer> shrink(SourceOfRandomness random, Object larger) {
@Override public List<Integer> shrink(
SourceOfRandomness random,
Object larger) {

int codePoint = (Integer) larger;

List<Integer> shrinks = new ArrayList<>();
addAll(shrinks, (int) 'a', (int) 'b', (int) 'c');
if (isUpperCase(codePoint))
shrinks.add(Character.toLowerCase(codePoint));
addAll(shrinks, (int) 'A', (int) 'B', (int) 'C',
(int) '1', (int) '2', (int) '3',
addAll(
shrinks,
(int) 'A', (int) 'B', (int) 'C', (int) '1', (int) '2', (int) '3',
(int) ' ', (int) '\n');
reverse(shrinks);

Comparator<Integer> comparator =
comparing((Function<Integer, Boolean>) Character::isLowerCase)
.thenComparing((Function<Integer, Boolean>) Character::isUpperCase)
.thenComparing((Function<Integer, Boolean>) Character::isDigit)
.thenComparing(
(Function<Integer, Boolean>) Character::isUpperCase)
.thenComparing(
(Function<Integer, Boolean>) Character::isDigit)
.thenComparing(cp -> Integer.valueOf(' ').equals(cp))
.thenComparing((Function<Integer, Boolean>) Character::isSpaceChar)
.thenComparing(
(Function<Integer, Boolean>) Character::isSpaceChar)
.thenComparing(naturalOrder());
return shrinks.stream()
.filter(filter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ public DoubleGenerator() {
* @param range annotation that gives the range's constraints
*/
public void configure(InRange range) {
min = range.min().isEmpty()
? range.minDouble()
: Double.parseDouble(range.min());
max = range.max().isEmpty()
? range.maxDouble()
: Double.parseDouble(range.max());
min =
range.min().isEmpty()
? range.minDouble()
: Double.parseDouble(range.min());
max =
range.max().isEmpty()
? range.maxDouble()
: Double.parseDouble(range.max());
}

@Override public Double generate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ public FloatGenerator() {
* @param range annotation that gives the range's constraints
*/
public void configure(InRange range) {
min = range.min().isEmpty()
? range.minFloat()
: Float.parseFloat(range.min());
max = range.max().isEmpty()
? range.maxFloat()
: Float.parseFloat(range.max());
min =
range.min().isEmpty()
? range.minFloat()
: Float.parseFloat(range.min());
max =
range.max().isEmpty()
? range.maxFloat()
: Float.parseFloat(range.max());
}

@Override public Float generate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ public IntegerGenerator() {
* @param range annotation that gives the range's constraints
*/
public void configure(InRange range) {
min = range.min().isEmpty()
? range.minInt()
: Integer.parseInt(range.min());
max = range.max().isEmpty()
? range.maxInt()
: Integer.parseInt(range.max());
min =
range.min().isEmpty()
? range.minInt()
: Integer.parseInt(range.min());
max =
range.max().isEmpty()
? range.maxInt()
: Integer.parseInt(range.max());
}

@Override public Integer generate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ public LongGenerator() {
* @param range annotation that gives the range's constraints
*/
public void configure(InRange range) {
min = range.min().isEmpty()
? range.minLong()
: Long.parseLong(range.min());
max = range.max().isEmpty()
? range.maxLong()
: Long.parseLong(range.max());
min =
range.min().isEmpty()
? range.minLong()
: Long.parseLong(range.min());
max =
range.max().isEmpty()
? range.maxLong()
: Long.parseLong(range.max());
}

@Override public Long generate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ public ShortGenerator() {
* @param range annotation that gives the range's constraints
*/
public void configure(InRange range) {
min = range.min().isEmpty()
? range.minShort()
: Short.parseShort(range.min());
max = range.max().isEmpty()
? range.maxShort()
: Short.parseShort(range.max());
min =
range.min().isEmpty()
? range.minShort()
: Short.parseShort(range.min());
max =
range.max().isEmpty()
? range.maxShort()
: Short.parseShort(range.max());
}

@Override public Short generate(
Expand Down

0 comments on commit 6896278

Please sign in to comment.