You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.3 KiB

2 months ago
  1. def test_chrono_system_clock():
  2. from pybind11_tests import test_chrono1
  3. import datetime
  4. # Get the time from both c++ and datetime
  5. date1 = 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. from pybind11_tests import test_chrono2
  20. import datetime
  21. date1 = datetime.datetime.today()
  22. # Roundtrip the time
  23. date2 = test_chrono2(date1)
  24. # The returned value should be a datetime
  25. assert isinstance(date2, datetime.datetime)
  26. # They should be identical (no information lost on roundtrip)
  27. diff = abs(date1 - date2)
  28. assert diff.days == 0
  29. assert diff.seconds == 0
  30. assert diff.microseconds == 0
  31. def test_chrono_duration_roundtrip():
  32. from pybind11_tests import test_chrono3
  33. import datetime
  34. # Get the difference between two times (a timedelta)
  35. date1 = datetime.datetime.today()
  36. date2 = datetime.datetime.today()
  37. diff = date2 - date1
  38. # Make sure this is a timedelta
  39. assert isinstance(diff, datetime.timedelta)
  40. cpp_diff = test_chrono3(diff)
  41. assert cpp_diff.days == diff.days
  42. assert cpp_diff.seconds == diff.seconds
  43. assert cpp_diff.microseconds == diff.microseconds
  44. def test_chrono_duration_subtraction_equivalence():
  45. from pybind11_tests import test_chrono4
  46. import datetime
  47. date1 = datetime.datetime.today()
  48. date2 = datetime.datetime.today()
  49. diff = date2 - date1
  50. cpp_diff = test_chrono4(date2, date1)
  51. assert cpp_diff.days == diff.days
  52. assert cpp_diff.seconds == diff.seconds
  53. assert cpp_diff.microseconds == diff.microseconds
  54. def test_chrono_steady_clock():
  55. from pybind11_tests import test_chrono5
  56. import datetime
  57. time1 = test_chrono5()
  58. time2 = test_chrono5()
  59. assert isinstance(time1, datetime.timedelta)
  60. assert isinstance(time2, datetime.timedelta)
  61. def test_chrono_steady_clock_roundtrip():
  62. from pybind11_tests import test_chrono6
  63. import datetime
  64. time1 = datetime.timedelta(days=10, seconds=10, microseconds=100)
  65. time2 = test_chrono6(time1)
  66. assert isinstance(time2, datetime.timedelta)
  67. # They should be identical (no information lost on roundtrip)
  68. assert time1.days == time2.days
  69. assert time1.seconds == time2.seconds
  70. assert time1.microseconds == time2.microseconds
  71. def test_floating_point_duration():
  72. from pybind11_tests import test_chrono7, test_chrono_float_diff
  73. import datetime
  74. # Test using 35.525123 seconds as an example floating point number in seconds
  75. time = test_chrono7(35.525123)
  76. assert isinstance(time, datetime.timedelta)
  77. assert time.seconds == 35
  78. assert 525122 <= time.microseconds <= 525123
  79. diff = test_chrono_float_diff(43.789012, 1.123456)
  80. assert diff.seconds == 42
  81. assert 665556 <= diff.microseconds <= 665557