File: kformattest.cpp

package info (click to toggle)
kcoreaddons 5.54.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,632 kB
  • sloc: cpp: 17,338; xml: 3,412; python: 53; sh: 41; makefile: 9
file content (392 lines) | stat: -rw-r--r-- 25,271 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*  This file is part of the KDE Frameworks

    Copyright (C) 2013 John Layt <jlayt@kde.org>,
    Copyright (C) 2010 Michael Leupold <lemma@confuego.org>
    Copyright (C) 2009 Michael Pyne <mpyne@kde.org>
    Copyright (C) 2008 Albert Astals Cid <aacid@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "kformattest.h"

#include <QtTest>

#include "kformat.h"

void KFormatTest::formatByteSize()
{
    QLocale locale(QLocale::c());
    locale.setNumberOptions(QLocale::DefaultNumberOptions); // Qt >= 5.6 sets QLocale::OmitGroupSeparator for the C locale
    KFormat format(locale);

    QCOMPARE(format.formatByteSize(0), QStringLiteral("0 B"));
    QCOMPARE(format.formatByteSize(50), QStringLiteral("50 B"));
    QCOMPARE(format.formatByteSize(500), QStringLiteral("500 B"));
    QCOMPARE(format.formatByteSize(5000), QStringLiteral("4.9 KiB"));
    QCOMPARE(format.formatByteSize(50000), QStringLiteral("48.8 KiB"));
    QCOMPARE(format.formatByteSize(500000), QStringLiteral("488.3 KiB"));
    QCOMPARE(format.formatByteSize(5000000), QStringLiteral("4.8 MiB"));
    QCOMPARE(format.formatByteSize(50000000), QStringLiteral("47.7 MiB"));
    QCOMPARE(format.formatByteSize(500000000), QStringLiteral("476.8 MiB"));
#if (defined(__WORDSIZE) && (__WORDSIZE == 64)) || defined (_LP64) || defined(__LP64__) || defined(__ILP64__)
    QCOMPARE(format.formatByteSize(5000000000), QStringLiteral("4.7 GiB"));
    QCOMPARE(format.formatByteSize(50000000000), QStringLiteral("46.6 GiB"));
    QCOMPARE(format.formatByteSize(500000000000), QStringLiteral("465.7 GiB"));
    QCOMPARE(format.formatByteSize(5000000000000), QStringLiteral("4.5 TiB"));
    QCOMPARE(format.formatByteSize(50000000000000), QStringLiteral("45.5 TiB"));
    QCOMPARE(format.formatByteSize(500000000000000), QStringLiteral("454.7 TiB"));
#endif

    QCOMPARE(format.formatByteSize(1024.0, 1, KFormat::IECBinaryDialect), QStringLiteral("1.0 KiB"));
    QCOMPARE(format.formatByteSize(1023.0, 1, KFormat::IECBinaryDialect), QStringLiteral("1,023 B"));
    QCOMPARE(format.formatByteSize(1163000.0, 1, KFormat::IECBinaryDialect), QStringLiteral("1.1 MiB")); // 1.2 metric

    QCOMPARE(format.formatByteSize(-1024.0, 1, KFormat::IECBinaryDialect), QStringLiteral("-1.0 KiB"));
    QCOMPARE(format.formatByteSize(-1023.0, 1, KFormat::IECBinaryDialect), QStringLiteral("-1,023 B"));
    QCOMPARE(format.formatByteSize(-1163000.0, 1, KFormat::IECBinaryDialect), QStringLiteral("-1.1 MiB")); // 1.2 metric

    QCOMPARE(format.formatByteSize(1024.0, 1, KFormat::JEDECBinaryDialect), QStringLiteral("1.0 KB"));
    QCOMPARE(format.formatByteSize(1023.0, 1, KFormat::JEDECBinaryDialect), QStringLiteral("1,023 B"));
    QCOMPARE(format.formatByteSize(1163000.0, 1, KFormat::JEDECBinaryDialect), QStringLiteral("1.1 MB"));

    QCOMPARE(format.formatByteSize(-1024.0, 1, KFormat::JEDECBinaryDialect), QStringLiteral("-1.0 KB"));
    QCOMPARE(format.formatByteSize(-1023.0, 1, KFormat::JEDECBinaryDialect), QStringLiteral("-1,023 B"));
    QCOMPARE(format.formatByteSize(-1163000.0, 1, KFormat::JEDECBinaryDialect), QStringLiteral("-1.1 MB"));

    QCOMPARE(format.formatByteSize(1024.0, 1, KFormat::MetricBinaryDialect), QStringLiteral("1.0 kB"));
    QCOMPARE(format.formatByteSize(1023.0, 1, KFormat::MetricBinaryDialect), QStringLiteral("1.0 kB"));
    QCOMPARE(format.formatByteSize(1163000.0, 1, KFormat::MetricBinaryDialect), QStringLiteral("1.2 MB"));

    QCOMPARE(format.formatByteSize(-1024.0, 1, KFormat::MetricBinaryDialect), QStringLiteral("-1.0 kB"));
    QCOMPARE(format.formatByteSize(-1023.0, 1, KFormat::MetricBinaryDialect), QStringLiteral("-1.0 kB"));
    QCOMPARE(format.formatByteSize(-1163000.0, 1, KFormat::MetricBinaryDialect), QStringLiteral("-1.2 MB"));

    // Ensure all units are represented
    QCOMPARE(format.formatByteSize(2.0e9, 1, KFormat::MetricBinaryDialect), QStringLiteral("2.0 GB"));
    QCOMPARE(format.formatByteSize(3.2e12, 1, KFormat::MetricBinaryDialect), QStringLiteral("3.2 TB"));
    QCOMPARE(format.formatByteSize(4.1e15, 1, KFormat::MetricBinaryDialect), QStringLiteral("4.1 PB"));
    QCOMPARE(format.formatByteSize(6.7e18, 2, KFormat::MetricBinaryDialect), QStringLiteral("6.70 EB"));
    QCOMPARE(format.formatByteSize(5.6e20, 2, KFormat::MetricBinaryDialect), QStringLiteral("560.00 EB"));
    QCOMPARE(format.formatByteSize(2.3e22, 2, KFormat::MetricBinaryDialect), QStringLiteral("23.00 ZB"));
    QCOMPARE(format.formatByteSize(1.0e27, 1, KFormat::MetricBinaryDialect), QStringLiteral("1,000.0 YB"));

    // Spattering of specific units
    QCOMPARE(format.formatByteSize(823000, 3, KFormat::IECBinaryDialect, KFormat::UnitMegaByte), QStringLiteral("0.785 MiB"));
    QCOMPARE(format.formatByteSize(1234034.0, 4, KFormat::JEDECBinaryDialect, KFormat::UnitByte), QStringLiteral("1,234,034 B"));

    // Check examples from the documentation
    QCOMPARE(format.formatByteSize(1000, 1, KFormat::MetricBinaryDialect, KFormat::UnitKiloByte), QStringLiteral("1.0 kB"));
    QCOMPARE(format.formatByteSize(1000, 1, KFormat::IECBinaryDialect, KFormat::UnitKiloByte), QStringLiteral("1.0 KiB"));
    QCOMPARE(format.formatByteSize(1000, 1, KFormat::JEDECBinaryDialect, KFormat::UnitKiloByte), QStringLiteral("1.0 KB"));
}

void KFormatTest::formatValue()
{
    QLocale locale(QLocale::c());
    locale.setNumberOptions(QLocale::DefaultNumberOptions); // Qt >= 5.6 sets QLocale::OmitGroupSeparator for the C locale
    KFormat format(locale);

    // Check examples from the documentation
    QCOMPARE(format.formatValue(1000, KFormat::Unit::Byte, 1, KFormat::UnitPrefix::Kilo, KFormat::MetricBinaryDialect), QStringLiteral("1.0 kB"));
    QCOMPARE(format.formatValue(1000, KFormat::Unit::Byte, 1, KFormat::UnitPrefix::Kilo, KFormat::IECBinaryDialect), QStringLiteral("1.0 KiB"));
    QCOMPARE(format.formatValue(1000, KFormat::Unit::Byte, 1, KFormat::UnitPrefix::Kilo, KFormat::JEDECBinaryDialect), QStringLiteral("1.0 KB"));

    // Check examples from the documentation
    QCOMPARE(format.formatValue(1000, KFormat::Unit::Bit, 1, KFormat::UnitPrefix::Kilo, KFormat::MetricBinaryDialect), QStringLiteral("1.0 kbit"));
    QCOMPARE(format.formatValue(1000, QStringLiteral("bit"), 1, KFormat::UnitPrefix::Kilo), QStringLiteral("1.0 kbit"));
    QCOMPARE(format.formatValue(1000, QStringLiteral("bit/s"), 1, KFormat::UnitPrefix::Kilo), QStringLiteral("1.0 kbit/s"));

    QCOMPARE(format.formatValue(100, QStringLiteral("bit/s")), QStringLiteral("100.0 bit/s"));
    QCOMPARE(format.formatValue(1000, QStringLiteral("bit/s")), QStringLiteral("1.0 kbit/s"));
    QCOMPARE(format.formatValue(10e3, QStringLiteral("bit/s")), QStringLiteral("10.0 kbit/s"));
    QCOMPARE(format.formatValue(10e6, QStringLiteral("bit/s")), QStringLiteral("10.0 Mbit/s"));

    QCOMPARE(format.formatValue(0.010, KFormat::Unit::Meter, 1, KFormat::UnitPrefix::Milli, KFormat::MetricBinaryDialect), QStringLiteral("10.0 mm"));
    QCOMPARE(format.formatValue(10.12e-6, KFormat::Unit::Meter, 2, KFormat::UnitPrefix::Micro, KFormat::MetricBinaryDialect), QStringLiteral("10.12 µm"));
    QCOMPARE(format.formatValue(10.55e-6, KFormat::Unit::Meter, 1, KFormat::UnitPrefix::AutoAdjust, KFormat::MetricBinaryDialect), QStringLiteral("10.6 µm"));
}

enum TimeConstants {
    MSecsInDay = 86400000,
    MSecsInHour = 3600000,
    MSecsInMinute = 60000,
    MSecsInSecond = 1000
};

void KFormatTest::formatDuration()
{
    KFormat format(QLocale::c());

    quint64 singleSecond = 3 * MSecsInSecond + 700;
    quint64 doubleSecond = 33 * MSecsInSecond + 700;
    quint64 singleMinute = 8 * MSecsInMinute + 3 * MSecsInSecond + 700;
    quint64 doubleMinute = 38 * MSecsInMinute + 3 * MSecsInSecond + 700;
    quint64 singleHour = 5 * MSecsInHour + 8 * MSecsInMinute + 3 * MSecsInSecond + 700;
    quint64 doubleHour = 15 * MSecsInHour + 8 * MSecsInMinute + 3 * MSecsInSecond + 700;
    quint64 singleDay = 1 * MSecsInDay + 5 * MSecsInHour + 8 * MSecsInMinute + 3 * MSecsInSecond + 700;
    quint64 doubleDay = 10 * MSecsInDay + 5 * MSecsInHour + 8 * MSecsInMinute + 3 * MSecsInSecond + 700;
    quint64 roundingIssues = 2* MSecsInHour +  59 * MSecsInMinute + 59 * MSecsInSecond + 900;
    quint64 largeValue = 9999999999;

    // Default format
    QCOMPARE(format.formatDuration(singleSecond), QStringLiteral("0:00:04"));
    QCOMPARE(format.formatDuration(doubleSecond), QStringLiteral("0:00:34"));
    QCOMPARE(format.formatDuration(singleMinute), QStringLiteral("0:08:04"));
    QCOMPARE(format.formatDuration(doubleMinute), QStringLiteral("0:38:04"));
    QCOMPARE(format.formatDuration(singleHour), QStringLiteral("5:08:04"));
    QCOMPARE(format.formatDuration(doubleHour), QStringLiteral("15:08:04"));
    QCOMPARE(format.formatDuration(singleDay), QStringLiteral("29:08:04"));
    QCOMPARE(format.formatDuration(doubleDay), QStringLiteral("245:08:04"));
    QCOMPARE(format.formatDuration(roundingIssues), QStringLiteral("3:00:00"));
    QCOMPARE(format.formatDuration(largeValue), QStringLiteral("2777:46:40"));


    // ShowMilliseconds format
    KFormat::DurationFormatOptions options = KFormat::ShowMilliseconds;
    QCOMPARE(format.formatDuration(singleSecond, options), QStringLiteral("0:00:03.700"));
    QCOMPARE(format.formatDuration(doubleSecond, options), QStringLiteral("0:00:33.700"));
    QCOMPARE(format.formatDuration(singleMinute, options), QStringLiteral("0:08:03.700"));
    QCOMPARE(format.formatDuration(doubleMinute, options), QStringLiteral("0:38:03.700"));
    QCOMPARE(format.formatDuration(singleHour, options), QStringLiteral("5:08:03.700"));
    QCOMPARE(format.formatDuration(doubleHour, options), QStringLiteral("15:08:03.700"));
    QCOMPARE(format.formatDuration(singleDay, options), QStringLiteral("29:08:03.700"));
    QCOMPARE(format.formatDuration(doubleDay, options), QStringLiteral("245:08:03.700"));
    QCOMPARE(format.formatDuration(roundingIssues, options), QStringLiteral("2:59:59.900"));
    QCOMPARE(format.formatDuration(largeValue,options), QStringLiteral("2777:46:39.999"));


    // HideSeconds format
    options = KFormat::HideSeconds;
    QCOMPARE(format.formatDuration(singleSecond, options), QStringLiteral("0:00"));
    QCOMPARE(format.formatDuration(doubleSecond, options), QStringLiteral("0:01"));
    QCOMPARE(format.formatDuration(singleMinute, options), QStringLiteral("0:08"));
    QCOMPARE(format.formatDuration(doubleMinute, options), QStringLiteral("0:38"));
    QCOMPARE(format.formatDuration(singleHour, options), QStringLiteral("5:08"));
    QCOMPARE(format.formatDuration(doubleHour, options), QStringLiteral("15:08"));
    QCOMPARE(format.formatDuration(singleDay, options), QStringLiteral("29:08"));
    QCOMPARE(format.formatDuration(doubleDay, options), QStringLiteral("245:08"));
    QCOMPARE(format.formatDuration(roundingIssues, options), QStringLiteral("3:00"));
    QCOMPARE(format.formatDuration(largeValue,options), QStringLiteral("2777:47"));


    // FoldHours format
    options = KFormat::FoldHours;
    QCOMPARE(format.formatDuration(singleSecond, options), QStringLiteral("0:04"));
    QCOMPARE(format.formatDuration(doubleSecond, options), QStringLiteral("0:34"));
    QCOMPARE(format.formatDuration(singleMinute, options), QStringLiteral("8:04"));
    QCOMPARE(format.formatDuration(doubleMinute, options), QStringLiteral("38:04"));
    QCOMPARE(format.formatDuration(singleHour, options), QStringLiteral("308:04"));
    QCOMPARE(format.formatDuration(doubleHour, options), QStringLiteral("908:04"));
    QCOMPARE(format.formatDuration(singleDay, options), QStringLiteral("1748:04"));
    QCOMPARE(format.formatDuration(doubleDay, options), QStringLiteral("14708:04"));
    QCOMPARE(format.formatDuration(roundingIssues, options), QStringLiteral("180:00"));
    QCOMPARE(format.formatDuration(largeValue,options), QStringLiteral("166666:40"));


    // FoldHours ShowMilliseconds format
    options = KFormat::FoldHours;
    options = options | KFormat::ShowMilliseconds;
    QCOMPARE(format.formatDuration(singleSecond, options), QStringLiteral("0:03.700"));
    QCOMPARE(format.formatDuration(doubleSecond, options), QStringLiteral("0:33.700"));
    QCOMPARE(format.formatDuration(singleMinute, options), QStringLiteral("8:03.700"));
    QCOMPARE(format.formatDuration(doubleMinute, options), QStringLiteral("38:03.700"));
    QCOMPARE(format.formatDuration(singleHour, options), QStringLiteral("308:03.700"));
    QCOMPARE(format.formatDuration(doubleHour, options), QStringLiteral("908:03.700"));
    QCOMPARE(format.formatDuration(singleDay, options), QStringLiteral("1748:03.700"));
    QCOMPARE(format.formatDuration(doubleDay, options), QStringLiteral("14708:03.700"));
    QCOMPARE(format.formatDuration(roundingIssues, options), QStringLiteral("179:59.900"));
    QCOMPARE(format.formatDuration(largeValue,options), QStringLiteral("166666:39.999"));


    // InitialDuration format
    options = KFormat::InitialDuration;
    QCOMPARE(format.formatDuration(singleSecond, options), QStringLiteral("0h00m04s"));
    QCOMPARE(format.formatDuration(doubleSecond, options), QStringLiteral("0h00m34s"));
    QCOMPARE(format.formatDuration(singleMinute, options), QStringLiteral("0h08m04s"));
    QCOMPARE(format.formatDuration(doubleMinute, options), QStringLiteral("0h38m04s"));
    QCOMPARE(format.formatDuration(singleHour, options), QStringLiteral("5h08m04s"));
    QCOMPARE(format.formatDuration(doubleHour, options), QStringLiteral("15h08m04s"));
    QCOMPARE(format.formatDuration(singleDay, options), QStringLiteral("29h08m04s"));
    QCOMPARE(format.formatDuration(doubleDay, options), QStringLiteral("245h08m04s"));
    QCOMPARE(format.formatDuration(roundingIssues, options), QStringLiteral("3h00m00s"));
    QCOMPARE(format.formatDuration(largeValue,options), QStringLiteral("2777h46m40s"));


    // InitialDuration and ShowMilliseconds format
    options = KFormat::InitialDuration;
    options = options | KFormat::ShowMilliseconds;
    QCOMPARE(format.formatDuration(singleSecond, options), QStringLiteral("0h00m03.700s"));
    QCOMPARE(format.formatDuration(doubleSecond, options), QStringLiteral("0h00m33.700s"));
    QCOMPARE(format.formatDuration(singleMinute, options), QStringLiteral("0h08m03.700s"));
    QCOMPARE(format.formatDuration(doubleMinute, options), QStringLiteral("0h38m03.700s"));
    QCOMPARE(format.formatDuration(singleHour, options), QStringLiteral("5h08m03.700s"));
    QCOMPARE(format.formatDuration(doubleHour, options), QStringLiteral("15h08m03.700s"));
    QCOMPARE(format.formatDuration(singleDay, options), QStringLiteral("29h08m03.700s"));
    QCOMPARE(format.formatDuration(doubleDay, options), QStringLiteral("245h08m03.700s"));
    QCOMPARE(format.formatDuration(roundingIssues, options), QStringLiteral("2h59m59.900s"));
    QCOMPARE(format.formatDuration(largeValue,options), QStringLiteral("2777h46m39.999s"));


    // InitialDuration and HideSeconds format
    options = KFormat::InitialDuration;
    options = options | KFormat::HideSeconds;
    QCOMPARE(format.formatDuration(singleSecond, options), QStringLiteral("0h00m"));
    QCOMPARE(format.formatDuration(doubleSecond, options), QStringLiteral("0h01m"));
    QCOMPARE(format.formatDuration(singleMinute, options), QStringLiteral("0h08m"));
    QCOMPARE(format.formatDuration(doubleMinute, options), QStringLiteral("0h38m"));
    QCOMPARE(format.formatDuration(singleHour, options), QStringLiteral("5h08m"));
    QCOMPARE(format.formatDuration(doubleHour, options), QStringLiteral("15h08m"));
    QCOMPARE(format.formatDuration(singleDay, options), QStringLiteral("29h08m"));
    QCOMPARE(format.formatDuration(doubleDay, options), QStringLiteral("245h08m"));
    QCOMPARE(format.formatDuration(roundingIssues, options), QStringLiteral("3h00m"));
    QCOMPARE(format.formatDuration(largeValue,options), QStringLiteral("2777h47m"));


    // InitialDuration and FoldHours format
    options = KFormat::InitialDuration;
    options = options | KFormat::FoldHours;
    QCOMPARE(format.formatDuration(singleSecond, options), QStringLiteral("0m04s"));
    QCOMPARE(format.formatDuration(doubleSecond, options), QStringLiteral("0m34s"));
    QCOMPARE(format.formatDuration(singleMinute, options), QStringLiteral("8m04s"));
    QCOMPARE(format.formatDuration(doubleMinute, options), QStringLiteral("38m04s"));
    QCOMPARE(format.formatDuration(singleHour, options), QStringLiteral("308m04s"));
    QCOMPARE(format.formatDuration(doubleHour, options), QStringLiteral("908m04s"));
    QCOMPARE(format.formatDuration(singleDay, options), QStringLiteral("1748m04s"));
    QCOMPARE(format.formatDuration(doubleDay, options), QStringLiteral("14708m04s"));
    QCOMPARE(format.formatDuration(roundingIssues, options), QStringLiteral("180m00s"));
    QCOMPARE(format.formatDuration(largeValue,options), QStringLiteral("166666m40s"));


    // InitialDuration and FoldHours and ShowMilliseconds format
    options = KFormat::InitialDuration;
    options = options | KFormat::FoldHours | KFormat::ShowMilliseconds;
    QCOMPARE(format.formatDuration(singleSecond, options), QStringLiteral("0m03.700s"));
    QCOMPARE(format.formatDuration(doubleSecond, options), QStringLiteral("0m33.700s"));
    QCOMPARE(format.formatDuration(singleMinute, options), QStringLiteral("8m03.700s"));
    QCOMPARE(format.formatDuration(doubleMinute, options), QStringLiteral("38m03.700s"));
    QCOMPARE(format.formatDuration(singleHour, options), QStringLiteral("308m03.700s"));
    QCOMPARE(format.formatDuration(doubleHour, options), QStringLiteral("908m03.700s"));
    QCOMPARE(format.formatDuration(singleDay, options), QStringLiteral("1748m03.700s"));
    QCOMPARE(format.formatDuration(doubleDay, options), QStringLiteral("14708m03.700s"));
    QCOMPARE(format.formatDuration(roundingIssues, options), QStringLiteral("179m59.900s"));
    QCOMPARE(format.formatDuration(largeValue,options), QStringLiteral("166666m39.999s"));
}

void KFormatTest::formatDecimalDuration()
{
    KFormat format(QLocale::c());

    QCOMPARE(format.formatDecimalDuration(10), QStringLiteral("10 milliseconds"));
    QCOMPARE(format.formatDecimalDuration(10, 3), QStringLiteral("10 milliseconds"));
    QCOMPARE(format.formatDecimalDuration(1 * MSecsInSecond + 10), QStringLiteral("1.01 seconds"));
    QCOMPARE(format.formatDecimalDuration(1 * MSecsInSecond + 1, 3), QStringLiteral("1.001 seconds"));
    QCOMPARE(format.formatDecimalDuration(1 * MSecsInMinute + 10 * MSecsInSecond), QStringLiteral("1.17 minutes"));
    QCOMPARE(format.formatDecimalDuration(1 * MSecsInMinute + 10 * MSecsInSecond, 3), QStringLiteral("1.167 minutes"));
    QCOMPARE(format.formatDecimalDuration(1 * MSecsInHour + 10 * MSecsInMinute), QStringLiteral("1.17 hours"));
    QCOMPARE(format.formatDecimalDuration(1 * MSecsInHour + 10 * MSecsInMinute, 3), QStringLiteral("1.167 hours"));
    QCOMPARE(format.formatDecimalDuration(1 * MSecsInDay + 10 * MSecsInHour), QStringLiteral("1.42 days"));
    QCOMPARE(format.formatDecimalDuration(1 * MSecsInDay + 10 * MSecsInHour, 3), QStringLiteral("1.417 days"));
}

void KFormatTest::formatSpelloutDuration()
{
    KFormat format(QLocale::c());

    QCOMPARE(format.formatSpelloutDuration(1000), QStringLiteral("1 second"));
    QCOMPARE(format.formatSpelloutDuration(5000), QStringLiteral("5 seconds"));
    QCOMPARE(format.formatSpelloutDuration(60000), QStringLiteral("1 minute"));
    QCOMPARE(format.formatSpelloutDuration(300000), QStringLiteral("5 minutes"));
    QCOMPARE(format.formatSpelloutDuration(3600000), QStringLiteral("1 hour"));
    QCOMPARE(format.formatSpelloutDuration(18000000), QStringLiteral("5 hours"));
    QCOMPARE(format.formatSpelloutDuration(75000), QStringLiteral("1 minute and 15 seconds"));
    // Problematic case #1 (there is a reference to this case on kformat.cpp)
    QCOMPARE(format.formatSpelloutDuration(119999), QStringLiteral("2 minutes"));
    // This case is strictly 2 hours, 15 minutes and 59 seconds. However, since the range is
    // pretty high between hours and seconds, formatSpelloutDuration always omits seconds when there
    // are hours in scene.
    QCOMPARE(format.formatSpelloutDuration(8159000), QStringLiteral("2 hours and 15 minutes"));
    // This case is strictly 1 hour and 10 seconds. For the same reason, formatSpelloutDuration
    // detects that 10 seconds is just garbage compared to 1 hour, and omits it on the result.
    QCOMPARE(format.formatSpelloutDuration(3610000), QStringLiteral("1 hour"));
}

void KFormatTest::formatRelativeDate()
{
    KFormat format(QLocale::c());

    QDate testDate = QDate::currentDate();
    QCOMPARE(format.formatRelativeDate(testDate, QLocale::LongFormat), QStringLiteral("Today"));
    QCOMPARE(format.formatRelativeDate(testDate, QLocale::ShortFormat), QStringLiteral("Today"));
    QCOMPARE(format.formatRelativeDate(testDate, QLocale::NarrowFormat), QStringLiteral("Today"));

    testDate = QDate::currentDate().addDays(1);
    QCOMPARE(format.formatRelativeDate(testDate, QLocale::LongFormat), QStringLiteral("Tomorrow"));
    QCOMPARE(format.formatRelativeDate(testDate, QLocale::ShortFormat), QStringLiteral("Tomorrow"));
    QCOMPARE(format.formatRelativeDate(testDate, QLocale::NarrowFormat), QStringLiteral("Tomorrow"));

    testDate = QDate::currentDate().addDays(-1);
    QCOMPARE(format.formatRelativeDate(testDate, QLocale::LongFormat), QStringLiteral("Yesterday"));
    QCOMPARE(format.formatRelativeDate(testDate, QLocale::ShortFormat), QStringLiteral("Yesterday"));
    QCOMPARE(format.formatRelativeDate(testDate, QLocale::NarrowFormat), QStringLiteral("Yesterday"));

    // Relative dates within a week are up to translators but there's no
    // expectation that day names are shortened -- per the API docs, the date
    // format is only used when the date is outside the relative window
    testDate = QDate::currentDate().addDays(-7);
    QCOMPARE(format.formatRelativeDate(testDate, QLocale::LongFormat),
             QStringLiteral("Last %1").arg(QLocale::c().dayName(testDate.dayOfWeek(), QLocale::LongFormat)));
    QCOMPARE(format.formatRelativeDate(testDate, QLocale::ShortFormat),
             QStringLiteral("Last %1").arg(QLocale::c().dayName(testDate.dayOfWeek(), QLocale::LongFormat)));
    QCOMPARE(format.formatRelativeDate(testDate, QLocale::NarrowFormat),
             QStringLiteral("Last %1").arg(QLocale::c().dayName(testDate.dayOfWeek(), QLocale::LongFormat)));

    testDate = QDate::currentDate().addDays(7);
    QCOMPARE(format.formatRelativeDate(testDate, QLocale::LongFormat),
             QStringLiteral("Next %1").arg(QLocale::c().dayName(testDate.dayOfWeek(), QLocale::LongFormat)));
    QCOMPARE(format.formatRelativeDate(testDate, QLocale::ShortFormat),
             QStringLiteral("Next %1").arg(QLocale::c().dayName(testDate.dayOfWeek(), QLocale::LongFormat)));
    QCOMPARE(format.formatRelativeDate(testDate, QLocale::NarrowFormat),
             QStringLiteral("Next %1").arg(QLocale::c().dayName(testDate.dayOfWeek(), QLocale::LongFormat)));

    testDate = QDate::currentDate().addDays(-8);
    QCOMPARE(format.formatRelativeDate(testDate, QLocale::LongFormat),
             QLocale::c().toString(testDate, QLocale::LongFormat));
    QCOMPARE(format.formatRelativeDate(testDate, QLocale::ShortFormat),
             QLocale::c().toString(testDate, QLocale::ShortFormat));
    QCOMPARE(format.formatRelativeDate(testDate, QLocale::NarrowFormat),
             QLocale::c().toString(testDate, QLocale::NarrowFormat));

    testDate = QDate::currentDate().addDays(8);
    QCOMPARE(format.formatRelativeDate(testDate, QLocale::LongFormat),
             QLocale::c().toString(testDate, QLocale::LongFormat));
    QCOMPARE(format.formatRelativeDate(testDate, QLocale::ShortFormat),
             QLocale::c().toString(testDate, QLocale::ShortFormat));
    QCOMPARE(format.formatRelativeDate(testDate, QLocale::NarrowFormat),
             QLocale::c().toString(testDate, QLocale::NarrowFormat));

    testDate = QDate(); // invalid date
    QCOMPARE(format.formatRelativeDate(testDate, QLocale::LongFormat), QStringLiteral("Invalid date"));

    QDateTime testDateTime = QDateTime(QDate::currentDate(), QTime(3, 0, 0));
    QCOMPARE(format.formatRelativeDateTime(testDateTime, QLocale::ShortFormat), QStringLiteral("Today, 03:00:00"));

    testDateTime = QDateTime(QDate::currentDate().addDays(8), QTime(3, 0, 0));
    QCOMPARE(format.formatRelativeDateTime(testDateTime, QLocale::LongFormat),
             QLocale::c().toString(testDateTime, QLocale::LongFormat));
}

QTEST_MAIN(KFormatTest)