Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 1 | |
| 2 | |
| 3 | def test_chrono_system_clock(): |
| 4 | from pybind11_tests import test_chrono1 |
| 5 | import datetime |
| 6 | |
| 7 | # Get the time from both c++ and datetime |
| 8 | date1 = test_chrono1() |
| 9 | date2 = datetime.datetime.today() |
| 10 | |
| 11 | # The returned value should be a datetime |
| 12 | assert isinstance(date1, datetime.datetime) |
| 13 | |
| 14 | # The numbers should vary by a very small amount (time it took to execute) |
| 15 | diff = abs(date1 - date2) |
| 16 | |
| 17 | # There should never be a days/seconds difference |
| 18 | assert diff.days == 0 |
| 19 | assert diff.seconds == 0 |
| 20 | |
Trent Houliston | 253e41c | 2016-09-28 00:59:21 +1000 | [diff] [blame] | 21 | # We test that no more than about 0.5 seconds passes here |
| 22 | # This makes sure that the dates created are very close to the same |
| 23 | # but if the testing system is incredibly overloaded this should still pass |
| 24 | assert diff.microseconds < 500000 |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 25 | |
| 26 | |
| 27 | def test_chrono_system_clock_roundtrip(): |
| 28 | from pybind11_tests import test_chrono2 |
| 29 | import datetime |
| 30 | |
| 31 | date1 = datetime.datetime.today() |
| 32 | |
| 33 | # Roundtrip the time |
| 34 | date2 = test_chrono2(date1) |
| 35 | |
| 36 | # The returned value should be a datetime |
| 37 | assert isinstance(date2, datetime.datetime) |
| 38 | |
| 39 | # They should be identical (no information lost on roundtrip) |
| 40 | diff = abs(date1 - date2) |
| 41 | assert diff.days == 0 |
| 42 | assert diff.seconds == 0 |
| 43 | assert diff.microseconds == 0 |
| 44 | |
| 45 | |
| 46 | def test_chrono_duration_roundtrip(): |
| 47 | from pybind11_tests import test_chrono3 |
| 48 | import datetime |
| 49 | |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 50 | # Get the difference between two times (a timedelta) |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 51 | date1 = datetime.datetime.today() |
| 52 | date2 = datetime.datetime.today() |
| 53 | diff = date2 - date1 |
| 54 | |
| 55 | # Make sure this is a timedelta |
| 56 | assert isinstance(diff, datetime.timedelta) |
| 57 | |
| 58 | cpp_diff = test_chrono3(diff) |
| 59 | |
| 60 | assert cpp_diff.days == diff.days |
| 61 | assert cpp_diff.seconds == diff.seconds |
| 62 | assert cpp_diff.microseconds == diff.microseconds |
| 63 | |
| 64 | |
| 65 | def test_chrono_duration_subtraction_equivalence(): |
| 66 | from pybind11_tests import test_chrono4 |
| 67 | import datetime |
| 68 | |
| 69 | date1 = datetime.datetime.today() |
| 70 | date2 = datetime.datetime.today() |
| 71 | |
| 72 | diff = date2 - date1 |
| 73 | cpp_diff = test_chrono4(date2, date1) |
| 74 | |
| 75 | assert cpp_diff.days == diff.days |
| 76 | assert cpp_diff.seconds == diff.seconds |
| 77 | assert cpp_diff.microseconds == diff.microseconds |
| 78 | |
| 79 | |
| 80 | def test_chrono_steady_clock(): |
| 81 | from pybind11_tests import test_chrono5 |
| 82 | import datetime |
| 83 | |
| 84 | time1 = test_chrono5() |
| 85 | time2 = test_chrono5() |
| 86 | |
Trent Houliston | 2f59768 | 2016-09-13 20:40:28 +1000 | [diff] [blame] | 87 | assert isinstance(time1, datetime.timedelta) |
| 88 | assert isinstance(time2, datetime.timedelta) |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 89 | |
| 90 | |
| 91 | def test_chrono_steady_clock_roundtrip(): |
| 92 | from pybind11_tests import test_chrono6 |
| 93 | import datetime |
| 94 | |
Trent Houliston | 2f59768 | 2016-09-13 20:40:28 +1000 | [diff] [blame] | 95 | time1 = datetime.timedelta(days=10, seconds=10, microseconds=100) |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 96 | time2 = test_chrono6(time1) |
| 97 | |
Trent Houliston | 2f59768 | 2016-09-13 20:40:28 +1000 | [diff] [blame] | 98 | assert isinstance(time2, datetime.timedelta) |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 99 | |
| 100 | # They should be identical (no information lost on roundtrip) |
Trent Houliston | 2f59768 | 2016-09-13 20:40:28 +1000 | [diff] [blame] | 101 | assert time1.days == time2.days |
| 102 | assert time1.seconds == time2.seconds |
| 103 | assert time1.microseconds == time2.microseconds |
| 104 | |
| 105 | |
| 106 | def test_floating_point_duration(): |
Jason Rhinelander | e5456c2 | 2017-03-11 22:29:25 -0400 | [diff] [blame] | 107 | from pybind11_tests import test_chrono7, test_chrono_float_diff |
Trent Houliston | 2f59768 | 2016-09-13 20:40:28 +1000 | [diff] [blame] | 108 | import datetime |
| 109 | |
| 110 | # Test using 35.525123 seconds as an example floating point number in seconds |
| 111 | time = test_chrono7(35.525123) |
| 112 | |
| 113 | assert isinstance(time, datetime.timedelta) |
| 114 | |
| 115 | assert time.seconds == 35 |
Trent Houliston | 253e41c | 2016-09-28 00:59:21 +1000 | [diff] [blame] | 116 | assert 525122 <= time.microseconds <= 525123 |
Jason Rhinelander | e5456c2 | 2017-03-11 22:29:25 -0400 | [diff] [blame] | 117 | |
| 118 | diff = test_chrono_float_diff(43.789012, 1.123456) |
| 119 | assert diff.seconds == 42 |
| 120 | assert 665556 <= diff.microseconds <= 665557 |