Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 1 | /* |
| 2 | tests/test_chrono.cpp -- test conversions to/from std::chrono types |
| 3 | |
| 4 | Copyright (c) 2016 Trent Houliston <trent@houliston.me> and |
| 5 | Wenzel Jakob <wenzel.jakob@epfl.ch> |
| 6 | |
| 7 | All rights reserved. Use of this source code is governed by a |
| 8 | BSD-style license that can be found in the LICENSE file. |
| 9 | */ |
| 10 | |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 11 | #include "pybind11_tests.h" |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 12 | #include <pybind11/chrono.h> |
Yannick Jadoul | 6a19278 | 2020-08-28 15:21:43 +0200 | [diff] [blame^] | 13 | #include <chrono> |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 14 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 15 | TEST_SUBMODULE(chrono, m) { |
| 16 | using system_time = std::chrono::system_clock::time_point; |
| 17 | using steady_time = std::chrono::steady_clock::time_point; |
Alexander Gagarin | b3bf248 | 2019-06-13 12:17:10 +0500 | [diff] [blame] | 18 | |
| 19 | using timespan = std::chrono::duration<int64_t, std::nano>; |
| 20 | using timestamp = std::chrono::time_point<std::chrono::system_clock, timespan>; |
| 21 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 22 | // test_chrono_system_clock |
| 23 | // Return the current time off the wall clock |
| 24 | m.def("test_chrono1", []() { return std::chrono::system_clock::now(); }); |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 25 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 26 | // test_chrono_system_clock_roundtrip |
| 27 | // Round trip the passed in system clock time |
| 28 | m.def("test_chrono2", [](system_time t) { return t; }); |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 29 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 30 | // test_chrono_duration_roundtrip |
| 31 | // Round trip the passed in duration |
| 32 | m.def("test_chrono3", [](std::chrono::system_clock::duration d) { return d; }); |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 33 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 34 | // test_chrono_duration_subtraction_equivalence |
| 35 | // Difference between two passed in time_points |
| 36 | m.def("test_chrono4", [](system_time a, system_time b) { return a - b; }); |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 37 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 38 | // test_chrono_steady_clock |
| 39 | // Return the current time off the steady_clock |
| 40 | m.def("test_chrono5", []() { return std::chrono::steady_clock::now(); }); |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 41 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 42 | // test_chrono_steady_clock_roundtrip |
| 43 | // Round trip a steady clock timepoint |
| 44 | m.def("test_chrono6", [](steady_time t) { return t; }); |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 45 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 46 | // test_floating_point_duration |
| 47 | // Roundtrip a duration in microseconds from a float argument |
| 48 | m.def("test_chrono7", [](std::chrono::microseconds t) { return t; }); |
| 49 | // Float durations (issue #719) |
| 50 | m.def("test_chrono_float_diff", [](std::chrono::duration<float> a, std::chrono::duration<float> b) { |
| 51 | return a - b; }); |
Alexander Gagarin | b3bf248 | 2019-06-13 12:17:10 +0500 | [diff] [blame] | 52 | |
| 53 | m.def("test_nano_timepoint", [](timestamp start, timespan delta) -> timestamp { |
| 54 | return start + delta; |
| 55 | }); |
Trent Houliston | 2f59768 | 2016-09-13 20:40:28 +1000 | [diff] [blame] | 56 | } |