comboctl-main: Fix computeShortRTButtonPress() cyclic quantity bug

Signed-off-by: Carlos Rafael Giani <crg7475@mailbox.org>
This commit is contained in:
Carlos Rafael Giani 2022-11-19 11:21:34 +01:00
parent 3f265c855c
commit 20d4a0c692
2 changed files with 23 additions and 3 deletions

View file

@ -1045,10 +1045,17 @@ internal fun computeShortRTButtonPress(
require(stepSize > 0)
val distance = (targetQuantity - currentQuantity).absoluteValue
if (cyclicQuantityRange != null) {
// With a cyclic quantity, if the absolute distance between
// quantities exceeds half of that range, we have the option
// to change the quantity in the opposite direction which
// requires fewer button presses. For example, if the range
// is 60, and the absolute distance is 40, we'd normally have
// to press a button 40 times to get to the target quantity.
// But since cyclic quantities wrap around, we can instead
// press the opposite button 60-40 = 20 times to also get
// to the target quantity.
if (distance > (cyclicQuantityRange / 2)) {
val firstPart = (cyclicQuantityRange - targetQuantity)
val secondPart = currentQuantity - 0
numNeededShortRTButtonPresses = computeNumSteps(stepSize, firstPart + secondPart)
numNeededShortRTButtonPresses = computeNumSteps(stepSize, cyclicQuantityRange - distance)
shortRTButtonToPress = if (targetQuantity < currentQuantity) incrementButton else decrementButton
} else {
numNeededShortRTButtonPresses = computeNumSteps(stepSize, distance)

View file

@ -274,6 +274,19 @@ class RTNavigationTest {
)
assertEquals(Pair(((60 - 50) + (10 - 0)) / 1, RTNavigationButton.DOWN), result)
// Another cyclic quantity range test with a wrap around,
// this time from the other direction (wrapping around
// from quantity 58 to target quantity 1).
result = computeShortRTButtonPress(
currentQuantity = 58,
targetQuantity = 1,
cyclicQuantityRange = 60,
incrementSteps = arrayOf(Pair(0, 1)),
incrementButton = RTNavigationButton.UP,
decrementButton = RTNavigationButton.DOWN
)
assertEquals(Pair(3, RTNavigationButton.UP), result)
// Additional test to check that cyclic ranges are handled correctly
// even if currentQuantity is slightly higher than targetQuantity. This
// verifies that the computation does not produce negative distances.