importsysimporttimeimportunittestfrom..importSDL_Init,SDL_Quit,SDL_QuitSubSystem,SDL_INIT_TIMERfrom..importtimerifsys.version_info[0]>=3:long=intcalls=[]classSDLTimerTest(unittest.TestCase):__tags__=["sdl"]defsetUp(self):SDL_Init(SDL_INIT_TIMER)deftearDown(self):SDL_QuitSubSystem(SDL_INIT_TIMER)SDL_Quit()deftest_SDL_GetTicks(self):ticks=timer.SDL_GetTicks()time.sleep(1)ticks2=timer.SDL_GetTicks()time.sleep(1)ticks3=timer.SDL_GetTicks()# Add some latency, since the final numbers can heavily depend# on the system's context switching behaviour, load, etc., etc.,# etc.self.assertTrue(abs(ticks2-1000-ticks)<=3,"1: %f is not <= 3 for %f and %f"%(abs(ticks2-1000-ticks),ticks2,ticks))self.assertTrue(abs(ticks3-1000-ticks2)<=3,"2: %f is not <= 3 for %f and %f"%(abs(ticks3-1000-ticks2),ticks3,ticks2))self.assertTrue(abs(ticks3-2000-ticks)<=3,"3: %f is not <= 3 for %f and %f"%(abs(ticks3-2000-ticks2),ticks3,ticks))deftest_SDL_GetPerformanceCounter(self):perf=timer.SDL_GetPerformanceCounter()self.assertTrue(type(perf)in(int,long))deftest_SDL_GetPerformanceFrequency(self):freq=timer.SDL_GetPerformanceFrequency()self.assertTrue(type(freq)in(int,long))deftest_SDL_Delay(self):forwaitinrange(5,200,5):start=time.time()*1000timer.SDL_Delay(wait)end=time.time()*1000sm=(end-start)self.assertTrue(abs(wait-sm)<=3,"%f is not <= 3 for %f and %f"%(abs(wait-sm),wait,sm))@unittest.skipIf(hasattr(sys,"pypy_version_info"),"PyPy can't access other variables properly from a separate thread")deftest_SDL_AddRemoveTimer(self):calls=[]deftimerfunc(interval,param):calls.append(param)returnintervalcallback=timer.SDL_TimerCallback(timerfunc)timerid=timer.SDL_AddTimer(100,callback,"Test")start=timer.SDL_GetTicks()end=long(start)while(end-start)<1100:# One second waitend=timer.SDL_GetTicks()# check for <=11, since it can happen that a last call is still# executingself.assertLessEqual(len(calls),11)timer.SDL_RemoveTimer(timerid)self.assertLessEqual(len(calls),11)timer.SDL_RemoveTimer(timerid)# Wait a bit, so the last executing handlers can finishtimer.SDL_Delay(10)if__name__=='__main__':sys.exit(unittest.main())