etl/test/test_chrono_time_point.cpp
John Wellbelove afa17c5042 Squashed commit of the following:
commit c5dc331004b4d9611ab996935bedada203676c71
Author: John Wellbelove <john.wellbelove@etlcpp.com>
Date:   Sat Jul 11 12:32:20 2026 +0100

    Updated release notes

commit 4bf6ed6ce8d79857658d763e368ee56617542b2c
Author: vcoselev <145198867+vcoselev@users.noreply.github.com>
Date:   Sat Jul 11 12:05:39 2026 +0100

    Fix for 1405: etl::exchange doesn't work with unique_ptr (#1477)

    * Separate bit order and endianness in bit_stream.h (#1495)

    * Factor out ranges tests from test_algorithm.cpp to test_algorithm_ranges.cpp (#1497)

    * Fix exchange to use forwarding reference (U&&) and move

    - etl::exchange was using the copy constructor. Changed for etl::move.
    - The assignment to the new object is now done with forward.
    - Added a test with unique_ptr.

    ---------

    Co-authored-by: Roland Reichwein <Roland.Reichwein@bmw.de>
    Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>

commit 2ed9019df04d470530c41c1f27d35adc30b9a32d
Author: Moritz Pflanzer <moritz@pflanzer.eu>
Date:   Fri Jul 10 11:42:32 2026 +0200

    Update operator[] docs for map class (#1499)

    * Separate bit order and endianness in bit_stream.h (#1495)

    * Update operator[] docs for map class

    No const variant is implemented for the operator[].

    ---------

    Co-authored-by: Roland Reichwein <Roland.Reichwein@bmw.de>

commit 423e18d96dc645f1c733cb0d8ae6b675b9eb7e83
Author: Roland Reichwein <Roland.Reichwein@bmw.de>
Date:   Fri Jul 10 11:14:08 2026 +0200

    Factor out ranges tests from test_algorithm.cpp to test_algorithm_ranges.cpp (#1497)

commit 338bfdbb1f4df3c5c08a68a9038f13b8dae1c28b
Author: John Wellbelove <jwellbelove@users.noreply.github.com>
Date:   Fri Jul 10 10:12:04 2026 +0100

    Add coverage report to docs (#1498)

    * Updated release notes

    * Added link to coverage report

    ---------

    Co-authored-by: John Wellbelove <john.wellbelove@etlcpp.com>

commit 269025424f4c6f61cebac090f4df82985fb3b239
Merge: c004cc9d faa4c15b
Author: John Wellbelove <john.wellbelove@etlcpp.com>
Date:   Thu Jul 9 14:18:32 2026 +0100

    Merge branch 'development' of https://github.com/ETLCPP/etl into development

commit c004cc9d508efcf398630758a47aae95b37a7eb8
Author: John Wellbelove <john.wellbelove@etlcpp.com>
Date:   Thu Jul 9 10:06:17 2026 +0100

    Updated release notes

commit faa4c15b49c8ad15475c83ae23e00f5a46f803ca
Author: John Wellbelove <john.wellbelove@etlcpp.com>
Date:   Thu Jul 9 10:06:17 2026 +0100

    Updated release notes

commit 1523e63e35a15cfd1fa72d230a3c79efa7b9c3e0
Author: Roland Reichwein <Roland.Reichwein@bmw.de>
Date:   Wed Jul 8 20:48:33 2026 +0200

    Support non-assignable alternatives in variant (#1496)

commit 878a0ba5a28f6b0dcae9f472a6a6a1f107de0a74
Author: John Wellbelove <john.wellbelove@etlcpp.com>
Date:   Wed Jul 8 11:07:01 2026 +0100

    Updated version and release notes

commit 5287fe4b3b9f58330881d2a62abf6ce8e1159b8d
Author: John Wellbelove <jwellbelove@users.noreply.github.com>
Date:   Wed Jul 8 09:06:50 2026 +0100

    Allow etl::vector to store pointers to functions and member functions (#1492)

    * Refactored vector.h

    Deleted ivectorpointer.h

    * Minor changes

    * Fixed code incompatible with C++03

    * Clang-format changes

    * Removed redundant double-qualified name

    * Fixed is_is_object_pointer_v to is_object_pointer_v

    * Added new test files to meson.build

    * Added overflow checks to vector_ext copy and move constructors and assignments

    Added assert throw tests.

    * Added diagnostic pushes to eliminate GCC warning false positives

    * Changed the GCC C++23 Github action to use GCC14

    * Applied clang-format

    * Added guard to pop_heap

    * Added guard to adjust_heap

    * Attempt to fix GCC diagnostic

    * Attempt to fix GCC diagnostic

    Re-enable ranges algorithm tests

    * Attempt to fix GCC diagnostic

    Re-enable ranges algorithm tests

    * clang-format

    * Attempt to fix GCC diagnostic

    Re-enable ranges algorithm tests

    * Attempt to fix GCC diagnostic

    Re-enable ranges algorithm tests

    * Attempt to fix GCC diagnostic

    Re-enable ranges algorithm tests

    * Missing newline at end of file

    * Attempt to fix GCC diagnostic

    Re-enable ranges algorithm tests

    * Attempt to fix GCC diagnostic

    Re-enable ranges algorithm tests

    * Attempt to fix GCC diagnostic

    Re-enable ranges algorithm tests

    * Attempt to fix GCC diagnostic

    Re-enable ranges algorithm tests

    * Attempt to fix GCC diagnostic

    * clang-format

    * Changed gcc --version to gcc-14 --version

    ---------

    Co-authored-by: John Wellbelove <john.wellbelove@etlcpp.com>
    Co-authored-by: Roland Reichwein <Roland.Reichwein@bmw.de>

commit 40dddd7f9601985ae7350e60a404af9028bc9fe9
Author: Roland Reichwein <Roland.Reichwein@bmw.de>
Date:   Tue Jul 7 23:41:17 2026 +0200

    Add optional etl_profile label_flag for Bazel profile injection (#1491)

    Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>

commit 84fca4cd62f72c382c062376ca934803116a20d5
Author: Roland Reichwein <Roland.Reichwein@bmw.de>
Date:   Tue Jul 7 21:45:47 2026 +0200

    Add missing memory functions, according the standard (#1490)

    Adding:

    - etl::align
    - etl::assume_aligned
    - etl::is_sufficiently_aligned
    - etl::launder
    - etl::pointer_traits
    - etl::start_lifetime_as

    Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>

commit dca6653b3759b2cdc79307fec7425589feea478d
Author: Roland Reichwein <Roland.Reichwein@bmw.de>
Date:   Tue Jul 7 21:00:09 2026 +0200

    Add missing interfaces to etl::chrono (#1489)

    Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>

commit c52b2ac7b1f675e6b1d34052239423b7f3634033
Author: Roland Reichwein <Roland.Reichwein@bmw.de>
Date:   Tue Jul 7 18:06:05 2026 +0200

    Fix etl::mem_cast cross-size copy constructor and assignment (#1488)

    The templated cross-size copy constructor and assignment operator were
    broken in two ways:

    - They accessed other.buffer / rhs.buffer, which is private in a
      different mem_cast instantiation, so the cross-size overloads failed
      to compile whenever they were actually instantiated.
    - They copied Size_ (the destination size) bytes from a source buffer
      that is only Other_Size bytes large, reading past the end of the
      source when the destination was larger.

    Use the public data() accessor and copy Other_Size bytes. The existing
    static_assert(Size >= Other_Size) guarantees the destination is big
    enough.

    Add test_mem_cast_copy_and_assign_from_smaller regression test.

    Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>

commit 165674cb23dda1fd4f7e5a1182f49554a796bfe8
Author: Roland Reichwein <Roland.Reichwein@bmw.de>
Date:   Tue Jul 7 16:41:50 2026 +0200

    Fix etl::pearson::reset() not resetting the first-byte flag (#1487)

    reset() cleared the hash array but left the `first` flag false, so a
    calculator reused after reset() took the XOR update path on its next
    add() instead of the initialisation path. This produced an incorrect
    hash for any pearson object that was reset and reused.

    Reset `first` to true in reset() so a reset calculator behaves like a
    freshly constructed one.

    Add test_pearson_reset regression test.

    Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>

commit 032ec4cc80f1c0f1baa0383bdbc5f9239b68f268
Author: Roland Reichwein <Roland.Reichwein@bmw.de>
Date:   Tue Jul 7 13:40:04 2026 +0200

    Fix etl::bitset single-element from_string shift on empty string (#1486)

    The single-element from_string overloads (char, wchar_t, char16_t,
    char32_t) computed element_type(1) << (string_length - 1U) before the
    copy loop. For an empty string, or when active_bits is 0, string_length
    is 0, so the shift count underflows to SIZE_MAX. Shifting by more than
    the element width is undefined behaviour, which also breaks constexpr
    evaluation.

    Guard the mask with a zero-length check so the shift is only performed
    when string_length > 0. The multi-element from_string overloads were
    already safe.

    Add test_construct_from_empty_string regression test.

    Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>

commit 98cb365d4c28e7b72f8547b734a03a4068acd3f5
Author: Roland Reichwein <Roland.Reichwein@bmw.de>
Date:   Tue Jul 7 12:32:56 2026 +0200

    Fix etl::histogram copy/move not preserving start_index (#1485)

    The run-time-offset specialization of etl::histogram stores a start_index
    member used to map keys to bins (accumulator[key - start_index]). Its copy
    constructor, move constructor, copy assignment, and move assignment copied
    only the accumulator and left start_index uninitialized, so a copied or
    moved histogram indexed the wrong bin in operator[]/add(), causing
    out-of-bounds access (undefined behavior).

    All four special member functions now also copy start_index. The
    compile-time-offset specialization is unaffected, as its start index is a
    template constant rather than a data member.

    Add regression tests covering copy/move construction and assignment.

    Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>

commit 5cd2f28cd54e4019f7be126bcf7394a6a8318eb9
Author: Drew Rife <darife@jlg.com>
Date:   Tue Jul 7 03:14:48 2026 -0400

    Refactor variant::emplace to perfect forward args instead of copy/move construct (#1494)

    * refactor: emplace logic to use do_emplace for in-place construction with forwarded arguments #1493

    * Add test for variant_variadic

    ---------

    Co-authored-by: Roland Reichwein <Roland.Reichwein@bmw.de>

commit a47c0aae81b886259918961d5fcee20faa06fdd0
Author: Roland Reichwein <Roland.Reichwein@bmw.de>
Date:   Mon Jul 6 17:32:26 2026 +0200

    Fix etl::message_timer firing following timers early on unregister (#1484)

    active_list.remove(id, has_expired) adjusts the next timer's delta only
    when has_expired is false. When unregistering an active, non-expired timer,
    unregister_timer() incorrectly passed true, skipping that adjustment, so
    every timer after the removed one in the active list fired early by the
    removed timer's delta.

    Pass false instead, matching the etl::callback_timer family. tick() still
    passes true, since the timer has genuinely expired there. The bug affected
    all four variants: message_timer, message_timer_interrupt,
    message_timer_atomic and message_timer_locked. Existing tests missed it
    because they only ever unregistered the tail timer, where the delta
    adjustment is a no-op.

    Add a regression test that unregisters a non-tail active timer and checks
    the following timer still fires at its original absolute time.
2026-07-11 13:02:42 +01:00

456 lines
15 KiB
C++

/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Documentation:
Copyright(c) 2025 John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#include "etl/platform.h"
#include "unit_test_framework.h"
#include "etl/chrono.h"
#include <algorithm>
#include <vector>
// Set to 0 to reference against std::chrono
#define ETL_USING_ETL_CHRONO 1
#if ETL_USING_ETL_CHRONO
#define Chrono etl::chrono
#else
#if ETL_USING_CPP20
#include <chrono>
#define Chrono std::chrono
#else
#error std::chrono not supported
#endif
#endif
namespace
{
struct test_clock
{
using duration = Chrono::hours;
};
using TimePoint = Chrono::time_point<test_clock>;
SUITE(test_chrono_time_point)
{
//*************************************************************************
TEST(test_default_constructor)
{
TimePoint tp;
CHECK_EQUAL(0, tp.time_since_epoch().count());
}
//*************************************************************************
TEST(test_constructor_with_duration)
{
auto num_days = Chrono::days::period::num;
auto num_hours = Chrono::hours::period::num;
for (int d = 1; d < 100; ++d)
{
TimePoint tp{Chrono::days(d)};
auto hours = tp.time_since_epoch().count();
auto hours_scaled_to_days = (hours * num_hours) / num_days;
CHECK_EQUAL(d, hours_scaled_to_days);
}
}
//*************************************************************************
TEST(test_copy_constructor)
{
auto num_days = Chrono::days::period::num;
auto num_hours = Chrono::hours::period::num;
for (int d = 1; d < 100; ++d)
{
TimePoint tp{Chrono::days(d)};
TimePoint tp2{tp};
auto hours = tp2.time_since_epoch().count();
auto hours_scaled_to_days = (hours * num_hours) / num_days;
CHECK_EQUAL(d, hours_scaled_to_days);
}
}
//*************************************************************************
TEST(test_plus_equal_days)
{
auto num_days = Chrono::days::period::num;
auto num_hours = Chrono::hours::period::num;
Chrono::days ds(2);
TimePoint tp{Chrono::days(0)};
for (int d = 1; d < 100; ++d)
{
tp += ds;
auto hours = tp.time_since_epoch().count();
auto hours_scaled_to_days = (hours * num_hours) / num_days;
CHECK_EQUAL(d * 2, hours_scaled_to_days);
}
}
//*************************************************************************
TEST(test_plus_equal_hours)
{
auto num_days = Chrono::days::period::num;
auto num_hours = Chrono::hours::period::num;
Chrono::hours hs(48);
TimePoint tp{Chrono::days(0)};
for (int d = 1; d < 100; ++d)
{
tp += hs;
auto hours = tp.time_since_epoch().count();
auto hours_scaled_to_days = (hours * num_hours) / num_days;
CHECK_EQUAL(d * 2, hours_scaled_to_days);
}
}
//*************************************************************************
TEST(test_minus_equal_days)
{
auto num_days = Chrono::days::period::num;
auto num_hours = Chrono::hours::period::num;
Chrono::days ds(2);
TimePoint tp{Chrono::days(100)};
for (int d = 1; d < 100; ++d)
{
tp -= ds;
auto hours = tp.time_since_epoch().count();
auto hours_scaled_to_days = (hours * num_hours) / num_days;
CHECK_EQUAL(100 - (d * 2), hours_scaled_to_days);
}
}
//*************************************************************************
TEST(test_minus_equal_hours)
{
auto num_days = Chrono::days::period::num;
auto num_hours = Chrono::hours::period::num;
Chrono::hours hs(48);
TimePoint tp{Chrono::days(100)};
for (int d = 1; d < 100; ++d)
{
tp -= hs;
auto hours = tp.time_since_epoch().count();
auto hours_scaled_to_days = (hours * num_hours) / num_days;
CHECK_EQUAL(100 - (d * 2), hours_scaled_to_days);
}
}
//*************************************************************************
TEST(test_operator_plus_time_point_duration)
{
TimePoint tp{Chrono::hours(10)};
// Same duration type.
auto result = tp + Chrono::hours(5);
CHECK_EQUAL(15, result.time_since_epoch().count());
// Mixed duration types resolve to the common (finer) type.
auto result_mixed = tp + Chrono::days(1); // 10 hours + 24 hours
CHECK_EQUAL(34, result_mixed.time_since_epoch().count());
CHECK_TRUE((std::is_same<decltype(result_mixed)::duration, Chrono::hours>::value));
}
//*************************************************************************
TEST(test_operator_plus_duration_time_point)
{
TimePoint tp{Chrono::hours(10)};
// Same duration type.
auto result = Chrono::hours(5) + tp;
CHECK_EQUAL(15, result.time_since_epoch().count());
// Mixed duration types resolve to the common (finer) type.
auto result_mixed = Chrono::days(1) + tp; // 24 hours + 10 hours
CHECK_EQUAL(34, result_mixed.time_since_epoch().count());
CHECK_TRUE((std::is_same<decltype(result_mixed)::duration, Chrono::hours>::value));
}
//*************************************************************************
TEST(test_operator_minus_time_point_duration)
{
TimePoint tp{Chrono::hours(50)};
// Same duration type.
auto result = tp - Chrono::hours(5);
CHECK_EQUAL(45, result.time_since_epoch().count());
// Mixed duration types resolve to the common (finer) type.
auto result_mixed = tp - Chrono::days(1); // 50 hours - 24 hours
CHECK_EQUAL(26, result_mixed.time_since_epoch().count());
CHECK_TRUE((std::is_same<decltype(result_mixed)::duration, Chrono::hours>::value));
}
//*************************************************************************
TEST(test_operator_minus_time_point_time_point)
{
TimePoint tp1{Chrono::hours(50)};
TimePoint tp2{Chrono::hours(20)};
// Subtracting two time_points yields the duration between them.
auto diff = tp1 - tp2;
CHECK_EQUAL(30, diff.count());
CHECK_TRUE((std::is_same<decltype(diff), Chrono::hours>::value));
// Mixed duration types resolve to the common (finer) duration.
Chrono::time_point<test_clock, Chrono::days> tp_days{Chrono::days(2)}; // 48 hours
auto diff_mixed = tp1 - tp_days; // 50 hours - 48 hours
CHECK_EQUAL(2, diff_mixed.count());
CHECK_TRUE((std::is_same<decltype(diff_mixed), Chrono::hours>::value));
// The reverse difference is negative.
auto diff_negative = tp2 - tp1;
CHECK_EQUAL(-30, diff_negative.count());
}
//*************************************************************************
TEST(test_increment_decrement_operators)
{
TimePoint tp{Chrono::hours(10)};
// Pre-increment.
auto& pre_inc = ++tp;
CHECK_EQUAL(11, tp.time_since_epoch().count());
CHECK_EQUAL(11, pre_inc.time_since_epoch().count());
// Post-increment returns the previous value.
TimePoint post_inc = tp++;
CHECK_EQUAL(11, post_inc.time_since_epoch().count());
CHECK_EQUAL(12, tp.time_since_epoch().count());
// Pre-decrement.
auto& pre_dec = --tp;
CHECK_EQUAL(11, tp.time_since_epoch().count());
CHECK_EQUAL(11, pre_dec.time_since_epoch().count());
// Post-decrement returns the previous value.
TimePoint post_dec = tp--;
CHECK_EQUAL(11, post_dec.time_since_epoch().count());
CHECK_EQUAL(10, tp.time_since_epoch().count());
}
#if ETL_USING_ETL_CHRONO
//*************************************************************************
TEST(test_min_max_time_point)
{
using Rep = TimePoint::duration::rep;
CHECK_EQUAL(etl::integral_limits<Rep>::min, TimePoint::min().time_since_epoch().count());
CHECK_EQUAL(etl::integral_limits<Rep>::max, TimePoint::max().time_since_epoch().count());
}
#endif
//*************************************************************************
TEST(test_time_point_comparison_operators)
{
// 10 days
TimePoint tp10h{Chrono::hours(240)};
TimePoint tp10d{Chrono::days(10)};
// 20 days
TimePoint tp20h{Chrono::hours(480)};
TimePoint tp20d{Chrono::days(20)};
// ==
CHECK_TRUE(tp10h == tp10h);
CHECK_TRUE(tp10d == tp10h);
CHECK_TRUE(tp10h == tp10d);
CHECK_FALSE(tp10h == tp20h);
CHECK_FALSE(tp20h == tp10h);
CHECK_FALSE(tp10h == tp20d);
CHECK_FALSE(tp10d == tp20h);
CHECK_FALSE(tp20h == tp10d);
CHECK_FALSE(tp20d == tp10h);
// <
CHECK_TRUE(tp10h < tp20h);
CHECK_TRUE(tp10d < tp20h);
CHECK_TRUE(tp10h < tp20d);
CHECK_FALSE(tp10h < tp10h);
CHECK_FALSE(tp10d < tp10h);
CHECK_FALSE(tp10h < tp10d);
CHECK_FALSE(tp20h < tp10h);
CHECK_FALSE(tp20d < tp10h);
CHECK_FALSE(tp20h < tp10d);
// <=
CHECK_TRUE(tp10h <= tp20h);
CHECK_TRUE(tp10d <= tp20h);
CHECK_TRUE(tp10h <= tp20d);
CHECK_TRUE(tp10h <= tp10h);
CHECK_TRUE(tp10d <= tp10h);
CHECK_TRUE(tp10h <= tp10d);
CHECK_FALSE(tp20h <= tp10h);
CHECK_FALSE(tp20d <= tp10h);
CHECK_FALSE(tp20h <= tp10d);
// >
CHECK_TRUE(tp20h > tp10h);
CHECK_TRUE(tp20d > tp10h);
CHECK_TRUE(tp20h > tp10d);
CHECK_FALSE(tp10h > tp10h);
CHECK_FALSE(tp10d > tp10h);
CHECK_FALSE(tp10h > tp10d);
CHECK_FALSE(tp10h > tp20h);
CHECK_FALSE(tp10d > tp20h);
CHECK_FALSE(tp10h > tp20d);
// >=
CHECK_TRUE(tp20h >= tp10h);
CHECK_TRUE(tp20d >= tp10h);
CHECK_TRUE(tp20h >= tp10d);
CHECK_TRUE(tp10h >= tp10h);
CHECK_TRUE(tp10d >= tp10h);
CHECK_TRUE(tp10h >= tp10d);
CHECK_FALSE(tp10h >= tp20h);
CHECK_FALSE(tp10d >= tp20h);
CHECK_FALSE(tp10h >= tp20d);
#if ETL_USING_CPP20
CHECK_TRUE((tp10h <=> tp10h) == 0);
CHECK_TRUE((tp10h <=> tp20h) < 0);
CHECK_TRUE((tp20h <=> tp10h) > 0);
CHECK_TRUE((tp10h <=> tp10d) == 0);
CHECK_TRUE((tp10d <=> tp20h) < 0);
CHECK_TRUE((tp20h <=> tp10d) > 0);
CHECK_TRUE((tp10d <=> tp10h) == 0);
CHECK_TRUE((tp10h <=> tp20d) < 0);
CHECK_TRUE((tp20d <=> tp10h) > 0);
#endif
}
//*************************************************************************
TEST(test_time_point_common_type)
{
using TimePointHours = Chrono::time_point<test_clock, Chrono::hours>;
using TimePointDays = Chrono::time_point<test_clock, Chrono::days>;
using CommonType = etl::common_type_t<TimePointHours, TimePointDays>;
CHECK_TRUE((std::is_same<TimePointHours::duration, CommonType::duration>::value));
CHECK_FALSE((std::is_same<TimePointDays::duration, CommonType::duration>::value));
}
//*************************************************************************
TEST(test_floor)
{
using TimePoint = Chrono::time_point<test_clock, Chrono::milliseconds>;
TimePoint tp(Chrono::milliseconds(1234)); // 1234 milliseconds
auto floored_tp = floor<Chrono::seconds>(tp);
CHECK_EQUAL(1, floored_tp.time_since_epoch().count());
TimePoint negative_tp(Chrono::milliseconds(-1234)); // -1234 milliseconds
auto floored_negative_tp = floor<Chrono::seconds>(negative_tp);
CHECK_EQUAL(-2, floored_negative_tp.time_since_epoch().count());
}
//*************************************************************************
TEST(test_ceil)
{
using TimePoint = Chrono::time_point<test_clock, Chrono::milliseconds>;
TimePoint tp(Chrono::milliseconds(1234)); // 1234 milliseconds
auto ceil_tp = ceil<Chrono::seconds>(tp);
CHECK_EQUAL(2, ceil_tp.time_since_epoch().count());
TimePoint negative_tp(Chrono::milliseconds(-1234)); // -1234 milliseconds
auto ceil_negative_tp = ceil<Chrono::seconds>(negative_tp);
CHECK_EQUAL(-1, ceil_negative_tp.time_since_epoch().count());
}
//*************************************************************************
TEST(test_round)
{
using TimePoint = Chrono::time_point<test_clock, Chrono::milliseconds>;
TimePoint tp1(Chrono::milliseconds(1500)); // 1500 milliseconds
auto round1 = round<Chrono::seconds>(tp1); // Round to seconds
CHECK_EQUAL(2, round1.time_since_epoch().count());
TimePoint tp2(Chrono::milliseconds(2500)); // 2500 milliseconds
auto round2 = round<Chrono::seconds>(tp2); // Round to seconds
CHECK_EQUAL(2, round2.time_since_epoch().count());
TimePoint tp3(Chrono::milliseconds(-1500)); // -1500 milliseconds
auto round3 = round<Chrono::seconds>(tp3); // Round to seconds
CHECK_EQUAL(-2, round3.time_since_epoch().count());
}
//*************************************************************************
TEST(test_time_point_cast)
{
using TimePointFrom = Chrono::time_point<test_clock, Chrono::seconds>;
using TimePointTo = Chrono::time_point<test_clock, Chrono::milliseconds>;
TimePointFrom from(Chrono::seconds(2));
TimePointTo to = Chrono::time_point_cast<TimePointTo::duration>(from);
auto expected = from.time_since_epoch().count() * (TimePointTo::period::den / TimePointFrom::period::den);
auto actual = to.time_since_epoch().count();
CHECK_EQUAL(expected, actual);
}
}
} // namespace