Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -223,25 +223,36 @@ protected Reader<ByteSizeValue> instanceReader() {
}

@Override
protected ByteSizeValue mutateInstance(ByteSizeValue instance) throws IOException {
long size = instance.getSize();
ByteSizeUnit unit = instance.getUnit();
protected ByteSizeValue mutateInstance(final ByteSizeValue instance) {
final long instanceSize = instance.getSize();
final ByteSizeUnit instanceUnit = instance.getUnit();
final long mutateSize;
final ByteSizeUnit mutateUnit;
switch (between(0, 1)) {
case 0:
long unitBytes = unit.toBytes(1);
size = randomValueOtherThan(size, () -> randomNonNegativeLong() / unitBytes);
final long unitBytes = instanceUnit.toBytes(1);
mutateSize = randomValueOtherThan(instanceSize, () -> randomNonNegativeLong() / unitBytes);
mutateUnit = instanceUnit;
break;
case 1:
unit = randomValueOtherThan(unit, () -> randomFrom(ByteSizeUnit.values()));
long newUnitBytes = unit.toBytes(1);
if (size >= Long.MAX_VALUE / newUnitBytes) {
size = randomValueOtherThan(size, () -> randomNonNegativeLong() / newUnitBytes);
mutateUnit = randomValueOtherThan(instanceUnit, () -> randomFrom(ByteSizeUnit.values()));
final long newUnitBytes = mutateUnit.toBytes(1);
/*
* If size is zero we can not reuse zero because zero with any unit will be equal to zero with any other unit so in this case we
* need to randomize a new size. Additionally, if the size unit pair is such that the representation would be such that the
* number of represented bytes would exceed Long.Max_VALUE, we have to randomize a new size too.
*/
if (instanceSize == 0 || instanceSize >= Long.MAX_VALUE / newUnitBytes) {
mutateSize = randomValueOtherThanMany(
v -> v == instanceSize && v >= Long.MAX_VALUE / newUnitBytes, () -> randomNonNegativeLong() / newUnitBytes);
} else {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The crux of the change is this if block. I added a condition that we need to randomize size of the input size is zero (otherwise we would end up with zero bytes equals zero bytes but the mutation is suppose to have a different value). Additionally, I added a condition to not randomly select a value whose bytes representation would exceed Long.MAX_VALUE.

mutateSize = instanceSize;
}
break;
default:
throw new AssertionError("Invalid randomisation branch");
}
return new ByteSizeValue(size, unit);
return new ByteSizeValue(mutateSize, mutateUnit);
}

public void testParse() {
Expand Down