Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17:52

0001 /*
0002     tests/test_chrono.cpp -- test conversions to/from std::chrono types
0003 
0004     Copyright (c) 2016 Trent Houliston <trent@houliston.me> and
0005                        Wenzel Jakob <wenzel.jakob@epfl.ch>
0006 
0007     All rights reserved. Use of this source code is governed by a
0008     BSD-style license that can be found in the LICENSE file.
0009 */
0010 
0011 #include <pybind11/chrono.h>
0012 
0013 #include "pybind11_tests.h"
0014 
0015 #include <chrono>
0016 
0017 struct different_resolutions {
0018     using time_point_h = std::chrono::time_point<std::chrono::system_clock, std::chrono::hours>;
0019     using time_point_m = std::chrono::time_point<std::chrono::system_clock, std::chrono::minutes>;
0020     using time_point_s = std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>;
0021     using time_point_ms
0022         = std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>;
0023     using time_point_us
0024         = std::chrono::time_point<std::chrono::system_clock, std::chrono::microseconds>;
0025     time_point_h timestamp_h;
0026     time_point_m timestamp_m;
0027     time_point_s timestamp_s;
0028     time_point_ms timestamp_ms;
0029     time_point_us timestamp_us;
0030 };
0031 
0032 TEST_SUBMODULE(chrono, m) {
0033     using system_time = std::chrono::system_clock::time_point;
0034     using steady_time = std::chrono::steady_clock::time_point;
0035 
0036     using timespan = std::chrono::duration<int64_t, std::nano>;
0037     using timestamp = std::chrono::time_point<std::chrono::system_clock, timespan>;
0038 
0039     // test_chrono_system_clock
0040     // Return the current time off the wall clock
0041     m.def("test_chrono1", []() { return std::chrono::system_clock::now(); });
0042 
0043     // test_chrono_system_clock_roundtrip
0044     // Round trip the passed in system clock time
0045     m.def("test_chrono2", [](system_time t) { return t; });
0046 
0047     // test_chrono_duration_roundtrip
0048     // Round trip the passed in duration
0049     m.def("test_chrono3", [](std::chrono::system_clock::duration d) { return d; });
0050 
0051     // test_chrono_duration_subtraction_equivalence
0052     // Difference between two passed in time_points
0053     m.def("test_chrono4", [](system_time a, system_time b) { return a - b; });
0054 
0055     // test_chrono_steady_clock
0056     // Return the current time off the steady_clock
0057     m.def("test_chrono5", []() { return std::chrono::steady_clock::now(); });
0058 
0059     // test_chrono_steady_clock_roundtrip
0060     // Round trip a steady clock timepoint
0061     m.def("test_chrono6", [](steady_time t) { return t; });
0062 
0063     // test_floating_point_duration
0064     // Roundtrip a duration in microseconds from a float argument
0065     m.def("test_chrono7", [](std::chrono::microseconds t) { return t; });
0066     // Float durations (issue #719)
0067     m.def("test_chrono_float_diff",
0068           [](std::chrono::duration<float> a, std::chrono::duration<float> b) { return a - b; });
0069 
0070     m.def("test_nano_timepoint",
0071           [](timestamp start, timespan delta) -> timestamp { return start + delta; });
0072 
0073     // Test different resolutions
0074     py::class_<different_resolutions>(m, "different_resolutions")
0075         .def(py::init<>())
0076         .def_readwrite("timestamp_h", &different_resolutions::timestamp_h)
0077         .def_readwrite("timestamp_m", &different_resolutions::timestamp_m)
0078         .def_readwrite("timestamp_s", &different_resolutions::timestamp_s)
0079         .def_readwrite("timestamp_ms", &different_resolutions::timestamp_ms)
0080         .def_readwrite("timestamp_us", &different_resolutions::timestamp_us);
0081 }