# Path: luma/core/sprite_system.py
# class framerate_regulator(object):
#     """
#     Implements a variable sleep mechanism to give the appearance of a consistent
#     frame rate. Using a fixed-time sleep will cause animations to be jittery
#     (looking like they are speeding up or slowing down, depending on what other
#     work is occurring), whereas this class keeps track of when the last time the
#     ``sleep()`` method was called, and calculates a sleep period to smooth out
#     the jitter.
# 
#     :param fps: The desired frame rate, expressed numerically in
#         frames-per-second.  By default, this is set at 16.67, to give a frame
#         render time of approximately 60ms. This can be overridden as necessary,
#         and if no FPS limiting is required, the ``fps`` can be set to zero.
#     :type fps: float
#     """
#     def __init__(self, fps=16.67):
#         if fps == 0:
#             fps = -1
# 
#         self.max_sleep_time = 1.0 / fps
#         self.total_transit_time = 0
#         self.called = 0
#         self.start_time = None
#         self.last_time = None
# 
#     def __enter__(self):
#         self.enter_time = perf_counter()
#         if not self.start_time:
#             self.start_time = self.enter_time
#             self.last_time = self.enter_time
# 
#         return self
# 
#     def __exit__(self, *args):
#         """
#         Sleeps for a variable amount of time (dependent on when it was last
#         called), to give a consistent frame rate. If it cannot meet the desired
#         frame rate (i.e. too much time has occurred since the last call), then
#         it simply exits without blocking.
#         """
#         self.called += 1
#         self.total_transit_time += perf_counter() - self.enter_time
#         if self.max_sleep_time >= 0:
#             elapsed = perf_counter() - self.last_time
#             sleep_for = self.max_sleep_time - elapsed
# 
#             if sleep_for > 0:
#                 sleep(sleep_for)
# 
#         self.last_time = perf_counter()
# 
#     def effective_FPS(self):
#         """
#         Calculates the effective frames-per-second - this should largely
#         correlate to the desired FPS supplied in the constructor, but no
#         guarantees are given.
# 
#         :returns: The effective frame rate.
#         :rtype: float
#         """
#         if self.start_time is None:
#             self.start_time = 0
#         elapsed = perf_counter() - self.start_time
#         return self.called / elapsed
# 
#     def average_transit_time(self):
#         """
#         Calculates the average transit time between the enter and exit methods,
#         and return the time in milliseconds.
# 
#         :returns: The average transit in milliseconds.
#         :rtype: float
#         """
#         return self.total_transit_time * 1000.0 / self.called


from time import perf_counter
from luma.core.sprite_system import framerate_regulator


def test_init_default():
    regulator = framerate_regulator()
    assert regulator.start_time is None
    assert regulator.last_time is None
    assert regulator.called == 0
    before = perf_counter()
    with regulator:
        pass
    after = perf_counter()

    assert regulator.max_sleep_time == 1 / 16.67
    assert before <= regulator.start_time <= after
    assert regulator.called == 1


def test_init_unlimited():
    regulator = framerate_regulator(fps=0)
    before = perf_counter()
    with regulator:
        pass
    after = perf_counter()

    assert regulator.max_sleep_time == -1
    assert before <= regulator.start_time <= after
    assert regulator.called == 1


def test_init_30fps():

# Path: luma/core/sprite_system.py
# class framerate_regulator(object):
#     """
#     Implements a variable sleep mechanism to give the appearance of a consistent
#     frame rate. Using a fixed-time sleep will cause animations to be jittery
#     (looking like they are speeding up or slowing down, depending on what other
#     work is occurring), whereas this class keeps track of when the last time the
#     ``sleep()`` method was called, and calculates a sleep period to smooth out
#     the jitter.
# 
#     :param fps: The desired frame rate, expressed numerically in
#         frames-per-second.  By default, this is set at 16.67, to give a frame
#         render time of approximately 60ms. This can be overridden as necessary,
#         and if no FPS limiting is required, the ``fps`` can be set to zero.
#     :type fps: float
#     """
#     def __init__(self, fps=16.67):
#         if fps == 0:
#             fps = -1
# 
#         self.max_sleep_time = 1.0 / fps
#         self.total_transit_time = 0
#         self.called = 0
#         self.start_time = None
#         self.last_time = None
# 
#     def __enter__(self):
#         self.enter_time = perf_counter()
#         if not self.start_time:
#             self.start_time = self.enter_time
#             self.last_time = self.enter_time
# 
#         return self
# 
#     def __exit__(self, *args):
#         """
#         Sleeps for a variable amount of time (dependent on when it was last
#         called), to give a consistent frame rate. If it cannot meet the desired
#         frame rate (i.e. too much time has occurred since the last call), then
#         it simply exits without blocking.
#         """
#         self.called += 1
#         self.total_transit_time += perf_counter() - self.enter_time
#         if self.max_sleep_time >= 0:
#             elapsed = perf_counter() - self.last_time
#             sleep_for = self.max_sleep_time - elapsed
# 
#             if sleep_for > 0:
#                 sleep(sleep_for)
# 
#         self.last_time = perf_counter()
# 
#     def effective_FPS(self):
#         """
#         Calculates the effective frames-per-second - this should largely
#         correlate to the desired FPS supplied in the constructor, but no
#         guarantees are given.
# 
#         :returns: The effective frame rate.
#         :rtype: float
#         """
#         if self.start_time is None:
#             self.start_time = 0
#         elapsed = perf_counter() - self.start_time
#         return self.called / elapsed
# 
#     def average_transit_time(self):
#         """
#         Calculates the average transit time between the enter and exit methods,
#         and return the time in milliseconds.
# 
#         :returns: The average transit in milliseconds.
#         :rtype: float
#         """
#         return self.total_transit_time * 1000.0 / self.called


# Path: tests/test_framerate_regulator.py
from time import perf_counter
from luma.core.sprite_system import framerate_regulator



def test_init_default():
    regulator = framerate_regulator()
    assert regulator.start_time is None
    assert regulator.last_time is None
    assert regulator.called == 0
    before = perf_counter()
    with regulator:
        pass
    after = perf_counter()

    assert regulator.max_sleep_time == 1 / 16.67
    assert before <= regulator.start_time <= after
    assert regulator.called == 1


def test_init_unlimited():
    regulator = framerate_regulator(fps=0)
    before = perf_counter()
    with regulator:
        pass
    after = perf_counter()

    assert regulator.max_sleep_time == -1
    assert before <= regulator.start_time <= after
    assert regulator.called == 1


def test_init_30fps():

    regulator = framerate_regulator(fps=30)