Skip to content

Commit

Permalink
Add unit tests for faketime
Browse files Browse the repository at this point in the history
  • Loading branch information
virtuald committed Jan 6, 2015
1 parent d7f8109 commit 141afcc
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/pyfrc/test_support/fake_time.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

import threading

import hal
from hal_impl.mode_helpers import notify_new_ds_data


Expand Down
68 changes: 68 additions & 0 deletions tests/test_faketime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

import pytest

from pyfrc.test_support.fake_time import TestEnded, TestRanTooLong, FakeTime

def test_faketime_1():
'''Test expiration'''

ft = FakeTime()
ft._setup()
ft.set_time_limit(5)

with pytest.raises(TestRanTooLong):
ft.increment_time_by(10)

def assert_float(f1, f2):
assert abs(f1 - f2) < 0.001

class StepChecker:

def __init__(self):
self.expected = 0.02

def on_step(self, tm):
assert_float(tm, self.expected)
self.expected += 0.02
return True

def test_faketime_2():
'''Test calling the step function '''

ft = FakeTime()
ft._setup()
ft.set_time_limit(5)

sc = StepChecker()
ft._ds_cond._on_step = sc.on_step

ft.increment_new_packet()
ft.increment_new_packet()
ft.increment_new_packet()

assert_float(ft.get(), 0.06)
assert_float(sc.expected, 0.06 + 0.02)

def test_faketime_3():
'''Test calling the step function with varying lengths'''

ft = FakeTime()
ft._setup()
ft.set_time_limit(5)

sc = StepChecker()
ft._ds_cond._on_step = sc.on_step

ft.increment_time_by(0.005)
ft.increment_time_by(0.01)
ft.increment_time_by(0.02)
ft.increment_time_by(0.03)
ft.increment_time_by(0.04)
ft.increment_time_by(0.05)

tm = 0.005 + 0.01 + 0.02 + 0.03 + 0.04 + 0.05
assert_float(ft.get(), tm)
assert_float(sc.expected, 0.16)



0 comments on commit 141afcc

Please sign in to comment.