Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 1 | from pybind11_tests import chrono as m |
| 2 | import datetime |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 3 | |
| 4 | |
| 5 | def test_chrono_system_clock(): |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 6 | |
| 7 | # Get the time from both c++ and datetime |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 8 | date1 = m.test_chrono1() |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 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(): |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 28 | date1 = datetime.datetime.today() |
| 29 | |
| 30 | # Roundtrip the time |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 31 | date2 = m.test_chrono2(date1) |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 32 | |
| 33 | # The returned value should be a datetime |
| 34 | assert isinstance(date2, datetime.datetime) |
| 35 | |
| 36 | # They should be identical (no information lost on roundtrip) |
| 37 | diff = abs(date1 - date2) |
| 38 | assert diff.days == 0 |
| 39 | assert diff.seconds == 0 |
| 40 | assert diff.microseconds == 0 |
| 41 | |
| 42 | |
| 43 | def test_chrono_duration_roundtrip(): |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 44 | |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 45 | # Get the difference between two times (a timedelta) |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 46 | date1 = datetime.datetime.today() |
| 47 | date2 = datetime.datetime.today() |
| 48 | diff = date2 - date1 |
| 49 | |
| 50 | # Make sure this is a timedelta |
| 51 | assert isinstance(diff, datetime.timedelta) |
| 52 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 53 | cpp_diff = m.test_chrono3(diff) |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 54 | |
| 55 | assert cpp_diff.days == diff.days |
| 56 | assert cpp_diff.seconds == diff.seconds |
| 57 | assert cpp_diff.microseconds == diff.microseconds |
| 58 | |
| 59 | |
| 60 | def test_chrono_duration_subtraction_equivalence(): |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 61 | |
| 62 | date1 = datetime.datetime.today() |
| 63 | date2 = datetime.datetime.today() |
| 64 | |
| 65 | diff = date2 - date1 |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 66 | cpp_diff = m.test_chrono4(date2, date1) |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 67 | |
| 68 | assert cpp_diff.days == diff.days |
| 69 | assert cpp_diff.seconds == diff.seconds |
| 70 | assert cpp_diff.microseconds == diff.microseconds |
| 71 | |
| 72 | |
| 73 | def test_chrono_steady_clock(): |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 74 | time1 = m.test_chrono5() |
Trent Houliston | 2f59768 | 2016-09-13 20:40:28 +1000 | [diff] [blame] | 75 | assert isinstance(time1, datetime.timedelta) |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 76 | |
| 77 | |
| 78 | def test_chrono_steady_clock_roundtrip(): |
Trent Houliston | 2f59768 | 2016-09-13 20:40:28 +1000 | [diff] [blame] | 79 | time1 = datetime.timedelta(days=10, seconds=10, microseconds=100) |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 80 | time2 = m.test_chrono6(time1) |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 81 | |
Trent Houliston | 2f59768 | 2016-09-13 20:40:28 +1000 | [diff] [blame] | 82 | assert isinstance(time2, datetime.timedelta) |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 83 | |
| 84 | # They should be identical (no information lost on roundtrip) |
Trent Houliston | 2f59768 | 2016-09-13 20:40:28 +1000 | [diff] [blame] | 85 | assert time1.days == time2.days |
| 86 | assert time1.seconds == time2.seconds |
| 87 | assert time1.microseconds == time2.microseconds |
| 88 | |
| 89 | |
| 90 | def test_floating_point_duration(): |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 91 | # Test using a floating point number in seconds |
| 92 | time = m.test_chrono7(35.525123) |
Trent Houliston | 2f59768 | 2016-09-13 20:40:28 +1000 | [diff] [blame] | 93 | |
| 94 | assert isinstance(time, datetime.timedelta) |
| 95 | |
| 96 | assert time.seconds == 35 |
Trent Houliston | 253e41c | 2016-09-28 00:59:21 +1000 | [diff] [blame] | 97 | assert 525122 <= time.microseconds <= 525123 |
Jason Rhinelander | e5456c2 | 2017-03-11 22:29:25 -0400 | [diff] [blame] | 98 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 99 | diff = m.test_chrono_float_diff(43.789012, 1.123456) |
Jason Rhinelander | e5456c2 | 2017-03-11 22:29:25 -0400 | [diff] [blame] | 100 | assert diff.seconds == 42 |
| 101 | assert 665556 <= diff.microseconds <= 665557 |
Alexander Gagarin | b3bf248 | 2019-06-13 12:17:10 +0500 | [diff] [blame^] | 102 | |
| 103 | |
| 104 | def test_nano_timepoint(): |
| 105 | time = datetime.datetime.now() |
| 106 | time1 = m.test_nano_timepoint(time, datetime.timedelta(seconds=60)) |
| 107 | assert(time1 == time + datetime.timedelta(seconds=60)) |