test_chrono.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from pybind11_tests import chrono as m
  2. import datetime
  3. def test_chrono_system_clock():
  4. # Get the time from both c++ and datetime
  5. date1 = m.test_chrono1()
  6. date2 = datetime.datetime.today()
  7. # The returned value should be a datetime
  8. assert isinstance(date1, datetime.datetime)
  9. # The numbers should vary by a very small amount (time it took to execute)
  10. diff = abs(date1 - date2)
  11. # There should never be a days/seconds difference
  12. assert diff.days == 0
  13. assert diff.seconds == 0
  14. # We test that no more than about 0.5 seconds passes here
  15. # This makes sure that the dates created are very close to the same
  16. # but if the testing system is incredibly overloaded this should still pass
  17. assert diff.microseconds < 500000
  18. def test_chrono_system_clock_roundtrip():
  19. date1 = datetime.datetime.today()
  20. # Roundtrip the time
  21. date2 = m.test_chrono2(date1)
  22. # The returned value should be a datetime
  23. assert isinstance(date2, datetime.datetime)
  24. # They should be identical (no information lost on roundtrip)
  25. diff = abs(date1 - date2)
  26. assert diff.days == 0
  27. assert diff.seconds == 0
  28. assert diff.microseconds == 0
  29. def test_chrono_duration_roundtrip():
  30. # Get the difference between two times (a timedelta)
  31. date1 = datetime.datetime.today()
  32. date2 = datetime.datetime.today()
  33. diff = date2 - date1
  34. # Make sure this is a timedelta
  35. assert isinstance(diff, datetime.timedelta)
  36. cpp_diff = m.test_chrono3(diff)
  37. assert cpp_diff.days == diff.days
  38. assert cpp_diff.seconds == diff.seconds
  39. assert cpp_diff.microseconds == diff.microseconds
  40. def test_chrono_duration_subtraction_equivalence():
  41. date1 = datetime.datetime.today()
  42. date2 = datetime.datetime.today()
  43. diff = date2 - date1
  44. cpp_diff = m.test_chrono4(date2, date1)
  45. assert cpp_diff.days == diff.days
  46. assert cpp_diff.seconds == diff.seconds
  47. assert cpp_diff.microseconds == diff.microseconds
  48. def test_chrono_steady_clock():
  49. time1 = m.test_chrono5()
  50. assert isinstance(time1, datetime.timedelta)
  51. def test_chrono_steady_clock_roundtrip():
  52. time1 = datetime.timedelta(days=10, seconds=10, microseconds=100)
  53. time2 = m.test_chrono6(time1)
  54. assert isinstance(time2, datetime.timedelta)
  55. # They should be identical (no information lost on roundtrip)
  56. assert time1.days == time2.days
  57. assert time1.seconds == time2.seconds
  58. assert time1.microseconds == time2.microseconds
  59. def test_floating_point_duration():
  60. # Test using a floating point number in seconds
  61. time = m.test_chrono7(35.525123)
  62. assert isinstance(time, datetime.timedelta)
  63. assert time.seconds == 35
  64. assert 525122 <= time.microseconds <= 525123
  65. diff = m.test_chrono_float_diff(43.789012, 1.123456)
  66. assert diff.seconds == 42
  67. assert 665556 <= diff.microseconds <= 665557