### Question:
TargetingGasLimitCalculator implements GasLimitCalculator { @Override public long nextGasLimit(final long previousGasLimit) { final long nextGasLimit; if (targetGasLimit > previousGasLimit) { nextGasLimit = Math.min(targetGasLimit, safeAdd(previousGasLimit)); } else if (targetGasLimit < previousGasLimit) { nextGasLimit = Math.max(targetGasLimit, safeSub(previousGasLimit)); } else { nextGasLimit = previousGasLimit; } if (nextGasLimit != previousGasLimit) { LOG.debug("Adjusting block gas limit from {} to {}", previousGasLimit, nextGasLimit); } return nextGasLimit; } TargetingGasLimitCalculator(final Long targetGasLimit); @Override long nextGasLimit(final long previousGasLimit); @Override boolean equals(final Object o); @Override int hashCode(); static final long ADJUSTMENT_FACTOR; }### Answer:
@Test public void verifyGasLimitIsIncreasedWithinLimits() { TargetingGasLimitCalculator gasLimitCalculator = new TargetingGasLimitCalculator(10_000_000L); assertThat(gasLimitCalculator.nextGasLimit(8_000_000L)) .isEqualTo(8_000_000L + TargetingGasLimitCalculator.ADJUSTMENT_FACTOR); }
@Test public void verifyGasLimitIsDecreasedWithinLimits() { TargetingGasLimitCalculator gasLimitCalculator = new TargetingGasLimitCalculator(10_000_000L); assertThat(gasLimitCalculator.nextGasLimit(12_000_000L)) .isEqualTo(12_000_000L - TargetingGasLimitCalculator.ADJUSTMENT_FACTOR); }
@Test public void verifyGasLimitReachesTarget() { final long target = 10_000_000L; final long offset = TargetingGasLimitCalculator.ADJUSTMENT_FACTOR / 2; TargetingGasLimitCalculator gasLimitCalculator = new TargetingGasLimitCalculator(target); assertThat(gasLimitCalculator.nextGasLimit(target - offset)).isEqualTo(target); assertThat(gasLimitCalculator.nextGasLimit(target + offset)).isEqualTo(target); }
@Test public void verifyNoNegative() { final long target = 0L; final long offset = TargetingGasLimitCalculator.ADJUSTMENT_FACTOR / 2; TargetingGasLimitCalculator gasLimitCalculator = new TargetingGasLimitCalculator(target); assertThat(gasLimitCalculator.nextGasLimit(target + offset)).isEqualTo(target); }
@Test public void verifyNoOverflow() { final long target = Long.MAX_VALUE; final long offset = TargetingGasLimitCalculator.ADJUSTMENT_FACTOR / 2; TargetingGasLimitCalculator gasLimitCalculator = new TargetingGasLimitCalculator(target); assertThat(gasLimitCalculator.nextGasLimit(target - offset)).isEqualTo(target); }