Skip to content

Commit

Permalink
fix: missing case for number overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulvdberge committed Dec 27, 2024
1 parent d0a11f2 commit 19889b6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.refinedmods.refinedstorage.api.autocrafting.calculation;

import com.refinedmods.refinedstorage.api.autocrafting.Pattern;
import com.refinedmods.refinedstorage.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.api.resource.list.MutableResourceList;
import com.refinedmods.refinedstorage.api.resource.list.MutableResourceListImpl;
Expand Down Expand Up @@ -28,10 +29,18 @@ void extractFromStorage(final ResourceKey resource, final long amount) {

void addOutputsToInternalStorage(final Pattern pattern, final Amount amount) {
pattern.getOutputs().forEach(
output -> internalStorage.add(output.resource(), output.amount() * amount.iterations())
output -> addOutputToInternalStorage(amount, output)
);
}

private void addOutputToInternalStorage(final Amount amount, final ResourceAmount output) {
final long totalAmount = output.amount() * amount.iterations();
if (totalAmount < 0) {
throw new NumberOverflowDuringCalculationException();
}
internalStorage.add(output.resource(), totalAmount);
}

CraftingState copy() {
return new CraftingState(storage.copy(), internalStorage.copy());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ void shouldDetectNumberOverflowInIngredient() {
}

@Test
void shouldDetectNumberOverflowInChildPattern() {
void shouldDetectNumberOverflowWithRootPattern() {
// Arrange
final RootStorage storage = storage();
final PatternRepository patterns = patterns(
Expand All @@ -650,4 +650,28 @@ void shouldDetectNumberOverflowInChildPattern() {
// Assert
assertThat(preview).usingRecursiveComparison().isEqualTo(PreviewBuilder.ofType(OVERFLOW).build());
}

@Test
void shouldDetectNumberOverflowWithOutputOfChildPattern() {
// Arrange
final RootStorage storage = storage();
final PatternRepository patterns = patterns(
pattern()
.ingredient(OAK_LOG, 1)
.output(OAK_PLANKS, 4)
.output(SIGN, Long.MAX_VALUE)
.build(),
pattern()
.ingredient(OAK_PLANKS, 4)
.output(CRAFTING_TABLE, 1)
.build()
);
final CraftingCalculator sut = new CraftingCalculatorImpl(patterns, storage);

// Act
final Preview preview = calculatePreview(sut, CRAFTING_TABLE, 2);

// Assert
assertThat(preview).usingRecursiveComparison().isEqualTo(PreviewBuilder.ofType(OVERFLOW).build());
}
}

0 comments on commit 19889b6

Please sign in to comment.