The Battle for Wesnoth  1.17.0-dev
test_formula_timespan.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2019 - 2021
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 
18 #include "formula/string_utils.hpp"
19 #include "tstring.hpp"
20 
21 #include <algorithm>
22 
23 BOOST_AUTO_TEST_SUITE( formula_timespan )
24 
25 using std::time_t;
26 
27 namespace {
28 
30 {
31  YEAR = 31104000, /* 12 months */
32  MONTH = 2592000, /* 30 days */
33  WEEK = 604800,
34  DAY = 86400,
35  HOUR = 3600,
36  MIN = 60,
37  SEC = 1,
38 };
39 
40 inline std::string minifmt(time_t t, const std::string& singular, const std::string& plural)
41 {
42  return t ? std::to_string(t) + " " + (t > 1 ? plural : singular) : "";
43 }
44 
45 typedef std::tuple<
46  time_t /*sec*/,
47  time_t /*min*/,
48  time_t /*hr*/,
49  time_t /*day*/,
50  time_t /*wk*/,
51  time_t /*mo*/,
52  time_t /*yr*/> time_detailed;
53 
54 inline time_t gen_as_time_t(const time_detailed& params)
55 {
56  auto [sec, min, hr, day, wk, mo, yr] = params;
57 
58  return YEAR*yr + MONTH*mo + WEEK*wk + DAY*day + HOUR*hr + MIN*min + SEC*sec;
59 }
60 
61 inline std::string gen_as_str(const time_detailed& params)
62 {
63  auto [sec, min, hr, day, wk, mo, yr] = params;
64 
65  std::vector<t_string> bits;
66  std::string res;
67 
68  bits.emplace_back(minifmt(yr, "year", "years"));
69  bits.emplace_back(minifmt(mo, "month", "months"));
70  bits.emplace_back(minifmt(wk, "week", "weeks"));
71  bits.emplace_back(minifmt(day, "day", "days"));
72  bits.emplace_back(minifmt(hr, "hour", "hours"));
73  bits.emplace_back(minifmt(min, "minute", "minutes"));
74  bits.emplace_back(minifmt(sec, "second", "seconds"));
75 
76  // Drop zeroes
77  auto p = std::remove_if(bits.begin(), bits.end(), [](const t_string& t) { return t.empty(); });
78  if(p != bits.end()) {
79  bits.erase(p);
80  }
81 
82  return utils::format_conjunct_list("expired", bits);
83 }
84 
85 }
86 
87 BOOST_AUTO_TEST_CASE( test_formula_timespan )
88 {
89  time_detailed t;
90 
91  t = time_detailed{ 1, 0, 0, 0, 0, 0, 0 };
92  BOOST_CHECK_EQUAL("1 second", utils::format_timespan(gen_as_time_t(t)));
93 
94  t = time_detailed{ 2, 0, 0, 0, 0, 0, 0 };
95  BOOST_CHECK_EQUAL("2 seconds", utils::format_timespan(gen_as_time_t(t)));
96 
97  t = time_detailed{ 0, 1, 0, 0, 0, 0, 0 };
98  BOOST_CHECK_EQUAL("1 minute", utils::format_timespan(gen_as_time_t(t)));
99 
100  t = time_detailed{ 0, 2, 0, 0, 0, 0, 0 };
101  BOOST_CHECK_EQUAL("2 minutes", utils::format_timespan(gen_as_time_t(t)));
102 
103  t = time_detailed{ 0, 0, 1, 0, 0, 0, 0 };
104  BOOST_CHECK_EQUAL("1 hour", utils::format_timespan(gen_as_time_t(t)));
105 
106  t = time_detailed{ 0, 0, 2, 0, 0, 0, 0 };
107  BOOST_CHECK_EQUAL("2 hours", utils::format_timespan(gen_as_time_t(t)));
108 
109  t = time_detailed{ 0, 0, 0, 1, 0, 0, 0 };
110  BOOST_CHECK_EQUAL("1 day", utils::format_timespan(gen_as_time_t(t)));
111 
112  t = time_detailed{ 0, 0, 0, 2, 0, 0, 0 };
113  BOOST_CHECK_EQUAL("2 days", utils::format_timespan(gen_as_time_t(t)));
114 
115  t = time_detailed{ 0, 0, 0, 0, 1, 0, 0 };
116  BOOST_CHECK_EQUAL("1 week", utils::format_timespan(gen_as_time_t(t)));
117 
118  t = time_detailed{ 0, 0, 0, 0, 2, 0, 0 };
119  BOOST_CHECK_EQUAL("2 weeks", utils::format_timespan(gen_as_time_t(t)));
120 
121  t = time_detailed{ 0, 0, 0, 0, 0, 1, 0 };
122  BOOST_CHECK_EQUAL("1 month", utils::format_timespan(gen_as_time_t(t)));
123 
124  t = time_detailed{ 0, 0, 0, 0, 0, 2, 0 };
125  BOOST_CHECK_EQUAL("2 months", utils::format_timespan(gen_as_time_t(t)));
126 
127  t = time_detailed{ 0, 0, 0, 0, 0, 0, 1 };
128  BOOST_CHECK_EQUAL("1 year", utils::format_timespan(gen_as_time_t(t)));
129 
130  t = time_detailed{ 0, 0, 0, 0, 0, 0, 2 };
131  BOOST_CHECK_EQUAL("2 years", utils::format_timespan(gen_as_time_t(t)));
132 
133  t = time_detailed{ 12, 1, 23, 3, 2, 5, 2 };
134  BOOST_CHECK_EQUAL(gen_as_str(t), utils::format_timespan(gen_as_time_t(t)));
135 
136  t = time_detailed{ 0, 0, 0, 0, 0, 0, 0 };
137  BOOST_CHECK_EQUAL(utils::format_timespan(gen_as_time_t(t)), utils::format_timespan(0));
138  BOOST_CHECK_EQUAL(utils::format_timespan(gen_as_time_t(t)), utils::format_timespan(-10000));
139 
140  t = time_detailed{ 4, 0, 49, 0, 0, 0, 0 };
141  BOOST_CHECK_EQUAL("2 days, 1 hour, and 4 seconds", utils::format_timespan(gen_as_time_t(t)));
142 
143  t = time_detailed{ 0, 40, 0, 11, 1, 0, 4 };
144  BOOST_CHECK_EQUAL("4 years, 2 weeks, 4 days, and 40 minutes", utils::format_timespan(gen_as_time_t(t)));
145 
146  t = time_detailed{ 0, 0, 1, 0, 0, 3, 4 };
147  BOOST_CHECK_EQUAL("4 years, 3 months, and 1 hour", utils::format_timespan(gen_as_time_t(t)));
148 
149  t = time_detailed{ 10, 0, 0, 0, 0, 2, 0 };
150  BOOST_CHECK_EQUAL("2 months and 10 seconds", utils::format_timespan(gen_as_time_t(t)));
151 }
152 
153 BOOST_AUTO_TEST_SUITE_END()
std::string format_timespan(std::time_t time)
Formats a timespan into human-readable text.
std::string format_conjunct_list(const t_string &empty, const std::vector< t_string > &elems)
Format a conjunctive list.
BOOST_AUTO_TEST_SUITE(filesystem)
mock_party p
BOOST_AUTO_TEST_CASE(test_formula_timespan)
double t
Definition: astarsearch.cpp:65