The Battle for Wesnoth  1.19.7+dev
test_formula_timespan.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2019 - 2024
3  by Iris Morelle <shadowm2006@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #include <boost/test/unit_test.hpp>
17 
19 #include "serialization/chrono.hpp"
20 
21 BOOST_AUTO_TEST_SUITE( formula_timespan )
22 
23 BOOST_AUTO_TEST_CASE( test_formula_timespan )
24 {
25  using namespace std::chrono_literals;
26 
27  BOOST_CHECK_EQUAL("1 second", utils::format_timespan(std::chrono::seconds{1}, true));
28  BOOST_CHECK_EQUAL("2 seconds", utils::format_timespan(std::chrono::seconds{2}, true));
29  BOOST_CHECK_EQUAL("1 minute", utils::format_timespan(std::chrono::minutes{1}, true));
30  BOOST_CHECK_EQUAL("2 minutes", utils::format_timespan(std::chrono::minutes{2}, true));
31  BOOST_CHECK_EQUAL("1 hour", utils::format_timespan(std::chrono::hours{1}, true));
32  BOOST_CHECK_EQUAL("2 hours", utils::format_timespan(std::chrono::hours{2}, true));
33  BOOST_CHECK_EQUAL("1 day", utils::format_timespan(chrono::days{1}, true));
34  BOOST_CHECK_EQUAL("2 days", utils::format_timespan(chrono::days{2}, true));
35  BOOST_CHECK_EQUAL("1 week", utils::format_timespan(chrono::weeks{1}, true));
36  BOOST_CHECK_EQUAL("2 weeks", utils::format_timespan(chrono::weeks{2}, true));
37  BOOST_CHECK_EQUAL("1 year", utils::format_timespan(chrono::years{1}, true));
38  BOOST_CHECK_EQUAL("2 years", utils::format_timespan(chrono::years{2}, true));
39 
40  BOOST_CHECK_EQUAL(utils::format_timespan(0s), utils::format_timespan(0min));
41  BOOST_CHECK_EQUAL(utils::format_timespan(0s, true), utils::format_timespan(0min));
42  BOOST_CHECK_EQUAL(utils::format_timespan(0s), utils::format_timespan(-10000min));
43  BOOST_CHECK_EQUAL(utils::format_timespan(0s, true), utils::format_timespan(-10000min));
44 
45  {
46  constexpr auto time = chrono::years{2} + chrono::months{5} + chrono::weeks{2} + chrono::days{3} +
47  std::chrono::hours{23} + std::chrono::minutes{1} + std::chrono::seconds{12};
48 
49  BOOST_CHECK_EQUAL("2 years, 5 months, 2 weeks, 3 days, 23 hours, 1 minute, and 12 seconds", utils::format_timespan(time, true));
50  BOOST_CHECK_EQUAL("2 years", utils::format_timespan(time));
51  }
52 
53  {
54  constexpr auto time = chrono::days{2} + std::chrono::hours{1} + std::chrono::seconds{4};
55  BOOST_CHECK_EQUAL("2 days, 1 hour, and 4 seconds", utils::format_timespan(time, true));
56  BOOST_CHECK_EQUAL("2 days", utils::format_timespan(time));
57  }
58 
59  {
60  constexpr auto time = chrono::years{4} + chrono::weeks{2} + chrono::days{4} + std::chrono::minutes{40};
61  BOOST_CHECK_EQUAL("4 years, 2 weeks, 4 days, and 40 minutes", utils::format_timespan(time, true));
62  BOOST_CHECK_EQUAL("4 years", utils::format_timespan(time));
63  }
64 
65  {
66  constexpr auto time = chrono::years{4} + chrono::months{3} + std::chrono::hours{1};
67  BOOST_CHECK_EQUAL("4 years, 3 months, and 1 hour", utils::format_timespan(time, true));
68  BOOST_CHECK_EQUAL("4 years", utils::format_timespan(time));
69  }
70 
71  {
72  constexpr auto time = chrono::months{2} + std::chrono::seconds{10};
73  BOOST_CHECK_EQUAL("2 months and 10 seconds", utils::format_timespan(time, true));
74  BOOST_CHECK_EQUAL("2 months", utils::format_timespan(time));
75  }
76 }
77 
78 BOOST_AUTO_TEST_SUITE_END()
std::chrono::duration< int, std::ratio< 2629746 > > months
Definition: chrono.hpp:41
std::chrono::duration< int, std::ratio< 31556952 > > years
Definition: chrono.hpp:42
std::chrono::duration< int, std::ratio< 604800 > > weeks
Definition: chrono.hpp:40
std::chrono::duration< int, std::ratio< 86400 > > days
Definition: chrono.hpp:39
static std::string format_timespan(const std::chrono::duration< Rep, Period > &span, bool detailed=false)
Formats a timespan into human-readable text for player authentication functions.
BOOST_AUTO_TEST_SUITE(filesystem)
BOOST_AUTO_TEST_CASE(test_formula_timespan)
static map_location::direction s