blob: 899d08d8d8542cad92a58bf32b04ab3b74aebb75 [file] [log] [blame]
Trent Houliston352149e2016-08-25 23:08:04 +10001/*
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 Houliston352149e2016-08-25 23:08:04 +100011#include "pybind11_tests.h"
Trent Houliston352149e2016-08-25 23:08:04 +100012#include <pybind11/chrono.h>
13
Jason Rhinelander391c7542017-07-25 16:47:36 -040014TEST_SUBMODULE(chrono, m) {
15 using system_time = std::chrono::system_clock::time_point;
16 using steady_time = std::chrono::steady_clock::time_point;
Alexander Gagarinb3bf2482019-06-13 12:17:10 +050017
18 using timespan = std::chrono::duration<int64_t, std::nano>;
19 using timestamp = std::chrono::time_point<std::chrono::system_clock, timespan>;
20
Jason Rhinelander391c7542017-07-25 16:47:36 -040021 // test_chrono_system_clock
22 // Return the current time off the wall clock
23 m.def("test_chrono1", []() { return std::chrono::system_clock::now(); });
Trent Houliston352149e2016-08-25 23:08:04 +100024
Jason Rhinelander391c7542017-07-25 16:47:36 -040025 // test_chrono_system_clock_roundtrip
26 // Round trip the passed in system clock time
27 m.def("test_chrono2", [](system_time t) { return t; });
Trent Houliston352149e2016-08-25 23:08:04 +100028
Jason Rhinelander391c7542017-07-25 16:47:36 -040029 // test_chrono_duration_roundtrip
30 // Round trip the passed in duration
31 m.def("test_chrono3", [](std::chrono::system_clock::duration d) { return d; });
Trent Houliston352149e2016-08-25 23:08:04 +100032
Jason Rhinelander391c7542017-07-25 16:47:36 -040033 // test_chrono_duration_subtraction_equivalence
34 // Difference between two passed in time_points
35 m.def("test_chrono4", [](system_time a, system_time b) { return a - b; });
Trent Houliston352149e2016-08-25 23:08:04 +100036
Jason Rhinelander391c7542017-07-25 16:47:36 -040037 // test_chrono_steady_clock
38 // Return the current time off the steady_clock
39 m.def("test_chrono5", []() { return std::chrono::steady_clock::now(); });
Trent Houliston352149e2016-08-25 23:08:04 +100040
Jason Rhinelander391c7542017-07-25 16:47:36 -040041 // test_chrono_steady_clock_roundtrip
42 // Round trip a steady clock timepoint
43 m.def("test_chrono6", [](steady_time t) { return t; });
Trent Houliston352149e2016-08-25 23:08:04 +100044
Jason Rhinelander391c7542017-07-25 16:47:36 -040045 // test_floating_point_duration
46 // Roundtrip a duration in microseconds from a float argument
47 m.def("test_chrono7", [](std::chrono::microseconds t) { return t; });
48 // Float durations (issue #719)
49 m.def("test_chrono_float_diff", [](std::chrono::duration<float> a, std::chrono::duration<float> b) {
50 return a - b; });
Alexander Gagarinb3bf2482019-06-13 12:17:10 +050051
52 m.def("test_nano_timepoint", [](timestamp start, timespan delta) -> timestamp {
53 return start + delta;
54 });
Trent Houliston2f597682016-09-13 20:40:28 +100055}