Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah_chronos_utils_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
41#include <gtest/gtest.h>
42
43#include <ah-chronos-utils.H>
44
45#include <chrono>
46
47using namespace std::chrono_literals;
48
49TEST(AhChronosUtils, ConvertsADurationToEachUnit)
50{
51 const std::chrono::duration<double> d = 1.5s;
52
53 EXPECT_DOUBLE_EQ(Aleph::duration_to_seconds(d), 1.5);
54 EXPECT_DOUBLE_EQ(Aleph::duration_to_milliseconds(d), 1500.0);
55 EXPECT_DOUBLE_EQ(Aleph::duration_to_microseconds(d), 1.5e6);
56 EXPECT_DOUBLE_EQ(Aleph::duration_to_nanoseconds(d), 1.5e9);
57}
58
59TEST(AhChronosUtils, CountsWholeMinutesHoursAndDays)
60{
61 EXPECT_DOUBLE_EQ(Aleph::duration_to_minutes(90s), 1.0);
62 EXPECT_DOUBLE_EQ(Aleph::duration_to_hours(5400s), 1.0);
63 EXPECT_DOUBLE_EQ(Aleph::duration_to_days(200000s), 2.0);
64}
65
66#if defined(__cpp_lib_format) || \
67 (defined(_LIBCPP_VERSION) && _LIBCPP_VERSION >= 170000)
68TEST(AhChronosUtils, DurationToStringPicksTheUnitByMagnitude)
69{
70 using seconds_d = std::chrono::duration<double>;
71
72 EXPECT_EQ(Aleph::duration_to_string(seconds_d(0.25)), "250 ms");
73 EXPECT_EQ(Aleph::duration_to_string(seconds_d(1.5)), "1.5 s");
74 EXPECT_EQ(Aleph::duration_to_string(seconds_d(90)), "1 min");
75 EXPECT_EQ(Aleph::duration_to_string(seconds_d(5400)), "1 h");
76 EXPECT_EQ(Aleph::duration_to_string(seconds_d(200000)), "2 days");
77}
78
79TEST(AhChronosUtils, DurationToStringTakesAnyDurationType)
80{
81 EXPECT_EQ(Aleph::duration_to_string(250ms), "250 ms");
82 EXPECT_EQ(Aleph::duration_to_string(2min), "2 min");
83}
84#endif
Time duration conversion utilities.
#define TEST(name)
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_min_function > > min(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4189
double duration_to_seconds(const std::chrono::duration< double > &d)
double duration_to_milliseconds(const std::chrono::duration< double > &d)
double duration_to_nanoseconds(const std::chrono::duration< double > &d)
double duration_to_microseconds(const std::chrono::duration< double > &d)
double duration_to_minutes(const std::chrono::duration< double > &d)
double duration_to_days(const std::chrono::duration< double > &d)
double duration_to_hours(const std::chrono::duration< double > &d)