Browse Source

tests: Account for clock inaccuracies in all ClockObject tests

Closes #998
Daniel 5 years ago
parent
commit
0d2dddd988
1 changed files with 17 additions and 3 deletions
  1. 17 3
      tests/putil/test_clockobject.py

+ 17 - 3
tests/putil/test_clockobject.py

@@ -1,4 +1,18 @@
 import time
+import sys
+
+# Epsilon for taking floating point calculation inaccuracies in mind
+EPSILON = 1e-06
+
+# We must account for clock inaccuracy
+if sys.platform == 'win32' or sys.platform == 'cygwin':
+    # Assume 19 milliseconds inaccuracy on Windows (worst case)
+    # 16 milliseconds plus 3 milliseconds for execution time
+    CLOCK_INACCURACY = 0.019
+else:
+    # On other platforms, assume 5 milliseconds, allowing for execution time
+    # (5000 times higher than their actual 1 microsecond accuracy)
+    CLOCK_INACCURACY = 0.005
 
 
 def test_clock_get_frame_time(clockobj):
@@ -16,13 +30,13 @@ def test_clock_jump_frame_time(clockobj):
 def test_clock_get_real_time(clockobj):
     current_time = clockobj.get_real_time()
     time.sleep(0.4)
-    assert clockobj.get_real_time() - current_time >= 0.39
+    assert clockobj.get_real_time() - current_time + EPSILON >= 0.4 - CLOCK_INACCURACY
 
 
 def test_clock_get_long_time(clockobj):
     current_time = clockobj.get_long_time()
     time.sleep(0.4)
-    assert clockobj.get_long_time() - current_time >= 0.39
+    assert clockobj.get_long_time() - current_time + EPSILON >= 0.4 - CLOCK_INACCURACY
 
 
 def test_clock_get_dt(clockobj):
@@ -37,4 +51,4 @@ def test_clock_reset(clockobj):
     clockobj.reset()
     assert clockobj.get_dt() == 0
     assert clockobj.get_frame_time() == 0
-    assert clockobj.get_real_time() < 0.01
+    assert clockobj.get_real_time() - EPSILON <= CLOCK_INACCURACY