The Battle for Wesnoth  1.15.1+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  time_t sec, min, hr, day, wk, mo, yr;
56  std::tie(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  time_t sec, min, hr, day, wk, mo, yr;
64  std::tie(sec, min, hr, day, wk, mo, yr) = params;
65 
66  std::vector<t_string> bits;
67  std::string res;
68 
69  bits.emplace_back(minifmt(yr, "year", "years"));
70  bits.emplace_back(minifmt(mo, "month", "months"));
71  bits.emplace_back(minifmt(wk, "week", "weeks"));
72  bits.emplace_back(minifmt(day, "day", "days"));
73  bits.emplace_back(minifmt(hr, "hour", "hours"));
74  bits.emplace_back(minifmt(min, "minute", "minutes"));
75  bits.emplace_back(minifmt(sec, "second", "seconds"));
76 
77  // Drop zeroes
78  auto p = std::remove_if(bits.begin(), bits.end(), [](const t_string& t) { return t.empty(); });
79  if(p != bits.end()) {
80  bits.erase(p);
81  }
82 
83  return utils::format_conjunct_list("expired", bits);
84 }
85 
86 }
87 
88 BOOST_AUTO_TEST_CASE( test_formula_timespan )
89 {
90  time_detailed t;
91 
92  t = time_detailed{ 1, 0, 0, 0, 0, 0, 0 };
93  BOOST_CHECK_EQUAL("1 second", utils::format_timespan(gen_as_time_t(t)));
94 
95  t = time_detailed{ 2, 0, 0, 0, 0, 0, 0 };
96  BOOST_CHECK_EQUAL("2 seconds", utils::format_timespan(gen_as_time_t(t)));
97 
98  t = time_detailed{ 0, 1, 0, 0, 0, 0, 0 };
99  BOOST_CHECK_EQUAL("1 minute", utils::format_timespan(gen_as_time_t(t)));
100 
101  t = time_detailed{ 0, 2, 0, 0, 0, 0, 0 };
102  BOOST_CHECK_EQUAL("2 minutes", utils::format_timespan(gen_as_time_t(t)));
103 
104  t = time_detailed{ 0, 0, 1, 0, 0, 0, 0 };
105  BOOST_CHECK_EQUAL("1 hour", utils::format_timespan(gen_as_time_t(t)));
106 
107  t = time_detailed{ 0, 0, 2, 0, 0, 0, 0 };
108  BOOST_CHECK_EQUAL("2 hours", utils::format_timespan(gen_as_time_t(t)));
109 
110  t = time_detailed{ 0, 0, 0, 1, 0, 0, 0 };
111  BOOST_CHECK_EQUAL("1 day", utils::format_timespan(gen_as_time_t(t)));
112 
113  t = time_detailed{ 0, 0, 0, 2, 0, 0, 0 };
114  BOOST_CHECK_EQUAL("2 days", utils::format_timespan(gen_as_time_t(t)));
115 
116  t = time_detailed{ 0, 0, 0, 0, 1, 0, 0 };
117  BOOST_CHECK_EQUAL("1 week", utils::format_timespan(gen_as_time_t(t)));
118 
119  t = time_detailed{ 0, 0, 0, 0, 2, 0, 0 };
120  BOOST_CHECK_EQUAL("2 weeks", utils::format_timespan(gen_as_time_t(t)));
121 
122  t = time_detailed{ 0, 0, 0, 0, 0, 1, 0 };
123  BOOST_CHECK_EQUAL("1 month", utils::format_timespan(gen_as_time_t(t)));
124 
125  t = time_detailed{ 0, 0, 0, 0, 0, 2, 0 };
126  BOOST_CHECK_EQUAL("2 months", utils::format_timespan(gen_as_time_t(t)));
127 
128  t = time_detailed{ 0, 0, 0, 0, 0, 0, 1 };
129  BOOST_CHECK_EQUAL("1 year", utils::format_timespan(gen_as_time_t(t)));
130 
131  t = time_detailed{ 0, 0, 0, 0, 0, 0, 2 };
132  BOOST_CHECK_EQUAL("2 years", utils::format_timespan(gen_as_time_t(t)));
133 
134  t = time_detailed{ 12, 1, 23, 3, 2, 5, 2 };
135  BOOST_CHECK_EQUAL(gen_as_str(t), utils::format_timespan(gen_as_time_t(t)));
136 
137  t = time_detailed{ 0, 0, 0, 0, 0, 0, 0 };
138  BOOST_CHECK_EQUAL(utils::format_timespan(gen_as_time_t(t)), utils::format_timespan(0));
139  BOOST_CHECK_EQUAL(utils::format_timespan(gen_as_time_t(t)), utils::format_timespan(-10000));
140 
141  t = time_detailed{ 4, 0, 49, 0, 0, 0, 0 };
142  BOOST_CHECK_EQUAL("2 days, 1 hour, and 4 seconds", utils::format_timespan(gen_as_time_t(t)));
143 
144  t = time_detailed{ 0, 40, 0, 11, 1, 0, 4 };
145  BOOST_CHECK_EQUAL("4 years, 2 weeks, 4 days, and 40 minutes", utils::format_timespan(gen_as_time_t(t)));
146 
147  t = time_detailed{ 0, 0, 1, 0, 0, 3, 4 };
148  BOOST_CHECK_EQUAL("4 years, 3 months, and 1 hour", utils::format_timespan(gen_as_time_t(t)));
149 
150  t = time_detailed{ 10, 0, 0, 0, 0, 2, 0 };
151  BOOST_CHECK_EQUAL("2 months and 10 seconds", utils::format_timespan(gen_as_time_t(t)));
152 }
153 
154 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.
mock_party p
BOOST_AUTO_TEST_CASE(test_formula_timespan)
double t
Definition: astarsearch.cpp:64