Line data Source code
1 : // Copyright 2024 Accenture. 2 : 3 : #include <estd/assert.h> 4 : #include <estd/chrono.h> 5 : 6 : #include <gtest/gtest.h> 7 : 8 : #include <iostream> 9 : 10 : namespace 11 : { 12 : namespace chrono_example 13 : { 14 : struct Clock 15 : { 16 : using duration = estd::chrono::minutes; 17 : }; 18 : } // namespace chrono_example 19 : 20 3 : TEST(UniquePoolPtrExample, sum1) 21 : { 22 : // [EXAMPLE_CHRONO_DURATION_START] 23 : 24 : // using milli = duration<int32_t>; 25 1 : estd::chrono::duration<int32_t, estd::milli> src(1001); 26 1 : { 27 : // using duration_cast to pass the value 28 1 : estd::chrono::duration<int32_t, estd::micro> cut 29 1 : = estd::chrono::duration_cast<estd::chrono::duration<int32_t, estd::micro>>(src); 30 : 31 : // using count to print duration 32 1 : std::cout << "Duration: " << cut.count() << "ms\n"; 33 : 34 : // using extreme duration values from duration 35 1 : estd::chrono::duration<int8_t, estd::milli>::min().count(); 36 1 : estd::chrono::duration<int8_t, estd::milli>::max().count(); 37 : } 38 : 39 : // using seconds = duration<int64_t>; 40 1 : auto const ts1 = ::estd::chrono::seconds(42); 41 : 42 : // using milliseconds = duration<int64_t, milli>; 43 1 : auto const ts2 = ::estd::chrono::milliseconds(42000); 44 : 45 : // duration_cast using equality operator 46 : 47 1 : estd_assert(::estd::chrono::duration_cast<::estd::chrono::seconds>(ts2) == ts1); 48 : // [EXAMPLE_CHRONO_DURATION_END] 49 1 : } 50 : 51 3 : TEST(UniquePoolPtrExample, sum2) 52 : { 53 : // [EXAMPLE_CHRONO_TIME_POINT_START] 54 : 55 : // time_point created 56 1 : estd::chrono::time_point<chrono_example::Clock, estd::chrono::milliseconds> cut; 57 : 58 : // the time after the clock start is 0 59 1 : std::cout << "time_point: " << cut.time_since_epoch().count() << "ms\n"; 60 : 61 : // value of 715 micro seconds is passed to time_point 62 1 : estd::chrono::time_point<chrono_example::Clock, estd::chrono::microseconds> cut1( 63 1 : estd::chrono::microseconds(715)); 64 : 65 : // the time after the clock start is 715 micro seconds 66 1 : std::cout << "time_point: " << cut1.time_since_epoch().count() << "ms\n"; 67 : 68 : // using extreme duration values from time_point 69 1 : estd::chrono::time_point<chrono_example::Clock, estd::chrono::duration<int8_t, estd::milli>>:: 70 1 : min() 71 1 : .time_since_epoch() 72 1 : .count(); 73 : 74 1 : estd::chrono::time_point<chrono_example::Clock, estd::chrono::duration<int8_t, estd::milli>>:: 75 1 : max() 76 1 : .time_since_epoch() 77 1 : .count(); 78 : 79 : // [EXAMPLE_CHRONO_TIME_POINT_END] 80 1 : } 81 : 82 : } // namespace