ToStringChrono.tests.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright Catch2 Authors
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // https://www.boost.org/LICENSE_1_0.txt)
  5. // SPDX-License-Identifier: BSL-1.0
  6. #include <catch2/catch_test_macros.hpp>
  7. #include <chrono>
  8. #include <cstdint>
  9. TEST_CASE("Stringifying std::chrono::duration helpers", "[toString][chrono]") {
  10. // No literals because we still support c++11
  11. auto hour = std::chrono::hours(1);
  12. auto minute = std::chrono::minutes(1);
  13. auto seconds = std::chrono::seconds(60);
  14. auto micro = std::chrono::microseconds(1);
  15. auto milli = std::chrono::milliseconds(1);
  16. auto nano = std::chrono::nanoseconds(1);
  17. REQUIRE(minute == seconds);
  18. REQUIRE(hour != seconds);
  19. REQUIRE(micro != milli);
  20. REQUIRE(nano != micro);
  21. }
  22. TEST_CASE("Stringifying std::chrono::duration with weird ratios", "[toString][chrono]") {
  23. std::chrono::duration<int64_t, std::ratio<30>> half_minute(1);
  24. std::chrono::duration<int64_t, std::ratio<1, 1000000000000>> pico_second(1);
  25. std::chrono::duration<int64_t, std::ratio<1, 1000000000000000>> femto_second(1);
  26. std::chrono::duration<int64_t, std::ratio<1, 1000000000000000000>> atto_second(1);
  27. REQUIRE(half_minute != femto_second);
  28. REQUIRE(pico_second != atto_second);
  29. }
  30. TEST_CASE("Stringifying std::chrono::time_point<system_clock>", "[toString][chrono]") {
  31. auto now = std::chrono::system_clock::now();
  32. auto later = now + std::chrono::minutes(2);
  33. REQUIRE(now != later);
  34. }
  35. TEST_CASE("Stringifying std::chrono::time_point<Clock>", "[toString][chrono][!nonportable]") {
  36. auto now = std::chrono::high_resolution_clock::now();
  37. auto later = now + std::chrono::minutes(2);
  38. REQUIRE(now != later);
  39. auto now2 = std::chrono::steady_clock::now();
  40. auto later2 = now2 + std::chrono::minutes(2);
  41. REQUIRE(now2 != later2);
  42. }