blob: 2b75bd19148637b47539de2b8fcd9473df86f7a6 [file] [log] [blame]
Jason Rhinelander391c7542017-07-25 16:47:36 -04001from pybind11_tests import chrono as m
2import datetime
Trent Houliston352149e2016-08-25 23:08:04 +10003
4
5def test_chrono_system_clock():
Trent Houliston352149e2016-08-25 23:08:04 +10006
7 # Get the time from both c++ and datetime
Jason Rhinelander391c7542017-07-25 16:47:36 -04008 date1 = m.test_chrono1()
Trent Houliston352149e2016-08-25 23:08:04 +10009 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 Houliston253e41c2016-09-28 00:59:21 +100021 # 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 Houliston352149e2016-08-25 23:08:04 +100025
26
27def test_chrono_system_clock_roundtrip():
Trent Houliston352149e2016-08-25 23:08:04 +100028 date1 = datetime.datetime.today()
29
30 # Roundtrip the time
Jason Rhinelander391c7542017-07-25 16:47:36 -040031 date2 = m.test_chrono2(date1)
Trent Houliston352149e2016-08-25 23:08:04 +100032
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
43def test_chrono_duration_roundtrip():
Trent Houliston352149e2016-08-25 23:08:04 +100044
Dean Moldovanbad17402016-11-20 21:21:54 +010045 # Get the difference between two times (a timedelta)
Trent Houliston352149e2016-08-25 23:08:04 +100046 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 Rhinelander391c7542017-07-25 16:47:36 -040053 cpp_diff = m.test_chrono3(diff)
Trent Houliston352149e2016-08-25 23:08:04 +100054
55 assert cpp_diff.days == diff.days
56 assert cpp_diff.seconds == diff.seconds
57 assert cpp_diff.microseconds == diff.microseconds
58
59
60def test_chrono_duration_subtraction_equivalence():
Trent Houliston352149e2016-08-25 23:08:04 +100061
62 date1 = datetime.datetime.today()
63 date2 = datetime.datetime.today()
64
65 diff = date2 - date1
Jason Rhinelander391c7542017-07-25 16:47:36 -040066 cpp_diff = m.test_chrono4(date2, date1)
Trent Houliston352149e2016-08-25 23:08:04 +100067
68 assert cpp_diff.days == diff.days
69 assert cpp_diff.seconds == diff.seconds
70 assert cpp_diff.microseconds == diff.microseconds
71
72
73def test_chrono_steady_clock():
Jason Rhinelander391c7542017-07-25 16:47:36 -040074 time1 = m.test_chrono5()
Trent Houliston2f597682016-09-13 20:40:28 +100075 assert isinstance(time1, datetime.timedelta)
Trent Houliston352149e2016-08-25 23:08:04 +100076
77
78def test_chrono_steady_clock_roundtrip():
Trent Houliston2f597682016-09-13 20:40:28 +100079 time1 = datetime.timedelta(days=10, seconds=10, microseconds=100)
Jason Rhinelander391c7542017-07-25 16:47:36 -040080 time2 = m.test_chrono6(time1)
Trent Houliston352149e2016-08-25 23:08:04 +100081
Trent Houliston2f597682016-09-13 20:40:28 +100082 assert isinstance(time2, datetime.timedelta)
Trent Houliston352149e2016-08-25 23:08:04 +100083
84 # They should be identical (no information lost on roundtrip)
Trent Houliston2f597682016-09-13 20:40:28 +100085 assert time1.days == time2.days
86 assert time1.seconds == time2.seconds
87 assert time1.microseconds == time2.microseconds
88
89
90def test_floating_point_duration():
Jason Rhinelander391c7542017-07-25 16:47:36 -040091 # Test using a floating point number in seconds
92 time = m.test_chrono7(35.525123)
Trent Houliston2f597682016-09-13 20:40:28 +100093
94 assert isinstance(time, datetime.timedelta)
95
96 assert time.seconds == 35
Trent Houliston253e41c2016-09-28 00:59:21 +100097 assert 525122 <= time.microseconds <= 525123
Jason Rhinelandere5456c22017-03-11 22:29:25 -040098
Jason Rhinelander391c7542017-07-25 16:47:36 -040099 diff = m.test_chrono_float_diff(43.789012, 1.123456)
Jason Rhinelandere5456c22017-03-11 22:29:25 -0400100 assert diff.seconds == 42
101 assert 665556 <= diff.microseconds <= 665557