From 6c2d1467fcac97e59078abb470a91ca7a51ce987 Mon Sep 17 00:00:00 2001 From: Philip Chimento Date: Tue, 2 Apr 2024 16:32:05 -0700 Subject: [PATCH] Temporal: Move some round() and total() tests out of staging Including tests for every possible combination of largest and smallest unit, for each type of relativeTo (undefined, PlainDate, ZonedDateTime). --- ...it-smallestunit-combinations-relativeto.js | 111 ++++++++++++++++++ .../largestunit-smallestunit-combinations.js | 66 +++++++++++ .../total/total-of-each-unit-relativeto.js | 43 +++++++ .../prototype/total/total-of-each-unit.js | 28 +++++ test/staging/Temporal/Duration/old/round.js | 97 --------------- test/staging/Temporal/Duration/old/total.js | 41 ------- 6 files changed, 248 insertions(+), 138 deletions(-) create mode 100644 test/built-ins/Temporal/Duration/prototype/round/largestunit-smallestunit-combinations-relativeto.js create mode 100644 test/built-ins/Temporal/Duration/prototype/round/largestunit-smallestunit-combinations.js create mode 100644 test/built-ins/Temporal/Duration/prototype/total/total-of-each-unit-relativeto.js create mode 100644 test/built-ins/Temporal/Duration/prototype/total/total-of-each-unit.js diff --git a/test/built-ins/Temporal/Duration/prototype/round/largestunit-smallestunit-combinations-relativeto.js b/test/built-ins/Temporal/Duration/prototype/round/largestunit-smallestunit-combinations-relativeto.js new file mode 100644 index 00000000000..4f73dd7ac3d --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/largestunit-smallestunit-combinations-relativeto.js @@ -0,0 +1,111 @@ +// Copyright (C) 2024 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.round +description: Test for all combinations of largestUnit and smallestUnit with relativeTo +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const duration = new Temporal.Duration(5, 5, 5, 5, 5, 5, 5, 5, 5, 5); +const plainRelativeTo = new Temporal.PlainDate(2000, 1, 1); +const zonedRelativeTo = new Temporal.ZonedDateTime(63072000_000_000_000n /* = 1972-01-01T00Z */, "UTC"); + +const exactResults = { + years: { + years: [6], + months: [5, 6], + weeks: [5, 6, 1], + days: [5, 6, 0, 10], + hours: [5, 6, 0, 10, 5], + minutes: [5, 6, 0, 10, 5, 5], + seconds: [5, 6, 0, 10, 5, 5, 5], + milliseconds: [5, 6, 0, 10, 5, 5, 5, 5], + microseconds: [5, 6, 0, 10, 5, 5, 5, 5, 5], + nanoseconds: [5, 6, 0, 10, 5, 5, 5, 5, 5, 5], + }, + months: { + months: [0, 66], + weeks: [0, 66, 1], + days: [0, 66, 0, 10], + hours: [0, 66, 0, 10, 5], + minutes: [0, 66, 0, 10, 5, 5], + seconds: [0, 66, 0, 10, 5, 5, 5], + milliseconds: [0, 66, 0, 10, 5, 5, 5, 5], + microseconds: [0, 66, 0, 10, 5, 5, 5, 5, 5], + nanoseconds: [0, 66, 0, 10, 5, 5, 5, 5, 5, 5], + }, + weeks: { + weeks: [0, 0, 288], + days: [0, 0, 288, 2], + hours: [0, 0, 288, 2, 5], + minutes: [0, 0, 288, 2, 5, 5], + seconds: [0, 0, 288, 2, 5, 5, 5], + milliseconds: [0, 0, 288, 2, 5, 5, 5, 5], + microseconds: [0, 0, 288, 2, 5, 5, 5, 5, 5], + nanoseconds: [0, 0, 288, 2, 5, 5, 5, 5, 5, 5], + }, + days: { + days: [0, 0, 0, 2018], + hours: [0, 0, 0, 2018, 5], + minutes: [0, 0, 0, 2018, 5, 5], + seconds: [0, 0, 0, 2018, 5, 5, 5], + milliseconds: [0, 0, 0, 2018, 5, 5, 5, 5], + microseconds: [0, 0, 0, 2018, 5, 5, 5, 5, 5], + nanoseconds: [0, 0, 0, 2018, 5, 5, 5, 5, 5, 5], + }, + hours: { + hours: [0, 0, 0, 0, 48437], + minutes: [0, 0, 0, 0, 48437, 5], + seconds: [0, 0, 0, 0, 48437, 5, 5], + milliseconds: [0, 0, 0, 0, 48437, 5, 5, 5], + microseconds: [0, 0, 0, 0, 48437, 5, 5, 5, 5], + nanoseconds: [0, 0, 0, 0, 48437, 5, 5, 5, 5, 5], + }, + minutes: { + minutes: [0, 0, 0, 0, 0, 2906225], + seconds: [0, 0, 0, 0, 0, 2906225, 5], + milliseconds: [0, 0, 0, 0, 0, 2906225, 5, 5], + microseconds: [0, 0, 0, 0, 0, 2906225, 5, 5, 5], + nanoseconds: [0, 0, 0, 0, 0, 2906225, 5, 5, 5, 5], + }, + seconds: { + seconds: [0, 0, 0, 0, 0, 0, 174373505], + milliseconds: [0, 0, 0, 0, 0, 0, 174373505, 5], + microseconds: [0, 0, 0, 0, 0, 0, 174373505, 5, 5], + nanoseconds: [0, 0, 0, 0, 0, 0, 174373505, 5, 5, 5], + }, + milliseconds: { + milliseconds: [0, 0, 0, 0, 0, 0, 0, 174373505005], + microseconds: [0, 0, 0, 0, 0, 0, 0, 174373505005, 5], + nanoseconds: [0, 0, 0, 0, 0, 0, 0, 174373505005, 5, 5], + }, + microseconds: { + microseconds: [0, 0, 0, 0, 0, 0, 0, 0, 174373505005005], + nanoseconds: [0, 0, 0, 0, 0, 0, 0, 0, 174373505005005, 5], + }, +}; +for (const [largestUnit, entry] of Object.entries(exactResults)) { + for (const [smallestUnit, expected] of Object.entries(entry)) { + for (const relativeTo of [plainRelativeTo, zonedRelativeTo]) { + const [y, mon = 0, w = 0, d = 0, h = 0, min = 0, s = 0, ms = 0, µs = 0, ns = 0] = expected; + TemporalHelpers.assertDuration( + duration.round({ largestUnit, smallestUnit, relativeTo }), + y, mon, w, d, h, min, s, ms, µs, ns, + `Combination of largestUnit ${largestUnit} and smallestUnit ${smallestUnit}, relative to ${relativeTo}` + ); + } + } +} + +// 174373505005005005 is not a safe integer. +// ℝ(𝔽(174373505005005005)) == 174373505005004992 + +for (const relativeTo of [plainRelativeTo, zonedRelativeTo]) { + TemporalHelpers.assertDuration( + duration.round({ largestUnit: "nanoseconds", smallestUnit: "nanoseconds", relativeTo }), + 0, 0, 0, 0, 0, 0, 0, 0, 0, 174373505005004992, + `Combination of largestUnit nanoseconds and smallestUnit nanoseconds, with precision loss, relative to ${relativeTo}` + ); +} diff --git a/test/built-ins/Temporal/Duration/prototype/round/largestunit-smallestunit-combinations.js b/test/built-ins/Temporal/Duration/prototype/round/largestunit-smallestunit-combinations.js new file mode 100644 index 00000000000..7cd9f7fef27 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/largestunit-smallestunit-combinations.js @@ -0,0 +1,66 @@ +// Copyright (C) 2024 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.round +description: Test for all combinations of largestUnit and smallestUnit without relativeTo +includes: [temporalHelpers.js] +features: [Temporal] +---*/ + +const duration = new Temporal.Duration(0, 0, 0, 5, 5, 5, 5, 5, 5, 5); + +const exactResults = { + days: { + days: [5], + hours: [5, 5], + minutes: [5, 5, 5], + seconds: [5, 5, 5, 5], + milliseconds: [5, 5, 5, 5, 5], + microseconds: [5, 5, 5, 5, 5, 5], + nanoseconds: [5, 5, 5, 5, 5, 5, 5], + }, + hours: { + hours: [0, 125], + minutes: [0, 125, 5], + seconds: [0, 125, 5, 5], + milliseconds: [0, 125, 5, 5, 5], + microseconds: [0, 125, 5, 5, 5, 5], + nanoseconds: [0, 125, 5, 5, 5, 5, 5], + }, + minutes: { + minutes: [0, 0, 7505], + seconds: [0, 0, 7505, 5], + milliseconds: [0, 0, 7505, 5, 5], + microseconds: [0, 0, 7505, 5, 5, 5], + nanoseconds: [0, 0, 7505, 5, 5, 5, 5], + }, + seconds: { + seconds: [0, 0, 0, 450305], + milliseconds: [0, 0, 0, 450305, 5], + microseconds: [0, 0, 0, 450305, 5, 5], + nanoseconds: [0, 0, 0, 450305, 5, 5, 5], + }, + milliseconds: { + milliseconds: [0, 0, 0, 0, 450305005], + microseconds: [0, 0, 0, 0, 450305005, 5], + nanoseconds: [0, 0, 0, 0, 450305005, 5, 5], + }, + microseconds: { + microseconds: [0, 0, 0, 0, 0, 450305005005], + nanoseconds: [0, 0, 0, 0, 0, 450305005005, 5], + }, + nanoseconds: { + nanoseconds: [0, 0, 0, 0, 0, 0, 450305005005005], + }, +}; +for (const [largestUnit, entry] of Object.entries(exactResults)) { + for (const [smallestUnit, expected] of Object.entries(entry)) { + const [d = 0, h = 0, min = 0, s = 0, ms = 0, µs = 0, ns = 0] = expected; + TemporalHelpers.assertDuration( + duration.round({ largestUnit, smallestUnit }), + 0, 0, 0, d, h, min, s, ms, µs, ns, + `Combination of largestUnit ${largestUnit} and smallestUnit ${smallestUnit}` + ); + } +} diff --git a/test/built-ins/Temporal/Duration/prototype/total/total-of-each-unit-relativeto.js b/test/built-ins/Temporal/Duration/prototype/total/total-of-each-unit-relativeto.js new file mode 100644 index 00000000000..2c73805bf3a --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/total/total-of-each-unit-relativeto.js @@ -0,0 +1,43 @@ +// Copyright (C) 2024 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.total +description: Test representative result for all units, with relativeTo +features: [Temporal] +---*/ + +const duration = new Temporal.Duration(5, 5, 5, 5, 5, 5, 5, 5, 5, 5); +const plainRelativeTo = new Temporal.PlainDate(2000, 1, 1); +const zonedRelativeTo = new Temporal.ZonedDateTime(63072000_000_000_000n /* = 1972-01-01T00Z */, "UTC"); + +const dayMilliseconds = 24 * 3600 * 1000; +const fullYears = 5; +const fullMonths = fullYears * 12 + 5 + 1; +const fullDays = 366 + 365 + 365 + 365 + 366 + 31 + 28 + 31 + 30 + 31 + 5 * 7 + 5; +const fullMilliseconds = fullDays * dayMilliseconds + 5 * 3600_000 + 5 * 60_000 + 5000 + 5; +const partialDayMilliseconds = fullMilliseconds - fullDays * dayMilliseconds + 0.005005; +const fractionalDay = partialDayMilliseconds / dayMilliseconds; +const partialYearDays = fullDays - (fullYears * 365 + 2); +const fractionalYear = partialYearDays / 365 + fractionalDay / 365; +const fractionalMonths = (10 /* = 2025-07-11 - 2025-07-01 */ * dayMilliseconds + partialDayMilliseconds) / (31 * dayMilliseconds); +const totalResults = { + years: fullYears + fractionalYear, + months: fullMonths + fractionalMonths, + weeks: Math.floor(fullDays / 7) + (2 + fractionalDay) / 7, + days: fullDays + fractionalDay, + hours: fullDays * 24 + partialDayMilliseconds / 3600000, + minutes: fullDays * 24 * 60 + partialDayMilliseconds / 60000, + seconds: fullDays * 24 * 60 * 60 + partialDayMilliseconds / 1000, + milliseconds: fullMilliseconds + 0.005005, + microseconds: fullMilliseconds * 1000 + 5.005, + nanoseconds: fullMilliseconds * 1000000 + 5005 +}; +for (const [unit, expected] of Object.entries(totalResults)) { + for (const relativeTo of [plainRelativeTo, zonedRelativeTo]) { + assert.sameValue( + duration.total({ unit, relativeTo }), expected, + `Duration.total results for ${unit} relative to ${relativeTo}` + ); + } +} diff --git a/test/built-ins/Temporal/Duration/prototype/total/total-of-each-unit.js b/test/built-ins/Temporal/Duration/prototype/total/total-of-each-unit.js new file mode 100644 index 00000000000..e08ea0f172c --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/total/total-of-each-unit.js @@ -0,0 +1,28 @@ +// Copyright (C) 2024 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.total +description: Test representative result for all units, without relativeTo +features: [Temporal] +---*/ + +const duration = new Temporal.Duration(0, 0, 0, 5, 5, 5, 5, 5, 5, 5); + +const dayMilliseconds = 24 * 3600 * 1000; +const fullDays = 5; +const fullMilliseconds = fullDays * dayMilliseconds + 5 * 3600_000 + 5 * 60_000 + 5000 + 5; +const partialDayMilliseconds = fullMilliseconds - fullDays * dayMilliseconds + 0.005005; +const fractionalDay = partialDayMilliseconds / dayMilliseconds; +const totalResults = { + days: fullDays + fractionalDay, + hours: fullDays * 24 + partialDayMilliseconds / 3600000, + minutes: fullDays * 24 * 60 + partialDayMilliseconds / 60000, + seconds: fullDays * 24 * 60 * 60 + partialDayMilliseconds / 1000, + milliseconds: fullMilliseconds + 0.005005, + microseconds: fullMilliseconds * 1000 + 5.005, + nanoseconds: fullMilliseconds * 1000000 + 5005 +}; +for (const [unit, expected] of Object.entries(totalResults)) { + assert.sameValue(duration.total(unit), expected, `Duration.total results for ${unit}`); +} diff --git a/test/staging/Temporal/Duration/old/round.js b/test/staging/Temporal/Duration/old/round.js index 2a4c379f84c..edfc1128ff3 100644 --- a/test/staging/Temporal/Duration/old/round.js +++ b/test/staging/Temporal/Duration/old/round.js @@ -320,103 +320,6 @@ assert.throws(RangeError, () => d.round({ largestUnit: "nanoseconds" })); var relativeTo = Temporal.PlainDate.from("2020-01-01"); var fortyDays = Temporal.Duration.from({ days: 40 }); assert.sameValue(`${ fortyDays.round({ smallestUnit: "seconds" }) }`, "P40D"); -var roundAndBalanceResults = { - years: { - years: "P6Y", - months: "P5Y6M", - weeks: "P5Y6M1W", - days: "P5Y6M10D", - hours: "P5Y6M10DT5H", - minutes: "P5Y6M10DT5H5M", - seconds: "P5Y6M10DT5H5M5S", - milliseconds: "P5Y6M10DT5H5M5.005S", - microseconds: "P5Y6M10DT5H5M5.005005S", - nanoseconds: "P5Y6M10DT5H5M5.005005005S" - }, - months: { - months: "P66M", - weeks: "P66M1W", - days: "P66M10D", - hours: "P66M10DT5H", - minutes: "P66M10DT5H5M", - seconds: "P66M10DT5H5M5S", - milliseconds: "P66M10DT5H5M5.005S", - microseconds: "P66M10DT5H5M5.005005S", - nanoseconds: "P66M10DT5H5M5.005005005S" - }, - weeks: { - weeks: "P288W", - days: "P288W2D", - hours: "P288W2DT5H", - minutes: "P288W2DT5H5M", - seconds: "P288W2DT5H5M5S", - milliseconds: "P288W2DT5H5M5.005S", - microseconds: "P288W2DT5H5M5.005005S", - nanoseconds: "P288W2DT5H5M5.005005005S" - }, - days: { - days: "P2018D", - hours: "P2018DT5H", - minutes: "P2018DT5H5M", - seconds: "P2018DT5H5M5S", - milliseconds: "P2018DT5H5M5.005S", - microseconds: "P2018DT5H5M5.005005S", - nanoseconds: "P2018DT5H5M5.005005005S" - }, - hours: { - hours: "PT48437H", - minutes: "PT48437H5M", - seconds: "PT48437H5M5S", - milliseconds: "PT48437H5M5.005S", - microseconds: "PT48437H5M5.005005S", - nanoseconds: "PT48437H5M5.005005005S" - }, - minutes: { - minutes: "PT2906225M", - seconds: "PT2906225M5S", - milliseconds: "PT2906225M5.005S", - microseconds: "PT2906225M5.005005S", - nanoseconds: "PT2906225M5.005005005S" - }, - seconds: { - seconds: "PT174373505S", - milliseconds: "PT174373505.005S", - microseconds: "PT174373505.005005S", - nanoseconds: "PT174373505.005005005S" - }, - milliseconds: { - milliseconds: "PT174373505.005S", - microseconds: "PT174373505.005005S", - nanoseconds: "PT174373505.005005005S" - } -}; -for (var [largestUnit, entry] of Object.entries(roundAndBalanceResults)) { - for (var [smallestUnit, expected] of Object.entries(entry)) { - assert.sameValue(`${ d.round({ - largestUnit, - smallestUnit, - relativeTo - }) }`, expected); - } -} -var balanceLosePrecisionResults = { - microseconds: [ - "microseconds", - "nanoseconds" - ], - nanoseconds: ["nanoseconds"] -}; - -// Round may lose precision below ms -for (var [largestUnit, entry] of Object.entries(balanceLosePrecisionResults)) { - for (var smallestUnit of entry) { - assert(`${ d.round({ - largestUnit, - smallestUnit, - relativeTo - }) }`.startsWith("PT174373505.005")); - } -} // halfExpand is the default assert.sameValue(`${ d.round({ diff --git a/test/staging/Temporal/Duration/old/total.js b/test/staging/Temporal/Duration/old/total.js index 31216e09991..edeb8a56889 100644 --- a/test/staging/Temporal/Duration/old/total.js +++ b/test/staging/Temporal/Duration/old/total.js @@ -172,47 +172,6 @@ assert(Math.abs(negativeD2.total({ unit: "seconds" }) - -totalD2.seconds) < Numb assert(Math.abs(negativeD2.total({ unit: "milliseconds" }) - -totalD2.milliseconds) < Number.EPSILON); assert(Math.abs(negativeD2.total({ unit: "microseconds" }) - -totalD2.microseconds) < Number.EPSILON); assert.sameValue(negativeD2.total({ unit: "nanoseconds" }), -totalD2.nanoseconds); -var endpoint = relativeTo.toPlainDateTime().add(d); -var options = unit => ({ - largestUnit: unit, - smallestUnit: unit, - roundingMode: "trunc" -}); -var fullYears = 5; -var fullDays = endpoint.since(relativeTo, options("days")).days; -var fullMilliseconds = endpoint.since(relativeTo, options("milliseconds")).milliseconds; -var partialDayMilliseconds = fullMilliseconds - fullDays * 24 * 3600000 + 0.005005; -var fractionalDay = partialDayMilliseconds / (24 * 3600000); -var partialYearDays = fullDays - (fullYears * 365 + 2); -var fractionalYear = partialYearDays / 365 + fractionalDay / 365; -var fractionalMonths = ((endpoint.day - 1) * (24 * 3600000) + partialDayMilliseconds) / (31 * 24 * 3600000); -var totalResults = { - years: fullYears + fractionalYear, - months: 66 + fractionalMonths, - weeks: (fullDays + fractionalDay) / 7, - days: fullDays + fractionalDay, - hours: fullDays * 24 + partialDayMilliseconds / 3600000, - minutes: fullDays * 24 * 60 + partialDayMilliseconds / 60000, - seconds: fullDays * 24 * 60 * 60 + partialDayMilliseconds / 1000, - milliseconds: fullMilliseconds + 0.005005, - microseconds: fullMilliseconds * 1000 + 5.005, - nanoseconds: fullMilliseconds * 1000000 + 5005 -}; -for (var [unit, expected] of Object.entries(totalResults)) { - assert.sameValue(d.total({ - unit, - relativeTo - }).toPrecision(15), expected.toPrecision(15)); -} -for (var unit of [ - "microseconds", - "nanoseconds" - ]) { - assert(d.total({ - unit, - relativeTo - }).toString().startsWith("174373505005")); -} // balances differently depending on relativeTo var fortyDays = Temporal.Duration.from({ days: 40 });