|
| 1 | +package org.evomaster.core.search.algorithms |
| 2 | + |
| 3 | +import org.evomaster.core.EMConfig |
| 4 | +import org.evomaster.core.search.service.SearchTimeController |
| 5 | +import org.junit.jupiter.api.Assertions.assertEquals |
| 6 | +import org.junit.jupiter.api.Assertions.assertTrue |
| 7 | +import org.junit.jupiter.api.Test |
| 8 | + |
| 9 | +class LipsBudgetTest { |
| 10 | + |
| 11 | + @Test |
| 12 | + fun computePerTargetBudget_Actions_FairShare() { |
| 13 | + val config = EMConfig().apply { |
| 14 | + stoppingCriterion = EMConfig.StoppingCriterion.ACTION_EVALUATIONS |
| 15 | + maxEvaluations = 100 |
| 16 | + } |
| 17 | + val time = SearchTimeController() |
| 18 | + val budget = LipsBudget(config, time) |
| 19 | + |
| 20 | + // remaining = 100 - 0 = 100; uncovered=5 -> 20 each |
| 21 | + val perTarget = budget.computePerTargetBudget(uncoveredSize = 5) |
| 22 | + assertEquals(20, perTarget) |
| 23 | + |
| 24 | + // simulate usage: evaluatedActions=30 -> remaining=70 ; uncovered=7 -> 10 each |
| 25 | + time.newActionEvaluation(30) |
| 26 | + val perTargetAfterUsage = budget.computePerTargetBudget(uncoveredSize = 7) |
| 27 | + assertEquals(10, perTargetAfterUsage) |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + fun computePerTargetBudget_Actions_ZeroUncovered_ReturnsRemaining() { |
| 32 | + val config = EMConfig().apply { |
| 33 | + stoppingCriterion = EMConfig.StoppingCriterion.ACTION_EVALUATIONS |
| 34 | + maxEvaluations = 50 |
| 35 | + } |
| 36 | + val time = SearchTimeController() |
| 37 | + val budget = LipsBudget(config, time) |
| 38 | + |
| 39 | + time.newActionEvaluation(5) // remaining = 45 |
| 40 | + val perTarget = budget.computePerTargetBudget(uncoveredSize = 0) |
| 41 | + assertEquals(45, perTarget) |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + fun computePerTargetBudget_Time_ZeroUncovered_UsesRemainingSeconds() { |
| 46 | + val config = EMConfig().apply { |
| 47 | + stoppingCriterion = EMConfig.StoppingCriterion.TIME |
| 48 | + maxTimeInSeconds = 120 |
| 49 | + } |
| 50 | + val time = SearchTimeController() |
| 51 | + val budget = LipsBudget(config, time) |
| 52 | + |
| 53 | + // If search never started, elapsed=0 -> remaining = maxTimeInSeconds |
| 54 | + val perTarget = budget.computePerTargetBudget(uncoveredSize = 0) |
| 55 | + assertEquals(120, perTarget) |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + fun updateAndSwitchBudget() { |
| 60 | + val config = EMConfig().apply { |
| 61 | + stoppingCriterion = EMConfig.StoppingCriterion.ACTION_EVALUATIONS |
| 62 | + maxEvaluations = 100 |
| 63 | + } |
| 64 | + val time = SearchTimeController() |
| 65 | + val budget = LipsBudget(config, time) |
| 66 | + |
| 67 | + budget.budgetLeftForCurrentTarget = 15 |
| 68 | + |
| 69 | + // usedForCurrentTarget = (current - start) -> 10 |
| 70 | + val startActions = time.evaluatedActions |
| 71 | + time.newActionEvaluation(10) |
| 72 | + budget.updatePerTargetBudget(startActions, secondsAtGenStart = 0) |
| 73 | + assertEquals(5, budget.budgetLeftForCurrentTarget) |
| 74 | + |
| 75 | + // should switch once out of budget |
| 76 | + val shouldNotSwitch = budget.shouldSwitchTarget(coveredNow = false) |
| 77 | + assertTrue(!shouldNotSwitch) |
| 78 | + |
| 79 | + // spend remaining |
| 80 | + val start2 = time.evaluatedActions |
| 81 | + time.newActionEvaluation(5) |
| 82 | + budget.updatePerTargetBudget(start2, secondsAtGenStart = 0) |
| 83 | + assertEquals(0, budget.budgetLeftForCurrentTarget) |
| 84 | + |
| 85 | + val shouldSwitch = budget.shouldSwitchTarget(coveredNow = false) |
| 86 | + assertTrue(shouldSwitch) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | + |
0 commit comments