From c593f9635c941ba4a876fa2f3a5ecc734ce02e4d Mon Sep 17 00:00:00 2001 From: Eddy Pedroni Date: Sat, 1 Mar 2025 11:08:20 +0100 Subject: Fix bugs in relative keypoint handler --- solo-tool-project/src/solo_tool/handlers.py | 9 +++--- solo-tool-project/test/handlers_integrationtest.py | 32 ++++++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 solo-tool-project/test/handlers_integrationtest.py diff --git a/solo-tool-project/src/solo_tool/handlers.py b/solo-tool-project/src/solo_tool/handlers.py index b178b3a..1820e86 100644 --- a/solo-tool-project/src/solo_tool/handlers.py +++ b/solo-tool-project/src/solo_tool/handlers.py @@ -56,14 +56,15 @@ def keyPointAbsolute(st: SoloTool, kp: float) -> Callable[[], None]: def keyPointRelative(st: SoloTool, delta: int) -> Callable[[], None]: from bisect import bisect_right, bisect_left def f(): + l = sorted(set(st.keyPoints + [st.keyPoint])) if delta > 0: - pivot = bisect_right(st.keyPoints, st.keyPoint) - 1 + pivot = bisect_right(l, st.keyPoint) - 1 elif delta < 0: - pivot = bisect_left(st.keyPoints, st.keyPoint) - 1 + pivot = bisect_left(l, st.keyPoint) else: return - new = max(min(pivot + delta, len(st.keyPoints) - 1), 0) - st.keyPoint = st.keyPoints[new] + new = max(min(pivot + delta, len(l) - 1), 0) + st.keyPoint = l[new] return f def rateAbsolute(st: SoloTool, value: float) -> Callable[[], None]: diff --git a/solo-tool-project/test/handlers_integrationtest.py b/solo-tool-project/test/handlers_integrationtest.py new file mode 100644 index 0000000..85050d6 --- /dev/null +++ b/solo-tool-project/test/handlers_integrationtest.py @@ -0,0 +1,32 @@ +import pytest + +from fixtures import soloTool, testSongs, mockPlayer, songPool + +from solo_tool.handlers import keyPointRelative + +testCases = [ + ([0.1, 0.3], 0.0, +1, 0.1, "Start +1"), + ([0.1, 0.3], 0.1, +1, 0.3, "First +1"), + ([0.1, 0.3], 0.2, +1, 0.3, "Between +1"), + ([0.1, 0.3], 0.3, +1, 0.3, "Second +1"), + ([0.1, 0.3], 0.4, +1, 0.4, "End +1"), + + ([0.1, 0.3], 0.0, -1, 0.0, "Start -1"), + ([0.1, 0.3], 0.1, -1, 0.1, "First -1"), + ([0.1, 0.3], 0.2, -1, 0.1, "Between -1"), + ([0.1, 0.3], 0.3, -1, 0.1, "Second -1"), + ([0.1, 0.3], 0.4, -1, 0.3, "End -1"), + + ([0.0, 0.3], 0.0, -1, 0.0, "0.0 -1"), +] + +@pytest.mark.parametrize("keyPoints,current,delta,expected,description", testCases) +def test_keyPointRelativeEdgeCases(soloTool, testSongs, keyPoints, current, delta, expected, description): + soloTool.addSong(testSongs[0]) + soloTool.keyPoints = keyPoints + soloTool.keyPoint = current + + handler = keyPointRelative(soloTool, delta) + handler() + + assert soloTool.keyPoint == expected, description -- cgit v1.2.3