test_clockobject.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import time
  2. import sys
  3. # Epsilon for taking floating point calculation inaccuracies in mind
  4. EPSILON = 1e-06
  5. # We must account for clock inaccuracy
  6. if sys.platform == 'win32' or sys.platform == 'cygwin':
  7. # Assume 19 milliseconds inaccuracy on Windows (worst case)
  8. # 16 milliseconds plus 3 milliseconds for execution time
  9. CLOCK_INACCURACY = 0.019
  10. else:
  11. # On other platforms, assume 5 milliseconds, allowing for execution time
  12. # (5000 times higher than their actual 1 microsecond accuracy)
  13. CLOCK_INACCURACY = 0.005
  14. def test_clock_get_frame_time(clockobj):
  15. current_time = clockobj.get_frame_time()
  16. time.sleep(0.2)
  17. assert clockobj.get_frame_time() == current_time
  18. def test_clock_jump_frame_time(clockobj):
  19. current_time = clockobj.get_frame_time()
  20. clockobj.tick()
  21. assert clockobj.get_frame_time() == current_time + clockobj.get_frame_time()
  22. def test_clock_get_real_time(clockobj):
  23. current_time = clockobj.get_real_time()
  24. time.sleep(0.4)
  25. assert clockobj.get_real_time() - current_time + EPSILON >= 0.4 - CLOCK_INACCURACY
  26. def test_clock_get_long_time(clockobj):
  27. current_time = clockobj.get_long_time()
  28. time.sleep(0.4)
  29. assert clockobj.get_long_time() - current_time + EPSILON >= 0.4 - CLOCK_INACCURACY
  30. def test_clock_get_dt(clockobj):
  31. clockobj.tick()
  32. first_tick = clockobj.get_frame_time()
  33. clockobj.tick()
  34. second_tick = clockobj.get_frame_time()
  35. assert clockobj.get_dt() == second_tick - first_tick
  36. def test_clock_reset(clockobj):
  37. clockobj.reset()
  38. assert clockobj.get_dt() == 0
  39. assert clockobj.get_frame_time() == 0
  40. assert clockobj.get_real_time() - EPSILON <= CLOCK_INACCURACY