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 | |
| 11 | |
| 12 | #include "pybind11_tests.h" |
| 13 | #include "constructor_stats.h" |
| 14 | #include <pybind11/chrono.h> |
| 15 | |
| 16 | // Return the current time off the wall clock |
| 17 | std::chrono::system_clock::time_point test_chrono1() { |
| 18 | return std::chrono::system_clock::now(); |
| 19 | } |
| 20 | |
| 21 | // Round trip the passed in system clock time |
| 22 | std::chrono::system_clock::time_point test_chrono2(std::chrono::system_clock::time_point t) { |
| 23 | return t; |
| 24 | } |
| 25 | |
| 26 | // Round trip the passed in duration |
| 27 | std::chrono::system_clock::duration test_chrono3(std::chrono::system_clock::duration d) { |
| 28 | return d; |
| 29 | } |
| 30 | |
| 31 | // Difference between two passed in time_points |
| 32 | std::chrono::system_clock::duration test_chrono4(std::chrono::system_clock::time_point a, std::chrono::system_clock::time_point b) { |
| 33 | return a - b; |
| 34 | } |
| 35 | |
| 36 | // Return the current time off the steady_clock |
| 37 | std::chrono::steady_clock::time_point test_chrono5() { |
| 38 | return std::chrono::steady_clock::now(); |
| 39 | } |
| 40 | |
| 41 | // Round trip a steady clock timepoint |
| 42 | std::chrono::steady_clock::time_point test_chrono6(std::chrono::steady_clock::time_point t) { |
| 43 | return t; |
| 44 | } |
| 45 | |
Trent Houliston | 2f59768 | 2016-09-13 20:40:28 +1000 | [diff] [blame] | 46 | // Roundtrip a duration in microseconds from a float argument |
| 47 | std::chrono::microseconds test_chrono7(std::chrono::microseconds t) { |
| 48 | return t; |
| 49 | } |
| 50 | |
Jason Rhinelander | e5456c2 | 2017-03-11 22:29:25 -0400 | [diff] [blame] | 51 | // Float durations (issue #719) |
| 52 | std::chrono::duration<double> test_chrono_float_diff(std::chrono::duration<float> a, std::chrono::duration<float> b) { |
| 53 | return a - b; |
| 54 | } |
| 55 | |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 56 | test_initializer chrono([] (py::module &m) { |
| 57 | m.def("test_chrono1", &test_chrono1); |
| 58 | m.def("test_chrono2", &test_chrono2); |
| 59 | m.def("test_chrono3", &test_chrono3); |
| 60 | m.def("test_chrono4", &test_chrono4); |
| 61 | m.def("test_chrono5", &test_chrono5); |
| 62 | m.def("test_chrono6", &test_chrono6); |
Trent Houliston | 2f59768 | 2016-09-13 20:40:28 +1000 | [diff] [blame] | 63 | m.def("test_chrono7", &test_chrono7); |
Jason Rhinelander | e5456c2 | 2017-03-11 22:29:25 -0400 | [diff] [blame] | 64 | m.def("test_chrono_float_diff", &test_chrono_float_diff); |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 65 | }); |