number_format_test_suite.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. // Copyright (c) 2014-2021 Thomas Fussell
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE
  20. //
  21. // @license: http://www.opensource.org/licenses/mit-license.php
  22. // @author: see AUTHORS file
  23. #include <iostream>
  24. #include <helpers/test_suite.hpp>
  25. #include <xlnt/styles/number_format.hpp>
  26. #include <xlnt/utils/date.hpp>
  27. #include <xlnt/utils/time.hpp>
  28. #include <xlnt/utils/timedelta.hpp>
  29. class number_format_test_suite : public test_suite
  30. {
  31. public:
  32. number_format_test_suite()
  33. {
  34. register_test(test_basic);
  35. register_test(test_simple_format);
  36. register_test(test_bad_date_format);
  37. register_test(test_simple_date);
  38. register_test(test_short_month);
  39. register_test(test_month_abbreviation);
  40. register_test(test_month_name);
  41. register_test(test_basic);
  42. register_test(test_upper_case_date);
  43. register_test(test_simple_date);
  44. register_test(test_short_day);
  45. register_test(test_long_day);
  46. register_test(test_long_year);
  47. register_test(test_day_name);
  48. register_test(test_day_abbreviation);
  49. register_test(test_month_letter);
  50. register_test(test_time_24_hour);
  51. register_test(test_elapsed_minutes);
  52. register_test(test_second_fractional_leading_zero);
  53. register_test(test_second_fractional);
  54. register_test(test_elapsed_seconds);
  55. register_test(test_time_12_hour_am);
  56. register_test(test_time_12_hour_pm);
  57. register_test(test_long_hour_12_hour);
  58. register_test(test_long_hour_12_hour_ap);
  59. register_test(test_long_hour_24_hour);
  60. register_test(test_short_minute);
  61. register_test(test_long_minute);
  62. register_test(test_short_second);
  63. register_test(test_long_second);
  64. register_test(test_trailing_space);
  65. register_test(test_text_section_string);
  66. register_test(test_text_section_no_string);
  67. register_test(test_text_section_no_text);
  68. register_test(test_conditional_format);
  69. register_test(test_space);
  70. register_test(test_fill);
  71. register_test(test_placeholders_zero);
  72. register_test(test_placeholders_space);
  73. register_test(test_scientific);
  74. register_test(test_locale_currency);
  75. register_test(test_bad_country);
  76. register_test(test_duplicate_bracket_sections);
  77. register_test(test_escaped_quote_string);
  78. register_test(test_thousands_scale);
  79. register_test(test_colors);
  80. register_test(test_bad_format);
  81. register_test(test_builtin_format_0);
  82. register_test(test_builtin_format_1);
  83. register_test(test_builtin_format_2);
  84. register_test(test_builtin_format_3);
  85. register_test(test_builtin_format_4);
  86. register_test(test_builtin_format_9);
  87. register_test(test_builtin_format_10);
  88. register_test(test_builtin_format_11);
  89. register_test(test_builtin_format_12);
  90. register_test(test_builtin_format_13);
  91. register_test(test_builtin_format_14);
  92. register_test(test_builtin_format_15);
  93. register_test(test_builtin_format_16);
  94. register_test(test_builtin_format_17);
  95. register_test(test_builtin_format_18);
  96. register_test(test_builtin_format_19);
  97. register_test(test_builtin_format_20);
  98. register_test(test_builtin_format_21);
  99. register_test(test_builtin_format_22);
  100. register_test(test_builtin_format_37);
  101. register_test(test_builtin_format_38);
  102. register_test(test_builtin_format_39);
  103. register_test(test_builtin_format_40);
  104. register_test(test_builtin_format_45);
  105. register_test(test_builtin_format_46);
  106. register_test(test_builtin_format_47);
  107. register_test(test_builtin_format_48);
  108. register_test(test_builtin_format_49);
  109. register_test(test_builtin_format_date_yyyymmdd);
  110. register_test(test_builtin_format_date_dmyslash);
  111. register_test(test_builtin_format_date_dmyminus);
  112. register_test(test_builtin_format_date_dmminus);
  113. register_test(test_builtin_format_date_myminus);
  114. }
  115. void test_basic()
  116. {
  117. xlnt::number_format no_id("#\\x\\y\\z");
  118. xlnt_assert_throws(no_id.id(), std::runtime_error);
  119. xlnt::number_format id("General", 200);
  120. xlnt_assert_equals(id.id(), 200);
  121. xlnt_assert_equals(id.format_string(), "General");
  122. xlnt::number_format general(0);
  123. xlnt_assert_equals(general, xlnt::number_format::general());
  124. xlnt_assert_equals(general.id(), 0);
  125. xlnt_assert_equals(general.format_string(), "General");
  126. }
  127. void test_simple_format()
  128. {
  129. xlnt::number_format nf;
  130. nf.format_string("\"positive\"General;\"negative\"General");
  131. auto formatted = nf.format(3.14, xlnt::calendar::windows_1900);
  132. xlnt_assert_equals(formatted, "positive3.14");
  133. formatted = nf.format(-3.14, xlnt::calendar::windows_1900);
  134. xlnt_assert_equals(formatted, "negative3.14");
  135. nf.format_string("\"any\"General");
  136. formatted = nf.format(-3.14, xlnt::calendar::windows_1900);
  137. xlnt_assert_equals(formatted, "-any3.14");
  138. nf.format_string("\"positive\"General;\"negative\"General;\"zero\"General");
  139. formatted = nf.format(3.14, xlnt::calendar::windows_1900);
  140. xlnt_assert_equals(formatted, "positive3.14");
  141. formatted = nf.format(-3.14, xlnt::calendar::windows_1900);
  142. xlnt_assert_equals(formatted, "negative3.14");
  143. formatted = nf.format(0, xlnt::calendar::windows_1900);
  144. xlnt_assert_equals(formatted, "zero0");
  145. }
  146. void test_bad_date_format()
  147. {
  148. auto date = xlnt::date(2016, 6, 18);
  149. auto date_number = date.to_number(xlnt::calendar::windows_1900);
  150. xlnt::number_format nf;
  151. nf.format_string("[x]");
  152. xlnt_assert_throws(nf.format(date_number, xlnt::calendar::windows_1900),
  153. std::runtime_error);
  154. nf.format_string("mmmmmm");
  155. xlnt_assert_throws(nf.format(date_number, xlnt::calendar::windows_1900),
  156. std::runtime_error);
  157. nf.format_string("ddddd");
  158. xlnt_assert_throws(nf.format(date_number, xlnt::calendar::windows_1900),
  159. std::runtime_error);
  160. nf.format_string("yyy");
  161. xlnt_assert_throws(nf.format(date_number, xlnt::calendar::windows_1900),
  162. std::runtime_error);
  163. nf.format_string("hhh");
  164. xlnt_assert_throws(nf.format(date_number, xlnt::calendar::windows_1900),
  165. std::runtime_error);
  166. nf.format_string("sss");
  167. xlnt_assert_throws(nf.format(date_number, xlnt::calendar::windows_1900),
  168. std::runtime_error);
  169. nf.format_string("AA");
  170. xlnt_assert_throws(nf.format(date_number, xlnt::calendar::windows_1900),
  171. std::runtime_error);
  172. nf.format_string("q");
  173. xlnt_assert_throws(nf.format(date_number, xlnt::calendar::windows_1900),
  174. std::runtime_error);
  175. }
  176. void test_upper_case_date()
  177. {
  178. auto date = xlnt::date(2016, 6, 18);
  179. auto date_number = date.to_number(xlnt::calendar::windows_1900);
  180. xlnt::number_format nf;
  181. nf.format_string("MM / DD / YYYY");
  182. auto formatted = nf.format(date_number, xlnt::calendar::windows_1900);
  183. xlnt_assert_equals(formatted, "06 / 18 / 2016");
  184. }
  185. void test_simple_date()
  186. {
  187. auto date = xlnt::date(2016, 6, 18);
  188. auto date_number = date.to_number(xlnt::calendar::windows_1900);
  189. xlnt::number_format nf = xlnt::number_format::date_ddmmyyyy();
  190. auto formatted = nf.format(date_number, xlnt::calendar::windows_1900);
  191. xlnt_assert_equals(formatted, "18/06/16");
  192. }
  193. void test_short_month()
  194. {
  195. auto date = xlnt::date(2016, 6, 18);
  196. auto date_number = date.to_number(xlnt::calendar::windows_1900);
  197. xlnt::number_format nf;
  198. nf.format_string("m");
  199. auto formatted = nf.format(date_number, xlnt::calendar::windows_1900);
  200. xlnt_assert_equals(formatted, "6");
  201. }
  202. void test_month_abbreviation()
  203. {
  204. auto date = xlnt::date(2016, 6, 18);
  205. auto date_number = date.to_number(xlnt::calendar::windows_1900);
  206. xlnt::number_format nf;
  207. nf.format_string("mmm");
  208. auto formatted = nf.format(date_number, xlnt::calendar::windows_1900);
  209. xlnt_assert_equals(formatted, "Jun");
  210. }
  211. void test_month_name()
  212. {
  213. auto date = xlnt::date(2016, 6, 18);
  214. auto date_number = date.to_number(xlnt::calendar::windows_1900);
  215. xlnt::number_format nf;
  216. nf.format_string("mmmm");
  217. auto formatted = nf.format(date_number, xlnt::calendar::windows_1900);
  218. xlnt_assert_equals(formatted, "June");
  219. }
  220. void test_short_day()
  221. {
  222. auto date = xlnt::date(2016, 6, 8);
  223. auto date_number = date.to_number(xlnt::calendar::windows_1900);
  224. xlnt::number_format nf;
  225. nf.format_string("d");
  226. auto formatted = nf.format(date_number, xlnt::calendar::windows_1900);
  227. xlnt_assert_equals(formatted, "8");
  228. }
  229. void test_long_day()
  230. {
  231. auto date = xlnt::date(2016, 6, 8);
  232. auto date_number = date.to_number(xlnt::calendar::windows_1900);
  233. xlnt::number_format nf;
  234. nf.format_string("dd");
  235. auto formatted = nf.format(date_number, xlnt::calendar::windows_1900);
  236. xlnt_assert_equals(formatted, "08");
  237. }
  238. void test_long_year()
  239. {
  240. auto date = xlnt::date(2016, 6, 18);
  241. auto date_number = date.to_number(xlnt::calendar::windows_1900);
  242. xlnt::number_format nf;
  243. nf.format_string("yyyy");
  244. auto formatted = nf.format(date_number, xlnt::calendar::windows_1900);
  245. xlnt_assert_equals(formatted, "2016");
  246. }
  247. void test_day_name()
  248. {
  249. auto date = xlnt::date(2016, 6, 18);
  250. auto date_number = date.to_number(xlnt::calendar::windows_1900);
  251. xlnt::number_format nf;
  252. nf.format_string("dddd");
  253. auto formatted = nf.format(date_number, xlnt::calendar::windows_1900);
  254. xlnt_assert_equals(formatted, "Saturday");
  255. }
  256. void test_day_abbreviation()
  257. {
  258. auto date = xlnt::date(2016, 6, 18);
  259. auto date_number = date.to_number(xlnt::calendar::windows_1900);
  260. xlnt::number_format nf;
  261. nf.format_string("ddd");
  262. auto formatted = nf.format(date_number, xlnt::calendar::windows_1900);
  263. xlnt_assert_equals(formatted, "Sat");
  264. }
  265. void test_month_letter()
  266. {
  267. auto date = xlnt::date(2016, 6, 18);
  268. auto date_number = date.to_number(xlnt::calendar::windows_1900);
  269. xlnt::number_format nf;
  270. nf.format_string("mmmmm");
  271. auto formatted = nf.format(date_number, xlnt::calendar::windows_1900);
  272. xlnt_assert_equals(formatted, "J");
  273. }
  274. void test_time_24_hour()
  275. {
  276. auto time = xlnt::time(20, 15, 10);
  277. auto time_number = time.to_number();
  278. xlnt::number_format nf = xlnt::number_format::date_time4();
  279. auto formatted = nf.format(time_number, xlnt::calendar::windows_1900);
  280. xlnt_assert_equals(formatted, "20:15:10");
  281. }
  282. void test_elapsed_minutes()
  283. {
  284. auto period = xlnt::timedelta(1, 2, 3, 4, 5);
  285. auto period_number = period.to_number();
  286. xlnt::number_format nf("[mm]:ss");
  287. auto formatted = nf.format(period_number, xlnt::calendar::windows_1900);
  288. xlnt_assert_equals(formatted, "1563:04");
  289. }
  290. void test_second_fractional_leading_zero()
  291. {
  292. auto time = xlnt::time(1, 2, 3, 400000);
  293. auto time_number = time.to_number();
  294. xlnt::number_format nf("ss.0");
  295. auto formatted = nf.format(time_number, xlnt::calendar::windows_1900);
  296. xlnt_assert_equals(formatted, "03.4");
  297. }
  298. void test_second_fractional()
  299. {
  300. auto time = xlnt::time(1, 2, 3, 400000);
  301. auto time_number = time.to_number();
  302. xlnt::number_format nf("s.0");
  303. auto formatted = nf.format(time_number, xlnt::calendar::windows_1900);
  304. xlnt_assert_equals(formatted, "3.4");
  305. }
  306. void test_elapsed_seconds()
  307. {
  308. auto period = xlnt::timedelta(1, 2, 3, 4, 5);
  309. auto period_number = period.to_number();
  310. xlnt::number_format nf("[ss]");
  311. auto formatted = nf.format(period_number, xlnt::calendar::windows_1900);
  312. xlnt_assert_equals(formatted, "93784");
  313. }
  314. void test_time_12_hour_am()
  315. {
  316. auto time = xlnt::time(8, 15, 10);
  317. auto time_number = time.to_number();
  318. xlnt::number_format nf = xlnt::number_format::date_time2();
  319. auto formatted = nf.format(time_number, xlnt::calendar::windows_1900);
  320. xlnt_assert_equals(formatted, "8:15:10 AM");
  321. }
  322. void test_time_12_hour_pm()
  323. {
  324. auto time = xlnt::time(20, 15, 10);
  325. auto time_number = time.to_number();
  326. xlnt::number_format nf = xlnt::number_format::date_time2();
  327. auto formatted = nf.format(time_number, xlnt::calendar::windows_1900);
  328. xlnt_assert_equals(formatted, "8:15:10 PM");
  329. }
  330. void test_long_hour_12_hour()
  331. {
  332. auto time = xlnt::time(20, 15, 10);
  333. auto time_number = time.to_number();
  334. xlnt::number_format nf;
  335. nf.format_string("hh AM/PM");
  336. auto formatted = nf.format(time_number, xlnt::calendar::windows_1900);
  337. xlnt_assert_equals(formatted, "08 PM");
  338. }
  339. void test_long_hour_12_hour_ap()
  340. {
  341. auto time1 = xlnt::time(20, 15, 10);
  342. auto time1_number = time1.to_number();
  343. auto time2 = xlnt::time(8, 15, 10);
  344. auto time2_number = time2.to_number();
  345. xlnt::number_format nf("hh A/P");
  346. auto formatted = nf.format(time1_number, xlnt::calendar::windows_1900);
  347. xlnt_assert_equals(formatted, "08 P");
  348. formatted = nf.format(time2_number, xlnt::calendar::windows_1900);
  349. xlnt_assert_equals(formatted, "08 A");
  350. }
  351. void test_long_hour_24_hour()
  352. {
  353. auto time = xlnt::time(20, 15, 10);
  354. auto time_number = time.to_number();
  355. xlnt::number_format nf;
  356. nf.format_string("hh");
  357. auto formatted = nf.format(time_number, xlnt::calendar::windows_1900);
  358. xlnt_assert_equals(formatted, "20");
  359. }
  360. void test_short_minute()
  361. {
  362. auto time = xlnt::time(20, 5, 10);
  363. auto time_number = time.to_number();
  364. xlnt::number_format nf;
  365. nf.format_string("h:m");
  366. auto formatted = nf.format(time_number, xlnt::calendar::windows_1900);
  367. xlnt_assert_equals(formatted, "20:5");
  368. }
  369. void test_long_minute()
  370. {
  371. auto time = xlnt::time(20, 5, 10);
  372. auto time_number = time.to_number();
  373. xlnt::number_format nf;
  374. nf.format_string("h:mm");
  375. auto formatted = nf.format(time_number, xlnt::calendar::windows_1900);
  376. xlnt_assert_equals(formatted, "20:05");
  377. }
  378. void test_short_second()
  379. {
  380. auto time = xlnt::time(20, 15, 1);
  381. auto time_number = time.to_number();
  382. xlnt::number_format nf;
  383. nf.format_string("h:m:s");
  384. auto formatted = nf.format(time_number, xlnt::calendar::windows_1900);
  385. xlnt_assert_equals(formatted, "20:15:1");
  386. }
  387. void test_long_second()
  388. {
  389. auto time = xlnt::time(20, 15, 1);
  390. auto time_number = time.to_number();
  391. xlnt::number_format nf;
  392. nf.format_string("h:m:ss");
  393. auto formatted = nf.format(time_number, xlnt::calendar::windows_1900);
  394. xlnt_assert_equals(formatted, "20:15:01");
  395. }
  396. void test_trailing_space()
  397. {
  398. auto time = xlnt::time(20, 15, 1);
  399. auto time_number = time.to_number();
  400. xlnt::number_format nf;
  401. nf.format_string("h:m:ss ");
  402. auto formatted = nf.format(time_number, xlnt::calendar::windows_1900);
  403. xlnt_assert_equals(formatted, "20:15:01 ");
  404. }
  405. void test_text_section_string()
  406. {
  407. xlnt::number_format nf;
  408. nf.format_string("General;General;General;[Green]\"a\"@\"b\"");
  409. auto formatted = nf.format("text");
  410. xlnt_assert_equals(formatted, "atextb");
  411. }
  412. void test_text_section_no_string()
  413. {
  414. xlnt::number_format nf;
  415. nf.format_string("General;General;General;[Green]m\"ab\"");
  416. auto formatted = nf.format("text");
  417. xlnt_assert_equals(formatted, "ab");
  418. }
  419. void test_text_section_no_text()
  420. {
  421. xlnt::number_format nf;
  422. nf.format_string("General;General;General;[Green]m");
  423. auto formatted = nf.format("text");
  424. xlnt_assert_equals(formatted, "text");
  425. }
  426. void test_conditional_format()
  427. {
  428. xlnt::number_format nf;
  429. nf.format_string("[>5]General\"first\";[>3]\"second\"General;\"third\"General");
  430. auto formatted = nf.format(6, xlnt::calendar::windows_1900);
  431. xlnt_assert_equals(formatted, "6first");
  432. formatted = nf.format(4, xlnt::calendar::windows_1900);
  433. xlnt_assert_equals(formatted, "second4");
  434. formatted = nf.format(5, xlnt::calendar::windows_1900);
  435. xlnt_assert_equals(formatted, "second5");
  436. formatted = nf.format(3, xlnt::calendar::windows_1900);
  437. xlnt_assert_equals(formatted, "third3");
  438. formatted = nf.format(2, xlnt::calendar::windows_1900);
  439. xlnt_assert_equals(formatted, "third2");
  440. nf.format_string("[>=5]\"first\"General;[>=3]\"second\"General;\"third\"General");
  441. formatted = nf.format(5, xlnt::calendar::windows_1900);
  442. xlnt_assert_equals(formatted, "first5");
  443. formatted = nf.format(6, xlnt::calendar::windows_1900);
  444. xlnt_assert_equals(formatted, "first6");
  445. formatted = nf.format(4, xlnt::calendar::windows_1900);
  446. xlnt_assert_equals(formatted, "second4");
  447. formatted = nf.format(3, xlnt::calendar::windows_1900);
  448. xlnt_assert_equals(formatted, "second3");
  449. formatted = nf.format(2, xlnt::calendar::windows_1900);
  450. xlnt_assert_equals(formatted, "third2");
  451. nf.format_string("[>=5]\"first\"General");
  452. formatted = nf.format(4, xlnt::calendar::windows_1900);
  453. xlnt_assert_equals(formatted, "###########");
  454. nf.format_string("[>=5]\"first\"General;[>=4]\"second\"General");
  455. formatted = nf.format(3, xlnt::calendar::windows_1900);
  456. xlnt_assert_equals(formatted, "###########");
  457. nf.format_string("[<1]\"first\"General;[<5]\"second\"General;\"third\"General");
  458. formatted = nf.format(0, xlnt::calendar::windows_1900);
  459. xlnt_assert_equals(formatted, "first0");
  460. formatted = nf.format(1, xlnt::calendar::windows_1900);
  461. xlnt_assert_equals(formatted, "second1");
  462. formatted = nf.format(5, xlnt::calendar::windows_1900);
  463. xlnt_assert_equals(formatted, "third5");
  464. formatted = nf.format(6, xlnt::calendar::windows_1900);
  465. xlnt_assert_equals(formatted, "third6");
  466. nf.format_string("[<=1]\"first\"General;[<=5]\"second\"General;\"third\"General");
  467. formatted = nf.format(-1000, xlnt::calendar::windows_1900);
  468. xlnt_assert_equals(formatted, "-first1000");
  469. formatted = nf.format(0, xlnt::calendar::windows_1900);
  470. xlnt_assert_equals(formatted, "first0");
  471. formatted = nf.format(1, xlnt::calendar::windows_1900);
  472. xlnt_assert_equals(formatted, "first1");
  473. formatted = nf.format(4, xlnt::calendar::windows_1900);
  474. xlnt_assert_equals(formatted, "second4");
  475. formatted = nf.format(5, xlnt::calendar::windows_1900);
  476. xlnt_assert_equals(formatted, "second5");
  477. formatted = nf.format(6, xlnt::calendar::windows_1900);
  478. xlnt_assert_equals(formatted, "third6");
  479. formatted = nf.format(1000, xlnt::calendar::windows_1900);
  480. xlnt_assert_equals(formatted, "third1000");
  481. nf.format_string("[=1]\"first\"General;[=2]\"second\"General;\"third\"General");
  482. formatted = nf.format(1, xlnt::calendar::windows_1900);
  483. xlnt_assert_equals(formatted, "first1");
  484. formatted = nf.format(2, xlnt::calendar::windows_1900);
  485. xlnt_assert_equals(formatted, "second2");
  486. formatted = nf.format(3, xlnt::calendar::windows_1900);
  487. xlnt_assert_equals(formatted, "third3");
  488. formatted = nf.format(0, xlnt::calendar::windows_1900);
  489. xlnt_assert_equals(formatted, "third0");
  490. nf.format_string("[<>1]\"first\"General;[<>2]\"second\"General");
  491. formatted = nf.format(2, xlnt::calendar::windows_1900);
  492. xlnt_assert_equals(formatted, "first2");
  493. formatted = nf.format(1, xlnt::calendar::windows_1900);
  494. xlnt_assert_equals(formatted, "second1");
  495. }
  496. void test_space()
  497. {
  498. xlnt::number_format nf;
  499. nf.format_string("_(General_)");
  500. auto formatted = nf.format(6, xlnt::calendar::windows_1900);
  501. xlnt_assert_equals(formatted, " 6 ");
  502. }
  503. void test_fill()
  504. {
  505. xlnt::number_format nf;
  506. nf.format_string("*-General");
  507. auto formatted = nf.format(6, xlnt::calendar::windows_1900);
  508. xlnt_assert_equals(formatted, "----------6");
  509. nf.format_string("General*-");
  510. formatted = nf.format(6, xlnt::calendar::windows_1900);
  511. xlnt_assert_equals(formatted, "6----------");
  512. nf.format_string("\\a*-\\b");
  513. formatted = nf.format(6, xlnt::calendar::windows_1900);
  514. xlnt_assert_equals(formatted, "a---------b");
  515. }
  516. void test_placeholders_zero()
  517. {
  518. xlnt::number_format nf;
  519. nf.format_string("00");
  520. auto formatted = nf.format(6, xlnt::calendar::windows_1900);
  521. xlnt_assert_equals(formatted, "06");
  522. nf.format_string("00");
  523. formatted = nf.format(63, xlnt::calendar::windows_1900);
  524. xlnt_assert_equals(formatted, "63");
  525. }
  526. void test_placeholders_space()
  527. {
  528. xlnt::number_format nf;
  529. nf.format_string("?0");
  530. auto formatted = nf.format(6, xlnt::calendar::windows_1900);
  531. xlnt_assert_equals(formatted, " 6");
  532. nf.format_string("?0");
  533. formatted = nf.format(63, xlnt::calendar::windows_1900);
  534. xlnt_assert_equals(formatted, "63");
  535. nf.format_string("?0");
  536. formatted = nf.format(637, xlnt::calendar::windows_1900);
  537. xlnt_assert_equals(formatted, "637");
  538. nf.format_string("0.?");
  539. formatted = nf.format(6, xlnt::calendar::windows_1900);
  540. xlnt_assert_equals(formatted, "6. ");
  541. nf.format_string("0.0?");
  542. formatted = nf.format(6.3, xlnt::calendar::windows_1900);
  543. xlnt_assert_equals(formatted, "6.3 ");
  544. formatted = nf.format(6.34, xlnt::calendar::windows_1900);
  545. xlnt_assert_equals(formatted, "6.34");
  546. }
  547. void test_scientific()
  548. {
  549. xlnt::number_format nf;
  550. nf.format_string("0.0E-0");
  551. auto formatted = nf.format(6.1, xlnt::calendar::windows_1900);
  552. xlnt_assert_equals(formatted, "6.1E0");
  553. }
  554. void test_locale_currency()
  555. {
  556. xlnt::number_format nf;
  557. nf.format_string("[$€-407]#,##0.00");
  558. auto formatted = nf.format(-45000.1, xlnt::calendar::windows_1900);
  559. xlnt_assert_equals(formatted, "-€45,000.10");
  560. nf.format_string("[$$-1009]#,##0.00");
  561. formatted = nf.format(-45000.1, xlnt::calendar::windows_1900);
  562. xlnt_assert_equals(formatted, "-$45,000.10");
  563. }
  564. void test_bad_country()
  565. {
  566. xlnt::number_format nf;
  567. nf.format_string("[$-]#,##0.00");
  568. xlnt_assert_throws(nf.format(1.2, xlnt::calendar::windows_1900), std::runtime_error);
  569. nf.format_string("[$-G]#,##0.00");
  570. xlnt_assert_throws(nf.format(1.2, xlnt::calendar::windows_1900), std::runtime_error);
  571. nf.format_string("[$-4002]#,##0.00");
  572. xlnt_assert_throws(nf.format(1.2, xlnt::calendar::windows_1900), std::runtime_error);
  573. nf.format_string("[-4001]#,##0.00");
  574. xlnt_assert_throws(nf.format(1.2, xlnt::calendar::windows_1900), std::runtime_error);
  575. }
  576. void test_duplicate_bracket_sections()
  577. {
  578. xlnt::number_format nf;
  579. nf.format_string("[Red][Green]#,##0.00");
  580. xlnt_assert_throws(nf.format(1.2, xlnt::calendar::windows_1900), std::runtime_error);
  581. nf.format_string("[$-403][$-4001]#,##0.00");
  582. xlnt_assert_throws(nf.format(1.2, xlnt::calendar::windows_1900), std::runtime_error);
  583. nf.format_string("[>3][>4]#,##0.00");
  584. xlnt_assert_throws(nf.format(1.2, xlnt::calendar::windows_1900), std::runtime_error);
  585. }
  586. void test_escaped_quote_string()
  587. {
  588. xlnt::number_format nf;
  589. nf.format_string("\"\\\"\"General");
  590. auto formatted = nf.format(6, xlnt::calendar::windows_1900);
  591. xlnt_assert_equals(formatted, "\"6");
  592. }
  593. void test_thousands_scale()
  594. {
  595. xlnt::number_format nf;
  596. nf.format_string("#,");
  597. auto formatted = nf.format(61234, xlnt::calendar::windows_1900);
  598. xlnt_assert_equals(formatted, "61");
  599. }
  600. void test_colors()
  601. {
  602. xlnt::number_format nf;
  603. nf.format_string("[Black]#");
  604. auto formatted = nf.format(6, xlnt::calendar::windows_1900);
  605. xlnt_assert_equals(formatted, "6");
  606. nf.format_string("[Black]#");
  607. formatted = nf.format(6, xlnt::calendar::windows_1900);
  608. xlnt_assert_equals(formatted, "6");
  609. nf.format_string("[Blue]#");
  610. formatted = nf.format(6, xlnt::calendar::windows_1900);
  611. xlnt_assert_equals(formatted, "6");
  612. nf.format_string("[Green]#");
  613. formatted = nf.format(6, xlnt::calendar::windows_1900);
  614. xlnt_assert_equals(formatted, "6");
  615. nf.format_string("[Red]#");
  616. formatted = nf.format(6, xlnt::calendar::windows_1900);
  617. xlnt_assert_equals(formatted, "6");
  618. nf.format_string("[Cyan]#");
  619. formatted = nf.format(6, xlnt::calendar::windows_1900);
  620. xlnt_assert_equals(formatted, "6");
  621. nf.format_string("[Magenta]#");
  622. formatted = nf.format(6, xlnt::calendar::windows_1900);
  623. xlnt_assert_equals(formatted, "6");
  624. nf.format_string("[Yellow]#");
  625. formatted = nf.format(6, xlnt::calendar::windows_1900);
  626. xlnt_assert_equals(formatted, "6");
  627. nf.format_string("[White]#");
  628. formatted = nf.format(6, xlnt::calendar::windows_1900);
  629. xlnt_assert_equals(formatted, "6");
  630. nf.format_string("[Color15]#");
  631. formatted = nf.format(6, xlnt::calendar::windows_1900);
  632. xlnt_assert_equals(formatted, "6");
  633. }
  634. void test_bad_format()
  635. {
  636. xlnt::number_format nf;
  637. nf.format_string("[=1]\"first\"General;[=2]\"second\"General;[=3]\"third\"General");
  638. xlnt_assert_throws(nf.format(1.2, xlnt::calendar::windows_1900), std::runtime_error);
  639. nf.format_string("\"first\"General;\"second\"General;\"third\"General;\"fourth\"General;\"fifth\"General");
  640. xlnt_assert_throws(nf.format(1.2, xlnt::calendar::windows_1900), std::runtime_error);
  641. nf.format_string("[");
  642. xlnt_assert_throws(nf.format(1.2, xlnt::calendar::windows_1900), std::runtime_error);
  643. nf.format_string("[]");
  644. xlnt_assert_throws(nf.format(1.2, xlnt::calendar::windows_1900), std::runtime_error);
  645. nf.format_string("[Redd]");
  646. xlnt_assert_throws(nf.format(1.2, xlnt::calendar::windows_1900), std::runtime_error);
  647. nf.format_string("[$1]#");
  648. xlnt_assert_throws(nf.format(1.2, xlnt::calendar::windows_1900), std::runtime_error);
  649. nf.format_string("Gee");
  650. xlnt_assert_throws(nf.format(1.2, xlnt::calendar::windows_1900), std::runtime_error);
  651. nf.format_string("!");
  652. xlnt_assert_throws(nf.format(1.2, xlnt::calendar::windows_1900), std::runtime_error);
  653. nf.format_string("A/");
  654. xlnt_assert_throws(nf.format(1.2, xlnt::calendar::windows_1900), std::runtime_error);
  655. }
  656. void format_and_test(const xlnt::number_format &nf, const std::array<std::string, 4> &expect)
  657. {
  658. double positive = 42503.1234;
  659. double negative = -1 * positive;
  660. double zero = 0;
  661. const std::string text = "text";
  662. xlnt::calendar calendar = xlnt::calendar::windows_1900;
  663. xlnt_assert_equals(nf.format(positive, calendar), expect[0]);
  664. xlnt_assert_equals(nf.format(negative, calendar), expect[1]);
  665. xlnt_assert_equals(nf.format(zero, calendar), expect[2]);
  666. xlnt_assert_equals(nf.format(text), expect[3]);
  667. }
  668. // General
  669. void test_builtin_format_0()
  670. {
  671. format_and_test(xlnt::number_format::general(), {{"42503.1234", "-42503.1234", "0", "text"}});
  672. }
  673. // 0
  674. void test_builtin_format_1()
  675. {
  676. format_and_test(xlnt::number_format::number(), {{"42503", "-42503", "0", "text"}});
  677. }
  678. // 0.00
  679. void test_builtin_format_2()
  680. {
  681. format_and_test(xlnt::number_format::number_00(), {{"42503.12", "-42503.12", "0.00", "text"}});
  682. }
  683. // #,##0
  684. void test_builtin_format_3()
  685. {
  686. format_and_test(xlnt::number_format::from_builtin_id(3), {{"42,503", "-42,503", "0", "text"}});
  687. }
  688. // #,##0.00
  689. void test_builtin_format_4()
  690. {
  691. format_and_test(xlnt::number_format::number_comma_separated1(), {{"42,503.12", "-42,503.12", "0.00", "text"}});
  692. }
  693. // 0%
  694. void test_builtin_format_9()
  695. {
  696. format_and_test(xlnt::number_format::percentage(), {{"4250312%", "-4250312%", "0%", "text"}});
  697. }
  698. // 0.00%
  699. void test_builtin_format_10()
  700. {
  701. format_and_test(xlnt::number_format::percentage_00(), {{"4250312.34%", "-4250312.34%", "0.00%", "text"}});
  702. }
  703. // 0.00E+00
  704. void test_builtin_format_11()
  705. {
  706. format_and_test(xlnt::number_format::from_builtin_id(11), {{"4.25E+04", "-4.25E+04", "0.00E+00", "text"}});
  707. }
  708. // # ?/?
  709. void test_builtin_format_12()
  710. {
  711. format_and_test(xlnt::number_format::from_builtin_id(12), {{"42503 1/8", "-42503 1/8", "0", "text"}});
  712. }
  713. // # ??/??
  714. void test_builtin_format_13()
  715. {
  716. format_and_test(xlnt::number_format::from_builtin_id(13), {{"42503 10/81", "-42503 10/81", "0", "text"}});
  717. }
  718. // mm-dd-yy
  719. void test_builtin_format_14()
  720. {
  721. format_and_test(xlnt::number_format::date_xlsx14(), {{"05-13-16", "###########", "01-00-00", "text"}});
  722. }
  723. // d-mmm-yy
  724. void test_builtin_format_15()
  725. {
  726. format_and_test(xlnt::number_format::date_xlsx15(), {{"13-May-16", "###########", "0-Jan-00", "text"}});
  727. }
  728. // d-mmm
  729. void test_builtin_format_16()
  730. {
  731. format_and_test(xlnt::number_format::date_xlsx16(), {{"13-May", "###########", "0-Jan", "text"}});
  732. }
  733. // mmm-yy
  734. void test_builtin_format_17()
  735. {
  736. format_and_test(xlnt::number_format::date_xlsx17(), {{"May-16", "###########", "Jan-00", "text"}});
  737. }
  738. // h:mm AM/PM
  739. void test_builtin_format_18()
  740. {
  741. format_and_test(xlnt::number_format::date_time1(), {{"2:57 AM", "###########", "12:00 AM", "text"}});
  742. }
  743. // h:mm:ss AM/PM
  744. void test_builtin_format_19()
  745. {
  746. format_and_test(xlnt::number_format::date_time2(), {{"2:57:42 AM", "###########", "12:00:00 AM", "text"}});
  747. }
  748. // h:mm
  749. void test_builtin_format_20()
  750. {
  751. format_and_test(xlnt::number_format::date_time3(), {{"2:57", "###########", "0:00", "text"}});
  752. }
  753. // h:mm:ss
  754. void test_builtin_format_21()
  755. {
  756. format_and_test(xlnt::number_format::date_time4(), {{"2:57:42", "###########", "0:00:00", "text"}});
  757. }
  758. // m/d/yy h:mm
  759. void test_builtin_format_22()
  760. {
  761. format_and_test(xlnt::number_format::date_xlsx22(), {{"5/13/16 2:57", "###########", "1/0/00 0:00", "text"}});
  762. }
  763. // #,##0 ;(#,##0)
  764. void test_builtin_format_37()
  765. {
  766. format_and_test(xlnt::number_format::from_builtin_id(37), {{"42,503 ", "(42,503)", "0 ", "text"}});
  767. }
  768. // #,##0 ;[Red](#,##0)
  769. void test_builtin_format_38()
  770. {
  771. format_and_test(xlnt::number_format::from_builtin_id(38), {{"42,503 ", "(42,503)", "0 ", "text"}});
  772. }
  773. // #,##0.00;(#,##0.00)
  774. void test_builtin_format_39()
  775. {
  776. format_and_test(xlnt::number_format::from_builtin_id(39), {{"42,503.12", "(42,503.12)", "0.00", "text"}});
  777. }
  778. // #,##0.00;[Red](#,##0.00)
  779. void test_builtin_format_40()
  780. {
  781. format_and_test(xlnt::number_format::from_builtin_id(40), {{"42,503.12", "(42,503.12)", "0.00", "text"}});
  782. }
  783. // mm:ss
  784. void test_builtin_format_45()
  785. {
  786. format_and_test(xlnt::number_format::date_time5(), {{"57:42", "###########", "00:00", "text"}});
  787. }
  788. // [h]:mm:ss
  789. void test_builtin_format_46()
  790. {
  791. format_and_test(xlnt::number_format::from_builtin_id(46), {{"1020074:57:42", "###########", "0:00:00", "text"}});
  792. }
  793. // mmss.0
  794. void test_builtin_format_47()
  795. {
  796. format_and_test(xlnt::number_format::from_builtin_id(47), {{"5741.8", "###########", "0000.0", "text"}});
  797. }
  798. // ##0.0E+0
  799. void test_builtin_format_48()
  800. {
  801. format_and_test(xlnt::number_format::from_builtin_id(48), {{"42.5E+3", "-42.5E+3", "000.0E+0", "text"}});
  802. }
  803. // @
  804. void test_builtin_format_49()
  805. {
  806. format_and_test(xlnt::number_format::text(), {{"42503.1234", "-42503.1234", "0", "text"}});
  807. }
  808. // yy-mm-dd
  809. void test_builtin_format_date_yyyymmdd()
  810. {
  811. format_and_test(xlnt::number_format::date_yymmdd(), {{"16-05-13", "###########", "00-01-00", "text"}});
  812. }
  813. // d/m/y
  814. void test_builtin_format_date_dmyslash()
  815. {
  816. format_and_test(xlnt::number_format::date_dmyslash(), {{"13/5/16", "###########", "0/1/00", "text"}});
  817. }
  818. // d-m-y
  819. void test_builtin_format_date_dmyminus()
  820. {
  821. format_and_test(xlnt::number_format::date_dmyminus(), {{"13-5-16", "###########", "0-1-00", "text"}});
  822. }
  823. // d-m
  824. void test_builtin_format_date_dmminus()
  825. {
  826. format_and_test(xlnt::number_format::date_dmminus(), {{"13-5", "###########", "0-1", "text"}});
  827. }
  828. // m-yy
  829. void test_builtin_format_date_myminus()
  830. {
  831. format_and_test(xlnt::number_format::date_myminus(), {{"5-16", "###########", "1-00", "text"}});
  832. }
  833. };
  834. static number_format_test_suite x;