Repository Public
1import Foundation2import Testing3import SheetCore4@testable import SheetFormat56func fmt(_ code: String, _ v: Double, locale: FormatLocale = .enUS, date1904: Bool = false) -> String {7 NumberFormat.cached(code).render(.number(v), locale: locale, date1904: date1904).text8}910@Suite struct RoundingTests {11 @Test func decimalCorrect() {12 #expect(fmt("0.00", 0.285) == "0.29")13 #expect(fmt("0.00", 1.005) == "1.01")14 #expect(fmt("0.00", 2.675) == "2.68")15 #expect(fmt("0.00", -1.005) == "-1.01")16 #expect(fmt("0.0", 0.25) == "0.3")17 #expect(fmt("0.0", 0.35) == "0.4")18 #expect(fmt("0", 0.5) == "1")19 #expect(fmt("0", 1.5) == "2")20 #expect(fmt("0", 2.5) == "3")21 #expect(fmt("0", -2.5) == "-3")22 #expect(fmt("0.000", 1.0005) == "1.001")23 #expect(fmt("0.00", 1.115) == "1.12")24 #expect(fmt("0.00", 8.325) == "8.33")25 #expect(fmt("0.00", 1234.565) == "1234.57")26 #expect(fmt("0%", 0.285) == "29%")27 #expect(fmt("0.0%", 0.0045) == "0.5%")28 #expect(fmt("0.00", 0.1 + 0.2) == "0.30")29 #expect(fmt("0.0000000000000000", 0.1 + 0.2) == "0.3000000000000000")30 #expect(fmt("0.00", 4.35 * 100) == "435.00")31 #expect(fmt("0", 0.49999999999999994) == "1") // 15 significant digits: 0.50000000000000032 }3334 @Test func fifteenSignificantDigits() {35 #expect(fmt("0.000000000", 123456789.123456789) == "123456789.123457000")36 #expect(fmt("0", 1.23456789012345678e20) == "123456789012346000000")37 #expect(fmt("#,##0", 1e15) == "1,000,000,000,000,000")38 #expect(fmt("0.00000000000000000000", 0.123456789012345678) == "0.12345678901234600000")39 #expect(fmt("0", 9007199254740992) == "9007199254740990")40 #expect(fmt("0.00", 1e-20) == "0.00")41 #expect(fmt("0.00", 1e300).hasPrefix("1000000"))42 }4344 @Test func negativeZeroAndSigns() {45 #expect(fmt("0.00", -0.001) == "-0.00") // Excel keeps the sign46 #expect(fmt("0.00;(0.00)", -0.001) == "(0.00)")47 #expect(fmt("0.00;(0.00)", -5) == "(5.00)")48 #expect(fmt("0.00;-0.00", -5) == "-5.00")49 #expect(fmt("$#,##0.00", -1234.5) == "-$1,234.50")50 #expect(fmt("0.00;;", -5) == "")51 #expect(fmt("0.00;;", 0) == "")52 #expect(fmt("0;-0;\"zero\"", 0) == "zero")53 #expect(fmt("\"abc\"", -5) == "-abc")54 #expect(fmt("+0;-0;0", 5) == "+5")55 #expect(fmt(";;;", 5) == "")56 }57}5859@Suite struct PlaceholderTests {60 @Test func digits() {61 #expect(fmt("#.##", 5) == "5.")62 #expect(fmt("#.##", 0) == ".")63 #expect(fmt("#.00", 0.5) == ".50")64 #expect(fmt(".00", 1.5) == "1.50")65 #expect(fmt("0.0#", 1.5) == "1.5")66 #expect(fmt("0.0#", 1.55) == "1.55")67 #expect(fmt("?.??", 1.5) == "1.5 ")68 #expect(fmt("??0.0", 1.5) == " 1.5")69 #expect(fmt("00000", 42) == "00042")70 #expect(fmt("000-00-0000", 123456789) == "123-45-6789")71 #expect(fmt("(###) ###-####", 5551234567) == "(555) 123-4567")72 #expect(fmt("[<=9999999]###-####;(###) ###-####", 5551234) == "555-1234")73 #expect(fmt("[<=9999999]###-####;(###) ###-####", 4185551234) == "(418) 555-1234")74 #expect(fmt("0,000", 5) == "0,005")75 #expect(fmt("#,##0", 1234567) == "1,234,567")76 #expect(fmt("#,###", 0) == "")77 #expect(fmt("#,##0.00", 1234567.891) == "1,234,567.89")78 #expect(fmt("0", 12345678901234) == "12345678901234")79 }8081 @Test func scalingAndPercent() {82 #expect(fmt("#,##0,", 1234567) == "1,235")83 #expect(fmt("0.0,,\"M\"", 1234567) == "1.2M")84 #expect(fmt("#,##0,,", 1_500_000_000) == "1,500")85 #expect(fmt("0.00,", 1500) == "1.50")86 #expect(fmt("0%", 0.5) == "50%")87 #expect(fmt("0.00%", 0.12345) == "12.35%")88 #expect(fmt("0%%", 0.5) == "5000%%")89 #expect(fmt("[<1000]0;[<1000000]0.0,\"K\";0.0,,\"M\"", 999) == "999")90 #expect(fmt("[<1000]0;[<1000000]0.0,\"K\";0.0,,\"M\"", 25000) == "25.0K")91 #expect(fmt("[<1000]0;[<1000000]0.0,\"K\";0.0,,\"M\"", 2_500_000) == "2.5M")92 }9394 @Test func literalsAndEscapes() {95 #expect(fmt("0.00\" kg\"", 2.5) == "2.50 kg")96 #expect(fmt("\\$0.00", 2.5) == "$2.50")97 #expect(fmt("0_)", 5) == "5 ")98 #expect(fmt("[$\u{20AC}-40C]#,##0.00", 1234.5) == "€1,234.50")99 #expect(fmt("#,##0.00 [$€-1]", 1234.5) == "1,234.50 €")100 #expect(fmt("[$-409]0.0", 2) == "2.0")101 #expect(fmt("[$USD] 0", 2) == "USD 2")102 #expect(fmt("\"a;b\"0", 2) == "a;b2")103 #expect(fmt("0\\;0", 12) == "1;2")104 #expect(fmt("0 \"x\"\"y\"", 2) == "2 xy")105 #expect(fmt("0!", 3) == "3!")106 #expect(fmt("0-0", 34) == "3-4")107 }108109 @Test func colors() {110 let f = NumberFormat(code: "[Blue]0.0;[Red]-0.0;[Color 10]0;[Magenta]@")111 #expect(f.render(.number(1)).color == RGBA(hex: 0x0000FF))112 #expect(f.render(.number(-1)).color == RGBA(hex: 0xFF0000))113 #expect(f.render(.number(-1)).text == "-1.0")114 #expect(f.render(.number(0)).color == RGBA(hex: 0x008000))115 #expect(f.render(.text("t")).color == RGBA(hex: 0xFF00FF))116 #expect(NumberFormat(code: "0").render(.number(1)).color == nil)117 #expect(NumberFormat(code: "[color3]0").render(.number(1)).color == RGBA(hex: 0xFF0000))118 #expect(NumberFormat(code: "#,##0.00_);[Red](#,##0.00)").render(.number(-5)) == RenderedCell(text: "(5.00)", color: RGBA(hex: 0xFF0000)))119 }120121 @Test func accountingFillAndSpacing() {122 let acc = NumberFormat(code: BuiltinFormats.code(forID: 44)!)123 let pos = acc.render(.number(1234.5))124 #expect(pos.text == " $1,234.50 ")125 #expect(pos.fillIndex == 2)126 #expect(pos.fillCharacter == " ")127 #expect(acc.render(.number(-1234.5)).text == " $(1,234.50)")128 #expect(acc.render(.number(0)).text == " $- ")129 #expect(acc.render(.text("abc")).text == " abc ")130 let stars = NumberFormat(code: "*-0").render(.number(7))131 #expect(stars.text == "7" && stars.fillIndex == 0 && stars.fillCharacter == "-")132 #expect(NumberFormat(code: "0").render(.number(7)).fillIndex == nil)133 }134}135136@Suite struct ScientificAndFractionTests {137 @Test func scientific() {138 #expect(fmt("0.00E+00", 12345) == "1.23E+04")139 #expect(fmt("0.00E+00", 0) == "0.00E+00")140 #expect(fmt("0.00E+00", 0.000123) == "1.23E-04")141 #expect(fmt("0.00E+00", 9.999) == "1.00E+01")142 #expect(fmt("0.00E+00", -12345) == "-1.23E+04")143 #expect(fmt("0.0E-0", 12345) == "1.2E4")144 #expect(fmt("0.0E-0", 0.00012345) == "1.2E-4")145 #expect(fmt("##0.0E+0", 123456) == "123.5E+3")146 #expect(fmt("##0.0E+0", 12345) == "12.3E+3")147 #expect(fmt("##0.0E+0", 0.00012345) == "123.5E-6")148 #expect(fmt("0E+0", 999.5) == "1E+3")149 #expect(fmt("0.00e+00", 12345) == "1.23e+04")150 #expect(fmt("0.000E+000", 1.5e120) == "1.500E+120")151 #expect(fmt("0.00E+00", 1e-300) == "1.00E-300")152 }153154 @Test func fractions() {155 #expect(fmt("# ?/?", 5.25) == "5 1/4")156 #expect(fmt("# ?/?", 0.5) == " 1/2")157 #expect(fmt("# ?/?", 5) == "5 ")158 #expect(fmt("# ?/?", 0) == "0 ")159 #expect(fmt("# ??/??", 3.14159) == "3 1/7 ") // Excel: continued-fraction convergent, not the closest 14/99160 #expect(fmt("# ???/???", Double.pi) == "3 16/113")161 #expect(fmt("# ?/?", 10.0625) == "10 ")162 #expect(fmt("# ??/??", 0.3333333333) == " 1/3 ")163 #expect(fmt("# ?/8", 0.5) == " 4/8")164 #expect(fmt("# ?/8", 2.99) == "3 ")165 #expect(fmt("# ??/16", 1.3125) == "1 5/16")166 #expect(fmt("?/?", 2.5) == "5/2")167 #expect(fmt("# ?/?", -5.25) == "-5 1/4")168 #expect(fmt("# ?/?", 0.999) == "1 ")169 #expect(fmt("# ???/???", 0.123) == " 23/187")170 }171}172173@Suite struct DateTimeTests {174 @Test func leapBugAndEpoch() {175 #expect(fmt("yyyy-mm-dd", 60) == "1900-02-29")176 #expect(fmt("yyyy-mm-dd", 59) == "1900-02-28")177 #expect(fmt("yyyy-mm-dd", 61) == "1900-03-01")178 #expect(fmt("yyyy-mm-dd", 1) == "1900-01-01")179 #expect(fmt("yyyy-mm-dd", 0) == "1900-01-00")180 #expect(fmt("dddd", 1) == "Sunday")181 #expect(fmt("dddd", 60) == "Wednesday")182 #expect(fmt("yyyy-mm-dd", 45000) == "2023-03-15")183 #expect(fmt("yyyy-mm-dd", 2958465) == "9999-12-31")184 #expect(fmt("yyyy-mm-dd", 2958466) == "########")185 #expect(fmt("yyyy-mm-dd", -1) == "########")186 #expect(fmt("h:mm", -0.5) == "########")187 #expect(fmt("yyyy-mm-dd", 0, date1904: true) == "1904-01-01")188 #expect(fmt("yyyy-mm-dd", 43538, date1904: true) == "2023-03-15")189 #expect(fmt("yyyy-mm-dd", -1, date1904: true) == "-1904-01-02")190 #expect(fmt("dddd", 0, date1904: true) == "Friday")191 }192193 @Test func tokens() {194 let v = 45000.5630787037 // 2023-03-15 13:30:50195 #expect(fmt("yyyy-mm-dd hh:mm:ss", v) == "2023-03-15 13:30:50")196 #expect(fmt("yy/m/d", v) == "23/3/15")197 #expect(fmt("yyy", v) == "2023")198 #expect(fmt("mmm", v) == "Mar")199 #expect(fmt("mmmm", v) == "March")200 #expect(fmt("mmmmm", v) == "M")201 #expect(fmt("ddd", v) == "Wed")202 #expect(fmt("dddd", v) == "Wednesday")203 #expect(fmt("h:mm AM/PM", v) == "1:30 PM")204 #expect(fmt("hh:mm:ss am/pm", v) == "01:30:50 PM")205 #expect(fmt("h:mm A/P", v) == "1:30 P")206 #expect(fmt("h:mm a/p", v) == "1:30 p")207 #expect(fmt("h AM/PM", 0) == "12 AM")208 #expect(fmt("h AM/PM", 0.5) == "12 PM")209 #expect(fmt("YYYY-MM-DD HH:MM:SS", v) == "2023-03-15 13:30:50")210 #expect(fmt("[$-409]mmmm d, yyyy", v) == "March 15, 2023")211 #expect(fmt("[$-F800]dddd, mmmm dd, yyyy", v) == "Wednesday, March 15, 2023")212 #expect(fmt("[Red]yyyy", v) == "2023")213 #expect(fmt("d\"th of\" mmmm", v) == "15th of March")214 }215216 @Test func minuteDisambiguation() {217 let v = 45000.5630787037218 #expect(fmt("m", v) == "3")219 #expect(fmt("mm", v) == "03")220 #expect(fmt("h:mm", v) == "13:30")221 #expect(fmt("mm:ss", v) == "30:50")222 #expect(fmt("m:ss", v) == "30:50")223 #expect(fmt("hh mm", v) == "13 30")224 #expect(fmt("m/d/yyyy h:mm", v) == "3/15/2023 13:30")225 #expect(fmt("mm/dd hh:mm", v) == "03/15 13:30")226 #expect(fmt("yyyy-mm-dd\"T\"hh:mm", v) == "2023-03-15T13:30")227 #expect(fmt("[h]:mm", v) == "1080013:30")228 #expect(fmt("mm-yyyy ss", v) == "03-2023 50")229 #expect(fmt("d mm ss", v) == "15 30 50")230 }231232 @Test func roundingToSeconds() {233 #expect(fmt("hh:mm:ss", 0.999994) == "23:59:59")234 #expect(fmt("hh:mm:ss", 0.9999999) == "00:00:00")235 #expect(fmt("yyyy-mm-dd hh:mm:ss", 44926.9999999) == "2023-01-01 00:00:00")236 #expect(fmt("yyyy-mm-dd", 44926.9999999) == "2023-01-01")237 #expect(fmt("hh:mm:ss.000", 0.5 + 0.0015 / 86400) == "12:00:00.002")238 #expect(fmt("hh:mm:ss.00", 0.5 + 0.126 / 86400) == "12:00:00.13")239 #expect(fmt("mm:ss.0", 0.5 + 1.26 / 86400) == "00:01.3")240 #expect(fmt("hh:mm:ss", 12.0 / 24 + 34.0 / 1440 + 56.0 / 86400) == "12:34:56")241 #expect(fmt("h:mm", 0.5) == "12:00")242 }243244 @Test func elapsed() {245 #expect(fmt("[h]:mm", 1.5) == "36:00")246 #expect(fmt("[h]:mm:ss", 2.75) == "66:00:00")247 #expect(fmt("[hh]:mm", 0.125) == "03:00")248 #expect(fmt("[m]", 1.5) == "2160")249 #expect(fmt("[mm]:ss", 0.001) == "01:26")250 #expect(fmt("[s]", 1.5) == "129600")251 #expect(fmt("[s].00", 0.5 / 86400) == "0.50")252 #expect(fmt("[h]:mm", -1) == "########")253 }254255 @Test func locales() {256 let v = 45000.5630787037257 #expect(fmt("dddd d mmmm yyyy", v, locale: .frCA) == "mercredi 15 mars 2023")258 #expect(fmt("ddd d mmm", v, locale: .frCA) == "mer. 15 mars")259 #expect(fmt("#,##0.00", 1234.5, locale: .frCA) == "1\u{00A0}234,50")260 #expect(fmt("0.0%", 0.125, locale: .frCA) == "12,5%")261 #expect(fmt("0.00E+00", 12345, locale: .frCA) == "1,23E+04")262 #expect(fmt("ss.00", 0.5 / 86400, locale: .frCA) == "00,50")263 #expect(NumberFormat.general(1234.5, maxChars: nil, locale: .frCA) == "1234,5")264 let de = FormatLocale(locale: Locale(identifier: "de_DE"))265 #expect(de.decimalSeparator == "," && de.groupingSeparator == ".")266 #expect(de.monthNames[2] == "März" && de.weekdayNames[0] == "Sonntag")267 #expect(fmt("#,##0.00", 1234.5, locale: de) == "1.234,50")268 #expect(FormatLocale(locale: Locale(identifier: "en_US")).monthNames == FormatLocale.enUS.monthNames)269 }270}271272@Suite struct GeneralTests {273 func g(_ n: Double, _ w: Int? = nil) -> String { NumberFormat.general(n, maxChars: w, locale: .enUS) }274275 @Test func unconstrained() {276 #expect(g(0) == "0")277 #expect(g(1) == "1")278 #expect(g(-1.5) == "-1.5")279 #expect(g(0.1 + 0.2) == "0.3")280 #expect(g(1.0 / 3) == "0.333333333333333")281 #expect(g(123456789012345) == "123456789012345")282 #expect(g(1e15) == "1E+15")283 #expect(g(1.5e20) == "1.5E+20")284 #expect(g(1e100) == "1E+100")285 #expect(g(1e-10) == "1E-10")286 #expect(g(0.000000001) == "0.000000001")287 #expect(g(-2.5e-12) == "-2.5E-12")288 #expect(g(100) == "100")289 #expect(g(1234.5678) == "1234.5678")290 #expect(g(.infinity) == "#NUM!")291 }292293 @Test func constrainedLikeAnExcelCell() {294 #expect(g(1.0 / 3, 11) == "0.333333333")295 #expect(g(-1.0 / 3, 12) == "-0.333333333")296 #expect(g(2.0 / 3, 11) == "0.666666667")297 #expect(g(123456789012, 11) == "1.23457E+11")298 #expect(g(12345678901, 11) == "12345678901")299 #expect(g(1234567.891, 11) == "1234567.891")300 #expect(g(12345678.912, 11) == "12345678.91")301 #expect(g(1234567890.6, 11) == "1234567891")302 #expect(g(99999999999.9, 11) == "1E+11")303 #expect(g(0.0000123456789, 11) == "1.23457E-05")304 #expect(g(0.000123456789, 11) == "0.000123457")305 #expect(g(0.000000001, 11) == "0.000000001")306 #expect(g(1e-10, 11) == "1E-10")307 #expect(g(123456, 3) == "###")308 #expect(g(123456, 5) == "1E+05")309 #expect(g(1234.5678, 6) == "1234.6")310 #expect(g(1234.5678, 5) == "1235")311 #expect(g(1234.5678, 4) == "1235")312 #expect(g(0.5, 2) == "1")313 #expect(g(3.14159, 100) == "3.14159")314 #expect(g(1.0 / 3, 11) == NumberFormat.cached("General").render(.number(1.0 / 3), generalMaxChars: 11).text)315 }316317 @Test func generalSections() {318 #expect(fmt("General", 1234.5) == "1234.5")319 #expect(fmt("General", -1234.5) == "-1234.5")320 #expect(fmt("\"x \"General", 2.5) == "x 2.5")321 #expect(fmt("General;(General)", -2.5) == "(2.5)")322 #expect(fmt("[>100]0.00", 50) == "50")323 #expect(fmt("[>100]0.00", 500) == "500.00")324 #expect(fmt("[>100]0;[<0]0.0", 50) == "########")325 #expect(fmt("", 2.5) == "2.5")326 #expect(fmt("@", 2.5) == "2.5")327 #expect(fmt("general", 7) == "7")328 }329}330331@Suite struct ValueKindTests {332 @Test func nonNumbers() {333 let f = NumberFormat.cached("0.00")334 #expect(f.render(.text("abc")).text == "abc")335 #expect(f.render(.bool(true)).text == "TRUE")336 #expect(f.render(.bool(false)).text == "FALSE")337 #expect(f.render(.error(.div0)).text == "#DIV/0!")338 #expect(f.render(.blank).text == "")339 #expect(f.render(.array(ArrayValue(row: [.number(1.5), .number(2)]))).text == "1.50")340 #expect(f.render(.number(.nan)).text == "#NUM!")341 #expect(NumberFormat.cached("0;0;0;\"[\"@\"]\"").render(.text("abc")).text == "[abc]")342 #expect(NumberFormat.cached("@\" !\"").render(.text("abc")).text == "abc !")343 #expect(NumberFormat.cached("@@").render(.text("ab")).text == "abab")344 #expect(NumberFormat.cached("0;@").render(.text("ab")).text == "ab")345 #expect(NumberFormat.cached("0;\"t:\"@").render(.text("ab")).text == "t:ab")346 #expect(NumberFormat.cached("0;\"t:\"@").render(.number(-3)).text == "-3")347 #expect(NumberFormat.cached("yyyy-mm-dd").render(.text("abc")).text == "abc")348 }349350 @Test func properties() {351 #expect(NumberFormat.cached("yyyy-mm-dd").isDateTime)352 #expect(NumberFormat.cached("[h]:mm").isDateTime)353 #expect(NumberFormat.cached("h:mm AM/PM").isDateTime)354 #expect(!NumberFormat.cached("0.00").isDateTime)355 #expect(!NumberFormat.cached("General").isDateTime)356 #expect(!NumberFormat.cached("0 \"days\"").isDateTime)357 #expect(!NumberFormat.cached("[Red]0").isDateTime)358 #expect(!NumberFormat.cached("[$-409]0.0").isDateTime)359 #expect(NumberFormat.cached("@").isText)360 #expect(!NumberFormat.cached("0").isText)361 #expect(NumberFormat.cached("0.0%").isPercent)362 #expect(!NumberFormat.cached("0.0\"%\"").isPercent)363 #expect(NumberFormat.cached("#,##0.000").decimalPlaces == 3)364 #expect(NumberFormat.cached("0").decimalPlaces == 0)365 #expect(NumberFormat.cached("0.00E+00").decimalPlaces == 2)366 #expect(NumberFormat.cached("General").decimalPlaces == nil)367 #expect(NumberFormat.cached("yyyy").decimalPlaces == nil)368 #expect(NumberFormat.cached("General").isGeneral)369 #expect(NumberFormat.cached("0.00") === NumberFormat.cached("0.00"))370 #expect(NumberFormat.cached("0.00").code == "0.00")371 }372373 @Test func cacheIsThreadSafe() async {374 await withTaskGroup(of: Bool.self) { group in375 for i in 0..<16 {376 group.addTask {377 var ok = true378 for k in 0..<2000 {379 let code = "0." + String(repeating: "0", count: (i + k) % 9 + 1)380 ok = ok && NumberFormat.cached(code).render(.number(1)).text.count == (i + k) % 9 + 3381 }382 return ok383 }384 }385 for await ok in group { #expect(ok) }386 }387 }388}389390@Suite struct BuiltinAndEditingTests {391 /// The full id → code list. SheetIO reads and writes XLSX through this table, so a change here is a392 /// file-format change (SheetIO pins the same list in `BuiltinNumberFormatTests`).393 @Test func builtinTableIsPinned() {394 let pinned: [(Int, String)] = [395 (0, "General"), (1, "0"), (2, "0.00"), (3, "#,##0"), (4, "#,##0.00"),396 (5, "$#,##0_);($#,##0)"), (6, "$#,##0_);[Red]($#,##0)"),397 (7, "$#,##0.00_);($#,##0.00)"), (8, "$#,##0.00_);[Red]($#,##0.00)"),398 (9, "0%"), (10, "0.00%"), (11, "0.00E+00"), (12, "# ?/?"), (13, "# ??/??"),399 (14, "m/d/yyyy"), (15, "d-mmm-yy"), (16, "d-mmm"), (17, "mmm-yy"),400 (18, "h:mm AM/PM"), (19, "h:mm:ss AM/PM"), (20, "h:mm"), (21, "h:mm:ss"), (22, "m/d/yyyy h:mm"),401 (37, "#,##0_);(#,##0)"), (38, "#,##0_);[Red](#,##0)"), (39, "#,##0.00_);(#,##0.00)"), (40, "#,##0.00_);[Red](#,##0.00)"),402 (41, "_(* #,##0_);_(* \\(#,##0\\);_(* \"-\"_);_(@_)"),403 (42, "_($* #,##0_);_($* \\(#,##0\\);_($* \"-\"_);_(@_)"),404 (43, "_(* #,##0.00_);_(* \\(#,##0.00\\);_(* \"-\"??_);_(@_)"),405 (44, "_($* #,##0.00_);_($* \\(#,##0.00\\);_($* \"-\"??_);_(@_)"),406 (45, "mm:ss"), (46, "[h]:mm:ss"), (47, "mm:ss.0"), (48, "##0.0E+0"), (49, "@"),407 ]408 #expect(BuiltinFormats.definedIDs == pinned.map(\.0))409 #expect(BuiltinFormats.definedIDs == Array(0...22) + Array(37...49))410 #expect(BuiltinFormats.codes.count == pinned.count)411 for (id, code) in pinned {412 #expect(BuiltinFormats.code(forID: id) == code, "id \(id)")413 #expect(BuiltinFormats.canonicalID(forCode: code) == id && BuiltinFormats.id(forCode: code) == id, "code \(code)")414 }415 for id in Array(23...36) + [-1, 50, 163, 164] { #expect(BuiltinFormats.code(forID: id) == nil, "id \(id)") }416 // ECMA-376 spellings: recognised as aliases, never canonical417 #expect(BuiltinFormats.id(forCode: "mm-dd-yy") == 14 && BuiltinFormats.id(forCode: "m/d/yy h:mm") == 22)418 #expect(BuiltinFormats.canonicalID(forCode: "mm-dd-yy") == nil && BuiltinFormats.canonicalID(forCode: "m/d/yy h:mm") == nil && BuiltinFormats.canonicalID(forCode: "mmss.0") == nil)419 // what Excel and LibreOffice (en-US) display for ids 14, 22 and 47420 #expect(fmt(BuiltinFormats.code(forID: 14)!, 46347) == "11/21/2026")421 #expect(fmt(BuiltinFormats.code(forID: 22)!, 46347.5625) == "11/21/2026 13:30")422 #expect(fmt(BuiltinFormats.code(forID: 47)!, 0.5 + 83.26 / 86400) == "01:23.3")423 }424425 @Test func builtinTable() {426 #expect(BuiltinFormats.code(forID: 0) == "General")427 #expect(BuiltinFormats.code(forID: 14) == "m/d/yyyy")428 #expect(BuiltinFormats.code(forID: 49) == "@")429 #expect(BuiltinFormats.code(forID: 23) == nil)430 #expect(BuiltinFormats.code(forID: 164) == nil)431 for id in 0...49 {432 guard let code = BuiltinFormats.code(forID: id) else { continue }433 #expect(BuiltinFormats.id(forCode: code) == id)434 _ = NumberFormat(code: code).render(.number(1234.5))435 }436 #expect(BuiltinFormats.id(forCode: "mm-dd-yy") == 14)437 #expect(BuiltinFormats.id(forCode: "0.0000") == nil)438 #expect(fmt(BuiltinFormats.code(forID: 4)!, 1234.5) == "1,234.50")439 #expect(fmt(BuiltinFormats.code(forID: 7)!, -1234.5) == "($1,234.50)")440 #expect(fmt(BuiltinFormats.code(forID: 9)!, 0.5) == "50%")441 #expect(fmt(BuiltinFormats.code(forID: 11)!, 1234.5) == "1.23E+03")442 #expect(fmt(BuiltinFormats.code(forID: 12)!, 1234.5) == "1234 1/2")443 #expect(fmt(BuiltinFormats.code(forID: 15)!, 45000) == "15-Mar-23")444 #expect(fmt(BuiltinFormats.code(forID: 18)!, 0.75) == "6:00 PM")445 #expect(fmt(BuiltinFormats.code(forID: 22)!, 45000.75) == "3/15/2023 18:00")446 #expect(fmt(BuiltinFormats.code(forID: 37)!, -1234.5) == "(1,235)")447 #expect(fmt(BuiltinFormats.code(forID: 45)!, 0.0211) == "30:23")448 #expect(fmt(BuiltinFormats.code(forID: 46)!, 1.5) == "36:00:00")449 #expect(fmt(BuiltinFormats.code(forID: 47)!, 0.5 + 1.26 / 86400) == "00:01.3")450 #expect(fmt(BuiltinFormats.code(forID: 48)!, 1234.5) == "1.2E+3")451 #expect(BuiltinFormats.catalog.count > 40)452 for entry in BuiltinFormats.catalog {453 #expect(!NumberFormat(code: entry.code).render(.number(1234.5)).text.contains("#"), "\(entry.code)")454 }455 #expect(Set(BuiltinFormats.catalog.map(\.category)).isSuperset(of: ["Number", "Currency", "Accounting", "Percent", "Scientific", "Fraction", "Date", "Time", "Text"]))456 }457458 @Test func editing() {459 #expect(FormatEditing.increaseDecimals("General") == "0.0")460 #expect(FormatEditing.increaseDecimals("0") == "0.0")461 #expect(FormatEditing.increaseDecimals("0.0") == "0.00")462 #expect(FormatEditing.increaseDecimals("#,##0") == "#,##0.0")463 #expect(FormatEditing.increaseDecimals("0%") == "0.0%")464 #expect(FormatEditing.increaseDecimals("0.00E+00") == "0.000E+00")465 #expect(FormatEditing.increaseDecimals("$#,##0_);($#,##0)") == "$#,##0.0_);($#,##0.0)")466 #expect(FormatEditing.increaseDecimals("#,##0.00_);[Red](#,##0.00)") == "#,##0.000_);[Red](#,##0.000)")467 #expect(FormatEditing.increaseDecimals("0\" kg\"") == "0.0\" kg\"")468 #expect(FormatEditing.increaseDecimals("yyyy-mm-dd") == "yyyy-mm-dd")469 #expect(FormatEditing.increaseDecimals("@") == "@")470 #expect(FormatEditing.increaseDecimals("# ?/?") == "# ?/?")471 #expect(FormatEditing.increaseDecimals(BuiltinFormats.code(forID: 43)!) == "_(* #,##0.000_);_(* \\(#,##0.000\\);_(* \"-\"???_);_(@_)")472 #expect(FormatEditing.decreaseDecimals(BuiltinFormats.code(forID: 43)!) == "_(* #,##0.0_);_(* \\(#,##0.0\\);_(* \"-\"?_);_(@_)")473 #expect(FormatEditing.decreaseDecimals("General") == "0")474 #expect(FormatEditing.decreaseDecimals("0.00") == "0.0")475 #expect(FormatEditing.decreaseDecimals("0.0") == "0")476 #expect(FormatEditing.decreaseDecimals("0") == "0")477 #expect(FormatEditing.decreaseDecimals("0.0%") == "0%")478 #expect(FormatEditing.decreaseDecimals("0.00E+00") == "0.0E+00")479 #expect(FormatEditing.decreaseDecimals("$#,##0.00_);($#,##0.00)") == "$#,##0.0_);($#,##0.0)")480 #expect(FormatEditing.decreaseDecimals("h:mm:ss") == "h:mm:ss")481 #expect(FormatEditing.currency(symbol: "$", decimals: 2) == "$#,##0.00_);($#,##0.00)")482 #expect(fmt(FormatEditing.currency(symbol: "€", decimals: 2), -1234.5) == "(€1,234.50)")483 #expect(fmt(FormatEditing.currency(symbol: "CHF ", decimals: 0), 1234.5) == "CHF 1,235 ")484 #expect(FormatEditing.percent(decimals: 0) == "0%")485 #expect(FormatEditing.percent(decimals: 2) == "0.00%")486 #expect(FormatEditing.thousands(decimals: 0) == "#,##0")487 #expect(FormatEditing.thousands(decimals: 2) == "#,##0.00")488 }489}490491@Suite struct BenchmarkTests {492 @Test func renderThroughput() {493 let f = NumberFormat.cached("#,##0.00")494 let n = 1_000_000495 var values = [Value]()496 values.reserveCapacity(n)497 var x = 0.37498 for i in 0..<n { x = (x * 1103.515245 + 12.345).truncatingRemainder(dividingBy: 1_000_000); values.append(.number(i % 7 == 0 ? -x : x)) }499 var total = 0500 let start = Date()501 for v in values { total &+= f.render(v).text.utf8.count }502 let seconds = Date().timeIntervalSince(start)503 #expect(total > n * 4)504 #if DEBUG505 let mode = "debug"506 #else507 let mode = "release"508 #endif509 print(String(format: "BENCHMARK #,##0.00: %.2f million renders/s (%@ build, %d renders in %.3f s)", Double(n) / seconds / 1e6, mode, n, seconds))510 let d = NumberFormat.cached("yyyy-mm-dd hh:mm:ss")511 let start2 = Date()512 for i in 0..<200_000 { total &+= d.render(.number(40000 + Double(i) * 0.37)).text.utf8.count }513 print(String(format: "BENCHMARK yyyy-mm-dd hh:mm:ss: %.2f million renders/s (%@ build)", 0.2 / Date().timeIntervalSince(start2), mode))514 let g = NumberFormat.cached("General")515 let start3 = Date()516 for v in values.prefix(200_000) { total &+= g.render(v, generalMaxChars: 11).text.utf8.count }517 print(String(format: "BENCHMARK General (11 chars): %.2f million renders/s (%@ build)", 0.2 / Date().timeIntervalSince(start3), mode))518 #expect(total > 0)519 }520}521522523@Suite struct RobustnessTests {524 /// Random format codes must never crash the parser or the renderers.525 @Test func randomFormatsDoNotCrash() {526 let alphabet = Array("0#?.,%E+-/@*_\\\"[]$;:ymdhsAMP <>=1289 ()GeneralRedColor")527 var seed: UInt64 = 0x9E3779B97F4A7C15528 func next() -> Int {529 seed = seed &* 6364136223846793005 &+ 1442695040888963407530 return Int(seed >> 33)531 }532 let values: [Value] = [.number(0), .number(-1.5), .number(1234567.891), .number(1e-7), .number(1e22), .number(45000.56789),533 .number(-0.0001), .number(2958465.99999), .text("abc"), .bool(true), .blank, .error(.na)]534 var rendered = 0535 for _ in 0..<20_000 {536 let length = next() % 14 + 1537 let code = String((0..<length).map { _ in alphabet[next() % alphabet.count] })538 let f = NumberFormat(code: code)539 for v in values {540 rendered += f.render(v).text.utf8.count >= 0 ? 1 : 0541 rendered += f.render(v, locale: .frCA, date1904: true, generalMaxChars: 5).text.utf8.count >= 0 ? 1 : 0542 }543 _ = FormatEditing.increaseDecimals(code)544 _ = FormatEditing.decreaseDecimals(code)545 _ = f.isDateTime || f.isText || f.isPercent || f.decimalPlaces != nil546 }547 #expect(rendered == 20_000 * values.count * 2)548 for w in 0...25 {549 for v in [0.0, 1, -1, 123456.789, -0.000012345, 1e15, 9.99999999e20, 1e-300, 1.7976931348623157e308, 5e-324] {550 let s = NumberFormat.general(v, maxChars: w, locale: .enUS)551 #expect(s.count <= max(w, 1) || s == "0", "\(v) in \(w): \(s)")552 }553 }554 }555}556