"""Frame multiplier: N generated-frame positions per real frame of a 30 fps game (pipeline.cadence_steps), and the settings that take any multiple of 30 fps from 60 to 300 (2x to 10x).""" import pathlib import sys from fractions import Fraction import pytest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from framegen.settings import BY_KEY, Settings, valid_hz pipeline = pytest.importorskip("framegen.pipeline") def test_the_old_rates_keep_their_cadence(): assert pipeline.cadence_steps(60) == ([0.5], []) assert pipeline.cadence_steps(90) == ([1 / 3, 2 / 3], []) assert pipeline.cadence_steps(120) == ([0.25, 0.5], [0.75]) @pytest.mark.parametrize("n", range(2, 11)) def test_every_multiplier_shows_n_evenly_spaced_frames_per_real_one(n): first, second = pipeline.cadence_steps(30 * n) shown = first + second + [1.0] # the real frame closes the pair assert [Fraction(t).limit_denominator(100) for t in shown] == [Fraction(j, n) for j in range(1, n + 1)] # the real frame's tick carries half of them (one more for an odd N): the next tick never has more to show assert len(first) - (len(second) + 1) in (0, 1) def test_settings_take_any_multiple_of_30_from_2x_to_10x(): assert all(valid_hz(h) for h in (60, 90, 210, 300)) assert not any(valid_hz(h) for h in (30, 100, 330, True, "120", 120.0)) s = Settings() s.override("hz", 210) # 7x, not a menu choice assert s.hz == 210 and BY_KEY["hz"].name(210) == "7x" with pytest.raises(ValueError): s.override("hz", 100) assert s.step("hz", 1) == 240 and s.step("hz", -1) == 180 # from a custom value, to the menu's neighbours assert BY_KEY["hz"].name(120) == "4x" def test_a_steady_60_fps_game_is_found_and_left_again(): from framegen.cadence import UNEVEN_DWELL, FrameTimeline t = FrameTimeline() for _ in range(60 + UNEVEN_DWELL + 1): # a second to count, then a second there t.tick(True) assert t.full and t.delay <= 1.1 # at most about a frame of delay (steady frames need next to none) for k in range(60 + UNEVEN_DWELL + 1): # 30 fps: new every other tick t.tick(k % 2 == 0) assert not t.full def test_a_short_60_fps_burst_isnt_a_60_fps_game(): from framegen.cadence import FrameTimeline t = FrameTimeline() for n in range(240): # a 30 fps game with a 60 fps menu for half a second t.tick(n % 2 == 0 or 100 <= n < 130) assert not t.full