The Battle for Wesnoth  1.15.12+dev
test_formula_timespan.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2019 by Iris Morelle <shadowm2006@gmail.com>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include <boost/test/unit_test.hpp>
16 
17 #include "formula/string_utils.hpp"
18 #include "tstring.hpp"
19 
20 #include <algorithm>
21 
22 BOOST_AUTO_TEST_SUITE( formula_timespan )
23 
24 using std::time_t;
25 
26 namespace {
27 
29 {
30  YEAR = 31104000, /* 12 months */
31  MONTH = 2592000, /* 30 days */
32  WEEK = 604800,
33  DAY = 86400,
34  HOUR = 3600,
35  MIN = 60,
36  SEC = 1,
37 };
38 
39 inline std::string minifmt(time_t t, const std::string& singular, const std::string& plural)
40 {
41  return t ? std::to_string(t) + " " + (t > 1 ? plural : singular) : "";
42 }
43 
44 typedef std::tuple<
45  time_t /*sec*/,
46  time_t /*min*/,
47  time_t /*hr*/,
48  time_t /*day*/,
49  time_t /*wk*/,
50  time_t /*mo*/,
51  time_t /*yr*/> time_detailed;
52 
53 inline time_t gen_as_time_t(const time_detailed& params)
54 {
55  auto [sec, min, hr, day, wk, mo, yr] = params;
56 
57  return YEAR*yr + MONTH*mo + WEEK*wk + DAY*day + HOUR*hr + MIN*min + SEC*sec;
58 }
59 
60 inline std::string gen_as_str(const time_detailed& params)
61 {
62  auto [sec, min, hr, day, wk, mo, yr] = params;
63 
64  std::vector<t_string> bits;
65  std::string res;
66 
67  bits.emplace_back(minifmt(yr, "year", "years"));
68  bits.emplace_back(minifmt(mo, "month", "months"));
69  bits.emplace_back(minifmt(wk, "week", "weeks"));
70  bits.emplace_back(minifmt(day, "day", "days"));
71  bits.emplace_back(minifmt(hr, "hour", "hours"));
72  bits.emplace_back(minifmt(min, "minute", "minutes"));
73  bits.emplace_back(minifmt(sec, "second", "seconds"));
74 
75  // Drop zeroes
76  auto p = std::remove_if(bits.begin(), bits.end(), [](const t_string& t) { return t.empty(); });
77  if(p != bits.end()) {
78  bits.erase(p);
79  }
80 
81  return utils::format_conjunct_list("expired", bits);
82 }
83 
84 }
85 
86 BOOST_AUTO_TEST_CASE( test_formula_timespan )
87 {
88  time_detailed t;
89 
90  t = time_detailed{ 1, 0, 0, 0, 0, 0, 0 };
91  BOOST_CHECK_EQUAL("1 second", utils::format_timespan(gen_as_time_t(t)));
92 
93  t = time_detailed{ 2, 0, 0, 0, 0, 0, 0 };
94  BOOST_CHECK_EQUAL("2 seconds", utils::format_timespan(gen_as_time_t(t)));
95 
96  t = time_detailed{ 0, 1, 0, 0, 0, 0, 0 };
97  BOOST_CHECK_EQUAL("1 minute", utils::format_timespan(gen_as_time_t(t)));
98 
99  t = time_detailed{ 0, 2, 0, 0, 0, 0, 0 };
100  BOOST_CHECK_EQUAL("2 minutes", utils::format_timespan(gen_as_time_t(t)));
101 
102  t = time_detailed{ 0, 0, 1, 0, 0, 0, 0 };
103  BOOST_CHECK_EQUAL("1 hour", utils::format_timespan(gen_as_time_t(t)));
104 
105  t = time_detailed{ 0, 0, 2, 0, 0, 0, 0 };
106  BOOST_CHECK_EQUAL("2 hours", utils::format_timespan(gen_as_time_t(t)));
107 
108  t = time_detailed{ 0, 0, 0, 1, 0, 0, 0 };
109  BOOST_CHECK_EQUAL("1 day", utils::format_timespan(gen_as_time_t(t)));
110 
111  t = time_detailed{ 0, 0, 0, 2, 0, 0, 0 };
112  BOOST_CHECK_EQUAL("2 days", utils::format_timespan(gen_as_time_t(t)));
113 
114  t = time_detailed{ 0, 0, 0, 0, 1, 0, 0 };
115  BOOST_CHECK_EQUAL("1 week", utils::format_timespan(gen_as_time_t(t)));
116 
117  t = time_detailed{ 0, 0, 0, 0, 2, 0, 0 };
118  BOOST_CHECK_EQUAL("2 weeks", utils::format_timespan(gen_as_time_t(t)));
119 
120  t = time_detailed{ 0, 0, 0, 0, 0, 1, 0 };
121  BOOST_CHECK_EQUAL("1 month", utils::format_timespan(gen_as_time_t(t)));
122 
123  t = time_detailed{ 0, 0, 0, 0, 0, 2, 0 };
124  BOOST_CHECK_EQUAL("2 months", utils::format_timespan(gen_as_time_t(t)));
125 
126  t = time_detailed{ 0, 0, 0, 0, 0, 0, 1 };
127  BOOST_CHECK_EQUAL("1 year", utils::format_timespan(gen_as_time_t(t)));
128 
129  t = time_detailed{ 0, 0, 0, 0, 0, 0, 2 };
130  BOOST_CHECK_EQUAL("2 years", utils::format_timespan(gen_as_time_t(t)));
131 
132  t = time_detailed{ 12, 1, 23, 3, 2, 5, 2 };
133  BOOST_CHECK_EQUAL(gen_as_str(t), utils::format_timespan(gen_as_time_t(t)));
134 
135  t = time_detailed{ 0, 0, 0, 0, 0, 0, 0 };
136  BOOST_CHECK_EQUAL(utils::format_timespan(gen_as_time_t(t)), utils::format_timespan(0));
137  BOOST_CHECK_EQUAL(utils::format_timespan(gen_as_time_t(t)), utils::format_timespan(-10000));
138 
139  t = time_detailed{ 4, 0, 49, 0, 0, 0, 0 };
140  BOOST_CHECK_EQUAL("2 days, 1 hour, and 4 seconds", utils::format_timespan(gen_as_time_t(t)));
141 
142  t = time_detailed{ 0, 40, 0, 11, 1, 0, 4 };
143  BOOST_CHECK_EQUAL("4 years, 2 weeks, 4 days, and 40 minutes", utils::format_timespan(gen_as_time_t(t)));
144 
145  t = time_detailed{ 0, 0, 1, 0, 0, 3, 4 };
146  BOOST_CHECK_EQUAL("4 years, 3 months, and 1 hour", utils::format_timespan(gen_as_time_t(t)));
147 
148  t = time_detailed{ 10, 0, 0, 0, 0, 2, 0 };
149  BOOST_CHECK_EQUAL("2 months and 10 seconds", utils::format_timespan(gen_as_time_t(t)));
150 }
151 
152 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:64