-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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).
- Loading branch information
Showing
6 changed files
with
248 additions
and
138 deletions.
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
...ins/Temporal/Duration/prototype/round/largestunit-smallestunit-combinations-relativeto.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}` | ||
); | ||
} |
66 changes: 66 additions & 0 deletions
66
test/built-ins/Temporal/Duration/prototype/round/largestunit-smallestunit-combinations.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}` | ||
); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
test/built-ins/Temporal/Duration/prototype/total/total-of-each-unit-relativeto.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}` | ||
); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
test/built-ins/Temporal/Duration/prototype/total/total-of-each-unit.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.