1 // The MIT License (MIT)
2 //
3 // Copyright (c) 2018 Tomasz Kamiński
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22
23
24 #include "date.h"
25
26 #include <cassert>
27 #include <sstream>
28
test_SI()29 void test_SI()
30 {
31 using namespace std::chrono;
32 using namespace date;
33
34 std::ostringstream os;
35
36 // atto
37 {
38 duration<int, std::atto> d(13);
39 os << d;
40 assert(os.str() == "13as");
41 os.str("");
42 }
43
44 // femto
45 {
46 duration<int, std::femto> d(13);
47 os << d;
48 assert(os.str() == "13fs");
49 os.str("");
50 }
51
52 // pico
53 {
54 duration<int, std::pico> d(13);
55 os << d;
56 assert(os.str() == "13ps");
57 os.str("");
58 }
59
60 // nano
61 {
62 duration<int, std::nano> d(13);
63 os << d;
64 assert(os.str() == "13ns");
65 os.str("");
66 }
67
68 // mikro
69 {
70 duration<int, std::micro> d(13);
71 os << d;
72 assert(os.str() == "13\xC2\xB5s");
73 os.str("");
74 }
75
76 // milli
77 {
78 duration<int, std::milli> d(13);
79 os << d;
80 assert(os.str() == "13ms");
81 os.str("");
82 }
83
84 // centi
85 {
86 duration<int, std::centi> d(13);
87 os << d;
88 assert(os.str() == "13cs");
89 os.str("");
90 }
91
92 // deci
93 {
94 duration<int, std::deci> d(13);
95 os << d;
96 assert(os.str() == "13ds");
97 os.str("");
98 }
99
100 // seconds
101 {
102 duration<int> d(13);
103 os << d;
104 assert(os.str() == "13s");
105 os.str("");
106 }
107
108 // deca
109 {
110 duration<int, std::deca> d(13);
111 os << d;
112 assert(os.str() == "13das");
113 os.str("");
114 }
115
116 // hecto
117 {
118 duration<int, std::hecto> d(13);
119 os << d;
120 assert(os.str() == "13hs");
121 os.str("");
122 }
123
124 // kilo
125 {
126 duration<int, std::kilo> d(13);
127 os << d;
128 assert(os.str() == "13ks");
129 os.str("");
130 }
131
132 // mega
133 {
134 duration<int, std::mega> d(13);
135 os << d;
136 assert(os.str() == "13Ms");
137 os.str("");
138 }
139
140 // giga
141 {
142 duration<int, std::giga> d(13);
143 os << d;
144 assert(os.str() == "13Gs");
145 os.str("");
146 }
147
148 // tera
149 {
150 duration<int, std::tera> d(13);
151 os << d;
152 assert(os.str() == "13Ts");
153 os.str("");
154 }
155
156 // peta
157 {
158 duration<int, std::peta> d(13);
159 os << d;
160 assert(os.str() == "13Ps");
161 os.str("");
162 }
163
164 // femto
165 {
166 duration<int, std::exa> d(13);
167 os << d;
168 assert(os.str() == "13Es");
169 os.str("");
170 }
171 }
172
test_calendar()173 void test_calendar()
174 {
175 using namespace std::chrono;
176 using namespace date;
177
178 std::ostringstream os;
179
180 // minutes
181 {
182 minutes d(13);
183 os << d;
184 assert(os.str() == "13min");
185 os.str("");
186 }
187
188 // hours
189 {
190 hours d(13);
191 os << d;
192 assert(os.str() == "13h");
193 os.str("");
194 }
195
196 // days
197 {
198 days d(13);
199 os << d;
200 assert(os.str() == "13d");
201 os.str("");
202 }
203 }
204
test_integral_scale()205 void test_integral_scale()
206 {
207 using namespace std::chrono;
208 using namespace date;
209
210 std::ostringstream os;
211
212 // ratio 123 / 1
213 {
214 duration<int, std::ratio<123, 1>> d(13);
215 os << d;
216 assert(os.str() == "13[123]s");
217 os.str("");
218 }
219
220 // ratio 100 / 4 = ratio 25 / 1
221 {
222 duration<int, std::ratio<25, 1>> d(13);
223 os << d;
224 assert(os.str() == "13[25]s");
225 os.str("");
226 }
227
228 // weeks = ratio 7 * 24 * 60 * 60 / 1 = ratio 604800 / 1
229 {
230 weeks d(13);
231 os << d;
232 assert(os.str() == "13[604800]s");
233 os.str("");
234 }
235
236 // years = 146097/400 days = ratio 146097/400 * 24 * 60 * 60 = ratio 31556952 / 1
237 {
238 years d(13);
239 os << d;
240 assert(os.str() == "13[31556952]s");
241 os.str("");
242 }
243
244 // months = 1/12 years = ratio 1/12 * 31556952 = ratio 2629746 / 1
245 {
246 months d(13);
247 os << d;
248 assert(os.str() == "13[2629746]s");
249 os.str("");
250 }
251 }
252
test_ratio_scale()253 void test_ratio_scale()
254 {
255 using namespace std::chrono;
256 using namespace date;
257
258 std::ostringstream os;
259
260 // ratio 1 / 2
261 {
262 duration<int, std::ratio<1, 2>> d(13);
263 os << d;
264 assert(os.str() == "13[1/2]s");
265 os.str("");
266 }
267
268 // ratio 100 / 3
269 {
270 duration<int, std::ratio<100, 3>> d(13);
271 os << d;
272 assert(os.str() == "13[100/3]s");
273 os.str("");
274 }
275
276 // ratio 100 / 6 = ratio 50 / 3
277 {
278 duration<int, std::ratio<100, 6>> d(13);
279 os << d;
280 assert(os.str() == "13[50/3]s");
281 os.str("");
282 }
283 }
284
test_constexpr()285 void test_constexpr()
286 {
287 using date::detail::get_units;
288
289 CONSTCD11 auto as = get_units<char>(std::atto{});
290 CONSTCD11 auto fs = get_units<char>(std::femto{});
291 CONSTCD11 auto ps = get_units<char>(std::pico{});
292 CONSTCD11 auto ns = get_units<char>(std::nano{});
293 CONSTCD11 auto us = get_units<char>(std::micro{});
294 CONSTCD11 auto usw = get_units<wchar_t>(std::micro{});
295 CONSTCD11 auto ms = get_units<char>(std::milli{});
296 CONSTCD11 auto cs = get_units<char>(std::centi{});
297 CONSTCD11 auto ds = get_units<char>(std::deci{});
298 CONSTCD11 auto s = get_units<char>(std::ratio<1>{});
299 CONSTCD11 auto das = get_units<char>(std::deca{});
300 CONSTCD11 auto hs = get_units<char>(std::hecto{});
301 CONSTCD11 auto ks = get_units<char>(std::kilo{});
302 CONSTCD11 auto Ms = get_units<char>(std::mega{});
303 CONSTCD11 auto Gs = get_units<char>(std::giga{});
304 CONSTCD11 auto Ts = get_units<char>(std::tera{});
305 CONSTCD11 auto Ps = get_units<char>(std::peta{});
306 CONSTCD11 auto Es = get_units<char>(std::exa{});
307 (void)as, (void)fs, (void)ps, (void)ns, (void)usw, (void)us,
308 (void)ms, (void)cs, (void)ds, (void)s, (void)das, (void)hs,
309 (void)ks, (void)Ms, (void)Gs, (void)Ts, (void)Ps, (void)Es;
310
311 CONSTCD11 auto min = get_units<char>(std::ratio<60>{});
312 CONSTCD11 auto h = get_units<char>(std::ratio<3600>{});
313 CONSTCD11 auto d = get_units<char>(std::ratio<86400>{});
314 (void)min, (void)h, (void)d;
315
316 CONSTCD14 auto integer = get_units<char>(std::ratio<123>{});
317 CONSTCD14 auto ratio = get_units<char>(std::ratio<123, 3>{});
318 (void)integer, (void)ratio;
319 }
320
321 int
main()322 main()
323 {
324 test_SI();
325 test_calendar();
326 test_integral_scale();
327 test_ratio_scale();
328 }
329