Skip to content

Commit

Permalink
Merge pull request #1880 from Gogotchuri/time_get_ticks_test
Browse files Browse the repository at this point in the history
Added time.get_ticks() tests & increase error deltas for other time module tests
  • Loading branch information
illume committed Jun 3, 2020
2 parents 0714244 + 180e44c commit 108d07f
Showing 1 changed file with 86 additions and 79 deletions.
165 changes: 86 additions & 79 deletions test/time_test.py
Expand Up @@ -134,86 +134,93 @@ def todo_test_tick_busy_loop(self):

self.fail()

class TimeModuleTest(unittest.TestCase):
def test_delay(self):
"""Tests time.delay() function."""
millis = 50 # millisecond to wait on each iteration
iterations = 20 # number of iterations
delta = 5 # Represents acceptable margin of error for wait in ms
# Call checking function
self._wait_delay_check(pygame.time.delay, millis, iterations, delta)
# After timing behaviour, check argument type exceptions
self._type_error_checks(pygame.time.delay)

def test_get_ticks(self):
"""Tests time.get_ticks()"""
"""
Iterates and delays for arbitrary amount of time for each iteration,
check get_ticks to equal correct gap time
"""
iterations = 20
millis = 50
delta = 15 # Acceptable margin of error in ms
# Assert return type to be int
self.assertTrue(type(pygame.time.get_ticks()) == int)
for i in range(iterations):
curr_ticks = pygame.time.get_ticks() # Save current tick count
curr_time = time.time() # Save current time
pygame.time.delay(millis) # Delay for millis
# Time and Ticks difference from start of the iteration
time_diff = round((time.time() - curr_time)*1000)
ticks_diff = pygame.time.get_ticks() - curr_ticks
# Assert almost equality of the ticking time and time difference
self.assertAlmostEqual(ticks_diff, time_diff, delta=delta)

def todo_test_set_timer(self):

# __doc__ (as of 2008-08-02) for pygame.time.set_timer:

# pygame.time.set_timer(eventid, milliseconds): return None
# repeatedly create an event on the event queue
#
# Set an event type to appear on the event queue every given number of
# milliseconds. The first event will not appear until the amount of
# time has passed.
#
# Every event type can have a separate timer attached to it. It is
# best to use the value between pygame.USEREVENT and pygame.NUMEVENTS.
#
# To disable the timer for an event, set the milliseconds argument to 0.

# class TimeModuleTest(unittest.TestCase):
# def test_delay(self):
# """Tests time.delay() function."""
# millis = 50 # millisecond to wait on each iteration
# iterations = 20 # number of iterations
# delta = 1 # Represents acceptable margin of error for wait in ms
# # Call checking function
# self._wait_delay_check(pygame.time.delay, millis, iterations, delta)
# # After timing behaviour, check argument type exceptions
# self._type_error_checks(pygame.time.delay)

# def todo_test_get_ticks(self):

# # __doc__ (as of 2008-08-02) for pygame.time.get_ticks:

# # pygame.time.get_ticks(): return milliseconds
# # get the time in milliseconds
# #
# # Return the number of milliseconds since pygame.init() was called.
# # Before pygame is initialized this will always be 0.
# #

# self.fail()

# def todo_test_set_timer(self):

# # __doc__ (as of 2008-08-02) for pygame.time.set_timer:

# # pygame.time.set_timer(eventid, milliseconds): return None
# # repeatedly create an event on the event queue
# #
# # Set an event type to appear on the event queue every given number of
# # milliseconds. The first event will not appear until the amount of
# # time has passed.
# #
# # Every event type can have a separate timer attached to it. It is
# # best to use the value between pygame.USEREVENT and pygame.NUMEVENTS.
# #
# # To disable the timer for an event, set the milliseconds argument to 0.

# self.fail()

# def test_wait(self):
# """Tests time.wait() function."""
# millis = 50 # millisecond to wait on each iteration
# iterations = 20 # number of iterations
# delta = 5 # Represents acceptable margin of error for wait in ms
# # Call checking function
# self._wait_delay_check(pygame.time.wait, millis, iterations, delta)
# # After timing behaviour, check argument type exceptions
# self._type_error_checks(pygame.time.wait)

# def _wait_delay_check(self, func_to_check, millis, iterations, delta):
# """"
# call func_to_check(millis) "iterations" times and check each time if
# function "waited" for given millisecond (+- delta). At the end, take
# average time for each call (whole_duration/iterations), which should
# be equal to millis (+- delta - acceptable margin of error).
# *Created to avoid code duplication during delay and wait tests
# """
# # take starting time for duration calculation
# start_time = time.time()
# for i in range(iterations):
# wait_time = func_to_check(millis)
# # Check equality of wait_time and millis with margin of error delta
# self.assertAlmostEqual(wait_time, millis, delta=delta)
# stop_time = time.time()
# # Cycle duration in millisecond
# duration = round((stop_time-start_time)*1000)
# # Duration/Iterations should be (almost) equal to predefined millis
# self.assertAlmostEqual(duration/iterations, millis, delta=delta)

# def _type_error_checks(self, func_to_check):
# """Checks 3 TypeError (float, tuple, string) for the func_to_check"""
# """Intended for time.delay and time.wait functions"""
# # Those methods throw no exceptions on negative integers
# self.assertRaises(TypeError, func_to_check, 0.1) # check float
# self.assertRaises(TypeError, pygame.time.delay, (0, 1)) # check tuple
# self.assertRaises(TypeError, pygame.time.delay, "10") # check string
self.fail()

def test_wait(self):
"""Tests time.wait() function."""
millis = 100 # millisecond to wait on each iteration
iterations = 10 # number of iterations
delta = 50 # Represents acceptable margin of error for wait in ms
# Call checking function
self._wait_delay_check(pygame.time.wait, millis, iterations, delta)
# After timing behaviour, check argument type exceptions
self._type_error_checks(pygame.time.wait)

def _wait_delay_check(self, func_to_check, millis, iterations, delta):
""""
call func_to_check(millis) "iterations" times and check each time if
function "waited" for given millisecond (+- delta). At the end, take
average time for each call (whole_duration/iterations), which should
be equal to millis (+- delta - acceptable margin of error).
*Created to avoid code duplication during delay and wait tests
"""
# take starting time for duration calculation
start_time = time.time()
for i in range(iterations):
wait_time = func_to_check(millis)
# Check equality of wait_time and millis with margin of error delta
self.assertAlmostEqual(wait_time, millis, delta=delta)
stop_time = time.time()
# Cycle duration in millisecond
duration = round((stop_time-start_time)*1000)
# Duration/Iterations should be (almost) equal to predefined millis
self.assertAlmostEqual(duration/iterations, millis, delta=delta)

def _type_error_checks(self, func_to_check):
"""Checks 3 TypeError (float, tuple, string) for the func_to_check"""
"""Intended for time.delay and time.wait functions"""
# Those methods throw no exceptions on negative integers
self.assertRaises(TypeError, func_to_check, 0.1) # check float
self.assertRaises(TypeError, pygame.time.delay, (0, 1)) # check tuple
self.assertRaises(TypeError, pygame.time.delay, "10") # check string

###############################################################################

Expand Down

0 comments on commit 108d07f

Please sign in to comment.