/trunk/Examples/test-suite/python/python_pybuf_runme3.py

# · Python · 42 lines · 29 code · 8 blank · 5 comment · 5 complexity · f6dd67b94f7fa2200a5fca401632cbac MD5 · raw file

  1. #run:
  2. # python python_pybuf_runme3.py benchmark
  3. #for the benchmark, other wise the test case will be run
  4. import python_pybuf
  5. import sys
  6. if len(sys.argv)>=2 and sys.argv[1]=="benchmark":
  7. #run the benchmark
  8. import time
  9. k=1000000 #number of times to excute the functions
  10. t=time.time()
  11. a = bytearray(b'hello world')
  12. for i in range(k):
  13. pybuf.title1(a)
  14. print("Time used by bytearray:",time.time()-t)
  15. t=time.time()
  16. b = 'hello world'
  17. for i in range(k):
  18. pybuf.title2(b)
  19. print("Time used by string:",time.time()-t)
  20. else:
  21. #run the test case
  22. buf1 = bytearray(10)
  23. buf2 = bytearray(50)
  24. pybuf.func1(buf1)
  25. assert buf1 == b'a'*10
  26. pybuf.func2(buf2)
  27. assert buf2.startswith(b"Hello world!\x00")
  28. count = pybuf.func3(buf2)
  29. assert count==10 #number of alpha and number in 'Hello world!'
  30. length = pybuf.func4(buf2)
  31. assert length==12
  32. buf3 = bytearray(b"hello")
  33. pybuf.title1(buf3)
  34. assert buf3==b'Hello'