def test_delayus(delayus):
  """ Tests microsecond delay function. """

  n_tests = [1000, 1000, 1000, 500, 500, 250, 100,  100,      10,       2]
  tests   = [   0,  0.5,    1,  10,  50, 100, 500, 1000,  100000, 1000000]

  total_error = 0.0
  time_no_delay = 0 

  for i in range(len(n_tests)):
    total = 0.0
    error = 0.0
    for j in range(n_tests[i]):
      before = time.time()
      t = tests[i]
      if (t != 0): 
        delayus(t)
        # If testing no delay, i.e. t=0, then we don't
        # call the delay function. That way we can record
        # the time it takes to call the time() functions,
        # get test values, etc.
      total += time.time() - before 

    avg = (total/n_tests[i])
    avg *= 1000000 # sec -> usec
    # Subtract time recorded without calling delayus():
    avg -= time_no_delay 
    if (t == 0): time_no_delay = avg # Record no delay time
    error = abs(avg-t)
    if (t): 
      total_error += error
      # Because our no delay test doesn't call delayus(),
      # it wouldn't make sense to include it in our average error.
    print "%10.1f usec delay: time = %0.3f usec, error = %0.3f" %\
           (tests[i], avg, error)

  print "\n  avg error = +- %0.3f usec\n" % (total_error/len(n_tests))
dataset/ETHPy150Open graycatlabs/PyBBIO/tests/sleep_test.py/test_delayus/original