kformattest.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. This file is part of the KDE Frameworks
  3. SPDX-FileCopyrightText: 2013 John Layt <jlayt@kde.org>
  4. SPDX-FileCopyrightText: 2010 Michael Leupold <lemma@confuego.org>
  5. SPDX-FileCopyrightText: 2009 Michael Pyne <mpyne@kde.org>
  6. SPDX-FileCopyrightText: 2008 Albert Astals Cid <aacid@kde.org>
  7. SPDX-License-Identifier: LGPL-2.0-or-later
  8. */
  9. #include "kformattest.h"
  10. #include <QTest>
  11. #include "kformat.h"
  12. void setupEnvironment()
  13. {
  14. #ifndef Q_OS_WIN
  15. // ignore translations
  16. qputenv("XDG_DATA_DIRS", "does-not-exist");
  17. #endif
  18. }
  19. Q_CONSTRUCTOR_FUNCTION(setupEnvironment)
  20. void KFormatTest::formatByteSize()
  21. {
  22. QLocale locale(QLocale::c());
  23. locale.setNumberOptions(QLocale::DefaultNumberOptions); // Qt >= 5.6 sets QLocale::OmitGroupSeparator for the C locale
  24. KFormat format(locale);
  25. QCOMPARE(format.formatByteSize(0), QStringLiteral("0 B"));
  26. QCOMPARE(format.formatByteSize(50), QStringLiteral("50 B"));
  27. QCOMPARE(format.formatByteSize(500), QStringLiteral("500 B"));
  28. QCOMPARE(format.formatByteSize(5000), QStringLiteral("4.9 KiB"));
  29. QCOMPARE(format.formatByteSize(50000), QStringLiteral("48.8 KiB"));
  30. QCOMPARE(format.formatByteSize(500000), QStringLiteral("488.3 KiB"));
  31. QCOMPARE(format.formatByteSize(5000000), QStringLiteral("4.8 MiB"));
  32. QCOMPARE(format.formatByteSize(50000000), QStringLiteral("47.7 MiB"));
  33. QCOMPARE(format.formatByteSize(500000000), QStringLiteral("476.8 MiB"));
  34. #if (defined(__WORDSIZE) && (__WORDSIZE == 64)) || defined(_LP64) || defined(__LP64__) || defined(__ILP64__)
  35. QCOMPARE(format.formatByteSize(5000000000), QStringLiteral("4.7 GiB"));
  36. QCOMPARE(format.formatByteSize(50000000000), QStringLiteral("46.6 GiB"));
  37. QCOMPARE(format.formatByteSize(500000000000), QStringLiteral("465.7 GiB"));
  38. QCOMPARE(format.formatByteSize(5000000000000), QStringLiteral("4.5 TiB"));
  39. QCOMPARE(format.formatByteSize(50000000000000), QStringLiteral("45.5 TiB"));
  40. QCOMPARE(format.formatByteSize(500000000000000), QStringLiteral("454.7 TiB"));
  41. #endif
  42. QCOMPARE(format.formatByteSize(1024.0, 1, KFormat::IECBinaryDialect), QStringLiteral("1.0 KiB"));
  43. QCOMPARE(format.formatByteSize(1023.0, 1, KFormat::IECBinaryDialect), QStringLiteral("1,023 B"));
  44. QCOMPARE(format.formatByteSize(1163000.0, 1, KFormat::IECBinaryDialect), QStringLiteral("1.1 MiB")); // 1.2 metric
  45. QCOMPARE(format.formatByteSize(-1024.0, 1, KFormat::IECBinaryDialect), QStringLiteral("-1.0 KiB"));
  46. QCOMPARE(format.formatByteSize(-1023.0, 1, KFormat::IECBinaryDialect), QStringLiteral("-1,023 B"));
  47. QCOMPARE(format.formatByteSize(-1163000.0, 1, KFormat::IECBinaryDialect), QStringLiteral("-1.1 MiB")); // 1.2 metric
  48. QCOMPARE(format.formatByteSize(1024.0, 1, KFormat::JEDECBinaryDialect), QStringLiteral("1.0 KB"));
  49. QCOMPARE(format.formatByteSize(1023.0, 1, KFormat::JEDECBinaryDialect), QStringLiteral("1,023 B"));
  50. QCOMPARE(format.formatByteSize(1163000.0, 1, KFormat::JEDECBinaryDialect), QStringLiteral("1.1 MB"));
  51. QCOMPARE(format.formatByteSize(-1024.0, 1, KFormat::JEDECBinaryDialect), QStringLiteral("-1.0 KB"));
  52. QCOMPARE(format.formatByteSize(-1023.0, 1, KFormat::JEDECBinaryDialect), QStringLiteral("-1,023 B"));
  53. QCOMPARE(format.formatByteSize(-1163000.0, 1, KFormat::JEDECBinaryDialect), QStringLiteral("-1.1 MB"));
  54. QCOMPARE(format.formatByteSize(1024.0, 1, KFormat::MetricBinaryDialect), QStringLiteral("1.0 kB"));
  55. QCOMPARE(format.formatByteSize(1023.0, 1, KFormat::MetricBinaryDialect), QStringLiteral("1.0 kB"));
  56. QCOMPARE(format.formatByteSize(1163000.0, 1, KFormat::MetricBinaryDialect), QStringLiteral("1.2 MB"));
  57. QCOMPARE(format.formatByteSize(-1024.0, 1, KFormat::MetricBinaryDialect), QStringLiteral("-1.0 kB"));
  58. QCOMPARE(format.formatByteSize(-1023.0, 1, KFormat::MetricBinaryDialect), QStringLiteral("-1.0 kB"));
  59. QCOMPARE(format.formatByteSize(-1163000.0, 1, KFormat::MetricBinaryDialect), QStringLiteral("-1.2 MB"));
  60. // Ensure all units are represented
  61. QCOMPARE(format.formatByteSize(2.0e9, 1, KFormat::MetricBinaryDialect), QStringLiteral("2.0 GB"));
  62. QCOMPARE(format.formatByteSize(3.2e12, 1, KFormat::MetricBinaryDialect), QStringLiteral("3.2 TB"));
  63. QCOMPARE(format.formatByteSize(4.1e15, 1, KFormat::MetricBinaryDialect), QStringLiteral("4.1 PB"));
  64. QCOMPARE(format.formatByteSize(6.7e18, 2, KFormat::MetricBinaryDialect), QStringLiteral("6.70 EB"));
  65. QCOMPARE(format.formatByteSize(5.6e20, 2, KFormat::MetricBinaryDialect), QStringLiteral("560.00 EB"));
  66. QCOMPARE(format.formatByteSize(2.3e22, 2, KFormat::MetricBinaryDialect), QStringLiteral("23.00 ZB"));
  67. QCOMPARE(format.formatByteSize(1.0e27, 1, KFormat::MetricBinaryDialect), QStringLiteral("1,000.0 YB"));
  68. // Spattering of specific units
  69. QCOMPARE(format.formatByteSize(823000, 3, KFormat::IECBinaryDialect, KFormat::UnitMegaByte), QStringLiteral("0.785 MiB"));
  70. QCOMPARE(format.formatByteSize(1234034.0, 4, KFormat::JEDECBinaryDialect, KFormat::UnitByte), QStringLiteral("1,234,034 B"));
  71. // Check examples from the documentation
  72. QCOMPARE(format.formatByteSize(1000, 1, KFormat::MetricBinaryDialect, KFormat::UnitKiloByte), QStringLiteral("1.0 kB"));
  73. QCOMPARE(format.formatByteSize(1000, 1, KFormat::IECBinaryDialect, KFormat::UnitKiloByte), QStringLiteral("1.0 KiB"));
  74. QCOMPARE(format.formatByteSize(1000, 1, KFormat::JEDECBinaryDialect, KFormat::UnitKiloByte), QStringLiteral("1.0 KB"));
  75. }
  76. void KFormatTest::formatValue()
  77. {
  78. QLocale locale(QLocale::c());
  79. locale.setNumberOptions(QLocale::DefaultNumberOptions); // Qt >= 5.6 sets QLocale::OmitGroupSeparator for the C locale
  80. KFormat format(locale);
  81. // Check examples from the documentation
  82. QCOMPARE(format.formatValue(1000, KFormat::Unit::Byte, 1, KFormat::UnitPrefix::Kilo, KFormat::MetricBinaryDialect), QStringLiteral("1.0 kB"));
  83. QCOMPARE(format.formatValue(1000, KFormat::Unit::Byte, 1, KFormat::UnitPrefix::Kilo, KFormat::IECBinaryDialect), QStringLiteral("1.0 KiB"));
  84. QCOMPARE(format.formatValue(1000, KFormat::Unit::Byte, 1, KFormat::UnitPrefix::Kilo, KFormat::JEDECBinaryDialect), QStringLiteral("1.0 KB"));
  85. // Check examples from the documentation
  86. QCOMPARE(format.formatValue(1000, KFormat::Unit::Bit, 1, KFormat::UnitPrefix::Kilo, KFormat::MetricBinaryDialect), QStringLiteral("1.0 kbit"));
  87. QCOMPARE(format.formatValue(1000, QStringLiteral("bit"), 1, KFormat::UnitPrefix::Kilo), QStringLiteral("1.0 kbit"));
  88. QCOMPARE(format.formatValue(1000, QStringLiteral("bit/s"), 1, KFormat::UnitPrefix::Kilo), QStringLiteral("1.0 kbit/s"));
  89. QCOMPARE(format.formatValue(100, QStringLiteral("bit/s")), QStringLiteral("100.0 bit/s"));
  90. QCOMPARE(format.formatValue(1000, QStringLiteral("bit/s")), QStringLiteral("1.0 kbit/s"));
  91. QCOMPARE(format.formatValue(10e3, QStringLiteral("bit/s")), QStringLiteral("10.0 kbit/s"));
  92. QCOMPARE(format.formatValue(10e6, QStringLiteral("bit/s")), QStringLiteral("10.0 Mbit/s"));
  93. QCOMPARE(format.formatValue(0.010, KFormat::Unit::Meter, 1, KFormat::UnitPrefix::Milli, KFormat::MetricBinaryDialect), QStringLiteral("10.0 mm"));
  94. QCOMPARE(format.formatValue(10.12e-6, KFormat::Unit::Meter, 2, KFormat::UnitPrefix::Micro, KFormat::MetricBinaryDialect), QString::fromUtf8("10.12 µm"));
  95. QCOMPARE(format.formatValue(10.55e-6, KFormat::Unit::Meter, 1, KFormat::UnitPrefix::AutoAdjust, KFormat::MetricBinaryDialect),
  96. QString::fromUtf8("10.6 µm"));
  97. }
  98. enum TimeConstants {
  99. MSecsInDay = 86400000,
  100. MSecsInHour = 3600000,
  101. MSecsInMinute = 60000,
  102. MSecsInSecond = 1000,
  103. };
  104. void KFormatTest::formatDuration()
  105. {
  106. KFormat format(QLocale::c());
  107. quint64 singleSecond = 3 * MSecsInSecond + 700;
  108. quint64 doubleSecond = 33 * MSecsInSecond + 700;
  109. quint64 singleMinute = 8 * MSecsInMinute + 3 * MSecsInSecond + 700;
  110. quint64 doubleMinute = 38 * MSecsInMinute + 3 * MSecsInSecond + 700;
  111. quint64 singleHour = 5 * MSecsInHour + 8 * MSecsInMinute + 3 * MSecsInSecond + 700;
  112. quint64 doubleHour = 15 * MSecsInHour + 8 * MSecsInMinute + 3 * MSecsInSecond + 700;
  113. quint64 singleDay = 1 * MSecsInDay + 5 * MSecsInHour + 8 * MSecsInMinute + 3 * MSecsInSecond + 700;
  114. quint64 doubleDay = 10 * MSecsInDay + 5 * MSecsInHour + 8 * MSecsInMinute + 3 * MSecsInSecond + 700;
  115. quint64 roundingIssues = 2 * MSecsInHour + 59 * MSecsInMinute + 59 * MSecsInSecond + 900;
  116. quint64 largeValue = 9999999999;
  117. // Default format
  118. QCOMPARE(format.formatDuration(singleSecond), QStringLiteral("0:00:04"));
  119. QCOMPARE(format.formatDuration(doubleSecond), QStringLiteral("0:00:34"));
  120. QCOMPARE(format.formatDuration(singleMinute), QStringLiteral("0:08:04"));
  121. QCOMPARE(format.formatDuration(doubleMinute), QStringLiteral("0:38:04"));
  122. QCOMPARE(format.formatDuration(singleHour), QStringLiteral("5:08:04"));
  123. QCOMPARE(format.formatDuration(doubleHour), QStringLiteral("15:08:04"));
  124. QCOMPARE(format.formatDuration(singleDay), QStringLiteral("29:08:04"));
  125. QCOMPARE(format.formatDuration(doubleDay), QStringLiteral("245:08:04"));
  126. QCOMPARE(format.formatDuration(roundingIssues), QStringLiteral("3:00:00"));
  127. QCOMPARE(format.formatDuration(largeValue), QStringLiteral("2777:46:40"));
  128. // ShowMilliseconds format
  129. KFormat::DurationFormatOptions options = KFormat::ShowMilliseconds;
  130. QCOMPARE(format.formatDuration(singleSecond, options), QStringLiteral("0:00:03.700"));
  131. QCOMPARE(format.formatDuration(doubleSecond, options), QStringLiteral("0:00:33.700"));
  132. QCOMPARE(format.formatDuration(singleMinute, options), QStringLiteral("0:08:03.700"));
  133. QCOMPARE(format.formatDuration(doubleMinute, options), QStringLiteral("0:38:03.700"));
  134. QCOMPARE(format.formatDuration(singleHour, options), QStringLiteral("5:08:03.700"));
  135. QCOMPARE(format.formatDuration(doubleHour, options), QStringLiteral("15:08:03.700"));
  136. QCOMPARE(format.formatDuration(singleDay, options), QStringLiteral("29:08:03.700"));
  137. QCOMPARE(format.formatDuration(doubleDay, options), QStringLiteral("245:08:03.700"));
  138. QCOMPARE(format.formatDuration(roundingIssues, options), QStringLiteral("2:59:59.900"));
  139. QCOMPARE(format.formatDuration(largeValue, options), QStringLiteral("2777:46:39.999"));
  140. // HideSeconds format
  141. options = KFormat::HideSeconds;
  142. QCOMPARE(format.formatDuration(singleSecond, options), QStringLiteral("0:00"));
  143. QCOMPARE(format.formatDuration(doubleSecond, options), QStringLiteral("0:01"));
  144. QCOMPARE(format.formatDuration(singleMinute, options), QStringLiteral("0:08"));
  145. QCOMPARE(format.formatDuration(doubleMinute, options), QStringLiteral("0:38"));
  146. QCOMPARE(format.formatDuration(singleHour, options), QStringLiteral("5:08"));
  147. QCOMPARE(format.formatDuration(doubleHour, options), QStringLiteral("15:08"));
  148. QCOMPARE(format.formatDuration(singleDay, options), QStringLiteral("29:08"));
  149. QCOMPARE(format.formatDuration(doubleDay, options), QStringLiteral("245:08"));
  150. QCOMPARE(format.formatDuration(roundingIssues, options), QStringLiteral("3:00"));
  151. QCOMPARE(format.formatDuration(largeValue, options), QStringLiteral("2777:47"));
  152. // FoldHours format
  153. options = KFormat::FoldHours;
  154. QCOMPARE(format.formatDuration(singleSecond, options), QStringLiteral("0:04"));
  155. QCOMPARE(format.formatDuration(doubleSecond, options), QStringLiteral("0:34"));
  156. QCOMPARE(format.formatDuration(singleMinute, options), QStringLiteral("8:04"));
  157. QCOMPARE(format.formatDuration(doubleMinute, options), QStringLiteral("38:04"));
  158. QCOMPARE(format.formatDuration(singleHour, options), QStringLiteral("308:04"));
  159. QCOMPARE(format.formatDuration(doubleHour, options), QStringLiteral("908:04"));
  160. QCOMPARE(format.formatDuration(singleDay, options), QStringLiteral("1748:04"));
  161. QCOMPARE(format.formatDuration(doubleDay, options), QStringLiteral("14708:04"));
  162. QCOMPARE(format.formatDuration(roundingIssues, options), QStringLiteral("180:00"));
  163. QCOMPARE(format.formatDuration(largeValue, options), QStringLiteral("166666:40"));
  164. // FoldHours ShowMilliseconds format
  165. options = KFormat::FoldHours;
  166. options = options | KFormat::ShowMilliseconds;
  167. QCOMPARE(format.formatDuration(singleSecond, options), QStringLiteral("0:03.700"));
  168. QCOMPARE(format.formatDuration(doubleSecond, options), QStringLiteral("0:33.700"));
  169. QCOMPARE(format.formatDuration(singleMinute, options), QStringLiteral("8:03.700"));
  170. QCOMPARE(format.formatDuration(doubleMinute, options), QStringLiteral("38:03.700"));
  171. QCOMPARE(format.formatDuration(singleHour, options), QStringLiteral("308:03.700"));
  172. QCOMPARE(format.formatDuration(doubleHour, options), QStringLiteral("908:03.700"));
  173. QCOMPARE(format.formatDuration(singleDay, options), QStringLiteral("1748:03.700"));
  174. QCOMPARE(format.formatDuration(doubleDay, options), QStringLiteral("14708:03.700"));
  175. QCOMPARE(format.formatDuration(roundingIssues, options), QStringLiteral("179:59.900"));
  176. QCOMPARE(format.formatDuration(largeValue, options), QStringLiteral("166666:39.999"));
  177. // InitialDuration format
  178. options = KFormat::InitialDuration;
  179. QCOMPARE(format.formatDuration(singleSecond, options), QStringLiteral("0h00m04s"));
  180. QCOMPARE(format.formatDuration(doubleSecond, options), QStringLiteral("0h00m34s"));
  181. QCOMPARE(format.formatDuration(singleMinute, options), QStringLiteral("0h08m04s"));
  182. QCOMPARE(format.formatDuration(doubleMinute, options), QStringLiteral("0h38m04s"));
  183. QCOMPARE(format.formatDuration(singleHour, options), QStringLiteral("5h08m04s"));
  184. QCOMPARE(format.formatDuration(doubleHour, options), QStringLiteral("15h08m04s"));
  185. QCOMPARE(format.formatDuration(singleDay, options), QStringLiteral("29h08m04s"));
  186. QCOMPARE(format.formatDuration(doubleDay, options), QStringLiteral("245h08m04s"));
  187. QCOMPARE(format.formatDuration(roundingIssues, options), QStringLiteral("3h00m00s"));
  188. QCOMPARE(format.formatDuration(largeValue, options), QStringLiteral("2777h46m40s"));
  189. // InitialDuration and ShowMilliseconds format
  190. options = KFormat::InitialDuration;
  191. options = options | KFormat::ShowMilliseconds;
  192. QCOMPARE(format.formatDuration(singleSecond, options), QStringLiteral("0h00m03.700s"));
  193. QCOMPARE(format.formatDuration(doubleSecond, options), QStringLiteral("0h00m33.700s"));
  194. QCOMPARE(format.formatDuration(singleMinute, options), QStringLiteral("0h08m03.700s"));
  195. QCOMPARE(format.formatDuration(doubleMinute, options), QStringLiteral("0h38m03.700s"));
  196. QCOMPARE(format.formatDuration(singleHour, options), QStringLiteral("5h08m03.700s"));
  197. QCOMPARE(format.formatDuration(doubleHour, options), QStringLiteral("15h08m03.700s"));
  198. QCOMPARE(format.formatDuration(singleDay, options), QStringLiteral("29h08m03.700s"));
  199. QCOMPARE(format.formatDuration(doubleDay, options), QStringLiteral("245h08m03.700s"));
  200. QCOMPARE(format.formatDuration(roundingIssues, options), QStringLiteral("2h59m59.900s"));
  201. QCOMPARE(format.formatDuration(largeValue, options), QStringLiteral("2777h46m39.999s"));
  202. // InitialDuration and HideSeconds format
  203. options = KFormat::InitialDuration;
  204. options = options | KFormat::HideSeconds;
  205. QCOMPARE(format.formatDuration(singleSecond, options), QStringLiteral("0h00m"));
  206. QCOMPARE(format.formatDuration(doubleSecond, options), QStringLiteral("0h01m"));
  207. QCOMPARE(format.formatDuration(singleMinute, options), QStringLiteral("0h08m"));
  208. QCOMPARE(format.formatDuration(doubleMinute, options), QStringLiteral("0h38m"));
  209. QCOMPARE(format.formatDuration(singleHour, options), QStringLiteral("5h08m"));
  210. QCOMPARE(format.formatDuration(doubleHour, options), QStringLiteral("15h08m"));
  211. QCOMPARE(format.formatDuration(singleDay, options), QStringLiteral("29h08m"));
  212. QCOMPARE(format.formatDuration(doubleDay, options), QStringLiteral("245h08m"));
  213. QCOMPARE(format.formatDuration(roundingIssues, options), QStringLiteral("3h00m"));
  214. QCOMPARE(format.formatDuration(largeValue, options), QStringLiteral("2777h47m"));
  215. // InitialDuration and FoldHours format
  216. options = KFormat::InitialDuration;
  217. options = options | KFormat::FoldHours;
  218. QCOMPARE(format.formatDuration(singleSecond, options), QStringLiteral("0m04s"));
  219. QCOMPARE(format.formatDuration(doubleSecond, options), QStringLiteral("0m34s"));
  220. QCOMPARE(format.formatDuration(singleMinute, options), QStringLiteral("8m04s"));
  221. QCOMPARE(format.formatDuration(doubleMinute, options), QStringLiteral("38m04s"));
  222. QCOMPARE(format.formatDuration(singleHour, options), QStringLiteral("308m04s"));
  223. QCOMPARE(format.formatDuration(doubleHour, options), QStringLiteral("908m04s"));
  224. QCOMPARE(format.formatDuration(singleDay, options), QStringLiteral("1748m04s"));
  225. QCOMPARE(format.formatDuration(doubleDay, options), QStringLiteral("14708m04s"));
  226. QCOMPARE(format.formatDuration(roundingIssues, options), QStringLiteral("180m00s"));
  227. QCOMPARE(format.formatDuration(largeValue, options), QStringLiteral("166666m40s"));
  228. // InitialDuration and FoldHours and ShowMilliseconds format
  229. options = KFormat::InitialDuration;
  230. options = options | KFormat::FoldHours | KFormat::ShowMilliseconds;
  231. QCOMPARE(format.formatDuration(singleSecond, options), QStringLiteral("0m03.700s"));
  232. QCOMPARE(format.formatDuration(doubleSecond, options), QStringLiteral("0m33.700s"));
  233. QCOMPARE(format.formatDuration(singleMinute, options), QStringLiteral("8m03.700s"));
  234. QCOMPARE(format.formatDuration(doubleMinute, options), QStringLiteral("38m03.700s"));
  235. QCOMPARE(format.formatDuration(singleHour, options), QStringLiteral("308m03.700s"));
  236. QCOMPARE(format.formatDuration(doubleHour, options), QStringLiteral("908m03.700s"));
  237. QCOMPARE(format.formatDuration(singleDay, options), QStringLiteral("1748m03.700s"));
  238. QCOMPARE(format.formatDuration(doubleDay, options), QStringLiteral("14708m03.700s"));
  239. QCOMPARE(format.formatDuration(roundingIssues, options), QStringLiteral("179m59.900s"));
  240. QCOMPARE(format.formatDuration(largeValue, options), QStringLiteral("166666m39.999s"));
  241. }
  242. void KFormatTest::formatDecimalDuration()
  243. {
  244. KFormat format(QLocale::c());
  245. QCOMPARE(format.formatDecimalDuration(10), QStringLiteral("10 millisecond(s)"));
  246. QCOMPARE(format.formatDecimalDuration(10, 3), QStringLiteral("10 millisecond(s)"));
  247. QCOMPARE(format.formatDecimalDuration(1 * MSecsInSecond + 10), QStringLiteral("1.01 seconds"));
  248. QCOMPARE(format.formatDecimalDuration(1 * MSecsInSecond + 1, 3), QStringLiteral("1.001 seconds"));
  249. QCOMPARE(format.formatDecimalDuration(1 * MSecsInMinute + 10 * MSecsInSecond), QStringLiteral("1.17 minutes"));
  250. QCOMPARE(format.formatDecimalDuration(1 * MSecsInMinute + 10 * MSecsInSecond, 3), QStringLiteral("1.167 minutes"));
  251. QCOMPARE(format.formatDecimalDuration(1 * MSecsInHour + 10 * MSecsInMinute), QStringLiteral("1.17 hours"));
  252. QCOMPARE(format.formatDecimalDuration(1 * MSecsInHour + 10 * MSecsInMinute, 3), QStringLiteral("1.167 hours"));
  253. QCOMPARE(format.formatDecimalDuration(1 * MSecsInDay + 10 * MSecsInHour), QStringLiteral("1.42 days"));
  254. QCOMPARE(format.formatDecimalDuration(1 * MSecsInDay + 10 * MSecsInHour, 3), QStringLiteral("1.417 days"));
  255. }
  256. void KFormatTest::formatSpelloutDuration()
  257. {
  258. KFormat format(QLocale::c());
  259. QCOMPARE(format.formatSpelloutDuration(1000), QStringLiteral("1 second(s)"));
  260. QCOMPARE(format.formatSpelloutDuration(5000), QStringLiteral("5 second(s)"));
  261. QCOMPARE(format.formatSpelloutDuration(60000), QStringLiteral("1 minute(s)"));
  262. QCOMPARE(format.formatSpelloutDuration(300000), QStringLiteral("5 minute(s)"));
  263. QCOMPARE(format.formatSpelloutDuration(3600000), QStringLiteral("1 hour(s)"));
  264. QCOMPARE(format.formatSpelloutDuration(18000000), QStringLiteral("5 hour(s)"));
  265. QCOMPARE(format.formatSpelloutDuration(75000), QStringLiteral("1 minute(s) and 15 second(s)"));
  266. // Problematic case #1 (there is a reference to this case on kformat.cpp)
  267. QCOMPARE(format.formatSpelloutDuration(119999), QStringLiteral("2 minute(s)"));
  268. // This case is strictly 2 hours, 15 minutes and 59 seconds. However, since the range is
  269. // pretty high between hours and seconds, formatSpelloutDuration always omits seconds when there
  270. // are hours in scene.
  271. QCOMPARE(format.formatSpelloutDuration(8159000), QStringLiteral("2 hour(s) and 15 minute(s)"));
  272. // This case is strictly 1 hour and 10 seconds. For the same reason, formatSpelloutDuration
  273. // detects that 10 seconds is just garbage compared to 1 hour, and omits it on the result.
  274. QCOMPARE(format.formatSpelloutDuration(3610000), QStringLiteral("1 hour(s)"));
  275. }
  276. void KFormatTest::formatRelativeDate()
  277. {
  278. KFormat format(QLocale::c());
  279. QDate testDate = QDate::currentDate();
  280. QCOMPARE(format.formatRelativeDate(testDate, QLocale::LongFormat), QStringLiteral("Today"));
  281. QCOMPARE(format.formatRelativeDate(testDate, QLocale::ShortFormat), QStringLiteral("Today"));
  282. QCOMPARE(format.formatRelativeDate(testDate, QLocale::NarrowFormat), QStringLiteral("Today"));
  283. testDate = QDate::currentDate().addDays(1);
  284. QCOMPARE(format.formatRelativeDate(testDate, QLocale::LongFormat), QStringLiteral("Tomorrow"));
  285. QCOMPARE(format.formatRelativeDate(testDate, QLocale::ShortFormat), QStringLiteral("Tomorrow"));
  286. QCOMPARE(format.formatRelativeDate(testDate, QLocale::NarrowFormat), QStringLiteral("Tomorrow"));
  287. testDate = QDate::currentDate().addDays(-1);
  288. QCOMPARE(format.formatRelativeDate(testDate, QLocale::LongFormat), QStringLiteral("Yesterday"));
  289. QCOMPARE(format.formatRelativeDate(testDate, QLocale::ShortFormat), QStringLiteral("Yesterday"));
  290. QCOMPARE(format.formatRelativeDate(testDate, QLocale::NarrowFormat), QStringLiteral("Yesterday"));
  291. testDate = QDate::currentDate().addDays(2);
  292. QCOMPARE(format.formatRelativeDate(testDate, QLocale::LongFormat), QStringLiteral("In two days"));
  293. QCOMPARE(format.formatRelativeDate(testDate, QLocale::ShortFormat), QStringLiteral("In two days"));
  294. QCOMPARE(format.formatRelativeDate(testDate, QLocale::NarrowFormat), QStringLiteral("In two days"));
  295. testDate = QDate::currentDate().addDays(-2);
  296. QCOMPARE(format.formatRelativeDate(testDate, QLocale::LongFormat), QStringLiteral("Two days ago"));
  297. QCOMPARE(format.formatRelativeDate(testDate, QLocale::ShortFormat), QStringLiteral("Two days ago"));
  298. QCOMPARE(format.formatRelativeDate(testDate, QLocale::NarrowFormat), QStringLiteral("Two days ago"));
  299. testDate = QDate::currentDate().addDays(-3);
  300. QCOMPARE(format.formatRelativeDate(testDate, QLocale::LongFormat), QLocale::c().toString(testDate, QLocale::LongFormat));
  301. QCOMPARE(format.formatRelativeDate(testDate, QLocale::ShortFormat), QLocale::c().toString(testDate, QLocale::ShortFormat));
  302. QCOMPARE(format.formatRelativeDate(testDate, QLocale::NarrowFormat), QLocale::c().toString(testDate, QLocale::NarrowFormat));
  303. testDate = QDate::currentDate().addDays(3);
  304. QCOMPARE(format.formatRelativeDate(testDate, QLocale::LongFormat), QLocale::c().toString(testDate, QLocale::LongFormat));
  305. QCOMPARE(format.formatRelativeDate(testDate, QLocale::ShortFormat), QLocale::c().toString(testDate, QLocale::ShortFormat));
  306. QCOMPARE(format.formatRelativeDate(testDate, QLocale::NarrowFormat), QLocale::c().toString(testDate, QLocale::NarrowFormat));
  307. testDate = QDate(); // invalid date
  308. QCOMPARE(format.formatRelativeDate(testDate, QLocale::LongFormat), QStringLiteral("Invalid date"));
  309. QDateTime now = QDateTime::currentDateTime();
  310. // An hour ago is **usually** today, except after midnight; just bump
  311. // to after 2am to make the "today" test work.
  312. if (now.time().hour() == 0) {
  313. now = now.addSecs(7201);
  314. }
  315. QDateTime testDateTime = now.addSecs(-3600);
  316. QCOMPARE(format.formatRelativeDateTime(testDateTime, QLocale::ShortFormat),
  317. QStringLiteral("Today at %1").arg(testDateTime.toString(QStringLiteral("hh:mm:ss"))));
  318. // 1 second ago
  319. now = QDateTime::currentDateTime();
  320. testDateTime = now.addSecs(-1);
  321. QCOMPARE(format.formatRelativeDateTime(testDateTime, QLocale::ShortFormat), QStringLiteral("Just now"));
  322. // 5 minutes ago
  323. testDateTime = now.addSecs(-300);
  324. QCOMPARE(format.formatRelativeDateTime(testDateTime, QLocale::ShortFormat), QStringLiteral("5 minute(s) ago"));
  325. testDateTime = QDateTime(QDate::currentDate().addDays(8), QTime(3, 0, 0));
  326. QCOMPARE(format.formatRelativeDateTime(testDateTime, QLocale::LongFormat),
  327. QStringLiteral("%1 at %2")
  328. .arg(QLocale::c().toString(testDateTime.date(), QLocale::LongFormat), QLocale::c().toString(testDateTime.time(), QLocale::ShortFormat)));
  329. // 2021-10-03 07:33:57.000
  330. testDateTime = QDateTime::fromMSecsSinceEpoch(1633239237000, Qt::UTC);
  331. QCOMPARE(format.formatRelativeDateTime(testDateTime, QLocale::LongFormat),
  332. QStringLiteral("%1 at %2")
  333. .arg(QLocale::c().toString(testDateTime.date(), QLocale::LongFormat), QLocale::c().toString(testDateTime.time(), QLocale::ShortFormat)));
  334. QCOMPARE(format.formatRelativeDateTime(testDateTime, QLocale::ShortFormat),
  335. QStringLiteral("%1 at %2")
  336. .arg(QLocale::c().toString(testDateTime.date(), QLocale::ShortFormat), QLocale::c().toString(testDateTime.time(), QLocale::ShortFormat)));
  337. // With a different local for double check
  338. QLocale englishLocal = QLocale::English;
  339. KFormat formatEnglish(englishLocal);
  340. QCOMPARE(formatEnglish.formatRelativeDateTime(testDateTime, QLocale::LongFormat), QStringLiteral("Sunday, October 3, 2021 at 5:33 AM"));
  341. }
  342. QTEST_MAIN(KFormatTest)
  343. #include "moc_kformattest.cpp"