1
0

add lab3 tests

This commit is contained in:
2024-09-05 11:21:13 +03:00
parent 19883103ed
commit 41be2390f3

View File

@@ -1,6 +1,8 @@
import org.junit.jupiter.api.Test;
import ru.lionarius.IntegralCalculator;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.BiConsumer;
import java.util.function.Function;
import static org.junit.jupiter.api.Assertions.*;
@@ -50,4 +52,44 @@ class IntegralCalculatorTests {
assertIntegral(-0.5, calculator, function, upperBound, lowerBound);
assertIntegral(0.0, calculator, function, lowerBound, lowerBound);
}
@Test
public void testProgressCallbackDifferentBounds() {
final var currentStep = new AtomicLong();
final var totalSteps = new AtomicLong();
final var callbackCount = new AtomicLong(0);
final BiConsumer<Long, Long> progressCallback = (current, total) -> {
currentStep.set(current);
totalSteps.set(total);
callbackCount.incrementAndGet();
};
final var calculator = new IntegralCalculator(0.01, progressCallback);
calculator.calculate(x -> x, 0, 1);
assertTrue(callbackCount.get() >= 2, "Callback should be called at least twice");
assertEquals(totalSteps.get(), currentStep.get(), "Final progress should be equal to total steps");
}
@Test
public void testProgressCallbackSameBounds() {
var currentStep = new AtomicLong();
var totalSteps = new AtomicLong();
var callbackCount = new AtomicLong(0);
BiConsumer<Long, Long> progressCallback = (current, total) -> {
currentStep.set(current);
totalSteps.set(total);
callbackCount.incrementAndGet();
};
var calculator = new IntegralCalculator(0.01, progressCallback);
calculator.calculate(x -> x, 0, 0);
assertTrue(callbackCount.get() >= 2, "Callback should be called at least twice");
assertEquals(totalSteps.get(), currentStep.get(), "Final progress should be equal to total steps");
}
}